http_SUITE.erl 24 KB

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