old_http_SUITE.erl 21 KB

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