http_stream_body.erl 729 B

12345678910111213141516171819202122
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(http_stream_body).
  3. -export([init/2]).
  4. init(Req, Opts) ->
  5. Body = proplists:get_value(body, Opts, "http_handler_stream_body"),
  6. Reply = proplists:get_value(reply, Opts),
  7. SFun = fun(Socket, Transport) -> Transport:send(Socket, Body) end,
  8. Req2 = case Reply of
  9. set_resp ->
  10. SLen = iolist_size(Body),
  11. cowboy_req:set_resp_body_fun(SLen, SFun, Req);
  12. set_resp_close ->
  13. cowboy_req:set_resp_body_fun(SFun, Req);
  14. set_resp_chunked ->
  15. %% Here Body should be a list of chunks, not a binary.
  16. SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end,
  17. cowboy_req:set_resp_body_fun(chunked, SFun2, Req)
  18. end,
  19. {ok, cowboy_req:reply(200, Req2), Opts}.