0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00

Add file option to cowboy_http_static

This commit is contained in:
Magnus Klaar 2012-04-30 23:50:23 +02:00
parent 48a2b177f5
commit 8168ae96c8
2 changed files with 80 additions and 2 deletions

View file

@ -31,7 +31,7 @@
%% The handler must be configured with a request path prefix to serve files %% The handler must be configured with a request path prefix to serve files
%% under and the path to a directory to read files from. The request path prefix %% under and the path to a directory to read files from. The request path prefix
%% is defined in the path pattern of the cowboy dispatch rule for the handler. %% is defined in the path pattern of the cowboy dispatch rule for the handler.
%% The request path pattern must end with a ``'...''' token. %% The request path pattern must end with a `...' token.
%% The directory path can be set to either an absolute or relative path in the %% The directory path can be set to either an absolute or relative path in the
%% form of a list or binary string representation of a file system path. A list %% form of a list or binary string representation of a file system path. A list
%% of binary path segments, as is used throughout cowboy, is also a valid %% of binary path segments, as is used throughout cowboy, is also a valid
@ -133,6 +133,40 @@
%% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "), %% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
%% {strong, iolist_to_binary(Checksum)}. %% {strong, iolist_to_binary(Checksum)}.
%% ''' %% '''
%%
%% == File configuration ==
%%
%% If the file system path being served does not share a common suffix with
%% the request path it is possible to override the file path using the `file'
%% option. The value of this option is expected to be a relative path within
%% the static file directory specified using the `directory' option.
%% The path must be in the form of a list or binary string representation of a
%% file system path. A list of binary path segments, as is used throughout
%% cowboy, is also a valid.
%%
%% When the `file' option is used the same file will be served for all requests
%% matching the cowboy dispatch fule for the handler. It is not necessary to
%% end the request path pattern with a `...' token because the request path
%% will not be used to determine which file to serve from the static directory.
%%
%% === Examples ===
%%
%% ```
%% %% Serve cowboy/priv/www/index.html as http://example.com/
%% {[], cowboy_http_static,
%% [{directory, {priv_dir, cowboy, [<<"www">>]}}
%% {file, <<"index.html">>}]}
%%
%% %% Serve cowboy/priv/www/page.html under http://example.com/*/page
%% {['*', <<"page">>], cowboy_http_static,
%% [{directory, {priv_dir, cowboy, [<<"www">>]}}
%% {file, <<"page.html">>}]}.
%%
%% %% Always serve cowboy/priv/www/other.html under http://example.com/other
%% {[<<"other">>, '...'], cowboy_http_static,
%% [{directory, {priv_dir, cowboy, [<<"www">>]}}
%% {file, "other.html"}]}
%% '''
-module(cowboy_http_static). -module(cowboy_http_static).
%% include files %% include files
@ -189,7 +223,10 @@ rest_init(Req, Opts) ->
{attributes, Attrs} -> {fun attr_etag_function/2, Attrs}; {attributes, Attrs} -> {fun attr_etag_function/2, Attrs};
{_, _}=ETagFunction1 -> ETagFunction1 {_, _}=ETagFunction1 -> ETagFunction1
end, end,
{Filepath, Req1} = cowboy_http_req:path_info(Req), {Filepath, Req1} = case lists:keyfind(file, 1, Opts) of
{_, Filepath2} -> {filepath_path(Filepath2), Req};
false -> cowboy_http_req:path_info(Req)
end,
State = case check_path(Filepath) of State = case check_path(Filepath) of
error -> error ->
#state{filepath=error, fileinfo=error, mimetypes=undefined, #state{filepath=error, fileinfo=error, mimetypes=undefined,
@ -341,6 +378,14 @@ directory_path({priv_dir, App, Path}) when is_binary(Path) ->
directory_path(Path) -> directory_path(Path) ->
Path. Path.
%% @private Ensure that a file path is of the same type as a request path.
-spec filepath_path(dirpath()) -> Path::[binary()].
filepath_path([H|_]=Path) when is_integer(H) ->
filename:split(list_to_binary(Path));
filepath_path(Path) when is_binary(Path) ->
filename:split(Path);
filepath_path([H|_]=Path) when is_binary(H) ->
Path.
%% @private Validate a request path for unsafe characters. %% @private Validate a request path for unsafe characters.
%% There is no way to escape special characters in a filesystem path. %% There is no way to escape special characters in a filesystem path.
@ -459,5 +504,14 @@ directory_path_test_() ->
?_eq("a/b", P("a/b")) ?_eq("a/b", P("a/b"))
]. ].
filepath_path_test_() ->
P = fun filepath_path/1,
[?_eq([<<"a">>], P("a")),
?_eq([<<"a">>], P(<<"a">>)),
?_eq([<<"a">>], P([<<"a">>])),
?_eq([<<"a">>, <<"b">>], P("a/b")),
?_eq([<<"a">>, <<"b">>], P(<<"a/b">>)),
?_eq([<<"a">>, <<"b">>], P([<<"a">>, <<"b">>]))
].
-endif. -endif.

View file

@ -58,6 +58,8 @@
-export([static_mimetypes_function/1]). -export([static_mimetypes_function/1]).
-export([static_test_file/1]). -export([static_test_file/1]).
-export([static_test_file_css/1]). -export([static_test_file_css/1]).
-export([static_specify_file/1]).
-export([static_specify_file_catchall/1]).
-export([stream_body_set_resp/1]). -export([stream_body_set_resp/1]).
-export([te_chunked/1]). -export([te_chunked/1]).
-export([te_chunked_delayed/1]). -export([te_chunked_delayed/1]).
@ -100,6 +102,8 @@ groups() ->
static_mimetypes_function, static_mimetypes_function,
static_test_file, static_test_file,
static_test_file_css, static_test_file_css,
static_specify_file,
static_specify_file_catchall,
stream_body_set_resp, stream_body_set_resp,
te_chunked, te_chunked,
te_chunked_delayed, te_chunked_delayed,
@ -221,6 +225,10 @@ init_dispatch(Config) ->
{[<<"static_function_etag">>, '...'], cowboy_http_static, {[<<"static_function_etag">>, '...'], cowboy_http_static,
[{directory, ?config(static_dir, Config)}, [{directory, ?config(static_dir, Config)},
{etag, {fun static_function_etag/2, etag_data}}]}, {etag, {fun static_function_etag/2, etag_data}}]},
{[<<"static_specify_file">>, '...'], cowboy_http_static,
[{directory, ?config(static_dir, Config)},
{mimetypes, [{<<".css">>, [<<"text/css">>]}]},
{file, <<"test_file.css">>}]},
{[<<"multipart">>], http_handler_multipart, []}, {[<<"multipart">>], http_handler_multipart, []},
{[<<"echo">>, <<"body">>], http_handler_echo_body, []}, {[<<"echo">>, <<"body">>], http_handler_echo_body, []},
{[<<"simple">>], rest_simple_resource, []}, {[<<"simple">>], rest_simple_resource, []},
@ -761,6 +769,22 @@ static_test_file_css(Config) ->
{<<"content-type">>, <<"text/css">>} {<<"content-type">>, <<"text/css">>}
= lists:keyfind(<<"content-type">>, 1, Headers). = lists:keyfind(<<"content-type">>, 1, Headers).
static_specify_file(Config) ->
Client = ?config(client, Config),
{ok, Client2} = cowboy_client:request(<<"GET">>,
build_url("/static_specify_file", Config), Client),
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
{ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
static_specify_file_catchall(Config) ->
Client = ?config(client, Config),
{ok, Client2} = cowboy_client:request(<<"GET">>,
build_url("/static_specify_file/none", Config), Client),
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
{ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
stream_body_set_resp(Config) -> stream_body_set_resp(Config) ->
Client = ?config(client, Config), Client = ?config(client, Config),
{ok, Client2} = cowboy_client:request(<<"GET">>, {ok, Client2} = cowboy_client:request(<<"GET">>,