old_http_SUITE.erl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. %% Check if sending requests whose size is around the MTU breaks something.
  233. echo_body(Config) ->
  234. MTU = ct_helper:get_loopback_mtu(),
  235. _ = [begin
  236. Body = list_to_binary(lists:duplicate(Size, $a)),
  237. ConnPid = gun_open(Config),
  238. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  239. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  240. {ok, Body} = gun:await_body(ConnPid, Ref)
  241. end || Size <- lists:seq(MTU - 500, MTU)],
  242. ok.
  243. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  244. echo_body_max_length(Config) ->
  245. ConnPid = gun_open(Config),
  246. Ref = gun:post(ConnPid, "/echo/body", [], << 0:10000000/unit:8 >>),
  247. {response, nofin, 413, _} = gun:await(ConnPid, Ref),
  248. ok.
  249. error_init_after_reply(Config) ->
  250. ConnPid = gun_open(Config),
  251. Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
  252. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  253. ok.
  254. headers_dupe(Config) ->
  255. ConnPid = gun_open(Config),
  256. Ref = gun:get(ConnPid, "/headers/dupe"),
  257. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  258. %% Ensure that only one connection header was received.
  259. [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
  260. gun_down(ConnPid).
  261. http10_chunkless(Config) ->
  262. ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
  263. Ref = gun:get(ConnPid, "/chunked_response"),
  264. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  265. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  266. {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
  267. gun_down(ConnPid).
  268. http10_hostless(Config) ->
  269. Name = http10_hostless,
  270. Port10 = config(port, Config) + 10,
  271. {Transport, Protocol} = case config(type, Config) of
  272. tcp -> {ranch_tcp, cowboy_clear};
  273. ssl -> {ranch_ssl, cowboy_tls}
  274. end,
  275. ranch:start_listener(Name, 5, Transport,
  276. config(opts, Config) ++ [{port, Port10}],
  277. Protocol, #{
  278. env =>#{dispatch => cowboy_router:compile([
  279. {'_', [{"/http1.0/hostless", http_handler, []}]}])},
  280. max_keepalive => 50,
  281. timeout => 500
  282. }),
  283. 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  284. [{port, Port10}|Config]),
  285. cowboy:stop_listener(http10_hostless).
  286. http10_keepalive_default(Config) ->
  287. Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
  288. Client = raw_open(Config),
  289. ok = raw_send(Client, Normal),
  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. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers)
  297. end,
  298. ok = raw_send(Client, Normal),
  299. case catch raw_recv_head(Client) of
  300. {'EXIT', _} -> closed;
  301. _ -> error(not_closed)
  302. end.
  303. http10_keepalive_forced(Config) ->
  304. Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
  305. Client = raw_open(Config),
  306. ok = raw_send(Client, Keepalive),
  307. case catch raw_recv_head(Client) of
  308. {'EXIT', _} -> error(closed);
  309. Data ->
  310. %% Cowboy always advertises itself as HTTP/1.1.
  311. {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
  312. {Headers, _} = cow_http:parse_headers(Rest),
  313. {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
  314. end,
  315. ok = raw_send(Client, Keepalive),
  316. case catch raw_recv_head(Client) of
  317. {'EXIT', Err} -> error({closed, Err});
  318. _ -> ok
  319. end.
  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. rest_param_all(Config) ->
  385. ConnPid = gun_open(Config),
  386. %% Accept without param.
  387. Ref1 = gun:get(ConnPid, "/param_all",
  388. [{<<"accept">>, <<"text/plain">>}]),
  389. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  390. {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
  391. %% Accept with param.
  392. Ref2 = gun:get(ConnPid, "/param_all",
  393. [{<<"accept">>, <<"text/plain;level=1">>}]),
  394. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  395. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
  396. %% Accept with param and quality.
  397. Ref3 = gun:get(ConnPid, "/param_all",
  398. [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
  399. {response, nofin, 200, _} = gun:await(ConnPid, Ref3),
  400. {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
  401. Ref4 = gun:get(ConnPid, "/param_all",
  402. [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
  403. {response, nofin, 200, _} = gun:await(ConnPid, Ref4),
  404. {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
  405. %% Without Accept.
  406. Ref5 = gun:get(ConnPid, "/param_all"),
  407. {response, nofin, 200, _} = gun:await(ConnPid, Ref5),
  408. {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
  409. %% Content-Type without param.
  410. Ref6 = gun:put(ConnPid, "/param_all",
  411. [{<<"content-type">>, <<"text/plain">>}]),
  412. gun:data(ConnPid, Ref6, fin, "Hello world!"),
  413. {response, fin, 204, _} = gun:await(ConnPid, Ref6),
  414. %% Content-Type with param.
  415. Ref7 = gun:put(ConnPid, "/param_all",
  416. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
  417. gun:data(ConnPid, Ref7, fin, "Hello world!"),
  418. {response, fin, 204, _} = gun:await(ConnPid, Ref7),
  419. ok.
  420. rest_bad_accept(Config) ->
  421. ConnPid = gun_open(Config),
  422. Ref = gun:get(ConnPid, "/bad_accept",
  423. [{<<"accept">>, <<"1">>}]),
  424. {response, fin, 400, _} = gun:await(ConnPid, Ref),
  425. ok.
  426. rest_bad_content_type(Config) ->
  427. ConnPid = gun_open(Config),
  428. Ref = gun:patch(ConnPid, "/bad_content_type",
  429. [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
  430. {response, fin, 415, _} = gun:await(ConnPid, Ref),
  431. ok.
  432. rest_expires(Config) ->
  433. ConnPid = gun_open(Config),
  434. Ref = gun:get(ConnPid, "/rest_expires"),
  435. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  436. {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
  437. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
  438. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  439. ok.
  440. rest_expires_binary(Config) ->
  441. ConnPid = gun_open(Config),
  442. Ref = gun:get(ConnPid, "/rest_expires_binary"),
  443. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  444. {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
  445. ok.
  446. rest_last_modified_undefined(Config) ->
  447. ConnPid = gun_open(Config),
  448. Ref = gun:get(ConnPid, "/simple",
  449. [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]),
  450. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  451. ok.
  452. rest_keepalive(Config) ->
  453. ConnPid = gun_open(Config),
  454. Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
  455. _ = [begin
  456. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  457. false = lists:keymember(<<"connection">>, 1, Headers)
  458. end || Ref <- Refs],
  459. ok.
  460. rest_keepalive_post(Config) ->
  461. ConnPid = gun_open(Config),
  462. Refs = [begin
  463. Ref1 = gun:post(ConnPid, "/forbidden_post", [
  464. {<<"content-type">>, <<"text/plain">>},
  465. {<<"content-length">>, <<"12">>}
  466. ]),
  467. gun:data(ConnPid, Ref1, fin, "Hello world!"),
  468. Ref2 = gun:post(ConnPid, "/simple_post", [
  469. {<<"content-type">>, <<"text/plain">>},
  470. {<<"content-length">>, <<"12">>}
  471. ]),
  472. gun:data(ConnPid, Ref2, fin, "Hello world!"),
  473. {Ref1, Ref2}
  474. end || _ <- lists:seq(1, 5)],
  475. _ = [begin
  476. {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
  477. false = lists:keymember(<<"connection">>, 1, Headers1),
  478. {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
  479. false = lists:keymember(<<"connection">>, 1, Headers2)
  480. end || {Ref1, Ref2} <- Refs],
  481. ok.
  482. rest_missing_get_callbacks(Config) ->
  483. ConnPid = gun_open(Config),
  484. Ref = gun:get(ConnPid, "/missing_get_callbacks"),
  485. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  486. ok.
  487. rest_missing_put_callbacks(Config) ->
  488. ConnPid = gun_open(Config),
  489. Ref = gun:put(ConnPid, "/missing_put_callbacks",
  490. [{<<"content-type">>, <<"application/json">>}], <<"{}">>),
  491. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  492. ok.
  493. rest_nodelete(Config) ->
  494. ConnPid = gun_open(Config),
  495. Ref = gun:delete(ConnPid, "/nodelete"),
  496. {response, fin, 500, _} = gun:await(ConnPid, Ref),
  497. ok.
  498. rest_options_default(Config) ->
  499. ConnPid = gun_open(Config),
  500. Ref = gun:options(ConnPid, "/rest_empty_resource"),
  501. {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
  502. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
  503. ok.
  504. rest_patch(Config) ->
  505. Tests = [
  506. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  507. {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  508. {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>},
  509. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  510. ],
  511. ConnPid = gun_open(Config),
  512. _ = [begin
  513. Ref = gun:patch(ConnPid, "/patch", Headers, Body),
  514. {response, fin, Status, _} = gun:await(ConnPid, Ref)
  515. end || {Status, Headers, Body} <- Tests],
  516. ok.
  517. rest_post_charset(Config) ->
  518. ConnPid = gun_open(Config),
  519. Ref = gun:post(ConnPid, "/post_charset",
  520. [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
  521. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  522. ok.
  523. rest_postonly(Config) ->
  524. ConnPid = gun_open(Config),
  525. Ref = gun:post(ConnPid, "/postonly",
  526. [{<<"content-type">>, <<"text/plain">>}], "12345"),
  527. {response, fin, 204, _} = gun:await(ConnPid, Ref),
  528. ok.
  529. rest_resource_get_etag(Config, Type) ->
  530. rest_resource_get_etag(Config, Type, []).
  531. rest_resource_get_etag(Config, Type, Headers) ->
  532. ConnPid = gun_open(Config),
  533. Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
  534. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  535. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  536. false -> {Status, false};
  537. {<<"etag">>, ETag} -> {Status, ETag}
  538. end.
  539. rest_resource_etags(Config) ->
  540. Tests = [
  541. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  542. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  543. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  544. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  545. {500, false, "binary-strong-unquoted"},
  546. {500, false, "binary-weak-unquoted"}
  547. ],
  548. _ = [{Status, ETag, Type} = begin
  549. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  550. {Ret, RespETag, Type}
  551. end || {Status, ETag, Type} <- Tests].
  552. rest_resource_etags_if_none_match(Config) ->
  553. Tests = [
  554. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  555. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  556. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  557. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  558. ],
  559. _ = [{Status, Type} = begin
  560. {Ret, _} = rest_resource_get_etag(Config, Type,
  561. [{<<"if-none-match">>, ETag}]),
  562. {Ret, Type}
  563. end || {Status, ETag, Type} <- Tests].
  564. set_env_dispatch(Config) ->
  565. ConnPid1 = gun_open(Config),
  566. Ref1 = gun:get(ConnPid1, "/"),
  567. {response, fin, 400, _} = gun:await(ConnPid1, Ref1),
  568. ok = cowboy:set_env(set_env, dispatch,
  569. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  570. ConnPid2 = gun_open(Config),
  571. Ref2 = gun:get(ConnPid2, "/"),
  572. {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
  573. ok.
  574. path_allow_colon(_Config) ->
  575. cowboy_router:compile([{'_', [{"/foo/bar:blah", http_handler, []}]}]),
  576. ok.
  577. set_resp_body(Config) ->
  578. ConnPid = gun_open(Config),
  579. Ref = gun:get(ConnPid, "/set_resp/body"),
  580. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  581. {ok, <<"A flameless dance does not equal a cycle">>}
  582. = gun:await_body(ConnPid, Ref),
  583. ok.
  584. set_resp_header(Config) ->
  585. ConnPid = gun_open(Config),
  586. Ref = gun:get(ConnPid, "/set_resp/header"),
  587. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  588. {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  589. {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
  590. ok.
  591. set_resp_overwrite(Config) ->
  592. ConnPid = gun_open(Config),
  593. Ref = gun:get(ConnPid, "/set_resp/overwrite"),
  594. {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
  595. {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
  596. ok.
  597. slowloris(Config) ->
  598. Client = raw_open(Config),
  599. try
  600. [begin
  601. ok = raw_send(Client, [C]),
  602. receive after 250 -> ok end
  603. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  604. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  605. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  606. error(failure)
  607. catch error:{badmatch, _} ->
  608. ok
  609. end.
  610. slowloris2(Config) ->
  611. Client = raw_open(Config),
  612. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  613. receive after 300 -> ok end,
  614. ok = raw_send(Client, "Host: localhost\r\n"),
  615. receive after 300 -> ok end,
  616. Data = raw_recv_head(Client),
  617. {_, 408, _, _} = cow_http:parse_status_line(Data),
  618. ok.
  619. te_chunked(Config) ->
  620. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  621. ConnPid = gun_open(Config),
  622. Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]),
  623. gun:data(ConnPid, Ref, fin, Body),
  624. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  625. {ok, Body} = gun:await_body(ConnPid, Ref),
  626. ok.
  627. do_body_to_chunks(_, <<>>, Acc) ->
  628. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  629. do_body_to_chunks(ChunkSize, Body, Acc) ->
  630. BodySize = byte_size(Body),
  631. ChunkSize2 = case BodySize < ChunkSize of
  632. true -> BodySize;
  633. false -> ChunkSize
  634. end,
  635. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  636. ChunkSizeBin = integer_to_binary(ChunkSize2, 16),
  637. do_body_to_chunks(ChunkSize, Rest,
  638. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  639. te_chunked_chopped(Config) ->
  640. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  641. Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])),
  642. ConnPid = gun_open(Config),
  643. Ref = gun:post(ConnPid, "/echo/body",
  644. [{<<"content-type">>, <<"text/plain">>}]),
  645. _ = [begin
  646. ok = gun:dbg_send_raw(ConnPid, << C >>),
  647. receive after 10 -> ok end
  648. end || << C >> <= Body2],
  649. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  650. {ok, Body} = gun:await_body(ConnPid, Ref),
  651. ok.
  652. te_chunked_delayed(Config) ->
  653. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  654. Chunks = do_body_to_chunks(50, Body, []),
  655. ConnPid = gun_open(Config),
  656. Ref = gun:post(ConnPid, "/echo/body",
  657. [{<<"content-type">>, <<"text/plain">>}]),
  658. _ = [begin
  659. ok = gun:dbg_send_raw(ConnPid, Chunk),
  660. receive after 10 -> ok end
  661. end || Chunk <- Chunks],
  662. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  663. {ok, Body} = gun:await_body(ConnPid, Ref),
  664. ok.
  665. te_chunked_split_body(Config) ->
  666. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  667. Chunks = do_body_to_chunks(50, Body, []),
  668. ConnPid = gun_open(Config),
  669. Ref = gun:post(ConnPid, "/echo/body",
  670. [{<<"content-type">>, <<"text/plain">>}]),
  671. _ = [begin
  672. case Chunk of
  673. <<"0\r\n\r\n">> ->
  674. ok = gun:dbg_send_raw(ConnPid, Chunk);
  675. _ ->
  676. [Size, ChunkBody, <<>>] =
  677. binary:split(Chunk, [<<"\r\n">>], [global]),
  678. PartASize = random:uniform(byte_size(ChunkBody)),
  679. <<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
  680. ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
  681. receive after 10 -> ok end,
  682. ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
  683. 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_chunked_split_crlf(Config) ->
  689. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  690. Chunks = do_body_to_chunks(50, Body, []),
  691. ConnPid = gun_open(Config),
  692. Ref = gun:post(ConnPid, "/echo/body",
  693. [{<<"content-type">>, <<"text/plain">>}]),
  694. _ = [begin
  695. %% Split in the newline just before the end of the chunk.
  696. Len = byte_size(Chunk) - (random:uniform(2) - 1),
  697. << Chunk2:Len/binary, End/binary >> = Chunk,
  698. ok = gun:dbg_send_raw(ConnPid, Chunk2),
  699. receive after 10 -> ok end,
  700. ok = gun:dbg_send_raw(ConnPid, End)
  701. end || Chunk <- Chunks],
  702. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  703. {ok, Body} = gun:await_body(ConnPid, Ref),
  704. ok.
  705. te_identity(Config) ->
  706. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  707. ConnPid = gun_open(Config),
  708. Ref = gun:post(ConnPid, "/echo/body", [], Body),
  709. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  710. {ok, Body} = gun:await_body(ConnPid, Ref),
  711. ok.
  712. request_timeout_infinity(Config) ->
  713. Pid = config(pid, Config),
  714. Ref = erlang:monitor(process, Pid),
  715. _ = gun_open(Config),
  716. receive
  717. {'DOWN', Ref, process, Pid, Reason} ->
  718. error(Reason)
  719. after 1000 ->
  720. ok
  721. end.
  722. idle_timeout_infinity(Config) ->
  723. Pid = config(pid, Config),
  724. Ref = erlang:monitor(process, Pid),
  725. ConnPid = gun_open(Config),
  726. gun:post(ConnPid, "/echo/body", [], <<"TEST">>),
  727. receive
  728. {'DOWN', Ref, process, Pid, Reason} ->
  729. error(Reason)
  730. after 1000 ->
  731. ok
  732. end.