aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_req.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-01-03 23:26:01 +0100
committerLoïc Hoguin <[email protected]>2012-01-03 23:39:45 +0100
commitbd8f31ed07f50d924788cb196fb4184accbfc4a2 (patch)
tree75984e557fe642dc3e2e74b8a0ae2e1c14c46aa3 /src/cowboy_http_req.erl
parent063f64a02a398336cdac08f3394905fd378adf60 (diff)
parent89870b22d9a27e40407ed037258b8b7dbf73aefa (diff)
downloadcowboy-bd8f31ed07f50d924788cb196fb4184accbfc4a2.tar.gz
cowboy-bd8f31ed07f50d924788cb196fb4184accbfc4a2.tar.bz2
cowboy-bd8f31ed07f50d924788cb196fb4184accbfc4a2.zip
Merge branch 'static-handler-split' of https://github.com/klaar/cowboy
Diffstat (limited to 'src/cowboy_http_req.erl')
-rw-r--r--src/cowboy_http_req.erl53
1 files changed, 45 insertions, 8 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index a995871..057ea4d 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -39,14 +39,14 @@
-export([
set_resp_cookie/4, set_resp_header/3, set_resp_body/2,
- has_resp_header/2, has_resp_body/1,
+ set_resp_body_fun/3, has_resp_header/2, has_resp_body/1,
reply/2, reply/3, reply/4,
chunked_reply/2, chunked_reply/3, chunk/2,
upgrade_reply/3
]). %% Response API.
-export([
- compact/1
+ compact/1, transport/1
]). %% Misc API.
-include("include/http.hrl").
@@ -419,11 +419,33 @@ set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
%% @doc Add a body to the response.
%%
%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3.
+%% anything other than reply/2 or reply/3. The response body is expected
+%% to be a binary or an iolist.
-spec set_resp_body(iodata(), #http_req{}) -> {ok, #http_req{}}.
set_resp_body(Body, Req) ->
{ok, Req#http_req{resp_body=Body}}.
+
+%% @doc Add a body function to the response.
+%%
+%% The response body may also be set to a content-length - stream-function pair.
+%% If the response body is of this type normal response headers will be sent.
+%% After the response headers has been sent the body function is applied.
+%% The body function is expected to write the response body directly to the
+%% socket using the transport module.
+%%
+%% If the body function crashes while writing the response body or writes fewer
+%% bytes than declared the behaviour is undefined. The body set here is ignored
+%% if the response is later sent using anything other than `reply/2' or
+%% `reply/3'.
+%%
+%% @see cowboy_http_req:transport/1.
+-spec set_resp_body_fun(non_neg_integer(), fun(() -> {sent, non_neg_integer()}),
+ #http_req{}) -> {ok, #http_req{}}.
+set_resp_body_fun(StreamLen, StreamFun, Req) ->
+ {ok, Req#http_req{resp_body={StreamLen, StreamFun}}}.
+
+
%% @doc Return whether the given header has been set for the response.
-spec has_resp_header(http_header(), #http_req{}) -> boolean().
has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
@@ -432,6 +454,8 @@ has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
%% @doc Return whether a body has been set for the response.
-spec has_resp_body(#http_req{}) -> boolean().
+has_resp_body(#http_req{resp_body={Length, _}}) ->
+ Length > 0;
has_resp_body(#http_req{resp_body=RespBody}) ->
iolist_size(RespBody) > 0.
@@ -452,16 +476,17 @@ reply(Status, Headers, Body, Req=#http_req{socket=Socket,
transport=Transport, connection=Connection,
method=Method, resp_state=waiting, resp_headers=RespHeaders}) ->
RespConn = response_connection(Headers, Connection),
+ ContentLen = case Body of {CL, _} -> CL; _ -> iolist_size(Body) end,
Head = response_head(Status, Headers, RespHeaders, [
{<<"Connection">>, atom_to_connection(Connection)},
- {<<"Content-Length">>,
- list_to_binary(integer_to_list(iolist_size(Body)))},
+ {<<"Content-Length">>, integer_to_list(ContentLen)},
{<<"Date">>, cowboy_clock:rfc1123()},
{<<"Server">>, <<"Cowboy">>}
]),
- case Method of
- 'HEAD' -> Transport:send(Socket, Head);
- _ -> Transport:send(Socket, [Head, Body])
+ case {Method, Body} of
+ {'HEAD', _} -> Transport:send(Socket, Head);
+ {_, {_, StreamFun}} -> Transport:send(Socket, Head), StreamFun();
+ {_, _} -> Transport:send(Socket, [Head, Body])
end,
{ok, Req#http_req{connection=RespConn, resp_state=done,
resp_headers=[], resp_body= <<>>}}.
@@ -523,6 +548,18 @@ compact(Req) ->
bindings=undefined, headers=[],
p_headers=[], cookies=[]}.
+%% @doc Return the transport module and socket associated with a request.
+%%
+%% This exposes the same socket interface used internally by the HTTP protocol
+%% implementation to developers that needs low level access to the socket.
+%%
+%% It is preferred to use this in conjuction with the stream function support
+%% in `set_resp_body_fun/3' if this is used to write a response body directly
+%% to the socket. This ensures that the response headers are set correctly.
+-spec transport(#http_req{}) -> {ok, module(), inet:socket()}.
+transport(#http_req{transport=Transport, socket=Socket}) ->
+ {ok, Transport, Socket}.
+
%% Internal.
-spec parse_qs(binary(), fun((binary()) -> binary())) ->