old_http_SUITE.erl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. ].
  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, "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\nHost: localhost"},
  171. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  172. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  173. {closed, Huge},
  174. {closed, ""},
  175. {closed, "\r\n"},
  176. {closed, "\r\n\r\n"},
  177. {closed, "GET / HTTP/1.1"}
  178. ],
  179. _ = [{Status, Packet} = begin
  180. Ret = do_raw(Packet, Config),
  181. {Ret, Packet}
  182. end || {Status, Packet} <- Tests],
  183. ok.
  184. check_status(Config) ->
  185. Tests = [
  186. {200, "/"},
  187. {200, "/simple"},
  188. {404, "/not/found"},
  189. {500, "/handler_errors?case=init_before_reply"}
  190. ],
  191. _ = [{Status, URL} = begin
  192. Ret = do_get(URL, Config),
  193. {Ret, URL}
  194. end || {Status, URL} <- Tests].
  195. error_init_after_reply(Config) ->
  196. ConnPid = gun_open(Config),
  197. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  198. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  199. ok.
  200. headers_dupe(Config) ->
  201. ConnPid = gun_open(Config),
  202. Ref = gun:get(ConnPid, "/headers/dupe"),
  203. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  204. %% Ensure that only one connection header was received.
  205. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  206. gun_down(ConnPid).
  207. http10_chunkless(Config) ->
  208. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  209. Ref = gun:get(ConnPid, "/chunked_response"),
  210. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  211. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  212. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  213. gun_down(ConnPid).
  214. http10_hostless(Config) ->
  215. Name = http10_hostless,
  216. Port10 = config(port, Config) + 10,
  217. {Transport, Protocol} = case config(type, Config) of
  218. tcp -> {ranch_tcp, cowboy_clear};
  219. ssl -> {ranch_ssl, cowboy_tls}
  220. end,
  221. ranch:start_listener(Name, 5, Transport,
  222. config(opts, Config) ++ [{port, Port10}],
  223. Protocol, #{
  224. env =>#{dispatch => cowboy_router:compile([
  225. {'_', [{"/http1.0/hostless", http_handler, []}]}])},
  226. max_keepalive => 50,
  227. timeout => 500
  228. }),
  229. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  230. [{port, Port10}|Config]),
  231. cowboy:stop_listener(http10_hostless).
  232. http10_keepalive_default(Config) ->
  233. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  234. Client = raw_open(Config),
  235. ok = raw_send(Client, Normal),
  236. case catch raw_recv_head(Client) of
  237. {'EXIT', _} -> error(closed);
  238. Data ->
  239. %% Cowboy always advertises itself as HTTP/1.1.
  240. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  241. {Headers, _} = cow_http:parse_headers(Rest),
  242. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  243. end,
  244. ok = raw_send(Client, Normal),
  245. case catch raw_recv_head(Client) of
  246. {'EXIT', _} -> closed;
  247. _ -> error(not_closed)
  248. end.
  249. http10_keepalive_forced(Config) ->
  250. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  251. Client = raw_open(Config),
  252. ok = raw_send(Client, Keepalive),
  253. case catch raw_recv_head(Client) of
  254. {'EXIT', _} -> error(closed);
  255. Data ->
  256. %% Cowboy always advertises itself as HTTP/1.1.
  257. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  258. {Headers, _} = cow_http:parse_headers(Rest),
  259. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  260. end,
  261. ok = raw_send(Client, Keepalive),
  262. case catch raw_recv_head(Client) of
  263. {'EXIT', Err} -> error({closed, Err});
  264. _ -> ok
  265. end.
  266. keepalive_nl(Config) ->
  267. ConnPid = gun_open(Config),
  268. Refs = [begin
  269. Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
  270. gun:dbg_send_raw(ConnPid, <<"\r\n">>),
  271. Ref
  272. end || _ <- lists:seq(1, 10)],
  273. _ = [begin
  274. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  275. false = lists:keymember(<<"connection">>, 1, Headers)
  276. end || Ref <- Refs],
  277. ok.
  278. keepalive_stream_loop(Config) ->
  279. ConnPid = gun_open(Config),
  280. Refs = [begin
  281. Ref = gun:post(ConnPid, "/loop_stream_recv",
  282. [{<<"content-type">>, <<"application/octet-stream">>}]),
  283. _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
  284. || ID <- lists:seq(1, 250)],
  285. gun:data(ConnPid, Ref, fin, <<>>),
  286. Ref
  287. end || _ <- lists:seq(1, 10)],
  288. _ = [begin
  289. {response, fin, 200, _} = gun:await(ConnPid, Ref)
  290. end || Ref <- Refs],
  291. ok.
  292. do_nc(Config, Input) ->
  293. Cat = os:find_executable("cat"),
  294. Nc = os:find_executable("nc"),
  295. case {Cat, Nc} of
  296. {false, _} ->
  297. {skip, {notfound, cat}};
  298. {_, false} ->
  299. {skip, {notfound, nc}};
  300. _Good ->
  301. %% Throw garbage at the server then check if it's still up.
  302. StrPort = integer_to_list(config(port, Config)),
  303. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  304. || _ <- lists:seq(1, 100)],
  305. 200 = do_get("/", Config)
  306. end.
  307. nc_rand(Config) ->
  308. do_nc(Config, "/dev/urandom").
  309. nc_zero(Config) ->
  310. do_nc(Config, "/dev/zero").
  311. parse_host(Config) ->
  312. ConnPid = gun_open(Config),
  313. Tests = [
  314. {<<"example.org:8080">>, <<"example.org\n8080">>},
  315. {<<"example.org">>, <<"example.org\n80">>},
  316. {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
  317. {<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
  318. {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
  319. {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
  320. {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
  321. {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
  322. ],
  323. [begin
  324. Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
  325. [{<<"host">>, Host}]),
  326. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  327. {ok, Body} = gun:await_body(ConnPid, Ref)
  328. end || {Host, Body} <- Tests],
  329. ok.
  330. rest_param_all(Config) ->
  331. ConnPid = gun_open(Config),
  332. %% Accept without param.
  333. Ref1 = gun:get(ConnPid, "/param_all",
  334. [{<<"accept">>, <<"text/plain">>}]),
  335. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  336. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  337. %% Accept with param.
  338. Ref2 = gun:get(ConnPid, "/param_all",
  339. [{<<"accept">>, <<"text/plain;level=1">>}]),
  340. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  341. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  342. %% Accept with param and quality.
  343. Ref3 = gun:get(ConnPid, "/param_all",
  344. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  345. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  346. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  347. Ref4 = gun:get(ConnPid, "/param_all",
  348. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  349. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  350. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  351. %% Without Accept.
  352. Ref5 = gun:get(ConnPid, "/param_all"),
  353. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  354. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  355. %% Content-Type without param.
  356. Ref6 = gun:put(ConnPid, "/param_all",
  357. [{<<"content-type">>, <<"text/plain">>}]),
  358. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  359. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  360. %% Content-Type with param.
  361. Ref7 = gun:put(ConnPid, "/param_all",
  362. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  363. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  364. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  365. ok.
  366. rest_bad_accept(Config) ->
  367. ConnPid = gun_open(Config),
  368. Ref = gun:get(ConnPid, "/bad_accept",
  369. [{<<"accept">>, <<"1">>}]),
  370. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  371. ok.
  372. rest_bad_content_type(Config) ->
  373. ConnPid = gun_open(Config),
  374. Ref = gun:patch(ConnPid, "/bad_content_type",
  375. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  376. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  377. ok.
  378. rest_expires(Config) ->
  379. ConnPid = gun_open(Config),
  380. Ref = gun:get(ConnPid, "/rest_expires"),
  381. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  382. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  383. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  384. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  385. ok.
  386. rest_expires_binary(Config) ->
  387. ConnPid = gun_open(Config),
  388. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  389. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  390. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  391. ok.
  392. rest_last_modified_undefined(Config) ->
  393. ConnPid = gun_open(Config),
  394. Ref = gun:get(ConnPid, "/simple",
  395. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  396. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  397. ok.
  398. rest_keepalive(Config) ->
  399. ConnPid = gun_open(Config),
  400. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  401. _ = [begin
  402. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  403. false = lists:keymember(<<"connection">>, 1, Headers)
  404. end || Ref <- Refs],
  405. ok.
  406. rest_keepalive_post(Config) ->
  407. ConnPid = gun_open(Config),
  408. Refs = [begin
  409. Ref1 = gun:post(ConnPid, "/forbidden_post", [
  410. {<<"content-type">>, <<"text/plain">>},
  411. {<<"content-length">>, <<"12">>}
  412. ]),
  413. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  414. Ref2 = gun:post(ConnPid, "/simple_post", [
  415. {<<"content-type">>, <<"text/plain">>},
  416. {<<"content-length">>, <<"12">>}
  417. ]),
  418. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  419. {Ref1, Ref2}
  420. end || _ <- lists:seq(1, 5)],
  421. _ = [begin
  422. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  423. false = lists:keymember(<<"connection">>, 1, Headers1),
  424. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  425. false = lists:keymember(<<"connection">>, 1, Headers2)
  426. end || {Ref1, Ref2} <- Refs],
  427. ok.
  428. rest_missing_get_callbacks(Config) ->
  429. ConnPid = gun_open(Config),
  430. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  431. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  432. ok.
  433. rest_missing_put_callbacks(Config) ->
  434. ConnPid = gun_open(Config),
  435. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  436. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  437. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  438. ok.
  439. rest_nodelete(Config) ->
  440. ConnPid = gun_open(Config),
  441. Ref = gun:delete(ConnPid, "/nodelete"),
  442. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  443. ok.
  444. rest_options_default(Config) ->
  445. ConnPid = gun_open(Config),
  446. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  447. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  448. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  449. ok.
  450. rest_patch(Config) ->
  451. Tests = [
  452. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  453. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  454. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  455. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  456. ],
  457. ConnPid = gun_open(Config),
  458. _ = [begin
  459. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  460. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  461. end || {Status, Headers, Body} <- Tests],
  462. ok.
  463. rest_post_charset(Config) ->
  464. ConnPid = gun_open(Config),
  465. Ref = gun:post(ConnPid, "/post_charset",
  466. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  467. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  468. ok.
  469. rest_postonly(Config) ->
  470. ConnPid = gun_open(Config),
  471. Ref = gun:post(ConnPid, "/postonly",
  472. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  473. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  474. ok.
  475. rest_resource_get_etag(Config, Type) ->
  476. rest_resource_get_etag(Config, Type, []).
  477. rest_resource_get_etag(Config, Type, Headers) ->
  478. ConnPid = gun_open(Config),
  479. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  480. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  481. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  482. false -> {Status, false};
  483. {<<"etag">>, ETag} -> {Status, ETag}
  484. end.
  485. rest_resource_etags(Config) ->
  486. Tests = [
  487. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  488. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  489. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  490. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  491. {500, false, "binary-strong-unquoted"},
  492. {500, false, "binary-weak-unquoted"}
  493. ],
  494. _ = [{Status, ETag, Type} = begin
  495. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  496. {Ret, RespETag, Type}
  497. end || {Status, ETag, Type} <- Tests].
  498. rest_resource_etags_if_none_match(Config) ->
  499. Tests = [
  500. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  501. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  502. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  503. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  504. ],
  505. _ = [{Status, Type} = begin
  506. {Ret, _} = rest_resource_get_etag(Config, Type,
  507. [{<<"if-none-match">>, ETag}]),
  508. {Ret, Type}
  509. end || {Status, ETag, Type} <- Tests].
  510. set_env_dispatch(Config) ->
  511. ConnPid1 = gun_open(Config),
  512. Ref1 = gun:get(ConnPid1, "/"),
  513. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  514. ok = cowboy:set_env(set_env, dispatch,
  515. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  516. ConnPid2 = gun_open(Config),
  517. Ref2 = gun:get(ConnPid2, "/"),
  518. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  519. ok.
  520. set_resp_overwrite(Config) ->
  521. ConnPid = gun_open(Config),
  522. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  523. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  524. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  525. ok.
  526. slowloris(Config) ->
  527. Client = raw_open(Config),
  528. try
  529. [begin
  530. ok = raw_send(Client, [C]),
  531. receive after 250 -> ok end
  532. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  533. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  534. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  535. error(failure)
  536. catch error:{badmatch, _} ->
  537. ok
  538. end.
  539. slowloris2(Config) ->
  540. Client = raw_open(Config),
  541. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  542. receive after 300 -> ok end,
  543. ok = raw_send(Client, "Host: localhost\r\n"),
  544. receive after 300 -> ok end,
  545. Data = raw_recv_head(Client),
  546. {_, 408, _, _} = cow_http:parse_status_line(Data),
  547. ok.
  548. te_chunked(Config) ->
  549. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  550. ConnPid = gun_open(Config),
  551. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  552. gun:data(ConnPid, Ref, fin, Body),
  553. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  554. {ok, Body} = gun:await_body(ConnPid, Ref),
  555. ok.
  556. do_body_to_chunks(_, <<>>, Acc) ->
  557. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  558. do_body_to_chunks(ChunkSize, Body, Acc) ->
  559. BodySize = byte_size(Body),
  560. ChunkSize2 = case BodySize < ChunkSize of
  561. true -> BodySize;
  562. false -> ChunkSize
  563. end,
  564. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  565. ChunkSizeBin = integer_to_binary(ChunkSize2, 16),
  566. do_body_to_chunks(ChunkSize, Rest,
  567. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  568. te_chunked_chopped(Config) ->
  569. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  570. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  571. ConnPid = gun_open(Config),
  572. Ref = gun:post(ConnPid, "/echo/body",
  573. [{<<"content-type">>, <<"text/plain">>}]),
  574. _ = [begin
  575. ok = gun:dbg_send_raw(ConnPid, << C >>),
  576. receive after 10 -> ok end
  577. end || << C >> <= Body2],
  578. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  579. {ok, Body} = gun:await_body(ConnPid, Ref),
  580. ok.
  581. te_chunked_delayed(Config) ->
  582. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  583. Chunks = do_body_to_chunks(50, Body, []),
  584. ConnPid = gun_open(Config),
  585. Ref = gun:post(ConnPid, "/echo/body",
  586. [{<<"content-type">>, <<"text/plain">>}]),
  587. _ = [begin
  588. ok = gun:dbg_send_raw(ConnPid, Chunk),
  589. receive after 10 -> ok end
  590. end || Chunk <- Chunks],
  591. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  592. {ok, Body} = gun:await_body(ConnPid, Ref),
  593. ok.
  594. te_chunked_split_body(Config) ->
  595. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  596. Chunks = do_body_to_chunks(50, Body, []),
  597. ConnPid = gun_open(Config),
  598. Ref = gun:post(ConnPid, "/echo/body",
  599. [{<<"content-type">>, <<"text/plain">>}]),
  600. _ = [begin
  601. case Chunk of
  602. <<"0\r\n\r\n">> ->
  603. ok = gun:dbg_send_raw(ConnPid, Chunk);
  604. _ ->
  605. [Size, ChunkBody, <<>>] =
  606. binary:split(Chunk, [<<"\r\n">>], [global]),
  607. PartASize = random:uniform(byte_size(ChunkBody)),
  608. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  609. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  610. receive after 10 -> ok end,
  611. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  612. end
  613. end || Chunk <- Chunks],
  614. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  615. {ok, Body} = gun:await_body(ConnPid, Ref),
  616. ok.
  617. te_chunked_split_crlf(Config) ->
  618. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  619. Chunks = do_body_to_chunks(50, Body, []),
  620. ConnPid = gun_open(Config),
  621. Ref = gun:post(ConnPid, "/echo/body",
  622. [{<<"content-type">>, <<"text/plain">>}]),
  623. _ = [begin
  624. %% Split in the newline just before the end of the chunk.
  625. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  626. << Chunk2:Len/binary, End/binary >> = Chunk,
  627. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  628. receive after 10 -> ok end,
  629. ok = gun:dbg_send_raw(ConnPid, End)
  630. end || Chunk <- Chunks],
  631. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  632. {ok, Body} = gun:await_body(ConnPid, Ref),
  633. ok.
  634. te_identity(Config) ->
  635. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  636. ConnPid = gun_open(Config),
  637. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  638. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  639. {ok, Body} = gun:await_body(ConnPid, Ref),
  640. ok.