stream_handler_h.erl 3.7 KB

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