http_stream_body.erl 903 B

12345678910111213141516171819202122232425262728
  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 () ->
  8. cowboy_req:send_body(Body, nofin, Req)
  9. end,
  10. Req2 = case Reply of
  11. set_resp ->
  12. SLen = iolist_size(Body),
  13. cowboy_req:set_resp_body({stream, SLen, SFun}, Req);
  14. %% @todo Hmm that one will be sent as chunked now.
  15. %% We need an option to disable chunked.
  16. set_resp_close ->
  17. cowboy_req:set_resp_body({stream, undefined, SFun}, Req);
  18. set_resp_chunked ->
  19. %% Here Body should be a list of chunks, not a binary.
  20. SFun2 = fun () ->
  21. lists:foreach(fun (Data) -> cowboy_req:send_body(Data, nofin, Req) end, Body)
  22. end,
  23. cowboy_req:set_resp_body({stream, undefined, SFun2}, Req)
  24. end,
  25. {ok, cowboy_req:reply(200, Req2), Opts}.