http_SUITE.erl 25 KB

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