aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_req.erl
diff options
context:
space:
mode:
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.