mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Introduce cowboy_http_req:body/1 to read the full request body.
This commit is contained in:
parent
e9781e77f1
commit
7cacb88fec
2 changed files with 15 additions and 13 deletions
|
@ -167,17 +167,8 @@ handler_terminate(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
|
||||||
ensure_body_processed(#http_req{body_state=done}) ->
|
ensure_body_processed(#http_req{body_state=done}) ->
|
||||||
ok;
|
ok;
|
||||||
ensure_body_processed(Req=#http_req{body_state=waiting}) ->
|
ensure_body_processed(Req=#http_req{body_state=waiting}) ->
|
||||||
{Length, Req2} = cowboy_http_req:header('Content-Length', Req),
|
case cowboy_http_req:body(Req) of
|
||||||
case Length of
|
{error, badarg} -> ok; %% No body.
|
||||||
"" -> ok;
|
|
||||||
_Any ->
|
|
||||||
Length2 = list_to_integer(Length),
|
|
||||||
skip_body(Length2, Req2)
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec skip_body(Length::non_neg_integer(), Req::#http_req{}) -> ok | close.
|
|
||||||
skip_body(Length, Req) ->
|
|
||||||
case cowboy_http_req:body(Length, Req) of
|
|
||||||
{error, _Reason} -> close;
|
{error, _Reason} -> close;
|
||||||
_Any -> ok
|
_Any -> ok
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
]). %% Request API.
|
]). %% Request API.
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
body/2
|
body/1, body/2
|
||||||
]). %% Request Body API.
|
]). %% Request Body API.
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -137,8 +137,19 @@ headers(Req) ->
|
||||||
|
|
||||||
%% Request Body API.
|
%% Request Body API.
|
||||||
|
|
||||||
%% @todo We probably want to configure the timeout.
|
|
||||||
%% @todo We probably want to allow a max length.
|
%% @todo We probably want to allow a max length.
|
||||||
|
-spec body(Req::#http_req{})
|
||||||
|
-> {Body::binary(), Req::#http_req{}} | {error, Reason::posix()}.
|
||||||
|
body(Req) ->
|
||||||
|
{Length, Req2} = cowboy_http_req:header('Content-Length', Req),
|
||||||
|
case Length of
|
||||||
|
"" -> {error, badarg};
|
||||||
|
_Any ->
|
||||||
|
Length2 = list_to_integer(Length),
|
||||||
|
body(Length2, Req2)
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% @todo We probably want to configure the timeout.
|
||||||
-spec body(Length::non_neg_integer(), Req::#http_req{})
|
-spec body(Length::non_neg_integer(), Req::#http_req{})
|
||||||
-> {Body::binary(), Req::#http_req{}} | {error, Reason::posix()}.
|
-> {Body::binary(), Req::#http_req{}} | {error, Reason::posix()}.
|
||||||
body(Length, Req=#http_req{socket=Socket, transport=Transport, body_state=waiting}) ->
|
body(Length, Req=#http_req{socket=Socket, transport=Transport, body_state=waiting}) ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue