aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_req.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-03-21 17:26:00 +0100
committerLoïc Hoguin <[email protected]>2011-03-21 17:47:17 +0100
commit8b02992e6abd63eab0aafe0d762c38dbbbb757b5 (patch)
tree31e93989da6065739ac3389073043553430bb63d /src/cowboy_http_req.erl
parente40001a8843dbb802331a422c78ce65d7c660484 (diff)
downloadcowboy-8b02992e6abd63eab0aafe0d762c38dbbbb757b5.tar.gz
cowboy-8b02992e6abd63eab0aafe0d762c38dbbbb757b5.tar.bz2
cowboy-8b02992e6abd63eab0aafe0d762c38dbbbb757b5.zip
Skip the request body if it hasn't been read by the handler.
Diffstat (limited to 'src/cowboy_http_req.erl')
-rw-r--r--src/cowboy_http_req.erl23
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(),