From 4f26d6a5730685d00ed1533fe33dc2eb3a51a4e8 Mon Sep 17 00:00:00 2001 From: lin Date: Wed, 15 Feb 2023 00:30:18 +0800 Subject: 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. --- .../priv/\344\270\255\346\226\207/\344\270\255\346\226\207.html" | 8 ++++++++ examples/file_server/src/directory_h.erl | 8 ++++++-- examples/file_server/src/file_server_app.erl | 3 ++- test/examples_SUITE.erl | 5 ++++- 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 "examples/file_server/priv/\344\270\255\346\226\207/\344\270\255\346\226\207.html" diff --git "a/examples/file_server/priv/\344\270\255\346\226\207/\344\270\255\346\226\207.html" "b/examples/file_server/priv/\344\270\255\346\226\207/\344\270\255\346\226\207.html" new file mode 100644 index 0000000..43ca75f --- /dev/null +++ "b/examples/file_server/priv/\344\270\255\346\226\207/\344\270\255\346\226\207.html" @@ -0,0 +1,8 @@ + + + + + + 中文! + + diff --git a/examples/file_server/src/directory_h.erl b/examples/file_server/src/directory_h.erl index 66e1466..7d7bd9a 100644 --- a/examples/file_server/src/directory_h.erl +++ b/examples/file_server/src/directory_h.erl @@ -8,6 +8,7 @@ -export([allowed_methods/2]). -export([resource_exists/2]). -export([content_types_provided/2]). +-export([charsets_provided/2]). %% Callback Callbacks -export([list_json/2]). @@ -31,12 +32,15 @@ content_types_provided(Req, State) -> {{<<"application">>, <<"json">>, []}, list_json} ], Req, State}. +charsets_provided(Req, State) -> + {[<<"utf-8">>], Req, State}. + 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}. list_html(Req, {Path, Fs}) -> - Body = [[ links(Path, F) || F <- [".."|Fs] ]], + Body = [[links(Path, unicode:characters_to_binary(F)) || F <- [".."|Fs]]], HTML = [<<"Index", "">>, Body, <<"\n">>], {HTML, Req, Path}. diff --git a/examples/file_server/src/file_server_app.erl b/examples/file_server/src/file_server_app.erl index 17e73b2..0ba8f6c 100644 --- a/examples/file_server/src/file_server_app.erl +++ b/examples/file_server/src/file_server_app.erl @@ -15,7 +15,8 @@ start(_Type, _Args) -> {'_', [ {"/[...]", cowboy_static, {priv_dir, file_server, "", [ {mimetypes, cow_mimetypes, all}, - {dir_handler, directory_h} + {dir_handler, directory_h}, + {charset, <<"utf-8">>} ]}} ]} ]), diff --git a/test/examples_SUITE.erl b/test/examples_SUITE.erl index ed90c2b..1e88f2f 100644 --- a/test/examples_SUITE.erl +++ b/test/examples_SUITE.erl @@ -372,13 +372,16 @@ file_server(Config) -> do_file_server(Transport, Protocol, Config) -> %% Directory. {200, DirHeaders, <<"", _/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), %% Files. {200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config), {200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config), {200, _, _} = do_get(Transport, Protocol, "/test.txt", 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. %% Markdown middleware. -- cgit v1.2.3