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

cowboy_static: Add support for files in EZ archives

If cowboy_static is initialized with `{priv_file, ...}` or `{priv_dir,
...}`, it is now able to read files from Erlang application .ez
archives.

When serving a file from an archive, the #file_info{} comes from the
archive, not the contained file, except for the size and type. The
erl_prim_loader module is used to read the latter's #file_info{} and the
actual file content (ie. sendfile(2) is not used in this case).

(cherry picked from commit 2166733628)
This commit is contained in:
Jean-Sébastien Pédron 2016-12-29 15:48:06 +01:00 committed by Loïc Hoguin
parent a62cc4260f
commit 98c2bc64e5
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
3 changed files with 144 additions and 19 deletions

View file

@ -74,6 +74,11 @@ groups() ->
init_per_suite(Config) ->
Dir = config(priv_dir, Config) ++ "/static",
ct_helper:create_static_dir(Dir),
%% Add a simple Erlang application archive containing one file
%% in its priv directory.
true = code:add_pathz(filename:join(
[config(data_dir, Config), "static_files_app", "ebin"])),
ok = application:load(static_files_app),
[{static_dir, Dir}|Config].
end_per_suite(Config) ->
@ -189,6 +194,10 @@ init_dispatch(Config) ->
[{etag, ?MODULE, do_etag_gen}]}},
{"/static_specify_file/[...]", cowboy_static,
{file, config(static_dir, Config) ++ "/style.css"}},
{"/ez_priv_file/index.html", cowboy_static, {priv_file, static_files_app, "www/index.html"}},
{"/bad/ez_priv_file/index.php", cowboy_static, {priv_file, static_files_app, "www/index.php"}},
{"/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "www"}},
{"/bad/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "cgi-bin"}},
{"/multipart", http_multipart, []},
{"/multipart/large", http_multipart_stream, []},
{"/echo/body", http_echo_body, []},
@ -966,6 +975,40 @@ static_test_file_css(Config) ->
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
ok.
priv_file_in_ez_archive(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/ez_priv_file/index.html"),
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
{ok, <<"<h1>It works!</h1>\n">>} = gun:await_body(ConnPid, Ref),
ok.
bad_priv_file_in_ez_archive(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/bad/ez_priv_file/index.php"),
{response, fin, 404, _} = gun:await(ConnPid, Ref),
ok.
priv_dir_in_ez_archive(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/ez_priv_dir/index.html"),
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
{ok, <<"<h1>It works!</h1>\n">>} = gun:await_body(ConnPid, Ref),
ok.
bad_file_in_priv_dir_in_ez_archive(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/ez_priv_dir/index.php"),
{response, fin, 404, _} = gun:await(ConnPid, Ref),
ok.
bad_priv_dir_in_ez_archive(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/bad/ez_priv_dir/index.html"),
{response, fin, 404, _} = gun:await(ConnPid, Ref),
ok.
stream_body_set_resp(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/stream_body/set_resp"),

Binary file not shown.