Abstract and tidy up context selector, move the HEAD / GET check for statoc content to the handler.
This commit is contained in:
parent
62cef8f770
commit
1036cb0dc6
|
@ -45,12 +45,7 @@ register_context_handler(Context, Prefix0, Handler, LinkText) ->
|
||||||
Prefix = context_path(Context, Prefix0),
|
Prefix = context_path(Context, Prefix0),
|
||||||
Listener = context_listener(Context),
|
Listener = context_listener(Context),
|
||||||
register_handler(
|
register_handler(
|
||||||
Context,
|
Context, context_selector(Prefix),
|
||||||
fun(Req) ->
|
|
||||||
"/" ++ Path = Req:get(raw_path),
|
|
||||||
(Prefix == "") orelse (Path == Prefix)
|
|
||||||
orelse (string:str(Path, Prefix ++ "/") == 1)
|
|
||||||
end,
|
|
||||||
fun (Req) -> Handler({Prefix, Listener}, Req) end,
|
fun (Req) -> Handler({Prefix, Listener}, Req) end,
|
||||||
{Prefix, LinkText}),
|
{Prefix, LinkText}),
|
||||||
{ok, Prefix}.
|
{ok, Prefix}.
|
||||||
|
@ -61,23 +56,20 @@ register_context_handler(Context, Prefix0, Handler, LinkText) ->
|
||||||
register_static_context(Context, Prefix0, Module, FSPath, LinkText) ->
|
register_static_context(Context, Prefix0, Module, FSPath, LinkText) ->
|
||||||
Prefix = context_path(Context, Prefix0),
|
Prefix = context_path(Context, Prefix0),
|
||||||
register_handler(Context,
|
register_handler(Context,
|
||||||
static_context_selector(Prefix),
|
context_selector(Prefix),
|
||||||
static_context_handler(Prefix, Module, FSPath),
|
static_context_handler(Prefix, Module, FSPath),
|
||||||
{Prefix, LinkText}),
|
{Prefix, LinkText}),
|
||||||
{ok, Prefix}.
|
{ok, Prefix}.
|
||||||
|
|
||||||
%% Produces a selector for use with register_handler that
|
context_selector(Prefix) ->
|
||||||
%% responds to GET and HEAD HTTP methods for resources within the
|
Prefix1 = "/" ++ Prefix,
|
||||||
%% given fixed context path.
|
case Prefix == "" of
|
||||||
static_context_selector(Prefix) ->
|
true -> fun(_Req) -> true end;
|
||||||
fun(Req) ->
|
false -> fun(Req) ->
|
||||||
case Req:get(method) of
|
Path = Req:get(raw_path),
|
||||||
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
|
(Path == Prefix1)
|
||||||
"/" ++ Path = Req:get(raw_path),
|
orelse (string:str(Path, Prefix1 ++ "/") == 1)
|
||||||
(Prefix == Path) or (string:str(Path, Prefix ++ "/") == 1);
|
end
|
||||||
_ ->
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Produces a handler for use with register_handler that serves
|
%% Produces a handler for use with register_handler that serves
|
||||||
|
@ -95,13 +87,22 @@ static_context_handler(Prefix, Module, FSPath) ->
|
||||||
static_context_handler("", LocalPath) ->
|
static_context_handler("", LocalPath) ->
|
||||||
fun(Req) ->
|
fun(Req) ->
|
||||||
"/" ++ Path = Req:get(raw_path),
|
"/" ++ Path = Req:get(raw_path),
|
||||||
Req:serve_file(Path, LocalPath)
|
serve_file(Req, Path, LocalPath)
|
||||||
end;
|
end;
|
||||||
static_context_handler(Prefix, LocalPath) ->
|
static_context_handler(Prefix, LocalPath) ->
|
||||||
fun(Req) ->
|
fun(Req) ->
|
||||||
"/" ++ Path = Req:get(raw_path),
|
"/" ++ Path = Req:get(raw_path),
|
||||||
case string:substr(Path, length(Prefix) + 1) of
|
case string:substr(Path, length(Prefix) + 1) of
|
||||||
"" -> Req:respond({301, [{"Location", "/" ++ Prefix ++ "/"}], ""});
|
"" -> Req:respond({301, [{"Location", "/" ++ Prefix ++ "/"}], ""});
|
||||||
"/" ++ P -> Req:serve_file(P, LocalPath)
|
"/" ++ P -> serve_file(Req, P, LocalPath)
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
serve_file(Req, Path, LocalPath) ->
|
||||||
|
case Req:get(method) of
|
||||||
|
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
|
||||||
|
Req:serve_file(Path, LocalPath);
|
||||||
|
_ ->
|
||||||
|
Req:respond({400, [],
|
||||||
|
"Only GET or HEAD supported for static content"})
|
||||||
|
end.
|
||||||
|
|
Loading…
Reference in New Issue