aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-06-03 18:31:05 +0200
committerLoïc Hoguin <[email protected]>2014-06-03 18:31:05 +0200
commit5d1d9af6cd2b91df567e482edfac2e1da104e267 (patch)
tree7caf428429849f637d4c14c0ea979937defe2f5d /src
parent0c379256423f9db3b1a280b686baa0b965a4c739 (diff)
downloadcowboy-5d1d9af6cd2b91df567e482edfac2e1da104e267.tar.gz
cowboy-5d1d9af6cd2b91df567e482edfac2e1da104e267.tar.bz2
cowboy-5d1d9af6cd2b91df567e482edfac2e1da104e267.zip
Add a return value to onresponse hook to override status/headers
This would allow us to override them without messing up the body, and would make it usable with the static file handler for example. Experimental at this point.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_req.erl27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index a651515..02089c2 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -1205,30 +1205,35 @@ response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{
_ -> response_merge_headers(Headers, RespHeaders, DefaultHeaders)
end,
Body2 = case Body of stream -> <<>>; _ -> Body end,
- Req2 = case OnResponse of
- already_called -> Req;
- undefined -> Req;
+ {Status2, FullHeaders2, Req2} = case OnResponse of
+ already_called -> {Status, FullHeaders, Req};
+ undefined -> {Status, FullHeaders, Req};
OnResponse ->
- OnResponse(Status, FullHeaders, Body2,
- %% Don't call 'onresponse' from the hook itself.
- Req#http_req{resp_headers=[], resp_body= <<>>,
- onresponse=already_called})
+ case OnResponse(Status, FullHeaders, Body2,
+ %% Don't call 'onresponse' from the hook itself.
+ Req#http_req{resp_headers=[], resp_body= <<>>,
+ onresponse=already_called}) of
+ StHdReq = {_, _, _} ->
+ StHdReq;
+ Req1 ->
+ {Status, FullHeaders, Req1}
+ end
end,
ReplyType = case Req2#http_req.resp_state of
waiting when Transport =:= cowboy_spdy, Body =:= stream ->
- cowboy_spdy:stream_reply(Socket, status(Status), FullHeaders),
+ cowboy_spdy:stream_reply(Socket, status(Status2), FullHeaders2),
ReqPid ! {?MODULE, resp_sent},
normal;
waiting when Transport =:= cowboy_spdy ->
- cowboy_spdy:reply(Socket, status(Status), FullHeaders, Body),
+ cowboy_spdy:reply(Socket, status(Status2), FullHeaders2, Body),
ReqPid ! {?MODULE, resp_sent},
normal;
RespState when RespState =:= waiting; RespState =:= waiting_stream ->
HTTPVer = atom_to_binary(Version, latin1),
StatusLine = << HTTPVer/binary, " ",
- (status(Status))/binary, "\r\n" >>,
+ (status(Status2))/binary, "\r\n" >>,
HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>]
- || {Key, Value} <- FullHeaders],
+ || {Key, Value} <- FullHeaders2],
Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>, Body2]),
ReqPid ! {?MODULE, resp_sent},
normal;