From 7cacb88fecc2d64609168167303aaca00181ee53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= <essen@dev-extend.eu>
Date: Mon, 21 Mar 2011 21:28:24 +0100
Subject: Introduce cowboy_http_req:body/1 to read the full request body.

---
 src/cowboy_http_protocol.erl | 13 ++-----------
 src/cowboy_http_req.erl      | 15 +++++++++++++--
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index 7209be4..34f0c0d 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -167,17 +167,8 @@ handler_terminate(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
 ensure_body_processed(#http_req{body_state=done}) ->
 	ok;
 ensure_body_processed(Req=#http_req{body_state=waiting}) ->
-	{Length, Req2} = cowboy_http_req:header('Content-Length', Req),
-	case Length of
-		"" -> 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
+	case cowboy_http_req:body(Req) of
+		{error, badarg} -> ok; %% No body.
 		{error, _Reason} -> close;
 		_Any -> ok
 	end.
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index a1688de..856c91c 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -25,7 +25,7 @@
 ]). %% Request API.
 
 -export([
-	body/2
+	body/1, body/2
 ]). %% Request Body API.
 
 -export([
@@ -137,8 +137,19 @@ headers(Req) ->
 
 %% Request Body API.
 
-%% @todo We probably want to configure the timeout.
 %% @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{})
 	-> {Body::binary(), Req::#http_req{}} | {error, Reason::posix()}.
 body(Length, Req=#http_req{socket=Socket, transport=Transport, body_state=waiting}) ->
-- 
cgit v1.2.3