set_options_h.erl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  9. #{pid := Pid, streamid := StreamID} = Req0,
  10. Pid ! {{Pid, StreamID}, {set_options, #{chunked => false}}},
  11. Req = cowboy_req:stream_reply(200, Req0),
  12. cowboy_req:stream_body(<<0:8000000>>, fin, Req),
  13. {ok, Req, State};
  14. set_options(<<"chunked_false_ignored">>, Req0, State) ->
  15. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  16. #{pid := Pid, streamid := StreamID} = Req0,
  17. Pid ! {{Pid, StreamID}, {set_options, #{chunked => false}}},
  18. Req = cowboy_req:reply(200, #{}, <<"Hello world!">>, Req0),
  19. {ok, Req, State};
  20. set_options(<<"idle_timeout_short">>, Req0, State) ->
  21. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  22. #{pid := Pid, streamid := StreamID} = Req0,
  23. Pid ! {{Pid, StreamID}, {set_options, #{idle_timeout => 500}}},
  24. {_, Body, Req} = cowboy_req:read_body(Req0),
  25. {ok, cowboy_req:reply(200, #{}, Body, Req), State};
  26. set_options(<<"idle_timeout_long">>, Req0, State) ->
  27. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  28. #{pid := Pid, streamid := StreamID} = Req0,
  29. Pid ! {{Pid, StreamID}, {set_options, #{idle_timeout => 60000}}},
  30. {_, Body, Req} = cowboy_req:read_body(Req0),
  31. {ok, cowboy_req:reply(200, #{}, Body, Req), State};
  32. set_options(<<"metrics_user_data">>, Req, State) ->
  33. %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
  34. #{pid := Pid, streamid := StreamID} = Req,
  35. Pid ! {{Pid, StreamID}, {set_options, #{metrics_user_data => #{handler => ?MODULE}}}},
  36. {ok, cowboy_req:reply(200, #{}, <<"Hello world!">>, Req), State}.