From 8b02992e6abd63eab0aafe0d762c38dbbbb757b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 21 Mar 2011 17:26:00 +0100 Subject: Skip the request body if it hasn't been read by the handler. --- src/cowboy_http_req.erl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/cowboy_http_req.erl') 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 @@ -24,6 +24,10 @@ %% cookie/2, cookie/3, cookies/1 @todo ]). %% 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(), -- cgit v1.2.3