http_SUITE.erl 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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(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 size is bigger than 1000000 bytes causes 413
  288. echo_body_max_length(Config) ->
  289. ConnPid = gun_open(Config),
  290. Ref = gun:post(ConnPid, "/echo/body", [], << 0:10000000/unit:8 >>),
  291. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  292. ok.
  293. % check if body_qs echo's back results
  294. echo_body_qs(Config) ->
  295. ConnPid = gun_open(Config),
  296. Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
  297. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  298. {ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
  299. ok.
  300. echo_body_qs_max_length(Config) ->
  301. ConnPid = gun_open(Config),
  302. Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:2000000/unit:8 >>),
  303. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  304. ok.
  305. error_init_after_reply(Config) ->
  306. ConnPid = gun_open(Config),
  307. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  308. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  309. ok.
  310. headers_dupe(Config) ->
  311. ConnPid = gun_open(Config),
  312. Ref = gun:get(ConnPid, "/headers/dupe"),
  313. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  314. %% Ensure that only one connection header was received.
  315. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  316. gun_down(ConnPid).
  317. http10_chunkless(Config) ->
  318. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  319. Ref = gun:get(ConnPid, "/chunked_response"),
  320. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  321. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  322. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  323. gun_down(ConnPid).
  324. http10_hostless(Config) ->
  325. Name = http10_hostless,
  326. Port10 = config(port, Config) + 10,
  327. {Transport, Protocol} = case config(type, Config) of
  328. tcp -> {ranch_tcp, cowboy_clear};
  329. ssl -> {ranch_ssl, cowboy_tls}
  330. end,
  331. ranch:start_listener(Name, 5, Transport,
  332. config(opts, Config) ++ [{port, Port10}],
  333. Protocol, #{
  334. env =>#{dispatch => cowboy_router:compile([
  335. {'_', [{"/http1.0/hostless", http_handler, []}]}])},
  336. max_keepalive => 50,
  337. timeout => 500
  338. }),
  339. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  340. [{port, Port10}|Config]),
  341. cowboy:stop_listener(http10_hostless).
  342. http10_keepalive_default(Config) ->
  343. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  344. Client = raw_open(Config),
  345. ok = raw_send(Client, Normal),
  346. case catch raw_recv_head(Client) of
  347. {'EXIT', _} -> error(closed);
  348. Data ->
  349. %% Cowboy always advertises itself as HTTP/1.1.
  350. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  351. {Headers, _} = cow_http:parse_headers(Rest),
  352. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  353. end,
  354. ok = raw_send(Client, Normal),
  355. case catch raw_recv_head(Client) of
  356. {'EXIT', _} -> closed;
  357. _ -> error(not_closed)
  358. end.
  359. http10_keepalive_forced(Config) ->
  360. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  361. Client = raw_open(Config),
  362. ok = raw_send(Client, Keepalive),
  363. case catch raw_recv_head(Client) of
  364. {'EXIT', _} -> error(closed);
  365. Data ->
  366. %% Cowboy always advertises itself as HTTP/1.1.
  367. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  368. {Headers, _} = cow_http:parse_headers(Rest),
  369. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  370. end,
  371. ok = raw_send(Client, Keepalive),
  372. case catch raw_recv_head(Client) of
  373. {'EXIT', Err} -> error({closed, Err});
  374. _ -> ok
  375. end.
  376. keepalive_max(Config) ->
  377. ConnPid = gun_open(Config),
  378. Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
  379. || _ <- lists:seq(1, 99)],
  380. CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  381. _ = [begin
  382. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  383. false = lists:keymember(<<"connection">>, 1, Headers)
  384. end || Ref <- Refs],
  385. {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef),
  386. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  387. gun_down(ConnPid).
  388. keepalive_nl(Config) ->
  389. ConnPid = gun_open(Config),
  390. Refs = [begin
  391. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  392. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  393. Ref
  394. end || _ <- lists:seq(1, 10)],
  395. _ = [begin
  396. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  397. false = lists:keymember(<<"connection">>, 1, Headers)
  398. end || Ref <- Refs],
  399. ok.
  400. keepalive_stream_loop(Config) ->
  401. ConnPid = gun_open(Config),
  402. Refs = [begin
  403. Ref = gun:post(ConnPid, "/loop_stream_recv",
  404. [{<<"content-type">>, <<"application/octet-stream">>}]),
  405. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  406. || ID <- lists:seq(1, 250)],
  407. gun:data(ConnPid, Ref, fin, <<>>),
  408. Ref
  409. end || _ <- lists:seq(1, 10)],
  410. _ = [begin
  411. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  412. end || Ref <- Refs],
  413. ok.
  414. multipart(Config) ->
  415. ConnPid = gun_open(Config),
  416. Body = <<
  417. "This is a preamble."
  418. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  419. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  420. "\r\n--OHai--\r\n"
  421. "This is an epilogue."
  422. >>,
  423. Ref = gun:post(ConnPid, "/multipart",
  424. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  425. Body),
  426. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  427. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  428. Parts = binary_to_term(RespBody),
  429. Parts = [
  430. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  431. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  432. ],
  433. ok.
  434. multipart_chunked(Config) ->
  435. ConnPid = gun_open(Config),
  436. Body = <<
  437. "This is a preamble."
  438. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  439. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  440. "\r\n--OHai--\r\n"
  441. "This is an epilogue."
  442. >>,
  443. Ref = gun:post(ConnPid, "/multipart",
  444. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}]),
  445. gun:data(ConnPid, Ref, fin, Body),
  446. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  447. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  448. Parts = binary_to_term(RespBody),
  449. Parts = [
  450. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  451. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  452. ],
  453. ok.
  454. multipart_large(Config) ->
  455. ConnPid = gun_open(Config),
  456. Boundary = "----------",
  457. Big = << 0:9000000/unit:8 >>,
  458. Bigger = << 0:9999999/unit:8 >>,
  459. Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
  460. "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
  461. "--", Boundary, "--\r\n"],
  462. Ref = gun:post(ConnPid, "/multipart/large",
  463. [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
  464. Body),
  465. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  466. ok.
  467. do_nc(Config, Input) ->
  468. Cat = os:find_executable("cat"),
  469. Nc = os:find_executable("nc"),
  470. case {Cat, Nc} of
  471. {false, _} ->
  472. {skip, {notfound, cat}};
  473. {_, false} ->
  474. {skip, {notfound, nc}};
  475. _Good ->
  476. %% Throw garbage at the server then check if it's still up.
  477. StrPort = integer_to_list(config(port, Config)),
  478. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  479. || _ <- lists:seq(1, 100)],
  480. 200 = do_get("/", Config)
  481. end.
  482. nc_rand(Config) ->
  483. do_nc(Config, "/dev/urandom").
  484. nc_zero(Config) ->
  485. do_nc(Config, "/dev/zero").
  486. onresponse_capitalize(Config) ->
  487. Client = raw_open(Config),
  488. ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  489. Data = raw_recv_head(Client),
  490. false = nomatch =:= binary:match(Data, <<"Content-Length">>),
  491. ok.
  492. %% Hook for the above onresponse_capitalize test.
  493. do_onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  494. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  495. || {N, V} <- Headers],
  496. cowboy_req:reply(Status, Headers2, Body, Req).
  497. onresponse_crash(Config) ->
  498. ConnPid = gun_open(Config),
  499. Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
  500. {response, fin, 777, Headers} = gun:await(ConnPid, Ref),
  501. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  502. onresponse_reply(Config) ->
  503. ConnPid = gun_open(Config),
  504. Ref = gun:get(ConnPid, "/"),
  505. {response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
  506. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  507. ok.
  508. %% Hook for the above onresponse tests.
  509. do_onresponse_hook(_, Headers, _, Req) ->
  510. cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req).
  511. parse_host(Config) ->
  512. ConnPid = gun_open(Config),
  513. Tests = [
  514. {<<"example.org:8080">>, <<"example.org\n8080">>},
  515. {<<"example.org">>, <<"example.org\n80">>},
  516. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  517. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  518. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  519. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  520. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  521. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  522. ],
  523. [begin
  524. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  525. [{<<"host">>, Host}]),
  526. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  527. {ok, Body} = gun:await_body(ConnPid, Ref)
  528. end || {Host, Body} <- Tests],
  529. ok.
  530. pipeline(Config) ->
  531. ConnPid = gun_open(Config),
  532. Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
  533. _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
  534. ok.
  535. rest_param_all(Config) ->
  536. ConnPid = gun_open(Config),
  537. %% Accept without param.
  538. Ref1 = gun:get(ConnPid, "/param_all",
  539. [{<<"accept">>, <<"text/plain">>}]),
  540. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  541. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  542. %% Accept with param.
  543. Ref2 = gun:get(ConnPid, "/param_all",
  544. [{<<"accept">>, <<"text/plain;level=1">>}]),
  545. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  546. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  547. %% Accept with param and quality.
  548. Ref3 = gun:get(ConnPid, "/param_all",
  549. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  550. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  551. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  552. Ref4 = gun:get(ConnPid, "/param_all",
  553. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  554. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  555. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  556. %% Without Accept.
  557. Ref5 = gun:get(ConnPid, "/param_all"),
  558. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  559. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  560. %% Content-Type without param.
  561. Ref6 = gun:put(ConnPid, "/param_all",
  562. [{<<"content-type">>, <<"text/plain">>}]),
  563. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  564. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  565. %% Content-Type with param.
  566. Ref7 = gun:put(ConnPid, "/param_all",
  567. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  568. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  569. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  570. ok.
  571. rest_bad_accept(Config) ->
  572. ConnPid = gun_open(Config),
  573. Ref = gun:get(ConnPid, "/bad_accept",
  574. [{<<"accept">>, <<"1">>}]),
  575. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  576. ok.
  577. rest_bad_content_type(Config) ->
  578. ConnPid = gun_open(Config),
  579. Ref = gun:patch(ConnPid, "/bad_content_type",
  580. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  581. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  582. ok.
  583. rest_expires(Config) ->
  584. ConnPid = gun_open(Config),
  585. Ref = gun:get(ConnPid, "/rest_expires"),
  586. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  587. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  588. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  589. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  590. ok.
  591. rest_expires_binary(Config) ->
  592. ConnPid = gun_open(Config),
  593. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  594. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  595. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  596. ok.
  597. rest_keepalive(Config) ->
  598. ConnPid = gun_open(Config),
  599. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  600. _ = [begin
  601. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  602. false = lists:keymember(<<"connection">>, 1, Headers)
  603. end || Ref <- Refs],
  604. ok.
  605. rest_keepalive_post(Config) ->
  606. ConnPid = gun_open(Config),
  607. Refs = [begin
  608. Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
  609. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  610. Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
  611. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  612. {Ref1, Ref2}
  613. end || _ <- lists:seq(1, 5)],
  614. _ = [begin
  615. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  616. false = lists:keymember(<<"connection">>, 1, Headers1),
  617. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  618. false = lists:keymember(<<"connection">>, 1, Headers2)
  619. end || {Ref1, Ref2} <- Refs],
  620. ok.
  621. rest_missing_get_callbacks(Config) ->
  622. ConnPid = gun_open(Config),
  623. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  624. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  625. ok.
  626. rest_missing_put_callbacks(Config) ->
  627. ConnPid = gun_open(Config),
  628. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  629. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  630. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  631. ok.
  632. rest_nodelete(Config) ->
  633. ConnPid = gun_open(Config),
  634. Ref = gun:delete(ConnPid, "/nodelete"),
  635. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  636. ok.
  637. rest_options_default(Config) ->
  638. ConnPid = gun_open(Config),
  639. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  640. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  641. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  642. ok.
  643. rest_patch(Config) ->
  644. Tests = [
  645. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  646. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  647. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  648. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  649. ],
  650. ConnPid = gun_open(Config),
  651. _ = [begin
  652. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  653. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  654. end || {Status, Headers, Body} <- Tests],
  655. ok.
  656. rest_post_charset(Config) ->
  657. ConnPid = gun_open(Config),
  658. Ref = gun:post(ConnPid, "/post_charset",
  659. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  660. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  661. ok.
  662. rest_postonly(Config) ->
  663. ConnPid = gun_open(Config),
  664. Ref = gun:post(ConnPid, "/postonly",
  665. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  666. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  667. ok.
  668. rest_resource_get_etag(Config, Type) ->
  669. rest_resource_get_etag(Config, Type, []).
  670. rest_resource_get_etag(Config, Type, Headers) ->
  671. ConnPid = gun_open(Config),
  672. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  673. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  674. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  675. false -> {Status, false};
  676. {<<"etag">>, ETag} -> {Status, ETag}
  677. end.
  678. rest_resource_etags(Config) ->
  679. Tests = [
  680. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  681. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  682. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  683. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  684. {400, false, "binary-strong-unquoted"},
  685. {400, false, "binary-weak-unquoted"}
  686. ],
  687. _ = [{Status, ETag, Type} = begin
  688. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  689. {Ret, RespETag, Type}
  690. end || {Status, ETag, Type} <- Tests].
  691. rest_resource_etags_if_none_match(Config) ->
  692. Tests = [
  693. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  694. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  695. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  696. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  697. ],
  698. _ = [{Status, Type} = begin
  699. {Ret, _} = rest_resource_get_etag(Config, Type,
  700. [{<<"if-none-match">>, ETag}]),
  701. {Ret, Type}
  702. end || {Status, ETag, Type} <- Tests].
  703. set_env_dispatch(Config) ->
  704. ConnPid1 = gun_open(Config),
  705. Ref1 = gun:get(ConnPid1, "/"),
  706. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  707. ok = cowboy:set_env(set_env, dispatch,
  708. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  709. ConnPid2 = gun_open(Config),
  710. Ref2 = gun:get(ConnPid2, "/"),
  711. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  712. ok.
  713. set_resp_body(Config) ->
  714. ConnPid = gun_open(Config),
  715. Ref = gun:get(ConnPid, "/set_resp/body"),
  716. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  717. {ok, <<"A flameless dance does not equal a cycle">>}
  718. = gun:await_body(ConnPid, Ref),
  719. ok.
  720. set_resp_header(Config) ->
  721. ConnPid = gun_open(Config),
  722. Ref = gun:get(ConnPid, "/set_resp/header"),
  723. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  724. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  725. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  726. ok.
  727. set_resp_overwrite(Config) ->
  728. ConnPid = gun_open(Config),
  729. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  730. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  731. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  732. ok.
  733. slowloris(Config) ->
  734. Client = raw_open(Config),
  735. try
  736. [begin
  737. ok = raw_send(Client, [C]),
  738. receive after 250 -> ok end
  739. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  740. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  741. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  742. error(failure)
  743. catch error:{badmatch, _} ->
  744. ok
  745. end.
  746. slowloris2(Config) ->
  747. Client = raw_open(Config),
  748. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  749. receive after 300 -> ok end,
  750. ok = raw_send(Client, "Host: localhost\r\n"),
  751. receive after 300 -> ok end,
  752. Data = raw_recv_head(Client),
  753. {_, 408, _, _} = cow_http:parse_status_line(Data),
  754. ok.
  755. static_attribute_etag(Config) ->
  756. ConnPid = gun_open(Config),
  757. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  758. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  759. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  760. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  761. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  762. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  763. true = ETag =/= undefined,
  764. ok.
  765. static_function_etag(Config) ->
  766. ConnPid = gun_open(Config),
  767. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  768. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  769. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  770. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  771. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  772. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  773. true = ETag =/= undefined,
  774. ok.
  775. static_mimetypes_function(Config) ->
  776. ConnPid = gun_open(Config),
  777. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  778. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  779. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  780. ok.
  781. static_specify_file(Config) ->
  782. ConnPid = gun_open(Config),
  783. Ref = gun:get(ConnPid, "/static_specify_file"),
  784. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  785. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  786. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  787. ok.
  788. static_specify_file_catchall(Config) ->
  789. ConnPid = gun_open(Config),
  790. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  791. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  792. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  793. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  794. ok.
  795. static_test_file(Config) ->
  796. ConnPid = gun_open(Config),
  797. Ref = gun:get(ConnPid, "/static/unknown"),
  798. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  799. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  800. ok.
  801. static_test_file_css(Config) ->
  802. ConnPid = gun_open(Config),
  803. Ref = gun:get(ConnPid, "/static/style.css"),
  804. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  805. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  806. ok.
  807. stream_body_set_resp(Config) ->
  808. ConnPid = gun_open(Config),
  809. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  810. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  811. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  812. ok.
  813. stream_body_set_resp_close(Config) ->
  814. ConnPid = gun_open(Config),
  815. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  816. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  817. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref),
  818. gun_down(ConnPid).
  819. stream_body_set_resp_chunked(Config) ->
  820. ConnPid = gun_open(Config),
  821. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  822. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  823. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  824. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  825. ok.
  826. stream_body_set_resp_chunked10(Config) ->
  827. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  828. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  829. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  830. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  831. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  832. gun_down(ConnPid).
  833. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  834. streamed_response(Config) ->
  835. Client = raw_open(Config),
  836. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  837. Data = raw_recv_head(Client),
  838. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  839. {Headers, Rest2} = cow_http:parse_headers(Rest),
  840. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  841. Rest2Size = byte_size(Rest2),
  842. ok = case <<"streamed_handler\r\nworks fine!">> of
  843. Rest2 -> ok;
  844. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  845. end.
  846. te_chunked(Config) ->
  847. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  848. ConnPid = gun_open(Config),
  849. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  850. gun:data(ConnPid, Ref, fin, Body),
  851. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  852. {ok, Body} = gun:await_body(ConnPid, Ref),
  853. ok.
  854. do_body_to_chunks(_, <<>>, Acc) ->
  855. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  856. do_body_to_chunks(ChunkSize, Body, Acc) ->
  857. BodySize = byte_size(Body),
  858. ChunkSize2 = case BodySize < ChunkSize of
  859. true -> BodySize;
  860. false -> ChunkSize
  861. end,
  862. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  863. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  864. do_body_to_chunks(ChunkSize, Rest,
  865. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  866. te_chunked_chopped(Config) ->
  867. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  868. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  869. ConnPid = gun_open(Config),
  870. Ref = gun:post(ConnPid, "/echo/body",
  871. [{<<"content-type">>, <<"text/plain">>}]),
  872. _ = [begin
  873. ok = gun:dbg_send_raw(ConnPid, << C >>),
  874. receive after 10 -> ok end
  875. end || << C >> <= Body2],
  876. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  877. {ok, Body} = gun:await_body(ConnPid, Ref),
  878. ok.
  879. te_chunked_delayed(Config) ->
  880. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  881. Chunks = do_body_to_chunks(50, Body, []),
  882. ConnPid = gun_open(Config),
  883. Ref = gun:post(ConnPid, "/echo/body",
  884. [{<<"content-type">>, <<"text/plain">>}]),
  885. _ = [begin
  886. ok = gun:dbg_send_raw(ConnPid, Chunk),
  887. receive after 10 -> ok end
  888. end || Chunk <- Chunks],
  889. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  890. {ok, Body} = gun:await_body(ConnPid, Ref),
  891. ok.
  892. te_chunked_split_body(Config) ->
  893. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  894. Chunks = do_body_to_chunks(50, Body, []),
  895. ConnPid = gun_open(Config),
  896. Ref = gun:post(ConnPid, "/echo/body",
  897. [{<<"content-type">>, <<"text/plain">>}]),
  898. _ = [begin
  899. case Chunk of
  900. <<"0\r\n\r\n">> ->
  901. ok = gun:dbg_send_raw(ConnPid, Chunk);
  902. _ ->
  903. [Size, ChunkBody, <<>>] =
  904. binary:split(Chunk, [<<"\r\n">>], [global]),
  905. PartASize = random:uniform(byte_size(ChunkBody)),
  906. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  907. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  908. receive after 10 -> ok end,
  909. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  910. end
  911. end || Chunk <- Chunks],
  912. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  913. {ok, Body} = gun:await_body(ConnPid, Ref),
  914. ok.
  915. te_chunked_split_crlf(Config) ->
  916. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  917. Chunks = do_body_to_chunks(50, Body, []),
  918. ConnPid = gun_open(Config),
  919. Ref = gun:post(ConnPid, "/echo/body",
  920. [{<<"content-type">>, <<"text/plain">>}]),
  921. _ = [begin
  922. %% Split in the newline just before the end of the chunk.
  923. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  924. << Chunk2:Len/binary, End/binary >> = Chunk,
  925. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  926. receive after 10 -> ok end,
  927. ok = gun:dbg_send_raw(ConnPid, End)
  928. end || Chunk <- Chunks],
  929. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  930. {ok, Body} = gun:await_body(ConnPid, Ref),
  931. ok.
  932. te_identity(Config) ->
  933. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  934. ConnPid = gun_open(Config),
  935. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  936. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  937. {ok, Body} = gun:await_body(ConnPid, Ref),
  938. ok.