http_SUITE.erl 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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 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_last_modified_undefined(Config) ->
  598. ConnPid = gun_open(Config),
  599. Ref = gun:get(ConnPid, "/simple",
  600. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  601. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  602. ok.
  603. rest_keepalive(Config) ->
  604. ConnPid = gun_open(Config),
  605. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  606. _ = [begin
  607. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  608. false = lists:keymember(<<"connection">>, 1, Headers)
  609. end || Ref <- Refs],
  610. ok.
  611. rest_keepalive_post(Config) ->
  612. ConnPid = gun_open(Config),
  613. Refs = [begin
  614. Ref1 = gun:post(ConnPid, "/forbidden_post", [{<<"content-type">>, <<"text/plain">>}]),
  615. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  616. Ref2 = gun:post(ConnPid, "/simple_post", [{<<"content-type">>, <<"text/plain">>}]),
  617. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  618. {Ref1, Ref2}
  619. end || _ <- lists:seq(1, 5)],
  620. _ = [begin
  621. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  622. false = lists:keymember(<<"connection">>, 1, Headers1),
  623. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  624. false = lists:keymember(<<"connection">>, 1, Headers2)
  625. end || {Ref1, Ref2} <- Refs],
  626. ok.
  627. rest_missing_get_callbacks(Config) ->
  628. ConnPid = gun_open(Config),
  629. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  630. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  631. ok.
  632. rest_missing_put_callbacks(Config) ->
  633. ConnPid = gun_open(Config),
  634. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  635. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  636. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  637. ok.
  638. rest_nodelete(Config) ->
  639. ConnPid = gun_open(Config),
  640. Ref = gun:delete(ConnPid, "/nodelete"),
  641. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  642. ok.
  643. rest_options_default(Config) ->
  644. ConnPid = gun_open(Config),
  645. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  646. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  647. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  648. ok.
  649. rest_patch(Config) ->
  650. Tests = [
  651. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  652. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  653. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  654. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  655. ],
  656. ConnPid = gun_open(Config),
  657. _ = [begin
  658. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  659. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  660. end || {Status, Headers, Body} <- Tests],
  661. ok.
  662. rest_post_charset(Config) ->
  663. ConnPid = gun_open(Config),
  664. Ref = gun:post(ConnPid, "/post_charset",
  665. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  666. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  667. ok.
  668. rest_postonly(Config) ->
  669. ConnPid = gun_open(Config),
  670. Ref = gun:post(ConnPid, "/postonly",
  671. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  672. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  673. ok.
  674. rest_resource_get_etag(Config, Type) ->
  675. rest_resource_get_etag(Config, Type, []).
  676. rest_resource_get_etag(Config, Type, Headers) ->
  677. ConnPid = gun_open(Config),
  678. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  679. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  680. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  681. false -> {Status, false};
  682. {<<"etag">>, ETag} -> {Status, ETag}
  683. end.
  684. rest_resource_etags(Config) ->
  685. Tests = [
  686. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  687. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  688. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  689. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  690. {400, false, "binary-strong-unquoted"},
  691. {400, false, "binary-weak-unquoted"}
  692. ],
  693. _ = [{Status, ETag, Type} = begin
  694. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  695. {Ret, RespETag, Type}
  696. end || {Status, ETag, Type} <- Tests].
  697. rest_resource_etags_if_none_match(Config) ->
  698. Tests = [
  699. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  700. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  701. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  702. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  703. ],
  704. _ = [{Status, Type} = begin
  705. {Ret, _} = rest_resource_get_etag(Config, Type,
  706. [{<<"if-none-match">>, ETag}]),
  707. {Ret, Type}
  708. end || {Status, ETag, Type} <- Tests].
  709. set_env_dispatch(Config) ->
  710. ConnPid1 = gun_open(Config),
  711. Ref1 = gun:get(ConnPid1, "/"),
  712. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  713. ok = cowboy:set_env(set_env, dispatch,
  714. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  715. ConnPid2 = gun_open(Config),
  716. Ref2 = gun:get(ConnPid2, "/"),
  717. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  718. ok.
  719. set_resp_body(Config) ->
  720. ConnPid = gun_open(Config),
  721. Ref = gun:get(ConnPid, "/set_resp/body"),
  722. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  723. {ok, <<"A flameless dance does not equal a cycle">>}
  724. = gun:await_body(ConnPid, Ref),
  725. ok.
  726. set_resp_header(Config) ->
  727. ConnPid = gun_open(Config),
  728. Ref = gun:get(ConnPid, "/set_resp/header"),
  729. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  730. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  731. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  732. ok.
  733. set_resp_overwrite(Config) ->
  734. ConnPid = gun_open(Config),
  735. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  736. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  737. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  738. ok.
  739. slowloris(Config) ->
  740. Client = raw_open(Config),
  741. try
  742. [begin
  743. ok = raw_send(Client, [C]),
  744. receive after 250 -> ok end
  745. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  746. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  747. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  748. error(failure)
  749. catch error:{badmatch, _} ->
  750. ok
  751. end.
  752. slowloris2(Config) ->
  753. Client = raw_open(Config),
  754. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  755. receive after 300 -> ok end,
  756. ok = raw_send(Client, "Host: localhost\r\n"),
  757. receive after 300 -> ok end,
  758. Data = raw_recv_head(Client),
  759. {_, 408, _, _} = cow_http:parse_status_line(Data),
  760. ok.
  761. static_attribute_etag(Config) ->
  762. ConnPid = gun_open(Config),
  763. Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  764. Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
  765. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  766. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  767. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  768. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  769. true = ETag =/= undefined,
  770. ok.
  771. static_function_etag(Config) ->
  772. ConnPid = gun_open(Config),
  773. Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
  774. Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
  775. {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
  776. {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
  777. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
  778. {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
  779. true = ETag =/= undefined,
  780. ok.
  781. static_mimetypes_function(Config) ->
  782. ConnPid = gun_open(Config),
  783. Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
  784. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  785. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  786. ok.
  787. static_specify_file(Config) ->
  788. ConnPid = gun_open(Config),
  789. Ref = gun:get(ConnPid, "/static_specify_file"),
  790. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  791. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  792. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  793. ok.
  794. static_specify_file_catchall(Config) ->
  795. ConnPid = gun_open(Config),
  796. Ref = gun:get(ConnPid, "/static_specify_file/none"),
  797. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  798. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  799. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
  800. ok.
  801. static_test_file(Config) ->
  802. ConnPid = gun_open(Config),
  803. Ref = gun:get(ConnPid, "/static/unknown"),
  804. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  805. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  806. ok.
  807. static_test_file_css(Config) ->
  808. ConnPid = gun_open(Config),
  809. Ref = gun:get(ConnPid, "/static/style.css"),
  810. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  811. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  812. ok.
  813. stream_body_set_resp(Config) ->
  814. ConnPid = gun_open(Config),
  815. Ref = gun:get(ConnPid, "/stream_body/set_resp"),
  816. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  817. {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
  818. ok.
  819. stream_body_set_resp_close(Config) ->
  820. ConnPid = gun_open(Config),
  821. Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
  822. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  823. {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref),
  824. gun_down(ConnPid).
  825. stream_body_set_resp_chunked(Config) ->
  826. ConnPid = gun_open(Config),
  827. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  828. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  829. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  830. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  831. ok.
  832. stream_body_set_resp_chunked10(Config) ->
  833. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  834. Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
  835. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  836. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  837. {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
  838. gun_down(ConnPid).
  839. %% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
  840. streamed_response(Config) ->
  841. Client = raw_open(Config),
  842. ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
  843. Data = raw_recv_head(Client),
  844. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  845. {Headers, Rest2} = cow_http:parse_headers(Rest),
  846. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  847. Rest2Size = byte_size(Rest2),
  848. ok = case <<"streamed_handler\r\nworks fine!">> of
  849. Rest2 -> ok;
  850. << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
  851. end.
  852. te_chunked(Config) ->
  853. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  854. ConnPid = gun_open(Config),
  855. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  856. gun:data(ConnPid, Ref, fin, Body),
  857. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  858. {ok, Body} = gun:await_body(ConnPid, Ref),
  859. ok.
  860. do_body_to_chunks(_, <<>>, Acc) ->
  861. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  862. do_body_to_chunks(ChunkSize, Body, Acc) ->
  863. BodySize = byte_size(Body),
  864. ChunkSize2 = case BodySize < ChunkSize of
  865. true -> BodySize;
  866. false -> ChunkSize
  867. end,
  868. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  869. ChunkSizeBin = integer_to_binary(ChunkSize2, 16),
  870. do_body_to_chunks(ChunkSize, Rest,
  871. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  872. te_chunked_chopped(Config) ->
  873. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  874. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  875. ConnPid = gun_open(Config),
  876. Ref = gun:post(ConnPid, "/echo/body",
  877. [{<<"content-type">>, <<"text/plain">>}]),
  878. _ = [begin
  879. ok = gun:dbg_send_raw(ConnPid, << C >>),
  880. receive after 10 -> ok end
  881. end || << C >> <= Body2],
  882. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  883. {ok, Body} = gun:await_body(ConnPid, Ref),
  884. ok.
  885. te_chunked_delayed(Config) ->
  886. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  887. Chunks = do_body_to_chunks(50, Body, []),
  888. ConnPid = gun_open(Config),
  889. Ref = gun:post(ConnPid, "/echo/body",
  890. [{<<"content-type">>, <<"text/plain">>}]),
  891. _ = [begin
  892. ok = gun:dbg_send_raw(ConnPid, Chunk),
  893. receive after 10 -> ok end
  894. end || Chunk <- Chunks],
  895. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  896. {ok, Body} = gun:await_body(ConnPid, Ref),
  897. ok.
  898. te_chunked_split_body(Config) ->
  899. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  900. Chunks = do_body_to_chunks(50, Body, []),
  901. ConnPid = gun_open(Config),
  902. Ref = gun:post(ConnPid, "/echo/body",
  903. [{<<"content-type">>, <<"text/plain">>}]),
  904. _ = [begin
  905. case Chunk of
  906. <<"0\r\n\r\n">> ->
  907. ok = gun:dbg_send_raw(ConnPid, Chunk);
  908. _ ->
  909. [Size, ChunkBody, <<>>] =
  910. binary:split(Chunk, [<<"\r\n">>], [global]),
  911. PartASize = random:uniform(byte_size(ChunkBody)),
  912. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  913. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  914. receive after 10 -> ok end,
  915. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  916. end
  917. end || Chunk <- Chunks],
  918. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  919. {ok, Body} = gun:await_body(ConnPid, Ref),
  920. ok.
  921. te_chunked_split_crlf(Config) ->
  922. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  923. Chunks = do_body_to_chunks(50, Body, []),
  924. ConnPid = gun_open(Config),
  925. Ref = gun:post(ConnPid, "/echo/body",
  926. [{<<"content-type">>, <<"text/plain">>}]),
  927. _ = [begin
  928. %% Split in the newline just before the end of the chunk.
  929. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  930. << Chunk2:Len/binary, End/binary >> = Chunk,
  931. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  932. receive after 10 -> ok end,
  933. ok = gun:dbg_send_raw(ConnPid, End)
  934. end || Chunk <- Chunks],
  935. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  936. {ok, Body} = gun:await_body(ConnPid, Ref),
  937. ok.
  938. te_identity(Config) ->
  939. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  940. ConnPid = gun_open(Config),
  941. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  942. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  943. {ok, Body} = gun:await_body(ConnPid, Ref),
  944. ok.