old_http_SUITE.erl 22 KB

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