From 0aaa717575dd4a9287a473381d90b7cef15cda95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sat, 15 Sep 2012 22:19:39 +0200 Subject: Remove duplicate code for ensure_response --- src/cowboy_req.erl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/cowboy_req.erl') 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. -- cgit v1.2.3