mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
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.
This commit is contained in:
parent
0c37925642
commit
5d1d9af6cd
1 changed files with 16 additions and 11 deletions
|
@ -1205,30 +1205,35 @@ response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{
|
||||||
_ -> response_merge_headers(Headers, RespHeaders, DefaultHeaders)
|
_ -> response_merge_headers(Headers, RespHeaders, DefaultHeaders)
|
||||||
end,
|
end,
|
||||||
Body2 = case Body of stream -> <<>>; _ -> Body end,
|
Body2 = case Body of stream -> <<>>; _ -> Body end,
|
||||||
Req2 = case OnResponse of
|
{Status2, FullHeaders2, Req2} = case OnResponse of
|
||||||
already_called -> Req;
|
already_called -> {Status, FullHeaders, Req};
|
||||||
undefined -> Req;
|
undefined -> {Status, FullHeaders, Req};
|
||||||
OnResponse ->
|
OnResponse ->
|
||||||
OnResponse(Status, FullHeaders, Body2,
|
case OnResponse(Status, FullHeaders, Body2,
|
||||||
%% Don't call 'onresponse' from the hook itself.
|
%% Don't call 'onresponse' from the hook itself.
|
||||||
Req#http_req{resp_headers=[], resp_body= <<>>,
|
Req#http_req{resp_headers=[], resp_body= <<>>,
|
||||||
onresponse=already_called})
|
onresponse=already_called}) of
|
||||||
|
StHdReq = {_, _, _} ->
|
||||||
|
StHdReq;
|
||||||
|
Req1 ->
|
||||||
|
{Status, FullHeaders, Req1}
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
ReplyType = case Req2#http_req.resp_state of
|
ReplyType = case Req2#http_req.resp_state of
|
||||||
waiting when Transport =:= cowboy_spdy, Body =:= stream ->
|
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},
|
ReqPid ! {?MODULE, resp_sent},
|
||||||
normal;
|
normal;
|
||||||
waiting when Transport =:= cowboy_spdy ->
|
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},
|
ReqPid ! {?MODULE, resp_sent},
|
||||||
normal;
|
normal;
|
||||||
RespState when RespState =:= waiting; RespState =:= waiting_stream ->
|
RespState when RespState =:= waiting; RespState =:= waiting_stream ->
|
||||||
HTTPVer = atom_to_binary(Version, latin1),
|
HTTPVer = atom_to_binary(Version, latin1),
|
||||||
StatusLine = << HTTPVer/binary, " ",
|
StatusLine = << HTTPVer/binary, " ",
|
||||||
(status(Status))/binary, "\r\n" >>,
|
(status(Status2))/binary, "\r\n" >>,
|
||||||
HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>]
|
HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>]
|
||||||
|| {Key, Value} <- FullHeaders],
|
|| {Key, Value} <- FullHeaders2],
|
||||||
Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>, Body2]),
|
Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>, Body2]),
|
||||||
ReqPid ! {?MODULE, resp_sent},
|
ReqPid ! {?MODULE, resp_sent},
|
||||||
normal;
|
normal;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue