aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_req.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-09-15 22:19:39 +0200
committerLoïc Hoguin <[email protected]>2012-09-15 22:19:39 +0200
commit0aaa717575dd4a9287a473381d90b7cef15cda95 (patch)
tree31df63155e8acf5f39828b32cf240d89cb3750ee /src/cowboy_req.erl
parent27d591180ca3dd8b0d3c63c1293da5a3c4f4321f (diff)
downloadcowboy-0aaa717575dd4a9287a473381d90b7cef15cda95.tar.gz
cowboy-0aaa717575dd4a9287a473381d90b7cef15cda95.tar.bz2
cowboy-0aaa717575dd4a9287a473381d90b7cef15cda95.zip
Remove duplicate code for ensure_response
Diffstat (limited to 'src/cowboy_req.erl')
-rw-r--r--src/cowboy_req.erl22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 51b1874..ad6c1a4 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -76,6 +76,7 @@
-export([chunked_reply/3]).
-export([chunk/2]).
-export([upgrade_reply/3]).
+-export([ensure_response/2]).
%% Misc API.
-export([compact/1]).
@@ -826,6 +827,27 @@ upgrade_reply(Status, Headers, Req=#http_req{
], Req),
{ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
+%% @doc Ensure the response has been sent fully.
+%% @private
+-spec ensure_response(req(), cowboy_http:status()) -> ok.
+%% The response has already been fully sent to the client.
+ensure_response(#http_req{resp_state=done}, _) ->
+ ok;
+%% No response has been sent but everything apparently went fine.
+%% Reply with the status code found in the second argument.
+ensure_response(Req=#http_req{resp_state=waiting}, Status) ->
+ _ = reply(Status, [], [], Req),
+ ok;
+%% Terminate the chunked body for HTTP/1.1 only.
+ensure_response(#http_req{method='HEAD', resp_state=chunks}, _) ->
+ ok;
+ensure_response(#http_req{version={1, 0}, resp_state=chunks}, _) ->
+ ok;
+ensure_response(#http_req{socket=Socket, transport=Transport,
+ resp_state=chunks}, _) ->
+ Transport:send(Socket, <<"0\r\n\r\n">>),
+ ok.
+
%% Misc API.
%% @doc Compact the request data by removing all non-system information.