stream_handler_h.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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=#state{test=shutdown_on_stream_stop}) ->
  32. Spawn = init_process(false, State),
  33. [{headers, 200, #{}}, {spawn, Spawn, 5000}, stop];
  34. init_commands(_, _, State=#state{test=shutdown_on_socket_close}) ->
  35. Spawn = init_process(false, State),
  36. [{headers, 200, #{}}, {spawn, Spawn, 5000}];
  37. init_commands(_, _, State=#state{test=shutdown_timeout_on_stream_stop}) ->
  38. Spawn = init_process(true, State),
  39. [{headers, 200, #{}}, {spawn, Spawn, 2000}, stop];
  40. init_commands(_, _, State=#state{test=shutdown_timeout_on_socket_close}) ->
  41. Spawn = init_process(true, State),
  42. [{headers, 200, #{}}, {spawn, Spawn, 2000}];
  43. init_commands(_, _, State=#state{test=switch_protocol_after_headers}) ->
  44. [{headers, 200, #{}}, {switch_protocol, #{}, ?MODULE, State}];
  45. init_commands(_, _, State=#state{test=switch_protocol_after_headers_data}) ->
  46. [{headers, 200, #{}}, {data, fin, <<"{}">>}, {switch_protocol, #{}, ?MODULE, State}];
  47. init_commands(_, _, State=#state{test=switch_protocol_after_response}) ->
  48. [{response, 200, #{}, <<"{}">>}, {switch_protocol, #{}, ?MODULE, State}];
  49. init_commands(_, _, State=#state{test=terminate_on_switch_protocol}) ->
  50. [{switch_protocol, #{}, ?MODULE, State}];
  51. init_commands(_, _, #state{test=terminate_on_stop}) ->
  52. [{response, 204, #{}, <<>>}];
  53. init_commands(_, _, _) ->
  54. [{headers, 200, #{}}].
  55. init_process(TrapExit, #state{pid=Pid}) ->
  56. Self = self(),
  57. Spawn = spawn_link(fun() ->
  58. process_flag(trap_exit, TrapExit),
  59. Pid ! {Pid, Self, spawned, self()},
  60. receive {Pid, ready} -> ok after 1000 -> error(timeout) end,
  61. Self ! {self(), ready},
  62. receive after 5000 ->
  63. Pid ! {Pid, Self, still_alive, self()}
  64. end
  65. end),
  66. receive {Spawn, ready} -> ok after 1000 -> error(timeout) end,
  67. Spawn.
  68. data(_, _, _, #state{test=crash_in_data}) ->
  69. error(crash);
  70. data(StreamID, IsFin, Data, State=#state{pid=Pid}) ->
  71. Pid ! {Pid, self(), data, StreamID, IsFin, Data, State},
  72. {[], State}.
  73. info(_, Resp={response, _, _, _}, State) ->
  74. {[Resp], State};
  75. info(_, crash, #state{test=crash_in_info}) ->
  76. error(crash);
  77. info(StreamID, Info, State=#state{pid=Pid}) ->
  78. Pid ! {Pid, self(), info, StreamID, Info, State},
  79. case Info of
  80. please_stop -> {[stop], State};
  81. _ -> {[Info], State}
  82. end.
  83. terminate(StreamID, Reason, State=#state{pid=Pid, test=crash_in_terminate}) ->
  84. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  85. error(crash);
  86. terminate(StreamID, Reason, State=#state{pid=Pid}) ->
  87. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  88. ok.
  89. %% This clause can only test for early errors that reached the required headers.
  90. early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
  91. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))),
  92. Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts},
  93. case cowboy_req:header(<<"x-test-case">>, PartialReq) of
  94. <<"crash_in_early_error",_/bits>> -> error(crash);
  95. _ -> Resp
  96. end.
  97. %% @todo It would be good if we could allow this function to return normally.
  98. -spec takeover(_, _, _, _, _, _, _) -> no_return().
  99. takeover(Parent, Ref, Socket, Transport, Opts, Buffer, State=#state{pid=Pid}) ->
  100. Pid ! {Pid, self(), takeover, Parent, Ref, Socket, Transport, Opts, Buffer, State},
  101. exit(normal).