http_SUITE.erl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. _ -> ok
  402. end,
  403. ok = raw_send(Client, Normal),
  404. case catch raw_recv_head(Client) of
  405. {'EXIT', _} -> closed;
  406. _ -> error(not_closed)
  407. end.
  408. http10_keepalive_forced(Config) ->
  409. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  410. Client = raw_open(Config),
  411. ok = raw_send(Client, Keepalive),
  412. case catch raw_recv_head(Client) of
  413. {'EXIT', _} -> error(closed);
  414. _ -> ok
  415. end,
  416. ok = raw_send(Client, Keepalive),
  417. case catch raw_recv_head(Client) of
  418. {'EXIT', Err} -> error({closed, Err});
  419. _ -> ok
  420. end.
  421. keepalive_max(Config) ->
  422. {ConnPid, MRef} = gun_monitor_open(Config),
  423. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  424. || _ <- lists:seq(1, 49)],
  425. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  426. _ = [begin
  427. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  428. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  429. end || Ref <- Refs],
  430. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
  431. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  432. gun_is_gone(ConnPid, MRef).
  433. keepalive_nl(Config) ->
  434. ConnPid = gun_open(Config),
  435. Refs = [begin
  436. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  437. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  438. Ref
  439. end || _ <- lists:seq(1, 10)],
  440. _ = [begin
  441. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  442. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  443. end || Ref <- Refs],
  444. ok.
  445. keepalive_stream_loop(Config) ->
  446. ConnPid = gun_open(Config),
  447. Refs = [begin
  448. Ref = gun:post(ConnPid, "/loop_stream_recv",
  449. [{<<"transfer-encoding">>, <<"chunked">>}]),
  450. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  451. || ID <- lists:seq(1, 250)],
  452. gun:data(ConnPid, Ref, fin, <<>>),
  453. Ref
  454. end || _ <- lists:seq(1, 10)],
  455. _ = [begin
  456. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  457. end || Ref <- Refs],
  458. ok.
  459. multipart(Config) ->
  460. ConnPid = gun_open(Config),
  461. Body = <<
  462. "This is a preamble."
  463. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  464. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  465. "\r\n--OHai--\r\n"
  466. "This is an epilogue."
  467. >>,
  468. Ref = gun:post(ConnPid, "/multipart",
  469. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  470. Body),
  471. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  472. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  473. Parts = binary_to_term(RespBody),
  474. Parts = [
  475. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  476. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  477. ],
  478. ok.
  479. multipart_chunked(Config) ->
  480. ConnPid = gun_open(Config),
  481. Body = <<
  482. "This is a preamble."
  483. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  484. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  485. "\r\n--OHai--\r\n"
  486. "This is an epilogue."
  487. >>,
  488. Ref = gun:post(ConnPid, "/multipart", [
  489. {<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>},
  490. {<<"transfer-encoding">>, <<"chunked">>}]),
  491. gun:data(ConnPid, Ref, fin, Body),
  492. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  493. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  494. Parts = binary_to_term(RespBody),
  495. Parts = [
  496. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  497. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  498. ],
  499. ok.
  500. multipart_large(Config) ->
  501. ConnPid = gun_open(Config),
  502. Boundary = "----------",
  503. Big = << 0:9000000/unit:8 >>,
  504. Bigger = << 0:9999999/unit:8 >>,
  505. Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
  506. "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
  507. "--", Boundary, "--\r\n"],
  508. Ref = gun:post(ConnPid, "/multipart/large",
  509. [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
  510. Body),
  511. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  512. ok.
  513. do_nc(Config, Input) ->
  514. Cat = os:find_executable("cat"),
  515. Nc = os:find_executable("nc"),
  516. case {Cat, Nc} of
  517. {false, _} ->
  518. {skip, {notfound, cat}};
  519. {_, false} ->
  520. {skip, {notfound, nc}};
  521. _Good ->
  522. %% Throw garbage at the server then check if it's still up.
  523. StrPort = integer_to_list(config(port, Config)),
  524. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  525. || _ <- lists:seq(1, 100)],
  526. 200 = do_get("/", Config)
  527. end.
  528. nc_rand(Config) ->
  529. do_nc(Config, "/dev/urandom").
  530. nc_zero(Config) ->
  531. do_nc(Config, "/dev/zero").
  532. onrequest(Config) ->
  533. ConnPid = gun_open(Config),
  534. Ref = gun:get(ConnPid, "/"),
  535. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  536. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  537. {ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref),
  538. ok.
  539. onrequest_reply(Config) ->
  540. ConnPid = gun_open(Config),
  541. Ref = gun:get(ConnPid, "/?reply=1"),
  542. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  543. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  544. {ok, <<"replied!">>} = gun:await_body(ConnPid, Ref),
  545. ok.
  546. %% Hook for the above onrequest tests.
  547. do_onrequest_hook(Req) ->
  548. case cowboy_req:qs_val(<<"reply">>, Req) of
  549. {undefined, Req2} ->
  550. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  551. {_, Req2} ->
  552. {ok, Req3} = cowboy_req:reply(
  553. 200, [], <<"replied!">>, Req2),
  554. Req3
  555. end.
  556. onresponse_capitalize(Config) ->
  557. Client = raw_open(Config),
  558. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  559. Data = raw_recv_head(Client),
  560. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  561. ok.
  562. %% Hook for the above onresponse_capitalize test.
  563. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  564. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  565. || {N, V} <- Headers],
  566. {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
  567. Req2.
  568. onresponse_crash(Config) ->
  569. ConnPid = gun_open(Config),
  570. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  571. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  572. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  573. onresponse_reply(Config) ->
  574. ConnPid = gun_open(Config),
  575. Ref = gun:get(ConnPid, "/"),
  576. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  577. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  578. ok.
  579. %% Hook for the above onresponse tests.
  580. do_onresponse_hook(_, Headers, _, Req) ->
  581. {ok, Req2} = cowboy_req:reply(
  582. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  583. Req2.
  584. parse_host(Config) ->
  585. ConnPid = gun_open(Config),
  586. Tests = [
  587. {<<"example.org:8080">>, <<"example.org\n8080">>},
  588. {<<"example.org">>, <<"example.org\n80">>},
  589. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  590. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  591. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  592. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  593. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  594. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  595. ],
  596. [begin
  597. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  598. [{<<"host">>, Host}]),
  599. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  600. {ok, Body} = gun:await_body(ConnPid, Ref)
  601. end || {Host, Body} <- Tests],
  602. ok.
  603. pipeline(Config) ->
  604. ConnPid = gun_open(Config),
  605. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  606. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  607. ok.
  608. rest_param_all(Config) ->
  609. ConnPid = gun_open(Config),
  610. %% Accept without param.
  611. Ref1 = gun:get(ConnPid, "/param_all",
  612. [{<<"accept">>, <<"text/plain">>}]),
  613. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  614. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  615. %% Accept with param.
  616. Ref2 = gun:get(ConnPid, "/param_all",
  617. [{<<"accept">>, <<"text/plain;level=1">>}]),
  618. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  619. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  620. %% Accept with param and quality.
  621. Ref3 = gun:get(ConnPid, "/param_all",
  622. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  623. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  624. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  625. Ref4 = gun:get(ConnPid, "/param_all",
  626. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  627. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  628. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  629. %% Without Accept.
  630. Ref5 = gun:get(ConnPid, "/param_all"),
  631. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  632. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  633. %% Content-Type without param.
  634. Ref6 = gun:put(ConnPid, "/param_all",
  635. [{<<"content-type">>, <<"text/plain">>}]),
  636. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  637. %% Content-Type with param.
  638. Ref7 = gun:put(ConnPid, "/param_all",
  639. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  640. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  641. ok.
  642. rest_bad_accept(Config) ->
  643. ConnPid = gun_open(Config),
  644. Ref = gun:get(ConnPid, "/bad_accept",
  645. [{<<"accept">>, <<"1">>}]),
  646. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  647. ok.
  648. rest_bad_content_type(Config) ->
  649. ConnPid = gun_open(Config),
  650. Ref = gun:patch(ConnPid, "/bad_content_type",
  651. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  652. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  653. ok.
  654. rest_expires(Config) ->
  655. ConnPid = gun_open(Config),
  656. Ref = gun:get(ConnPid, "/rest_expires"),
  657. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  658. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  659. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  660. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  661. ok.
  662. rest_expires_binary(Config) ->
  663. ConnPid = gun_open(Config),
  664. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  665. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  666. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  667. ok.
  668. rest_keepalive(Config) ->
  669. ConnPid = gun_open(Config),
  670. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  671. _ = [begin
  672. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  673. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  674. end || Ref <- Refs],
  675. ok.
  676. rest_keepalive_post(Config) ->
  677. ConnPid = gun_open(Config),
  678. Refs = [{
  679. gun:post(ConnPid, "/forbidden_post",
  680. [{<<"content-type">>, <<"text/plain">>}]),
  681. gun:post(ConnPid, "/simple_post",
  682. [{<<"content-type">>, <<"text/plain">>}])
  683. } || _ <- lists:seq(1, 5)],
  684. _ = [begin
  685. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  686. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
  687. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  688. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
  689. end || {Ref1, Ref2} <- Refs],
  690. ok.
  691. rest_missing_get_callbacks(Config) ->
  692. ConnPid = gun_open(Config),
  693. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  694. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  695. ok.
  696. rest_missing_put_callbacks(Config) ->
  697. ConnPid = gun_open(Config),
  698. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  699. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  700. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  701. ok.
  702. rest_nodelete(Config) ->
  703. ConnPid = gun_open(Config),
  704. Ref = gun:delete(ConnPid, "/nodelete"),
  705. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  706. ok.
  707. rest_options_default(Config) ->
  708. ConnPid = gun_open(Config),
  709. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  710. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  711. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  712. ok.
  713. rest_patch(Config) ->
  714. Tests = [
  715. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  716. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  717. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  718. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  719. ],
  720. ConnPid = gun_open(Config),
  721. _ = [begin
  722. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  723. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  724. end || {Status, Headers, Body} <- Tests],
  725. ok.
  726. rest_post_charset(Config) ->
  727. ConnPid = gun_open(Config),
  728. Ref = gun:post(ConnPid, "/post_charset",
  729. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  730. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  731. ok.
  732. rest_postonly(Config) ->
  733. ConnPid = gun_open(Config),
  734. Ref = gun:post(ConnPid, "/postonly",
  735. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  736. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  737. ok.
  738. rest_resource_get_etag(Config, Type) ->
  739. rest_resource_get_etag(Config, Type, []).
  740. rest_resource_get_etag(Config, Type, Headers) ->
  741. ConnPid = gun_open(Config),
  742. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  743. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  744. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  745. false -> {Status, false};
  746. {<<"etag">>, ETag} -> {Status, ETag}
  747. end.
  748. rest_resource_etags(Config) ->
  749. Tests = [
  750. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  751. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  752. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  753. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  754. {500, false, "binary-strong-unquoted"},
  755. {500, false, "binary-weak-unquoted"}
  756. ],
  757. _ = [{Status, ETag, Type} = begin
  758. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  759. {Ret, RespETag, Type}
  760. end || {Status, ETag, Type} <- Tests].
  761. rest_resource_etags_if_none_match(Config) ->
  762. Tests = [
  763. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  764. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  765. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  766. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  767. ],
  768. _ = [{Status, Type} = begin
  769. {Ret, _} = rest_resource_get_etag(Config, Type,
  770. [{<<"if-none-match">>, ETag}]),
  771. {Ret, Type}
  772. end || {Status, ETag, Type} <- Tests].
  773. set_env_dispatch(Config) ->
  774. ConnPid1 = gun_open(Config),
  775. Ref1 = gun:get(ConnPid1, "/"),
  776. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  777. ok = cowboy:set_env(set_env, dispatch,
  778. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  779. ConnPid2 = gun_open(Config),
  780. Ref2 = gun:get(ConnPid2, "/"),
  781. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  782. ok.
  783. set_resp_body(Config) ->
  784. ConnPid = gun_open(Config),
  785. Ref = gun:get(ConnPid, "/set_resp/body"),
  786. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  787. {ok, <<"A flameless dance does not equal a cycle">>}
  788. = gun:await_body(ConnPid, Ref),
  789. ok.
  790. set_resp_header(Config) ->
  791. ConnPid = gun_open(Config),
  792. Ref = gun:get(ConnPid, "/set_resp/header"),
  793. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  794. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  795. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  796. ok.
  797. set_resp_overwrite(Config) ->
  798. ConnPid = gun_open(Config),
  799. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  800. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  801. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  802. ok.
  803. slowloris(Config) ->
  804. Client = raw_open(Config),
  805. try
  806. [begin
  807. ok = raw_send(Client, [C]),
  808. receive after 25 -> ok end
  809. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  810. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  811. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  812. error(failure)
  813. catch error:{badmatch, _} ->
  814. ok
  815. end.
  816. slowloris2(Config) ->
  817. Client = raw_open(Config),
  818. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  819. receive after 300 -> ok end,
  820. ok = raw_send(Client, "Host: localhost\r\n"),
  821. receive after 300 -> ok end,
  822. Data = raw_recv_head(Client),
  823. {_, 408, _, _} = cow_http:parse_status_line(Data),
  824. ok.
  825. static_attribute_etag(Config) ->
  826. ConnPid = gun_open(Config),
  827. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  828. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  829. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  830. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  831. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  832. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  833. true = ETag =/= undefined,
  834. ok.
  835. static_function_etag(Config) ->
  836. ConnPid = gun_open(Config),
  837. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  838. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  839. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  840. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  841. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  842. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  843. true = ETag =/= undefined,
  844. ok.
  845. static_mimetypes_function(Config) ->
  846. ConnPid = gun_open(Config),
  847. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  848. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  849. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  850. ok.
  851. static_specify_file(Config) ->
  852. ConnPid = gun_open(Config),
  853. Ref = gun:get(ConnPid, "/static_specify_file"),
  854. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  855. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  856. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  857. ok.
  858. static_specify_file_catchall(Config) ->
  859. ConnPid = gun_open(Config),
  860. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  861. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  862. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  863. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  864. ok.
  865. static_test_file(Config) ->
  866. ConnPid = gun_open(Config),
  867. Ref = gun:get(ConnPid, "/static/unknown"),
  868. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  869. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  870. ok.
  871. static_test_file_css(Config) ->
  872. ConnPid = gun_open(Config),
  873. Ref = gun:get(ConnPid, "/static/style.css"),
  874. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  875. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  876. ok.
  877. stream_body_set_resp(Config) ->
  878. ConnPid = gun_open(Config),
  879. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  880. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  881. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  882. ok.
  883. stream_body_set_resp_close(Config) ->
  884. {ConnPid, MRef} = gun_monitor_open(Config),
  885. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  886. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  887. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
  888. gun_is_gone(ConnPid, MRef).
  889. stream_body_set_resp_chunked(Config) ->
  890. ConnPid = gun_open(Config),
  891. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  892. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  893. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  894. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  895. ok.
  896. stream_body_set_resp_chunked10(Config) ->
  897. {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
  898. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  899. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  900. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  901. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
  902. gun_is_gone(ConnPid, MRef).
  903. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  904. streamed_response(Config) ->
  905. Client = raw_open(Config),
  906. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  907. Data = raw_recv_head(Client),
  908. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  909. {Headers, Rest2} = cow_http:parse_headers(Rest),
  910. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  911. Rest2Size = byte_size(Rest2),
  912. ok = case <<"streamed_handler\r\nworks fine!">> of
  913. Rest2 -> ok;
  914. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  915. end.
  916. te_chunked(Config) ->
  917. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  918. ConnPid = gun_open(Config),
  919. Ref = gun:post(ConnPid, "/echo/body",
  920. [{<<"transfer-encoding">>, <<"chunked">>}], Body),
  921. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  922. {ok, Body} = gun:await_body(ConnPid, Ref),
  923. ok.
  924. do_body_to_chunks(_, <<>>, Acc) ->
  925. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  926. do_body_to_chunks(ChunkSize, Body, Acc) ->
  927. BodySize = byte_size(Body),
  928. ChunkSize2 = case BodySize < ChunkSize of
  929. true -> BodySize;
  930. false -> ChunkSize
  931. end,
  932. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  933. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  934. do_body_to_chunks(ChunkSize, Rest,
  935. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  936. te_chunked_chopped(Config) ->
  937. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  938. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  939. ConnPid = gun_open(Config),
  940. Ref = gun:post(ConnPid, "/echo/body",
  941. [{<<"transfer-encoding">>, <<"chunked">>}]),
  942. _ = [begin
  943. ok = gun:dbg_send_raw(ConnPid, << C >>),
  944. receive after 10 -> ok end
  945. end || << C >> <= Body2],
  946. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  947. {ok, Body} = gun:await_body(ConnPid, Ref),
  948. ok.
  949. te_chunked_delayed(Config) ->
  950. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  951. Chunks = do_body_to_chunks(50, Body, []),
  952. ConnPid = gun_open(Config),
  953. Ref = gun:post(ConnPid, "/echo/body",
  954. [{<<"transfer-encoding">>, <<"chunked">>}]),
  955. _ = [begin
  956. ok = gun:dbg_send_raw(ConnPid, Chunk),
  957. receive after 10 -> ok end
  958. end || Chunk <- Chunks],
  959. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  960. {ok, Body} = gun:await_body(ConnPid, Ref),
  961. ok.
  962. te_chunked_split_body(Config) ->
  963. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  964. Chunks = do_body_to_chunks(50, Body, []),
  965. ConnPid = gun_open(Config),
  966. Ref = gun:post(ConnPid, "/echo/body",
  967. [{<<"transfer-encoding">>, <<"chunked">>}]),
  968. _ = [begin
  969. case Chunk of
  970. <<"0\r\n\r\n">> ->
  971. ok = gun:dbg_send_raw(ConnPid, Chunk);
  972. _ ->
  973. [Size, ChunkBody, <<>>] =
  974. binary:split(Chunk, [<<"\r\n">>], [global]),
  975. PartASize = random:uniform(byte_size(ChunkBody)),
  976. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  977. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  978. receive after 10 -> ok end,
  979. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  980. end
  981. end || Chunk <- Chunks],
  982. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  983. {ok, Body} = gun:await_body(ConnPid, Ref),
  984. ok.
  985. te_chunked_split_crlf(Config) ->
  986. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  987. Chunks = do_body_to_chunks(50, Body, []),
  988. ConnPid = gun_open(Config),
  989. Ref = gun:post(ConnPid, "/echo/body",
  990. [{<<"transfer-encoding">>, <<"chunked">>}]),
  991. _ = [begin
  992. %% Split in the newline just before the end of the chunk.
  993. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  994. << Chunk2:Len/binary, End/binary >> = Chunk,
  995. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  996. receive after 10 -> ok end,
  997. ok = gun:dbg_send_raw(ConnPid, End)
  998. end || Chunk <- Chunks],
  999. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  1000. {ok, Body} = gun:await_body(ConnPid, Ref),
  1001. ok.
  1002. te_identity(Config) ->
  1003. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1004. ConnPid = gun_open(Config),
  1005. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  1006. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  1007. {ok, Body} = gun:await_body(ConnPid, Ref),
  1008. ok.