http_SUITE.erl 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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:match_qs(Req, [{reply, [], noreply}]) of
  549. #{reply := noreply} ->
  550. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req);
  551. _ ->
  552. cowboy_req:reply(200, [], <<"replied!">>, Req)
  553. end.
  554. onresponse_capitalize(Config) ->
  555. Client = raw_open(Config),
  556. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  557. Data = raw_recv_head(Client),
  558. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  559. ok.
  560. %% Hook for the above onresponse_capitalize test.
  561. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  562. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  563. || {N, V} <- Headers],
  564. cowboy_req:reply(Status, Headers2, Body, Req).
  565. onresponse_crash(Config) ->
  566. ConnPid = gun_open(Config),
  567. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  568. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  569. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  570. onresponse_reply(Config) ->
  571. ConnPid = gun_open(Config),
  572. Ref = gun:get(ConnPid, "/"),
  573. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  574. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  575. ok.
  576. %% Hook for the above onresponse tests.
  577. do_onresponse_hook(_, Headers, _, Req) ->
  578. cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req).
  579. parse_host(Config) ->
  580. ConnPid = gun_open(Config),
  581. Tests = [
  582. {<<"example.org:8080">>, <<"example.org\n8080">>},
  583. {<<"example.org">>, <<"example.org\n80">>},
  584. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  585. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  586. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  587. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  588. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  589. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  590. ],
  591. [begin
  592. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  593. [{<<"host">>, Host}]),
  594. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  595. {ok, Body} = gun:await_body(ConnPid, Ref)
  596. end || {Host, Body} <- Tests],
  597. ok.
  598. pipeline(Config) ->
  599. ConnPid = gun_open(Config),
  600. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  601. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  602. ok.
  603. rest_param_all(Config) ->
  604. ConnPid = gun_open(Config),
  605. %% Accept without param.
  606. Ref1 = gun:get(ConnPid, "/param_all",
  607. [{<<"accept">>, <<"text/plain">>}]),
  608. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  609. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  610. %% Accept with param.
  611. Ref2 = gun:get(ConnPid, "/param_all",
  612. [{<<"accept">>, <<"text/plain;level=1">>}]),
  613. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  614. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  615. %% Accept with param and quality.
  616. Ref3 = gun:get(ConnPid, "/param_all",
  617. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  618. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  619. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  620. Ref4 = gun:get(ConnPid, "/param_all",
  621. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  622. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  623. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  624. %% Without Accept.
  625. Ref5 = gun:get(ConnPid, "/param_all"),
  626. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  627. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  628. %% Content-Type without param.
  629. Ref6 = gun:put(ConnPid, "/param_all",
  630. [{<<"content-type">>, <<"text/plain">>}]),
  631. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  632. %% Content-Type with param.
  633. Ref7 = gun:put(ConnPid, "/param_all",
  634. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  635. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  636. ok.
  637. rest_bad_accept(Config) ->
  638. ConnPid = gun_open(Config),
  639. Ref = gun:get(ConnPid, "/bad_accept",
  640. [{<<"accept">>, <<"1">>}]),
  641. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  642. ok.
  643. rest_bad_content_type(Config) ->
  644. ConnPid = gun_open(Config),
  645. Ref = gun:patch(ConnPid, "/bad_content_type",
  646. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  647. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  648. ok.
  649. rest_expires(Config) ->
  650. ConnPid = gun_open(Config),
  651. Ref = gun:get(ConnPid, "/rest_expires"),
  652. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  653. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  654. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  655. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  656. ok.
  657. rest_expires_binary(Config) ->
  658. ConnPid = gun_open(Config),
  659. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  660. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  661. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  662. ok.
  663. rest_keepalive(Config) ->
  664. ConnPid = gun_open(Config),
  665. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  666. _ = [begin
  667. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  668. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  669. end || Ref <- Refs],
  670. ok.
  671. rest_keepalive_post(Config) ->
  672. ConnPid = gun_open(Config),
  673. Refs = [{
  674. gun:post(ConnPid, "/forbidden_post",
  675. [{<<"content-type">>, <<"text/plain">>}]),
  676. gun:post(ConnPid, "/simple_post",
  677. [{<<"content-type">>, <<"text/plain">>}])
  678. } || _ <- lists:seq(1, 5)],
  679. _ = [begin
  680. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  681. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
  682. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  683. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
  684. end || {Ref1, Ref2} <- Refs],
  685. ok.
  686. rest_missing_get_callbacks(Config) ->
  687. ConnPid = gun_open(Config),
  688. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  689. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  690. ok.
  691. rest_missing_put_callbacks(Config) ->
  692. ConnPid = gun_open(Config),
  693. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  694. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  695. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  696. ok.
  697. rest_nodelete(Config) ->
  698. ConnPid = gun_open(Config),
  699. Ref = gun:delete(ConnPid, "/nodelete"),
  700. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  701. ok.
  702. rest_options_default(Config) ->
  703. ConnPid = gun_open(Config),
  704. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  705. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  706. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  707. ok.
  708. rest_patch(Config) ->
  709. Tests = [
  710. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  711. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  712. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  713. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  714. ],
  715. ConnPid = gun_open(Config),
  716. _ = [begin
  717. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  718. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  719. end || {Status, Headers, Body} <- Tests],
  720. ok.
  721. rest_post_charset(Config) ->
  722. ConnPid = gun_open(Config),
  723. Ref = gun:post(ConnPid, "/post_charset",
  724. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  725. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  726. ok.
  727. rest_postonly(Config) ->
  728. ConnPid = gun_open(Config),
  729. Ref = gun:post(ConnPid, "/postonly",
  730. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  731. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  732. ok.
  733. rest_resource_get_etag(Config, Type) ->
  734. rest_resource_get_etag(Config, Type, []).
  735. rest_resource_get_etag(Config, Type, Headers) ->
  736. ConnPid = gun_open(Config),
  737. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  738. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  739. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  740. false -> {Status, false};
  741. {<<"etag">>, ETag} -> {Status, ETag}
  742. end.
  743. rest_resource_etags(Config) ->
  744. Tests = [
  745. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  746. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  747. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  748. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  749. {500, false, "binary-strong-unquoted"},
  750. {500, false, "binary-weak-unquoted"}
  751. ],
  752. _ = [{Status, ETag, Type} = begin
  753. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  754. {Ret, RespETag, Type}
  755. end || {Status, ETag, Type} <- Tests].
  756. rest_resource_etags_if_none_match(Config) ->
  757. Tests = [
  758. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  759. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  760. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  761. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  762. ],
  763. _ = [{Status, Type} = begin
  764. {Ret, _} = rest_resource_get_etag(Config, Type,
  765. [{<<"if-none-match">>, ETag}]),
  766. {Ret, Type}
  767. end || {Status, ETag, Type} <- Tests].
  768. set_env_dispatch(Config) ->
  769. ConnPid1 = gun_open(Config),
  770. Ref1 = gun:get(ConnPid1, "/"),
  771. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  772. ok = cowboy:set_env(set_env, dispatch,
  773. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  774. ConnPid2 = gun_open(Config),
  775. Ref2 = gun:get(ConnPid2, "/"),
  776. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  777. ok.
  778. set_resp_body(Config) ->
  779. ConnPid = gun_open(Config),
  780. Ref = gun:get(ConnPid, "/set_resp/body"),
  781. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  782. {ok, <<"A flameless dance does not equal a cycle">>}
  783. = gun:await_body(ConnPid, Ref),
  784. ok.
  785. set_resp_header(Config) ->
  786. ConnPid = gun_open(Config),
  787. Ref = gun:get(ConnPid, "/set_resp/header"),
  788. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  789. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  790. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  791. ok.
  792. set_resp_overwrite(Config) ->
  793. ConnPid = gun_open(Config),
  794. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  795. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  796. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  797. ok.
  798. slowloris(Config) ->
  799. Client = raw_open(Config),
  800. try
  801. [begin
  802. ok = raw_send(Client, [C]),
  803. receive after 25 -> ok end
  804. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  805. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  806. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  807. error(failure)
  808. catch error:{badmatch, _} ->
  809. ok
  810. end.
  811. slowloris2(Config) ->
  812. Client = raw_open(Config),
  813. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  814. receive after 300 -> ok end,
  815. ok = raw_send(Client, "Host: localhost\r\n"),
  816. receive after 300 -> ok end,
  817. Data = raw_recv_head(Client),
  818. {_, 408, _, _} = cow_http:parse_status_line(Data),
  819. ok.
  820. static_attribute_etag(Config) ->
  821. ConnPid = gun_open(Config),
  822. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  823. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  824. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  825. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  826. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  827. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  828. true = ETag =/= undefined,
  829. ok.
  830. static_function_etag(Config) ->
  831. ConnPid = gun_open(Config),
  832. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  833. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  834. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  835. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  836. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  837. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  838. true = ETag =/= undefined,
  839. ok.
  840. static_mimetypes_function(Config) ->
  841. ConnPid = gun_open(Config),
  842. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  843. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  844. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  845. ok.
  846. static_specify_file(Config) ->
  847. ConnPid = gun_open(Config),
  848. Ref = gun:get(ConnPid, "/static_specify_file"),
  849. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  850. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  851. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  852. ok.
  853. static_specify_file_catchall(Config) ->
  854. ConnPid = gun_open(Config),
  855. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  856. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  857. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  858. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  859. ok.
  860. static_test_file(Config) ->
  861. ConnPid = gun_open(Config),
  862. Ref = gun:get(ConnPid, "/static/unknown"),
  863. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  864. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  865. ok.
  866. static_test_file_css(Config) ->
  867. ConnPid = gun_open(Config),
  868. Ref = gun:get(ConnPid, "/static/style.css"),
  869. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  870. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  871. ok.
  872. stream_body_set_resp(Config) ->
  873. ConnPid = gun_open(Config),
  874. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  875. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  876. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  877. ok.
  878. stream_body_set_resp_close(Config) ->
  879. {ConnPid, MRef} = gun_monitor_open(Config),
  880. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  881. {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
  882. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
  883. gun_is_gone(ConnPid, MRef).
  884. stream_body_set_resp_chunked(Config) ->
  885. ConnPid = gun_open(Config),
  886. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  887. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  888. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  889. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  890. ok.
  891. stream_body_set_resp_chunked10(Config) ->
  892. {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
  893. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  894. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
  895. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  896. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
  897. gun_is_gone(ConnPid, MRef).
  898. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  899. streamed_response(Config) ->
  900. Client = raw_open(Config),
  901. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  902. Data = raw_recv_head(Client),
  903. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  904. {Headers, Rest2} = cow_http:parse_headers(Rest),
  905. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  906. Rest2Size = byte_size(Rest2),
  907. ok = case <<"streamed_handler\r\nworks fine!">> of
  908. Rest2 -> ok;
  909. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  910. end.
  911. te_chunked(Config) ->
  912. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  913. ConnPid = gun_open(Config),
  914. Ref = gun:post(ConnPid, "/echo/body",
  915. [{<<"transfer-encoding">>, <<"chunked">>}], Body),
  916. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  917. {ok, Body} = gun:await_body(ConnPid, Ref),
  918. ok.
  919. do_body_to_chunks(_, <<>>, Acc) ->
  920. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  921. do_body_to_chunks(ChunkSize, Body, Acc) ->
  922. BodySize = byte_size(Body),
  923. ChunkSize2 = case BodySize < ChunkSize of
  924. true -> BodySize;
  925. false -> ChunkSize
  926. end,
  927. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  928. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  929. do_body_to_chunks(ChunkSize, Rest,
  930. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  931. te_chunked_chopped(Config) ->
  932. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  933. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  934. ConnPid = gun_open(Config),
  935. Ref = gun:post(ConnPid, "/echo/body",
  936. [{<<"transfer-encoding">>, <<"chunked">>}]),
  937. _ = [begin
  938. ok = gun:dbg_send_raw(ConnPid, << C >>),
  939. receive after 10 -> ok end
  940. end || << C >> <= Body2],
  941. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  942. {ok, Body} = gun:await_body(ConnPid, Ref),
  943. ok.
  944. te_chunked_delayed(Config) ->
  945. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  946. Chunks = do_body_to_chunks(50, Body, []),
  947. ConnPid = gun_open(Config),
  948. Ref = gun:post(ConnPid, "/echo/body",
  949. [{<<"transfer-encoding">>, <<"chunked">>}]),
  950. _ = [begin
  951. ok = gun:dbg_send_raw(ConnPid, Chunk),
  952. receive after 10 -> ok end
  953. end || Chunk <- Chunks],
  954. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  955. {ok, Body} = gun:await_body(ConnPid, Ref),
  956. ok.
  957. te_chunked_split_body(Config) ->
  958. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  959. Chunks = do_body_to_chunks(50, Body, []),
  960. ConnPid = gun_open(Config),
  961. Ref = gun:post(ConnPid, "/echo/body",
  962. [{<<"transfer-encoding">>, <<"chunked">>}]),
  963. _ = [begin
  964. case Chunk of
  965. <<"0\r\n\r\n">> ->
  966. ok = gun:dbg_send_raw(ConnPid, Chunk);
  967. _ ->
  968. [Size, ChunkBody, <<>>] =
  969. binary:split(Chunk, [<<"\r\n">>], [global]),
  970. PartASize = random:uniform(byte_size(ChunkBody)),
  971. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  972. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  973. receive after 10 -> ok end,
  974. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  975. end
  976. end || Chunk <- Chunks],
  977. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  978. {ok, Body} = gun:await_body(ConnPid, Ref),
  979. ok.
  980. te_chunked_split_crlf(Config) ->
  981. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  982. Chunks = do_body_to_chunks(50, Body, []),
  983. ConnPid = gun_open(Config),
  984. Ref = gun:post(ConnPid, "/echo/body",
  985. [{<<"transfer-encoding">>, <<"chunked">>}]),
  986. _ = [begin
  987. %% Split in the newline just before the end of the chunk.
  988. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  989. << Chunk2:Len/binary, End/binary >> = Chunk,
  990. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  991. receive after 10 -> ok end,
  992. ok = gun:dbg_send_raw(ConnPid, End)
  993. end || Chunk <- Chunks],
  994. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  995. {ok, Body} = gun:await_body(ConnPid, Ref),
  996. ok.
  997. te_identity(Config) ->
  998. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  999. ConnPid = gun_open(Config),
  1000. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  1001. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  1002. {ok, Body} = gun:await_body(ConnPid, Ref),
  1003. ok.