set_options_h.erl 1.3 KB

123456789101112131415161718192021222324252627282930
  1. %% This module sets options dynamically and performs
  2. %% some related relevant operation for testing the change.
  3. -module(set_options_h).
  4. -export([init/2]).
  5. init(Req, State) ->
  6. set_options(cowboy_req:binding(key, Req), Req, State).
  7. set_options(<<"chunked_false">>, Req0, State) ->
  8. cowboy_req:cast({set_options, #{chunked => false}}, Req0),
  9. Req = cowboy_req:stream_reply(200, Req0),
  10. cowboy_req:stream_body(<<0:8000000>>, fin, Req),
  11. {ok, Req, State};
  12. set_options(<<"chunked_false_ignored">>, Req0, State) ->
  13. cowboy_req:cast({set_options, #{chunked => false}}, Req0),
  14. Req = cowboy_req:reply(200, #{}, <<"Hello world!">>, Req0),
  15. {ok, Req, State};
  16. set_options(<<"idle_timeout_short">>, Req0, State) ->
  17. cowboy_req:cast({set_options, #{idle_timeout => 500}}, Req0),
  18. {_, Body, Req} = cowboy_req:read_body(Req0),
  19. {ok, cowboy_req:reply(200, #{}, Body, Req), State};
  20. set_options(<<"idle_timeout_long">>, Req0, State) ->
  21. cowboy_req:cast({set_options, #{idle_timeout => 60000}}, Req0),
  22. {_, Body, Req} = cowboy_req:read_body(Req0),
  23. {ok, cowboy_req:reply(200, #{}, Body, Req), State};
  24. set_options(<<"metrics_user_data">>, Req, State) ->
  25. cowboy_req:cast({set_options, #{metrics_user_data => #{handler => ?MODULE}}}, Req),
  26. {ok, cowboy_req:reply(200, #{}, <<"Hello world!">>, Req), State}.