http_SUITE.erl 24 KB

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