http_SUITE.erl 37 KB

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