http_SUITE.erl 35 KB

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