diff options
Diffstat (limited to 'src/cowboy_req.erl')
-rw-r--r-- | src/cowboy_req.erl | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 89758dd..7f7ef32 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -163,7 +163,8 @@ | {non_neg_integer(), resp_body_fun()}, %% Functions. - onresponse = undefined :: undefined | cowboy_protocol:onresponse_fun() + onresponse = undefined :: undefined | already_called + | cowboy_protocol:onresponse_fun() }). -opaque req() :: #http_req{}. @@ -555,11 +556,10 @@ set_meta(Name, Value, Req=#http_req{meta=Meta}) -> %% Request Body API. %% @doc Return whether the request message has a body. --spec has_body(Req) -> {boolean(), Req} when Req::req(). +-spec has_body(cowboy_req:req()) -> boolean(). has_body(Req) -> - Has = lists:keymember(<<"content-length">>, 1, Req#http_req.headers) orelse - lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers), - {Has, Req}. + lists:keymember(<<"content-length">>, 1, Req#http_req.headers) orelse + lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers). %% @doc Return the request message body length, if known. %% @@ -728,7 +728,6 @@ skip_body(Req) -> %% @doc Return the full body sent with the request, parsed as an %% application/x-www-form-urlencoded string. Essentially a POST query string. -%% @todo We need an option to limit the size of the body for QS too. -spec body_qs(Req) -> {ok, [{binary(), binary() | true}], Req} | {error, atom()} when Req::req(). @@ -765,7 +764,6 @@ multipart_data(Req=#http_req{multipart={Length, Cont}}) -> multipart_data(Req=#http_req{body_state=done}) -> {eof, Req}. -%% @todo Typespecs. multipart_data(Req, Length, {headers, Headers, Cont}) -> {headers, Headers, Req#http_req{multipart={Length, Cont}}}; multipart_data(Req, Length, {body, Data, Cont}) -> @@ -868,6 +866,8 @@ has_resp_header(Name, #http_req{resp_headers=RespHeaders}) -> %% @doc Return whether a body has been set for the response. -spec has_resp_body(req()) -> boolean(). +has_resp_body(#http_req{resp_body=RespBody}) when is_function(RespBody) -> + true; has_resp_body(#http_req{resp_body={Length, _}}) -> Length > 0; has_resp_body(#http_req{resp_body=RespBody}) -> @@ -1163,13 +1163,17 @@ to_list(Req) -> response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{ socket=Socket, transport=Transport, version=Version, pid=ReqPid, onresponse=OnResponse}) -> - FullHeaders = response_merge_headers(Headers, RespHeaders, DefaultHeaders), + FullHeaders = case OnResponse of + already_called -> Headers; + _ -> response_merge_headers(Headers, RespHeaders, DefaultHeaders) + end, Req2 = case OnResponse of + already_called -> Req; undefined -> Req; OnResponse -> OnResponse(Status, FullHeaders, Body, %% Don't call 'onresponse' from the hook itself. Req#http_req{resp_headers=[], resp_body= <<>>, - onresponse=undefined}) + onresponse=already_called}) end, ReplyType = case Req2#http_req.resp_state of waiting -> |