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 the file_server example

This commit is contained in:
Dom J 2023-02-10 21:29:58 +01:00
parent 30ee75cea1
commit be419c5c70
2 changed files with 8 additions and 3 deletions

View file

@ -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))/binary>> || 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 = [<<"<!DOCTYPE html><html><head><title>Index</title></head>",
"<body>">>, Body, <<"</body></html>\n">>],
{HTML, Req, Path}.

View file

@ -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">>}
]}}
]}
]),