2011-12-28 18:00:27 +01:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
2013-04-24 20:28:44 +02:00
|
|
|
-module(http_stream_body).
|
2011-12-28 18:00:27 +01:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-export([init/2]).
|
2011-12-28 18:00:27 +01:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
init(Req, Opts) ->
|
2014-09-30 20:12:13 +03:00
|
|
|
Body = proplists:get_value(body, Opts, "http_handler_stream_body"),
|
|
|
|
Reply = proplists:get_value(reply, Opts),
|
2013-01-05 20:19:43 +01:00
|
|
|
SFun = fun(Socket, Transport) -> Transport:send(Socket, Body) end,
|
2013-01-05 23:35:30 +01:00
|
|
|
Req2 = case Reply of
|
|
|
|
set_resp ->
|
|
|
|
SLen = iolist_size(Body),
|
|
|
|
cowboy_req:set_resp_body_fun(SLen, SFun, Req);
|
|
|
|
set_resp_close ->
|
2013-02-18 21:20:36 +00:00
|
|
|
cowboy_req:set_resp_body_fun(SFun, Req);
|
|
|
|
set_resp_chunked ->
|
|
|
|
%% Here Body should be a list of chunks, not a binary.
|
|
|
|
SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end,
|
|
|
|
cowboy_req:set_resp_body_fun(chunked, SFun2, Req)
|
2013-01-05 23:35:30 +01:00
|
|
|
end,
|
2014-09-30 20:12:13 +03:00
|
|
|
{ok, cowboy_req:reply(200, Req2), Opts}.
|