http_SUITE.erl 29 KB

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