http_SUITE.erl 36 KB

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