old_http_SUITE.erl 25 KB

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