diff options
author | Loïc Hoguin <[email protected]> | 2014-09-26 15:58:44 +0300 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2014-09-26 15:58:44 +0300 |
commit | 5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 (patch) | |
tree | 7094d5f9d92c9c3bac1a60ca4b4922ba035b219d /src/cowboy_static.erl | |
parent | fd37fad592fc96a384bcd060696194f5fe074f6f (diff) | |
download | cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.tar.gz cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.tar.bz2 cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.zip |
Unify the init and terminate callbacks
This set of changes is the first step to simplify the
writing of handlers, by removing some extraneous
callbacks and making others optional.
init/3 is now init/2, its first argument being removed.
rest_init/2 and rest_terminate/2 have been removed.
websocket_init/3 and websocket_terminate/3 have been removed.
terminate/3 is now optional. It is called regardless of
the type of handler, including rest and websocket.
The return value of init/2 changed. It now returns
{Mod, Req, Opts} with Mod being either one of the four
handler type or a custom module. It can also return extra
timeout and hibernate options.
The signature for sub protocols has changed, they now
receive these extra timeout and hibernate options.
Loop handlers are now implemented in cowboy_long_polling,
and will be renamed throughout the project in a future commit.
Diffstat (limited to 'src/cowboy_static.erl')
-rw-r--r-- | src/cowboy_static.erl | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl index 1d5286e..ecca6fa 100644 --- a/src/cowboy_static.erl +++ b/src/cowboy_static.erl @@ -15,8 +15,7 @@ -module(cowboy_static). --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([malformed_request/2]). -export([forbidden/2]). -export([content_types_provided/2]). @@ -39,33 +38,27 @@ -type state() :: {binary(), {ok, #file_info{}} | {error, atom()}, extra()}. --spec init(_, _, _) -> {upgrade, protocol, cowboy_rest}. -init(_, _, _) -> - {upgrade, protocol, cowboy_rest}. - %% Resolve the file that will be sent and get its file information. %% If the handler is configured to manage a directory, check that the %% requested file is inside the configured directory. --spec rest_init(Req, opts()) - -> {ok, Req, error | state()} - when Req::cowboy_req:req(). -rest_init(Req, {Name, Path}) -> - rest_init_opts(Req, {Name, Path, []}); -rest_init(Req, {Name, App, Path}) +-spec init(Req, opts()) -> {rest, Req, error | state()} when Req::cowboy_req:req(). +init(Req, {Name, Path}) -> + init_opts(Req, {Name, Path, []}); +init(Req, {Name, App, Path}) when Name =:= priv_file; Name =:= priv_dir -> - rest_init_opts(Req, {Name, App, Path, []}); -rest_init(Req, Opts) -> - rest_init_opts(Req, Opts). - -rest_init_opts(Req, {priv_file, App, Path, Extra}) -> - rest_init_info(Req, absname(priv_path(App, Path)), Extra); -rest_init_opts(Req, {file, Path, Extra}) -> - rest_init_info(Req, absname(Path), Extra); -rest_init_opts(Req, {priv_dir, App, Path, Extra}) -> - rest_init_dir(Req, priv_path(App, Path), Extra); -rest_init_opts(Req, {dir, Path, Extra}) -> - rest_init_dir(Req, Path, Extra). + init_opts(Req, {Name, App, Path, []}); +init(Req, Opts) -> + init_opts(Req, Opts). + +init_opts(Req, {priv_file, App, Path, Extra}) -> + init_info(Req, absname(priv_path(App, Path)), Extra); +init_opts(Req, {file, Path, Extra}) -> + init_info(Req, absname(Path), Extra); +init_opts(Req, {priv_dir, App, Path, Extra}) -> + init_dir(Req, priv_path(App, Path), Extra); +init_opts(Req, {dir, Path, Extra}) -> + init_dir(Req, Path, Extra). priv_path(App, Path) -> case code:priv_dir(App) of @@ -83,18 +76,18 @@ absname(Path) when is_list(Path) -> absname(Path) when is_binary(Path) -> filename:absname(Path). -rest_init_dir(Req, Path, Extra) when is_list(Path) -> - rest_init_dir(Req, list_to_binary(Path), Extra); -rest_init_dir(Req, Path, Extra) -> +init_dir(Req, Path, Extra) when is_list(Path) -> + init_dir(Req, list_to_binary(Path), Extra); +init_dir(Req, Path, Extra) -> Dir = fullpath(filename:absname(Path)), PathInfo = cowboy_req:path_info(Req), Filepath = filename:join([Dir|PathInfo]), Len = byte_size(Dir), case fullpath(Filepath) of << Dir:Len/binary, $/, _/binary >> -> - rest_init_info(Req, Filepath, Extra); + init_info(Req, Filepath, Extra); _ -> - {ok, Req, error} + {rest, Req, error} end. fullpath(Path) -> @@ -110,9 +103,9 @@ fullpath([<<"..">>|Tail], [_|Acc]) -> fullpath([Segment|Tail], Acc) -> fullpath(Tail, [Segment|Acc]). -rest_init_info(Req, Path, Extra) -> +init_info(Req, Path, Extra) -> Info = file:read_file_info(Path, [{time, universal}]), - {ok, Req, {Path, Info, Extra}}. + {rest, Req, {Path, Info, Extra}}. -ifdef(TEST). fullpath_test_() -> |