stream_handler_h.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. %% This module behaves differently depending on a specific header.
  2. -module(stream_handler_h).
  3. -behavior(cowboy_stream).
  4. -export([init/3]).
  5. -export([data/4]).
  6. -export([info/3]).
  7. -export([terminate/3]).
  8. -export([early_error/5]).
  9. %% For switch_protocol.
  10. -export([takeover/7]).
  11. -record(state, {
  12. pid,
  13. test
  14. }).
  15. init(StreamID, Req, Opts) ->
  16. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
  17. Test = binary_to_atom(cowboy_req:header(<<"x-test-case">>, Req), latin1),
  18. State = #state{pid=Pid, test=Test},
  19. Pid ! {Pid, self(), init, StreamID, Req, Opts},
  20. {init_commands(StreamID, Req, State), State}.
  21. init_commands(_, _, #state{test=crash_in_init}) ->
  22. error(crash);
  23. init_commands(_, _, #state{test=crash_in_data}) ->
  24. [];
  25. init_commands(_, _, #state{test=crash_in_info}) ->
  26. [];
  27. init_commands(_, _, #state{test=crash_in_terminate}) ->
  28. [{response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>}, stop];
  29. init_commands(_, _, #state{test=crash_in_early_error}) ->
  30. error(crash);
  31. init_commands(_, _, #state{test=set_options_ignore_unknown}) ->
  32. [
  33. {set_options, #{unknown_options => true}},
  34. {response, 200, #{<<"content-length">> => <<"12">>}, <<"Hello world!">>},
  35. stop
  36. ];
  37. init_commands(_, _, State=#state{test=shutdown_on_stream_stop}) ->
  38. Spawn = init_process(false, State),
  39. [{headers, 200, #{}}, {spawn, Spawn, 5000}, stop];
  40. init_commands(_, _, State=#state{test=shutdown_on_socket_close}) ->
  41. Spawn = init_process(false, State),
  42. [{headers, 200, #{}}, {spawn, Spawn, 5000}];
  43. init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) ->
  44. Spawn = init_process(true, State),
  45. [{headers, 200, #{}}, {spawn, Spawn, 2000}, stop];
  46. init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) ->
  47. Spawn = init_process(true, State),
  48. [{headers, 200, #{}}, {spawn, Spawn, 2000}];
  49. init_commands(_, _, State=#state{test=switch_protocol_after_headers}) ->
  50. [{headers, 200, #{}}, {switch_protocol, #{}, ?MODULE, State}];
  51. init_commands(_, _, State=#state{test=switch_protocol_after_headers_data}) ->
  52. [{headers, 200, #{}}, {data, fin, <<"{}">>}, {switch_protocol, #{}, ?MODULE, State}];
  53. init_commands(_, _, State=#state{test=switch_protocol_after_response}) ->
  54. [{response, 200, #{}, <<"{}">>}, {switch_protocol, #{}, ?MODULE, State}];
  55. init_commands(_, _, State=#state{test=terminate_on_switch_protocol}) ->
  56. [{switch_protocol, #{}, ?MODULE, State}];
  57. init_commands(_, _, #state{test=terminate_on_stop}) ->
  58. [{response, 204, #{}, <<>>}];
  59. init_commands(_, _, _) ->
  60. [{headers, 200, #{}}].
  61. init_process(TrapExit, #state{pid=Pid}) ->
  62. Self = self(),
  63. Spawn = spawn_link(fun() ->
  64. process_flag(trap_exit, TrapExit),
  65. Pid ! {Pid, Self, spawned, self()},
  66. receive {Pid, ready} -> ok after 1000 -> error(timeout) end,
  67. Self ! {self(), ready},
  68. receive after 5000 ->
  69. Pid ! {Pid, Self, still_alive, self()}
  70. end
  71. end),
  72. receive {Spawn, ready} -> ok after 1000 -> error(timeout) end,
  73. Spawn.
  74. data(_, _, _, #state{test=crash_in_data}) ->
  75. error(crash);
  76. data(StreamID, IsFin, Data, State=#state{pid=Pid}) ->
  77. Pid ! {Pid, self(), data, StreamID, IsFin, Data, State},
  78. {[], State}.
  79. info(_, Resp={response, _, _, _}, State) ->
  80. {[Resp], State};
  81. info(_, crash, #state{test=crash_in_info}) ->
  82. error(crash);
  83. info(StreamID, Info, State=#state{pid=Pid}) ->
  84. Pid ! {Pid, self(), info, StreamID, Info, State},
  85. case Info of
  86. please_stop -> {[stop], State};
  87. _ -> {[Info], State}
  88. end.
  89. terminate(StreamID, Reason, State=#state{pid=Pid, test=crash_in_terminate}) ->
  90. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  91. error(crash);
  92. terminate(StreamID, Reason, State=#state{pid=Pid}) ->
  93. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  94. ok.
  95. %% This clause can only test for early errors that reached the required headers.
  96. early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
  97. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))),
  98. Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts},
  99. case cowboy_req:header(<<"x-test-case">>, PartialReq) of
  100. <<"crash_in_early_error",_/bits>> -> error(crash);
  101. _ -> Resp
  102. end.
  103. %% @todo It would be good if we could allow this function to return normally.
  104. -spec takeover(_, _, _, _, _, _, _) -> no_return().
  105. takeover(Parent, Ref, Socket, Transport, Opts, Buffer, State=#state{pid=Pid}) ->
  106. Pid ! {Pid, self(), takeover, Parent, Ref, Socket, Transport, Opts, Buffer, State},
  107. exit(normal).