aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_rest.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/cowboy_http_rest.erl')
-rw-r--r--src/cowboy_http_rest.erl18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/cowboy_http_rest.erl b/src/cowboy_http_rest.erl
index 9bb66fa..62e712e 100644
--- a/src/cowboy_http_rest.erl
+++ b/src/cowboy_http_rest.erl
@@ -53,7 +53,7 @@
%% You do not need to call this function manually. To upgrade to the REST
%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em>
%% in your <em>cowboy_http_handler:init/3</em> handler function.
--spec upgrade(pid(), module(), any(), #http_req{}) -> ok.
+-spec upgrade(pid(), module(), any(), #http_req{}) -> {ok, #http_req{}}.
upgrade(_ListenerPid, Handler, Opts, Req) ->
try
case erlang:function_exported(Handler, rest_init, 2) of
@@ -81,8 +81,12 @@ service_available(Req, State) ->
known_methods(Req=#http_req{method=Method}, State) ->
case call(Req, State, known_methods) of
- no_call ->
+ no_call when Method =:= 'HEAD'; Method =:= 'GET'; Method =:= 'POST';
+ Method =:= 'PUT'; Method =:= 'DELETE'; Method =:= 'TRACE';
+ Method =:= 'CONNECT'; Method =:= 'OPTIONS' ->
next(Req, State, fun uri_too_long/2);
+ no_call ->
+ next(Req, State, 501);
{List, Req2, HandlerState2} ->
State2 = State#state{handler_state=HandlerState2},
case lists:member(Method, List) of
@@ -96,8 +100,10 @@ uri_too_long(Req, State) ->
allowed_methods(Req=#http_req{method=Method}, State) ->
case call(Req, State, allowed_methods) of
- no_call ->
+ no_call when Method =:= 'HEAD'; Method =:= 'GET' ->
next(Req, State, fun malformed_request/2);
+ no_call ->
+ method_not_allowed(Req, State, ['GET', 'HEAD']);
{List, Req2, HandlerState2} ->
State2 = State#state{handler_state=HandlerState2},
case lists:member(Method, List) of
@@ -753,6 +759,8 @@ respond(Req, State, StatusCode) ->
terminate(Req, #state{handler=Handler, handler_state=HandlerState}) ->
case erlang:function_exported(Handler, rest_terminate, 2) of
- true -> ok = Handler:rest_terminate(Req, HandlerState);
+ true -> ok = Handler:rest_terminate(
+ Req#http_req{resp_state=locked}, HandlerState);
false -> ok
- end.
+ end,
+ {ok, Req}.