aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_static.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-05-21 15:53:31 +0200
committerLoïc Hoguin <[email protected]>2012-05-21 16:33:37 +0200
commit295dc64eaa002c8ce03ac4bedffd7bc4e3be025a (patch)
tree70ffa91498c5ec19155ef764bfc2cec9c83f8af0 /src/cowboy_http_static.erl
parent040c6dc31af3b17dfec7e2925bc8c4a15f301d7e (diff)
parent8168ae96c8543e02e5899187e6f7c7ed5b40c35d (diff)
downloadcowboy-295dc64eaa002c8ce03ac4bedffd7bc4e3be025a.tar.gz
cowboy-295dc64eaa002c8ce03ac4bedffd7bc4e3be025a.tar.bz2
cowboy-295dc64eaa002c8ce03ac4bedffd7bc4e3be025a.zip
Merge branch 'serve-static-file' of https://github.com/klaar/cowboy
Fix alphabetical order since @klaar seems to have issues with it. ;)
Diffstat (limited to 'src/cowboy_http_static.erl')
-rw-r--r--src/cowboy_http_static.erl58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/cowboy_http_static.erl b/src/cowboy_http_static.erl
index 8b9f558..65e2595 100644
--- a/src/cowboy_http_static.erl
+++ b/src/cowboy_http_static.erl
@@ -31,7 +31,7 @@
%% 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
%% 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
%% 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
@@ -133,6 +133,40 @@
%% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
%% {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).
%% include files
@@ -189,7 +223,10 @@ rest_init(Req, Opts) ->
{attributes, Attrs} -> {fun attr_etag_function/2, Attrs};
{_, _}=ETagFunction1 -> ETagFunction1
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
error ->
#state{filepath=error, fileinfo=error, mimetypes=undefined,
@@ -341,6 +378,14 @@ directory_path({priv_dir, App, Path}) when is_binary(Path) ->
directory_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.
%% 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"))
].
+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.