0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Add UTF-8 support to example file_server

LH: I have fixed issues in the PR and incorporated changes
from a sister PR by @djankovic (git author: Dom J). I also
made sure the UTF-8 files were readable without Chinese
fonts and added their downloading to the examples test suite.
This commit is contained in:
lin 2023-02-15 00:30:18 +08:00 committed by Loïc Hoguin
parent 32594a5199
commit 4f26d6a573
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
4 changed files with 20 additions and 4 deletions

View file

@ -0,0 +1,8 @@
<html>
<head>
<meta charset='utf-8'>
</head>
<body>
中文!
</body>
</html>

View file

@ -8,6 +8,7 @@
-export([allowed_methods/2]). -export([allowed_methods/2]).
-export([resource_exists/2]). -export([resource_exists/2]).
-export([content_types_provided/2]). -export([content_types_provided/2]).
-export([charsets_provided/2]).
%% Callback Callbacks %% Callback Callbacks
-export([list_json/2]). -export([list_json/2]).
@ -31,12 +32,15 @@ content_types_provided(Req, State) ->
{{<<"application">>, <<"json">>, []}, list_json} {{<<"application">>, <<"json">>, []}, list_json}
], Req, State}. ], Req, State}.
charsets_provided(Req, State) ->
{[<<"utf-8">>], Req, State}.
list_json(Req, {Path, Fs}) -> list_json(Req, {Path, Fs}) ->
Files = [ <<(list_to_binary(F))/binary>> || F <- Fs ], Files = [unicode:characters_to_binary(F) || F <- Fs],
{jsx:encode(Files), Req, Path}. {jsx:encode(Files), Req, Path}.
list_html(Req, {Path, Fs}) -> list_html(Req, {Path, Fs}) ->
Body = [[ links(Path, F) || F <- [".."|Fs] ]], Body = [[links(Path, unicode:characters_to_binary(F)) || F <- [".."|Fs]]],
HTML = [<<"<!DOCTYPE html><html><head><title>Index</title></head>", HTML = [<<"<!DOCTYPE html><html><head><title>Index</title></head>",
"<body>">>, Body, <<"</body></html>\n">>], "<body>">>, Body, <<"</body></html>\n">>],
{HTML, Req, Path}. {HTML, Req, Path}.

View file

@ -15,7 +15,8 @@ start(_Type, _Args) ->
{'_', [ {'_', [
{"/[...]", cowboy_static, {priv_dir, file_server, "", [ {"/[...]", cowboy_static, {priv_dir, file_server, "", [
{mimetypes, cow_mimetypes, all}, {mimetypes, cow_mimetypes, all},
{dir_handler, directory_h} {dir_handler, directory_h},
{charset, <<"utf-8">>}
]}} ]}}
]} ]}
]), ]),

View file

@ -372,13 +372,16 @@ file_server(Config) ->
do_file_server(Transport, Protocol, Config) -> do_file_server(Transport, Protocol, Config) ->
%% Directory. %% Directory.
{200, DirHeaders, <<"<!DOCTYPE html><html>", _/bits >>} = do_get(Transport, Protocol, "/", Config), {200, DirHeaders, <<"<!DOCTYPE html><html>", _/bits >>} = do_get(Transport, Protocol, "/", Config),
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, DirHeaders), {_, <<"text/html; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, DirHeaders),
_ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, undefined, Config), _ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, undefined, Config),
%% Files. %% Files.
{200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config), {200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config),
{200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config), {200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config),
{200, _, _} = do_get(Transport, Protocol, "/test.txt", Config), {200, _, _} = do_get(Transport, Protocol, "/test.txt", Config),
{200, _, _} = do_get(Transport, Protocol, "/video.html", Config), {200, _, _} = do_get(Transport, Protocol, "/video.html", Config),
{200, _, _} = do_get(Transport, Protocol,
["/", cow_uri:urlencode(<<"中文"/utf8>>), "/", cow_uri:urlencode(<<"中文.html"/utf8>>)],
Config),
ok. ok.
%% Markdown middleware. %% Markdown middleware.