http_SUITE.erl 35 KB

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