http_SUITE.erl 31 KB

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