old_http_SUITE.erl 23 KB

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