http_SUITE.erl 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. %% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -compile(export_all).
  17. -import(cowboy_test, [config/2]).
  18. -import(cowboy_test, [gun_open/1]).
  19. -import(cowboy_test, [gun_monitor_open/1]).
  20. -import(cowboy_test, [gun_monitor_open/2]).
  21. -import(cowboy_test, [gun_is_gone/2]).
  22. -import(cowboy_test, [raw_open/1]).
  23. -import(cowboy_test, [raw_send/2]).
  24. -import(cowboy_test, [raw_recv_head/1]).
  25. -import(cowboy_test, [raw_expect_recv/2]).
  26. %% ct.
  27. all() ->
  28. [
  29. {group, http},
  30. {group, https},
  31. {group, http_compress},
  32. {group, https_compress},
  33. {group, onresponse},
  34. {group, onresponse_capitalize},
  35. {group, parse_host},
  36. {group, set_env}
  37. ].
  38. groups() ->
  39. Tests = cowboy_test:all(?MODULE) -- [
  40. onresponse_crash, onresponse_reply, onresponse_capitalize,
  41. parse_host, set_env_dispatch
  42. ],
  43. [
  44. {http, [parallel], Tests},
  45. {https, [parallel], Tests},
  46. {http_compress, [parallel], Tests},
  47. {https_compress, [parallel], Tests},
  48. {onresponse, [parallel], [
  49. onresponse_crash,
  50. onresponse_reply
  51. ]},
  52. {onresponse_capitalize, [parallel], [
  53. onresponse_capitalize
  54. ]},
  55. {parse_host, [], [
  56. parse_host
  57. ]},
  58. {set_env, [], [
  59. set_env_dispatch
  60. ]}
  61. ].
  62. init_per_suite(Config) ->
  63. Dir = config(priv_dir, Config) ++ "/static",
  64. ct_helper:create_static_dir(Dir),
  65. [{static_dir, Dir}|Config].
  66. end_per_suite(Config) ->
  67. ct_helper:delete_static_dir(config(static_dir, Config)).
  68. init_per_group(Name = http, Config) ->
  69. cowboy_test:init_http(Name, [
  70. {env, [{dispatch, init_dispatch(Config)}]}
  71. ], Config);
  72. init_per_group(Name = https, Config) ->
  73. cowboy_test:init_https(Name, [
  74. {env, [{dispatch, init_dispatch(Config)}]}
  75. ], Config);
  76. init_per_group(Name = http_compress, Config) ->
  77. cowboy_test:init_http(Name, [
  78. {env, [{dispatch, init_dispatch(Config)}]},
  79. {compress, true}
  80. ], Config);
  81. init_per_group(Name = https_compress, Config) ->
  82. cowboy_test:init_https(Name, [
  83. {env, [{dispatch, init_dispatch(Config)}]},
  84. {compress, true}
  85. ], Config);
  86. %% Most, if not all of these, should be in separate test suites.
  87. init_per_group(onresponse, Config) ->
  88. {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
  89. {env, [{dispatch, init_dispatch(Config)}]},
  90. {max_keepalive, 50},
  91. {onresponse, fun do_onresponse_hook/4},
  92. {timeout, 500}
  93. ]),
  94. Port = ranch:get_port(onresponse),
  95. [{type, tcp}, {port, Port}, {opts, []}|Config];
  96. init_per_group(onresponse_capitalize, Config) ->
  97. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
  98. {env, [{dispatch, init_dispatch(Config)}]},
  99. {max_keepalive, 50},
  100. {onresponse, fun do_onresponse_capitalize_hook/4},
  101. {timeout, 500}
  102. ]),
  103. Port = ranch:get_port(onresponse_capitalize),
  104. [{type, tcp}, {port, Port}, {opts, []}|Config];
  105. init_per_group(parse_host, Config) ->
  106. Dispatch = cowboy_router:compile([
  107. {'_', [
  108. {"/req_attr", http_req_attr, []}
  109. ]}
  110. ]),
  111. {ok, _} = cowboy:start_http(parse_host, 100, [{port, 0}], [
  112. {env, [{dispatch, Dispatch}]},
  113. {max_keepalive, 50},
  114. {timeout, 500}
  115. ]),
  116. Port = ranch:get_port(parse_host),
  117. [{type, tcp}, {port, Port}, {opts, []}|Config];
  118. init_per_group(set_env, Config) ->
  119. {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
  120. {env, [{dispatch, []}]},
  121. {max_keepalive, 50},
  122. {timeout, 500}
  123. ]),
  124. Port = ranch:get_port(set_env),
  125. [{type, tcp}, {port, Port}, {opts, []}|Config].
  126. end_per_group(Name, _) ->
  127. ok = cowboy:stop_listener(Name).
  128. %% Dispatch configuration.
  129. init_dispatch(Config) ->
  130. cowboy_router:compile([
  131. {"localhost", [
  132. {"/chunked_response", http_chunked, []},
  133. {"/streamed_response", http_streamed, []},
  134. {"/init_shutdown", http_init_shutdown, []},
  135. {"/headers/dupe", http_handler,
  136. [{headers, [{<<"connection">>, <<"close">>}]}]},
  137. {"/set_resp/header", http_set_resp,
  138. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  139. {"/set_resp/overwrite", http_set_resp,
  140. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  141. {"/set_resp/body", http_set_resp,
  142. [{body, <<"A flameless dance does not equal a cycle">>}]},
  143. {"/stream_body/set_resp", http_stream_body,
  144. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  145. {"/stream_body/set_resp_close",
  146. http_stream_body, [
  147. {reply, set_resp_close},
  148. {body, <<"stream_body_set_resp_close">>}]},
  149. {"/stream_body/set_resp_chunked",
  150. http_stream_body, [
  151. {reply, set_resp_chunked},
  152. {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
  153. {"/static/[...]", cowboy_static,
  154. {dir, config(static_dir, Config)}},
  155. {"/static_mimetypes_function/[...]", cowboy_static,
  156. {dir, config(static_dir, Config),
  157. [{mimetypes, ?MODULE, do_mimetypes_text_html}]}},
  158. {"/handler_errors", http_errors, []},
  159. {"/static_attribute_etag/[...]", cowboy_static,
  160. {dir, config(static_dir, Config)}},
  161. {"/static_function_etag/[...]", cowboy_static,
  162. {dir, config(static_dir, Config),
  163. [{etag, ?MODULE, do_etag_gen}]}},
  164. {"/static_specify_file/[...]", cowboy_static,
  165. {file, config(static_dir, Config) ++ "/style.css"}},
  166. {"/multipart", http_multipart, []},
  167. {"/multipart/large", http_multipart_stream, []},
  168. {"/echo/body", http_echo_body, []},
  169. {"/echo/body_qs", http_body_qs, []},
  170. {"/crash/content-length", input_crash_h, content_length},
  171. {"/param_all", rest_param_all, []},
  172. {"/bad_accept", rest_simple_resource, []},
  173. {"/bad_content_type", rest_patch_resource, []},
  174. {"/simple", rest_simple_resource, []},
  175. {"/forbidden_post", rest_forbidden_resource, [true]},
  176. {"/simple_post", rest_forbidden_resource, [false]},
  177. {"/missing_get_callbacks", rest_missing_callbacks, []},
  178. {"/missing_put_callbacks", rest_missing_callbacks, []},
  179. {"/nodelete", rest_nodelete_resource, []},
  180. {"/post_charset", rest_post_charset_resource, []},
  181. {"/postonly", rest_postonly_resource, []},
  182. {"/patch", rest_patch_resource, []},
  183. {"/resetags", rest_resource_etags, []},
  184. {"/rest_expires", rest_expires, []},
  185. {"/rest_expires_binary", rest_expires_binary, []},
  186. {"/rest_empty_resource", rest_empty_resource, []},
  187. {"/loop_stream_recv", http_loop_stream_recv, []},
  188. {"/", http_handler, []}
  189. ]}
  190. ]).
  191. %% Callbacks.
  192. do_etag_gen(_, _, _) ->
  193. {strong, <<"etag">>}.
  194. do_mimetypes_text_html(_) ->
  195. <<"text/html">>.
  196. %% Convenience functions.
  197. do_raw(Data, Config) ->
  198. Client = raw_open(Config),
  199. ok = raw_send(Client, Data),
  200. case catch raw_recv_head(Client) of
  201. {'EXIT', _} -> closed;
  202. Resp -> element(2, cow_http:parse_status_line(Resp))
  203. end.
  204. do_get(Path, Config) ->
  205. ConnPid = gun_open(Config),
  206. Ref = gun:get(ConnPid, Path),
  207. {response, _, Status, _} = gun:await(ConnPid, Ref),
  208. gun:close(ConnPid),
  209. Status.
  210. %% Tests.
  211. check_raw_status(Config) ->
  212. Huge = [$0 || _ <- lists:seq(1, 5000)],
  213. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  214. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  215. ResponsePacket =
  216. "HTTP/1.0 302 Found\r
  217. Location: http://www.google.co.il/\r
  218. Cache-Control: private\r
  219. Content-Type: text/html; charset=UTF-8\r
  220. Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r
  221. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  222. Server: gws\r
  223. Content-Length: 221\r
  224. X-XSS-Protection: 1; mode=block\r
  225. X-Frame-Options: SAMEORIGIN\r
  226. \r
  227. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  228. <TITLE>302 Moved</TITLE></HEAD><BODY>
  229. <H1>302 Moved</H1>
  230. The document has moved
  231. <A HREF=\"http://www.google.co.il/\">here</A>.
  232. </BODY></HTML>",
  233. Tests = [
  234. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  235. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  236. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  237. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  238. {400, "\n"},
  239. {400, "Garbage\r\n\r\n"},
  240. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  241. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  242. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  243. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  244. {400, ["POST /crash/content-length HTTP/1.1\r\nHost: localhost\r\nContent-Length: 5000,5000\r\n\r\n", Huge]},
  245. {505, ResponsePacket},
  246. {408, "GET / HTTP/1.1\r\n"},
  247. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  248. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  249. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  250. {414, Huge},
  251. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  252. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  253. {closed, ""},
  254. {closed, "\r\n"},
  255. {closed, "\r\n\r\n"},
  256. {closed, "GET / HTTP/1.1"}
  257. ],
  258. _ = [{Status, Packet} = begin
  259. Ret = do_raw(Packet, Config),
  260. {Ret, Packet}
  261. end || {Status, Packet} <- Tests],
  262. ok.
  263. check_status(Config) ->
  264. Tests = [
  265. {200, "/"},
  266. {200, "/simple"},
  267. {400, "/static/%2f"},
  268. {400, "/static/%2e"},
  269. {400, "/static/%2e%2e"},
  270. {403, "/static/directory"},
  271. {403, "/static/directory/"},
  272. {403, "/static/unreadable"},
  273. {404, "/not/found"},
  274. {404, "/static/not_found"},
  275. {500, "/handler_errors?case=handle_before_reply"},
  276. {500, "/handler_errors?case=init_before_reply"},
  277. {666, "/init_shutdown"}
  278. ],
  279. _ = [{Status, URL} = begin
  280. Ret = do_get(URL, Config),
  281. {Ret, URL}
  282. end || {Status, URL} <- Tests].
  283. chunked_response(Config) ->
  284. ConnPid = gun_open(Config),
  285. Ref = gun:get(ConnPid, "/chunked_response"),
  286. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  287. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  288. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  289. ok.
  290. %% Check if sending requests whose size is around the MTU breaks something.
  291. echo_body(Config) ->
  292. MTU = ct_helper:get_loopback_mtu(),
  293. _ = [begin
  294. Body = list_to_binary(lists:duplicate(Size, $a)),
  295. ConnPid = gun_open(Config),
  296. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  297. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  298. {ok, Body} = gun:await_body(ConnPid, Ref)
  299. end || Size <- lists:seq(MTU - 500, MTU)],
  300. ok.
  301. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  302. echo_body_max_length(Config) ->
  303. ConnPid = gun_open(Config),
  304. Ref = gun:post(ConnPid, "/echo/body", [], << 0:2000000/unit:8 >>),
  305. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  306. ok.
  307. % check if body_qs echo's back results
  308. echo_body_qs(Config) ->
  309. ConnPid = gun_open(Config),
  310. Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
  311. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  312. {ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
  313. ok.
  314. echo_body_qs_max_length(Config) ->
  315. ConnPid = gun_open(Config),
  316. Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:2000000/unit:8 >>),
  317. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  318. ok.
  319. error_chain_handle_after_reply(Config) ->
  320. {ConnPid, MRef} = gun_monitor_open(Config),
  321. Ref1 = gun:get(ConnPid, "/"),
  322. Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
  323. {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
  324. {response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef),
  325. gun_is_gone(ConnPid, MRef).
  326. error_chain_handle_before_reply(Config) ->
  327. {ConnPid, MRef} = gun_monitor_open(Config),
  328. Ref1 = gun:get(ConnPid, "/"),
  329. Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"),
  330. {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
  331. {response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef),
  332. gun_is_gone(ConnPid, MRef).
  333. error_handle_after_reply(Config) ->
  334. {ConnPid, MRef} = gun_monitor_open(Config),
  335. Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
  336. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  337. gun_is_gone(ConnPid, MRef).
  338. error_init_after_reply(Config) ->
  339. {ConnPid, MRef} = gun_monitor_open(Config),
  340. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  341. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  342. gun_is_gone(ConnPid, MRef).
  343. error_init_reply_handle_error(Config) ->
  344. {ConnPid, MRef} = gun_monitor_open(Config),
  345. Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"),
  346. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  347. gun_is_gone(ConnPid, MRef).
  348. headers_dupe(Config) ->
  349. {ConnPid, MRef} = gun_monitor_open(Config),
  350. Ref = gun:get(ConnPid, "/headers/dupe"),
  351. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  352. %% Ensure that only one connection header was received.
  353. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  354. gun_is_gone(ConnPid, MRef).
  355. http10_chunkless(Config) ->
  356. {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
  357. Ref = gun:get(ConnPid, "/chunked_response"),
  358. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  359. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  360. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
  361. gun_is_gone(ConnPid, MRef).
  362. http10_hostless(Config) ->
  363. Name = http10_hostless,
  364. Port10 = config(port, Config) + 10,
  365. Transport = case config(type, Config) of
  366. tcp -> ranch_tcp;
  367. ssl -> ranch_ssl
  368. end,
  369. ranch:start_listener(Name, 5, Transport,
  370. config(opts, Config) ++ [{port, Port10}],
  371. cowboy_protocol, [
  372. {env, [{dispatch, cowboy_router:compile([
  373. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  374. {max_keepalive, 50},
  375. {timeout, 500}]
  376. ),
  377. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  378. [{port, Port10}|Config]),
  379. cowboy:stop_listener(http10_hostless).
  380. http10_keepalive_default(Config) ->
  381. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  382. Client = raw_open(Config),
  383. ok = raw_send(Client, Normal),
  384. case catch raw_recv_head(Client) of
  385. {'EXIT', _} -> error(closed);
  386. _ -> ok
  387. end,
  388. ok = raw_send(Client, Normal),
  389. case catch raw_recv_head(Client) of
  390. {'EXIT', _} -> closed;
  391. _ -> error(not_closed)
  392. end.
  393. http10_keepalive_forced(Config) ->
  394. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  395. Client = raw_open(Config),
  396. ok = raw_send(Client, Keepalive),
  397. case catch raw_recv_head(Client) of
  398. {'EXIT', _} -> error(closed);
  399. _ -> ok
  400. end,
  401. ok = raw_send(Client, Keepalive),
  402. case catch raw_recv_head(Client) of
  403. {'EXIT', Err} -> error({closed, Err});
  404. _ -> ok
  405. end.
  406. keepalive_max(Config) ->
  407. {ConnPid, MRef} = gun_monitor_open(Config),
  408. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  409. || _ <- lists:seq(1, 49)],
  410. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  411. _ = [begin
  412. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  413. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  414. end || Ref <- Refs],
  415. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
  416. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  417. gun_is_gone(ConnPid, MRef).
  418. keepalive_nl(Config) ->
  419. ConnPid = gun_open(Config),
  420. Refs = [begin
  421. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  422. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  423. Ref
  424. end || _ <- lists:seq(1, 10)],
  425. _ = [begin
  426. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  427. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  428. end || Ref <- Refs],
  429. ok.
  430. keepalive_stream_loop(Config) ->
  431. ConnPid = gun_open(Config),
  432. Refs = [begin
  433. Ref = gun:post(ConnPid, "/loop_stream_recv",
  434. [{<<"transfer-encoding">>, <<"chunked">>}]),
  435. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  436. || ID <- lists:seq(1, 250)],
  437. gun:data(ConnPid, Ref, fin, <<>>),
  438. Ref
  439. end || _ <- lists:seq(1, 10)],
  440. _ = [begin
  441. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  442. end || Ref <- Refs],
  443. ok.
  444. multipart(Config) ->
  445. ConnPid = gun_open(Config),
  446. Body = <<
  447. "This is a preamble."
  448. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  449. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  450. "\r\n--OHai--\r\n"
  451. "This is an epilogue."
  452. >>,
  453. Ref = gun:post(ConnPid, "/multipart",
  454. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  455. Body),
  456. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  457. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  458. Parts = binary_to_term(RespBody),
  459. Parts = [
  460. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  461. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  462. ],
  463. ok.
  464. multipart_chunked(Config) ->
  465. ConnPid = gun_open(Config),
  466. Body = <<
  467. "This is a preamble."
  468. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  469. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  470. "\r\n--OHai--\r\n"
  471. "This is an epilogue."
  472. >>,
  473. Ref = gun:post(ConnPid, "/multipart", [
  474. {<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>},
  475. {<<"transfer-encoding">>, <<"chunked">>}]),
  476. gun:data(ConnPid, Ref, fin, Body),
  477. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  478. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  479. Parts = binary_to_term(RespBody),
  480. Parts = [
  481. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  482. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  483. ],
  484. ok.
  485. multipart_large(Config) ->
  486. ConnPid = gun_open(Config),
  487. Boundary = "----------",
  488. Big = << 0:9000000/unit:8 >>,
  489. Bigger = << 0:9999999/unit:8 >>,
  490. Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
  491. "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
  492. "--", Boundary, "--\r\n"],
  493. Ref = gun:post(ConnPid, "/multipart/large",
  494. [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
  495. Body),
  496. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  497. ok.
  498. do_nc(Config, Input) ->
  499. Cat = os:find_executable("cat"),
  500. Nc = os:find_executable("nc"),
  501. case {Cat, Nc} of
  502. {false, _} ->
  503. {skip, {notfound, cat}};
  504. {_, false} ->
  505. {skip, {notfound, nc}};
  506. _Good ->
  507. %% Throw garbage at the server then check if it's still up.
  508. StrPort = integer_to_list(config(port, Config)),
  509. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  510. || _ <- lists:seq(1, 100)],
  511. 200 = do_get("/", Config)
  512. end.
  513. nc_rand(Config) ->
  514. do_nc(Config, "/dev/urandom").
  515. nc_zero(Config) ->
  516. do_nc(Config, "/dev/zero").
  517. onresponse_capitalize(Config) ->
  518. Client = raw_open(Config),
  519. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  520. Data = raw_recv_head(Client),
  521. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  522. ok.
  523. %% Hook for the above onresponse_capitalize test.
  524. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  525. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  526. || {N, V} <- Headers],
  527. cowboy_req:reply(Status, Headers2, Body, Req).
  528. onresponse_crash(Config) ->
  529. ConnPid = gun_open(Config),
  530. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  531. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  532. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  533. onresponse_reply(Config) ->
  534. ConnPid = gun_open(Config),
  535. Ref = gun:get(ConnPid, "/"),
  536. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  537. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  538. ok.
  539. %% Hook for the above onresponse tests.
  540. do_onresponse_hook(_, Headers, _, Req) ->
  541. cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req).
  542. parse_host(Config) ->
  543. ConnPid = gun_open(Config),
  544. Tests = [
  545. {<<"example.org:8080">>, <<"example.org\n8080">>},
  546. {<<"example.org">>, <<"example.org\n80">>},
  547. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  548. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  549. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  550. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  551. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  552. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  553. ],
  554. [begin
  555. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  556. [{<<"host">>, Host}]),
  557. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  558. {ok, Body} = gun:await_body(ConnPid, Ref)
  559. end || {Host, Body} <- Tests],
  560. ok.
  561. pipeline(Config) ->
  562. ConnPid = gun_open(Config),
  563. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  564. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  565. ok.
  566. rest_param_all(Config) ->
  567. ConnPid = gun_open(Config),
  568. %% Accept without param.
  569. Ref1 = gun:get(ConnPid, "/param_all",
  570. [{<<"accept">>, <<"text/plain">>}]),
  571. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  572. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  573. %% Accept with param.
  574. Ref2 = gun:get(ConnPid, "/param_all",
  575. [{<<"accept">>, <<"text/plain;level=1">>}]),
  576. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  577. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  578. %% Accept with param and quality.
  579. Ref3 = gun:get(ConnPid, "/param_all",
  580. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  581. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  582. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  583. Ref4 = gun:get(ConnPid, "/param_all",
  584. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  585. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  586. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  587. %% Without Accept.
  588. Ref5 = gun:get(ConnPid, "/param_all"),
  589. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  590. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  591. %% Content-Type without param.
  592. Ref6 = gun:put(ConnPid, "/param_all",
  593. [{<<"content-type">>, <<"text/plain">>}]),
  594. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  595. %% Content-Type with param.
  596. Ref7 = gun:put(ConnPid, "/param_all",
  597. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  598. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  599. ok.
  600. rest_bad_accept(Config) ->
  601. ConnPid = gun_open(Config),
  602. Ref = gun:get(ConnPid, "/bad_accept",
  603. [{<<"accept">>, <<"1">>}]),
  604. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  605. ok.
  606. rest_bad_content_type(Config) ->
  607. ConnPid = gun_open(Config),
  608. Ref = gun:patch(ConnPid, "/bad_content_type",
  609. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  610. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  611. ok.
  612. rest_expires(Config) ->
  613. ConnPid = gun_open(Config),
  614. Ref = gun:get(ConnPid, "/rest_expires"),
  615. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  616. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  617. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  618. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  619. ok.
  620. rest_expires_binary(Config) ->
  621. ConnPid = gun_open(Config),
  622. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  623. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  624. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  625. ok.
  626. rest_keepalive(Config) ->
  627. ConnPid = gun_open(Config),
  628. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  629. _ = [begin
  630. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  631. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  632. end || Ref <- Refs],
  633. ok.
  634. rest_keepalive_post(Config) ->
  635. ConnPid = gun_open(Config),
  636. Refs = [{
  637. gun:post(ConnPid, "/forbidden_post",
  638. [{<<"content-type">>, <<"text/plain">>}]),
  639. gun:post(ConnPid, "/simple_post",
  640. [{<<"content-type">>, <<"text/plain">>}])
  641. } || _ <- lists:seq(1, 5)],
  642. _ = [begin
  643. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  644. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
  645. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  646. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
  647. end || {Ref1, Ref2} <- Refs],
  648. ok.
  649. rest_missing_get_callbacks(Config) ->
  650. ConnPid = gun_open(Config),
  651. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  652. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  653. ok.
  654. rest_missing_put_callbacks(Config) ->
  655. ConnPid = gun_open(Config),
  656. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  657. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  658. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  659. ok.
  660. rest_nodelete(Config) ->
  661. ConnPid = gun_open(Config),
  662. Ref = gun:delete(ConnPid, "/nodelete"),
  663. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  664. ok.
  665. rest_options_default(Config) ->
  666. ConnPid = gun_open(Config),
  667. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  668. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  669. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  670. ok.
  671. rest_patch(Config) ->
  672. Tests = [
  673. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  674. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  675. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  676. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  677. ],
  678. ConnPid = gun_open(Config),
  679. _ = [begin
  680. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  681. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  682. end || {Status, Headers, Body} <- Tests],
  683. ok.
  684. rest_post_charset(Config) ->
  685. ConnPid = gun_open(Config),
  686. Ref = gun:post(ConnPid, "/post_charset",
  687. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  688. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  689. ok.
  690. rest_postonly(Config) ->
  691. ConnPid = gun_open(Config),
  692. Ref = gun:post(ConnPid, "/postonly",
  693. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  694. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  695. ok.
  696. rest_resource_get_etag(Config, Type) ->
  697. rest_resource_get_etag(Config, Type, []).
  698. rest_resource_get_etag(Config, Type, Headers) ->
  699. ConnPid = gun_open(Config),
  700. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  701. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  702. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  703. false -> {Status, false};
  704. {<<"etag">>, ETag} -> {Status, ETag}
  705. end.
  706. rest_resource_etags(Config) ->
  707. Tests = [
  708. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  709. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  710. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  711. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  712. {500, false, "binary-strong-unquoted"},
  713. {500, false, "binary-weak-unquoted"}
  714. ],
  715. _ = [{Status, ETag, Type} = begin
  716. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  717. {Ret, RespETag, Type}
  718. end || {Status, ETag, Type} <- Tests].
  719. rest_resource_etags_if_none_match(Config) ->
  720. Tests = [
  721. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  722. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  723. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  724. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  725. ],
  726. _ = [{Status, Type} = begin
  727. {Ret, _} = rest_resource_get_etag(Config, Type,
  728. [{<<"if-none-match">>, ETag}]),
  729. {Ret, Type}
  730. end || {Status, ETag, Type} <- Tests].
  731. set_env_dispatch(Config) ->
  732. ConnPid1 = gun_open(Config),
  733. Ref1 = gun:get(ConnPid1, "/"),
  734. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  735. ok = cowboy:set_env(set_env, dispatch,
  736. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  737. ConnPid2 = gun_open(Config),
  738. Ref2 = gun:get(ConnPid2, "/"),
  739. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  740. ok.
  741. set_resp_body(Config) ->
  742. ConnPid = gun_open(Config),
  743. Ref = gun:get(ConnPid, "/set_resp/body"),
  744. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  745. {ok, <<"A flameless dance does not equal a cycle">>}
  746. = gun:await_body(ConnPid, Ref),
  747. ok.
  748. set_resp_header(Config) ->
  749. ConnPid = gun_open(Config),
  750. Ref = gun:get(ConnPid, "/set_resp/header"),
  751. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  752. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  753. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  754. ok.
  755. set_resp_overwrite(Config) ->
  756. ConnPid = gun_open(Config),
  757. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  758. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  759. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  760. ok.
  761. slowloris(Config) ->
  762. Client = raw_open(Config),
  763. try
  764. [begin
  765. ok = raw_send(Client, [C]),
  766. receive after 25 -> ok end
  767. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  768. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  769. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  770. error(failure)
  771. catch error:{badmatch, _} ->
  772. ok
  773. end.
  774. slowloris2(Config) ->
  775. Client = raw_open(Config),
  776. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  777. receive after 300 -> ok end,
  778. ok = raw_send(Client, "Host: localhost\r\n"),
  779. receive after 300 -> ok end,
  780. Data = raw_recv_head(Client),
  781. {_, 408, _, _} = cow_http:parse_status_line(Data),
  782. ok.
  783. static_attribute_etag(Config) ->
  784. ConnPid = gun_open(Config),
  785. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  786. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  787. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  788. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  789. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  790. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  791. true = ETag =/= undefined,
  792. ok.
  793. static_function_etag(Config) ->
  794. ConnPid = gun_open(Config),
  795. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  796. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  797. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  798. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  799. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  800. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  801. true = ETag =/= undefined,
  802. ok.
  803. static_mimetypes_function(Config) ->
  804. ConnPid = gun_open(Config),
  805. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  806. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  807. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  808. ok.
  809. static_specify_file(Config) ->
  810. ConnPid = gun_open(Config),
  811. Ref = gun:get(ConnPid, "/static_specify_file"),
  812. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  813. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  814. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  815. ok.
  816. static_specify_file_catchall(Config) ->
  817. ConnPid = gun_open(Config),
  818. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  819. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  820. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  821. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  822. ok.
  823. static_test_file(Config) ->
  824. ConnPid = gun_open(Config),
  825. Ref = gun:get(ConnPid, "/static/unknown"),
  826. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  827. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  828. ok.
  829. static_test_file_css(Config) ->
  830. ConnPid = gun_open(Config),
  831. Ref = gun:get(ConnPid, "/static/style.css"),
  832. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  833. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  834. ok.
  835. stream_body_set_resp(Config) ->
  836. ConnPid = gun_open(Config),
  837. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  838. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  839. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  840. ok.
  841. stream_body_set_resp_close(Config) ->
  842. {ConnPid, MRef} = gun_monitor_open(Config),
  843. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  844. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  845. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
  846. gun_is_gone(ConnPid, MRef).
  847. stream_body_set_resp_chunked(Config) ->
  848. ConnPid = gun_open(Config),
  849. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  850. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  851. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  852. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  853. ok.
  854. stream_body_set_resp_chunked10(Config) ->
  855. {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
  856. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  857. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  858. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  859. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
  860. gun_is_gone(ConnPid, MRef).
  861. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  862. streamed_response(Config) ->
  863. Client = raw_open(Config),
  864. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  865. Data = raw_recv_head(Client),
  866. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  867. {Headers, Rest2} = cow_http:parse_headers(Rest),
  868. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  869. Rest2Size = byte_size(Rest2),
  870. ok = case <<"streamed_handler\r\nworks fine!">> of
  871. Rest2 -> ok;
  872. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  873. end.
  874. te_chunked(Config) ->
  875. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  876. ConnPid = gun_open(Config),
  877. Ref = gun:post(ConnPid, "/echo/body",
  878. [{<<"transfer-encoding">>, <<"chunked">>}], Body),
  879. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  880. {ok, Body} = gun:await_body(ConnPid, Ref),
  881. ok.
  882. do_body_to_chunks(_, <<>>, Acc) ->
  883. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  884. do_body_to_chunks(ChunkSize, Body, Acc) ->
  885. BodySize = byte_size(Body),
  886. ChunkSize2 = case BodySize < ChunkSize of
  887. true -> BodySize;
  888. false -> ChunkSize
  889. end,
  890. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  891. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  892. do_body_to_chunks(ChunkSize, Rest,
  893. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  894. te_chunked_chopped(Config) ->
  895. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  896. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  897. ConnPid = gun_open(Config),
  898. Ref = gun:post(ConnPid, "/echo/body",
  899. [{<<"transfer-encoding">>, <<"chunked">>}]),
  900. _ = [begin
  901. ok = gun:dbg_send_raw(ConnPid, << C >>),
  902. receive after 10 -> ok end
  903. end || << C >> <= Body2],
  904. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  905. {ok, Body} = gun:await_body(ConnPid, Ref),
  906. ok.
  907. te_chunked_delayed(Config) ->
  908. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  909. Chunks = do_body_to_chunks(50, Body, []),
  910. ConnPid = gun_open(Config),
  911. Ref = gun:post(ConnPid, "/echo/body",
  912. [{<<"transfer-encoding">>, <<"chunked">>}]),
  913. _ = [begin
  914. ok = gun:dbg_send_raw(ConnPid, Chunk),
  915. receive after 10 -> ok end
  916. end || Chunk <- Chunks],
  917. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  918. {ok, Body} = gun:await_body(ConnPid, Ref),
  919. ok.
  920. te_chunked_split_body(Config) ->
  921. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  922. Chunks = do_body_to_chunks(50, Body, []),
  923. ConnPid = gun_open(Config),
  924. Ref = gun:post(ConnPid, "/echo/body",
  925. [{<<"transfer-encoding">>, <<"chunked">>}]),
  926. _ = [begin
  927. case Chunk of
  928. <<"0\r\n\r\n">> ->
  929. ok = gun:dbg_send_raw(ConnPid, Chunk);
  930. _ ->
  931. [Size, ChunkBody, <<>>] =
  932. binary:split(Chunk, [<<"\r\n">>], [global]),
  933. PartASize = random:uniform(byte_size(ChunkBody)),
  934. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  935. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  936. receive after 10 -> ok end,
  937. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  938. end
  939. end || Chunk <- Chunks],
  940. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  941. {ok, Body} = gun:await_body(ConnPid, Ref),
  942. ok.
  943. te_chunked_split_crlf(Config) ->
  944. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  945. Chunks = do_body_to_chunks(50, Body, []),
  946. ConnPid = gun_open(Config),
  947. Ref = gun:post(ConnPid, "/echo/body",
  948. [{<<"transfer-encoding">>, <<"chunked">>}]),
  949. _ = [begin
  950. %% Split in the newline just before the end of the chunk.
  951. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  952. << Chunk2:Len/binary, End/binary >> = Chunk,
  953. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  954. receive after 10 -> ok end,
  955. ok = gun:dbg_send_raw(ConnPid, End)
  956. end || Chunk <- Chunks],
  957. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  958. {ok, Body} = gun:await_body(ConnPid, Ref),
  959. ok.
  960. te_identity(Config) ->
  961. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  962. ConnPid = gun_open(Config),
  963. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  964. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  965. {ok, Body} = gun:await_body(ConnPid, Ref),
  966. ok.