old_http_SUITE.erl 20 KB

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