aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_protocol.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-03-18 13:47:37 +0100
committerLoïc Hoguin <[email protected]>2011-03-18 13:47:37 +0100
commitc6ad0273a826809fcd3d6b47823623ba1c1e5127 (patch)
treec141a93fbb847dfd214a28f7b7938e03bef17f32 /src/cowboy_http_protocol.erl
parent5e80e4baaca6ca866555a62db96b8e2623050521 (diff)
downloadcowboy-c6ad0273a826809fcd3d6b47823623ba1c1e5127.tar.gz
cowboy-c6ad0273a826809fcd3d6b47823623ba1c1e5127.tar.bz2
cowboy-c6ad0273a826809fcd3d6b47823623ba1c1e5127.zip
Introduce Handler:terminate to cleanup the handler's state.
Diffstat (limited to 'src/cowboy_http_protocol.erl')
-rw-r--r--src/cowboy_http_protocol.erl23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index 594699d..5d1e232 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -120,10 +120,24 @@ handler_init(Req, State=#state{handler={Handler, Opts}}) ->
State::#state{}) -> ok.
handler_loop(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
case Handler:handle(Req, HandlerState) of
- %% @todo {ok, HandlerState}; {mode, active}
+ %% @todo {ok, Req2, HandlerState2} -> and use them in handler_terminate
%% @todo Move the reply code to the cowboy_http_req module.
{reply, RCode, RHeaders, RBody} ->
- reply(RCode, RHeaders, RBody, State)
+ reply(RCode, RHeaders, RBody, State),
+ handler_terminate(HandlerState, Req, State)
+ %% @todo {mode, active}
+ end.
+
+-spec handler_terminate(HandlerState::term(), Req::#http_req{},
+ State::#state{}) -> ok.
+handler_terminate(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
+ Res = Handler:terminate(Req, HandlerState),
+ %% @todo We need to check if the Req has been replied to.
+ %% All requests must have a reply, at worst an error.
+ %% If a request started but wasn't completed, complete it.
+ case Res of
+ ok -> next_request(State);
+ closed -> terminate(State)
end.
-spec error_terminate(Code::http_status(), State::#state{}) -> ok.
@@ -138,14 +152,13 @@ terminate(#state{socket=Socket, transport=Transport}) ->
-spec reply(Code::http_status(), Headers::http_headers(), Body::iolist(),
State::#state{}) -> ok.
%% @todo Don't be naive about the headers!
-reply(Code, Headers, Body, State=#state{socket=Socket,
+reply(Code, Headers, Body, #state{socket=Socket,
transport=TransportMod, connection=Connection}) ->
StatusLine = ["HTTP/1.1 ", status(Code), "\r\n"],
BaseHeaders = ["Connection: ", atom_to_connection(Connection),
"\r\nContent-Length: ", integer_to_list(iolist_size(Body)), "\r\n"],
TransportMod:send(Socket,
- [StatusLine, BaseHeaders, Headers, "\r\n", Body]),
- next_request(State).
+ [StatusLine, BaseHeaders, Headers, "\r\n", Body]).
-spec next_request(State::#state{}) -> ok.
next_request(State=#state{connection=keepalive}) ->