diff options
Diffstat (limited to 'src/cowboy_http_req.erl')
-rw-r--r-- | src/cowboy_http_req.erl | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index 40af601..a1688de 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -25,6 +25,10 @@ ]). %% Request API. -export([ + body/2 +]). %% Request Body API. + +-export([ reply/4 ]). %% Response API. @@ -115,8 +119,10 @@ bindings(Req) -> -spec header(Name::atom() | string(), Req::#http_req{}) -> {Value::string(), Req::#http_req{}}. header(Name, Req) -> - {Name, Value} = lists:keyfind(Name, 1, Req#http_req.headers), - {Value, Req}. + case lists:keyfind(Name, 1, Req#http_req.headers) of + {Name, Value} -> {Value, Req}; + false -> {"", Req} + end. -spec header(Name::atom() | string(), Default::term(), Req::#http_req{}) -> {Value::string() | term(), Req::#http_req{}}. @@ -129,6 +135,19 @@ header(Name, Default, Req) -> headers(Req) -> {Req#http_req.headers, Req}. +%% Request Body API. + +%% @todo We probably want to configure the timeout. +%% @todo We probably want to allow a max length. +-spec body(Length::non_neg_integer(), Req::#http_req{}) + -> {Body::binary(), Req::#http_req{}} | {error, Reason::posix()}. +body(Length, Req=#http_req{socket=Socket, transport=Transport, body_state=waiting}) -> + Transport:setopts(Socket, [{packet, raw}]), + case Transport:recv(Socket, Length, 5000) of + {ok, Body} -> {ok, Body, Req#http_req{body_state=done}}; + {error, Reason} -> {error, Reason} + end. + %% Response API. -spec reply(Code::http_status(), Headers::http_headers(), |