http_SUITE.erl 26 KB

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