http_SUITE.erl 36 KB

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