http_echo_body.erl 618 B

12345678910111213141516171819202122232425
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(http_echo_body).
  3. -export([init/2]).
  4. -export([handle/2]).
  5. init(Req, Opts) ->
  6. {http, Req, Opts}.
  7. handle(Req, State) ->
  8. true = cowboy_req:has_body(Req),
  9. Req3 = case cowboy_req:body(Req, [{length, 1000000}]) of
  10. {ok, Body, Req2} -> handle_body(Req2, Body);
  11. {more, _, Req2} -> handle_badlength(Req2)
  12. end,
  13. {ok, Req3, State}.
  14. handle_badlength(Req) ->
  15. cowboy_req:reply(413, [], <<"Request entity too large">>, Req).
  16. handle_body(Req, Body) ->
  17. Size = cowboy_req:body_length(Req),
  18. Size = byte_size(Body),
  19. cowboy_req:reply(200, [], Body, Req).