http_SUITE.erl 36 KB

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