old_http_SUITE.erl 25 KB

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