http_SUITE.erl 35 KB

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