http_SUITE.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(http_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(ct_helper, [get_remote_pid_tcp/1]).
  20. -import(cowboy_test, [gun_open/1]).
  21. -import(cowboy_test, [raw_open/1]).
  22. -import(cowboy_test, [raw_send/2]).
  23. -import(cowboy_test, [raw_recv_head/1]).
  24. -import(cowboy_test, [raw_recv/3]).
  25. -import(cowboy_test, [raw_expect_recv/2]).
  26. all() -> [{group, clear}].
  27. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  28. init_routes(_) -> [
  29. {"localhost", [
  30. {"/", hello_h, []},
  31. {"/echo/:key", echo_h, []},
  32. {"/resp/:key[/:arg]", resp_h, []},
  33. {"/set_options/:key", set_options_h, []}
  34. ]}
  35. ].
  36. chunked_false(Config) ->
  37. doc("Confirm the option chunked => false disables chunked "
  38. "transfer-encoding for HTTP/1.1 connections."),
  39. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  40. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  41. chunked => false
  42. }),
  43. Port = ranch:get_port(?FUNCTION_NAME),
  44. try
  45. Request = "GET /resp/stream_reply2/200 HTTP/1.1\r\nhost: localhost\r\n\r\n",
  46. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  47. ok = raw_send(Client, Request),
  48. Rest = case catch raw_recv_head(Client) of
  49. {'EXIT', _} -> error(closed);
  50. Data ->
  51. %% Cowboy always advertises itself as HTTP/1.1.
  52. {'HTTP/1.1', 200, _, Rest0} = cow_http:parse_status_line(Data),
  53. {Headers, Rest1} = cow_http:parse_headers(Rest0),
  54. false = lists:keyfind(<<"content-length">>, 1, Headers),
  55. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  56. Rest1
  57. end,
  58. Bits = 8000000 - bit_size(Rest),
  59. raw_expect_recv(Client, <<0:Bits>>),
  60. {error, closed} = raw_recv(Client, 1, 1000)
  61. after
  62. cowboy:stop_listener(?FUNCTION_NAME)
  63. end.
  64. http10_keepalive_false(Config) ->
  65. doc("Confirm the option http10_keepalive => false disables keep-alive "
  66. "completely for HTTP/1.0 connections."),
  67. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  68. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  69. http10_keepalive => false
  70. }),
  71. Port = ranch:get_port(?FUNCTION_NAME),
  72. try
  73. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  74. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  75. ok = raw_send(Client, Keepalive),
  76. _ = case catch raw_recv_head(Client) of
  77. {'EXIT', _} -> error(closed);
  78. Data ->
  79. %% Cowboy always advertises itself as HTTP/1.1.
  80. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  81. {Headers, _} = cow_http:parse_headers(Rest),
  82. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  83. end,
  84. ok = raw_send(Client, Keepalive),
  85. case catch raw_recv_head(Client) of
  86. {'EXIT', _} -> closed;
  87. _ -> error(not_closed)
  88. end
  89. after
  90. cowboy:stop_listener(?FUNCTION_NAME)
  91. end.
  92. idle_timeout_infinity(Config) ->
  93. doc("Ensure the idle_timeout option accepts the infinity value."),
  94. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  95. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  96. request_timeout => 500,
  97. idle_timeout => infinity
  98. }),
  99. Port = ranch:get_port(?FUNCTION_NAME),
  100. try
  101. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  102. {ok, http} = gun:await_up(ConnPid),
  103. _ = gun:post(ConnPid, "/echo/read_body",
  104. [{<<"content-type">>, <<"text/plain">>}]),
  105. #{socket := Socket} = gun:info(ConnPid),
  106. Pid = get_remote_pid_tcp(Socket),
  107. Ref = erlang:monitor(process, Pid),
  108. receive
  109. {'DOWN', Ref, process, Pid, Reason} ->
  110. error(Reason)
  111. after 1000 ->
  112. ok
  113. end
  114. after
  115. cowboy:stop_listener(?FUNCTION_NAME)
  116. end.
  117. request_timeout_infinity(Config) ->
  118. doc("Ensure the request_timeout option accepts the infinity value."),
  119. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  120. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  121. idle_timeout => infinity
  122. }),
  123. Port = ranch:get_port(?FUNCTION_NAME),
  124. try
  125. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  126. {ok, http} = gun:await_up(ConnPid),
  127. #{socket := Socket} = gun:info(ConnPid),
  128. Pid = get_remote_pid_tcp(Socket),
  129. Ref = erlang:monitor(process, Pid),
  130. receive
  131. {'DOWN', Ref, process, Pid, Reason} ->
  132. error(Reason)
  133. after 1000 ->
  134. ok
  135. end
  136. after
  137. cowboy:stop_listener(?FUNCTION_NAME)
  138. end.
  139. set_options_chunked_false(Config) ->
  140. doc("Confirm the option chunked can be dynamically set to disable "
  141. "chunked transfer-encoding. This results in the closing of the "
  142. "connection after the current request."),
  143. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  144. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  145. chunked => true
  146. }),
  147. Port = ranch:get_port(?FUNCTION_NAME),
  148. try
  149. Request = "GET /set_options/chunked_false HTTP/1.1\r\nhost: localhost\r\n\r\n",
  150. Client = raw_open([{type, tcp}, {port, Port}, {opts, []}|Config]),
  151. ok = raw_send(Client, Request),
  152. _ = case catch raw_recv_head(Client) of
  153. {'EXIT', _} -> error(closed);
  154. Data ->
  155. %% Cowboy always advertises itself as HTTP/1.1.
  156. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  157. {Headers, <<>>} = cow_http:parse_headers(Rest),
  158. false = lists:keyfind(<<"content-length">>, 1, Headers),
  159. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers)
  160. end,
  161. raw_expect_recv(Client, <<0:8000000>>),
  162. {error, closed} = raw_recv(Client, 1, 1000)
  163. after
  164. cowboy:stop_listener(?FUNCTION_NAME)
  165. end.
  166. set_options_chunked_false_ignored(Config) ->
  167. doc("Confirm the option chunked can be dynamically set to disable "
  168. "chunked transfer-encoding, and that it is ignored if the "
  169. "response is not streamed."),
  170. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  171. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  172. chunked => true
  173. }),
  174. Port = ranch:get_port(?FUNCTION_NAME),
  175. try
  176. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  177. %% We do a first request setting the option but not
  178. %% using chunked transfer-encoding in the response.
  179. StreamRef1 = gun:get(ConnPid, "/set_options/chunked_false_ignored"),
  180. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef1),
  181. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, StreamRef1),
  182. %% We then do a second request to confirm that chunked
  183. %% is not disabled for that second request.
  184. StreamRef2 = gun:get(ConnPid, "/resp/stream_reply2/200"),
  185. {response, nofin, 200, Headers} = gun:await(ConnPid, StreamRef2),
  186. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers)
  187. after
  188. cowboy:stop_listener(?FUNCTION_NAME)
  189. end.
  190. set_options_idle_timeout(Config) ->
  191. doc("Confirm that the idle_timeout option can be dynamically "
  192. "set to change how long Cowboy will wait before it closes the connection."),
  193. %% We start with a long timeout and then cut it short.
  194. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  195. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  196. idle_timeout => 60000
  197. }),
  198. Port = ranch:get_port(?FUNCTION_NAME),
  199. try
  200. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  201. {ok, http} = gun:await_up(ConnPid),
  202. _ = gun:post(ConnPid, "/set_options/idle_timeout_short",
  203. [{<<"content-type">>, <<"text/plain">>}]),
  204. #{socket := Socket} = gun:info(ConnPid),
  205. Pid = get_remote_pid_tcp(Socket),
  206. Ref = erlang:monitor(process, Pid),
  207. receive
  208. {'DOWN', Ref, process, Pid, _} ->
  209. ok
  210. after 2000 ->
  211. error(timeout)
  212. end
  213. after
  214. cowboy:stop_listener(?FUNCTION_NAME)
  215. end.
  216. set_options_idle_timeout_only_applies_to_current_request(Config) ->
  217. doc("Confirm that changes to the idle_timeout option only apply to the current stream."),
  218. %% We start with a long timeout and then cut it short.
  219. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], #{
  220. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  221. idle_timeout => 500
  222. }),
  223. Port = ranch:get_port(?FUNCTION_NAME),
  224. try
  225. ConnPid = gun_open([{type, tcp}, {protocol, http}, {port, Port}|Config]),
  226. {ok, http} = gun:await_up(ConnPid),
  227. StreamRef = gun:post(ConnPid, "/set_options/idle_timeout_long",
  228. [{<<"content-type">>, <<"text/plain">>}]),
  229. #{socket := Socket} = gun:info(ConnPid),
  230. Pid = get_remote_pid_tcp(Socket),
  231. Ref = erlang:monitor(process, Pid),
  232. receive
  233. {'DOWN', Ref, process, Pid, Reason} ->
  234. error(Reason)
  235. after 2000 ->
  236. ok
  237. end,
  238. %% Finish the first request and start a second one to confirm
  239. %% the idle_timeout option is back to normal.
  240. gun:data(ConnPid, StreamRef, fin, <<"Hello!">>),
  241. {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
  242. {ok, <<"Hello!">>} = gun:await_body(ConnPid, StreamRef),
  243. _ = gun:post(ConnPid, "/echo/read_body",
  244. [{<<"content-type">>, <<"text/plain">>}]),
  245. receive
  246. {'DOWN', Ref, process, Pid, _} ->
  247. ok
  248. after 2000 ->
  249. error(timeout)
  250. end
  251. after
  252. cowboy:stop_listener(?FUNCTION_NAME)
  253. end.
  254. switch_protocol_flush(Config) ->
  255. doc("Confirm that switch_protocol does not flush unrelated messages."),
  256. ProtoOpts = #{
  257. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  258. stream_handlers => [switch_protocol_flush_h]
  259. },
  260. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  261. Port = ranch:get_port(?FUNCTION_NAME),
  262. try
  263. Self = self(),
  264. ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]),
  265. _ = gun:get(ConnPid, "/", [
  266. {<<"x-test-pid">>, pid_to_list(Self)}
  267. ]),
  268. receive
  269. {Self, Events} ->
  270. switch_protocol_flush_h:validate(Events)
  271. after 5000 ->
  272. error(timeout)
  273. end
  274. after
  275. cowboy:stop_listener(?FUNCTION_NAME)
  276. end.