http_SUITE.erl 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. %% Copyright (c) 2011-2013, 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(http_SUITE).
  16. -include_lib("common_test/include/ct.hrl").
  17. %% ct.
  18. -export([all/0]).
  19. -export([groups/0]).
  20. -export([init_per_suite/1]).
  21. -export([end_per_suite/1]).
  22. -export([init_per_group/2]).
  23. -export([end_per_group/2]).
  24. %% Tests.
  25. -export([check_raw_status/1]).
  26. -export([check_status/1]).
  27. -export([chunked_response/1]).
  28. -export([echo_body/1]).
  29. -export([echo_body_max_length/1]).
  30. -export([echo_body_qs/1]).
  31. -export([echo_body_qs_max_length/1]).
  32. -export([error_chain_handle_after_reply/1]).
  33. -export([error_chain_handle_before_reply/1]).
  34. -export([error_handle_after_reply/1]).
  35. -export([error_init_after_reply/1]).
  36. -export([error_init_reply_handle_error/1]).
  37. -export([headers_dupe/1]).
  38. -export([http10_chunkless/1]).
  39. -export([http10_hostless/1]).
  40. -export([keepalive_max/1]).
  41. -export([keepalive_nl/1]).
  42. -export([multipart/1]).
  43. -export([nc_rand/1]).
  44. -export([nc_zero/1]).
  45. -export([onrequest/1]).
  46. -export([onrequest_reply/1]).
  47. -export([onresponse_capitalize/1]).
  48. -export([onresponse_crash/1]).
  49. -export([onresponse_reply/1]).
  50. -export([pipeline/1]).
  51. -export([pipeline_long_polling/1]).
  52. -export([rest_bad_accept/1]).
  53. -export([rest_bad_content_type/1]).
  54. -export([rest_expires/1]).
  55. -export([rest_keepalive/1]).
  56. -export([rest_keepalive_post/1]).
  57. -export([rest_missing_get_callbacks/1]).
  58. -export([rest_missing_put_callbacks/1]).
  59. -export([rest_nodelete/1]).
  60. -export([rest_options_default/1]).
  61. -export([rest_param_all/1]).
  62. -export([rest_patch/1]).
  63. -export([rest_postonly/1]).
  64. -export([rest_resource_etags/1]).
  65. -export([rest_resource_etags_if_none_match/1]).
  66. -export([set_env_dispatch/1]).
  67. -export([set_resp_body/1]).
  68. -export([set_resp_header/1]).
  69. -export([set_resp_overwrite/1]).
  70. -export([slowloris/1]).
  71. -export([slowloris2/1]).
  72. -export([static_attribute_etag/1]).
  73. -export([static_function_etag/1]).
  74. -export([static_mimetypes_function/1]).
  75. -export([static_specify_file/1]).
  76. -export([static_specify_file_catchall/1]).
  77. -export([static_test_file/1]).
  78. -export([static_test_file_css/1]).
  79. -export([stream_body_set_resp/1]).
  80. -export([stream_body_set_resp_close/1]).
  81. -export([stream_body_set_resp_chunked/1]).
  82. -export([stream_body_set_resp_chunked10/1]).
  83. -export([te_chunked/1]).
  84. -export([te_chunked_chopped/1]).
  85. -export([te_chunked_delayed/1]).
  86. -export([te_identity/1]).
  87. %% ct.
  88. all() ->
  89. [
  90. {group, http},
  91. {group, https},
  92. {group, http_compress},
  93. {group, https_compress},
  94. {group, onrequest},
  95. {group, onresponse},
  96. {group, onresponse_capitalize},
  97. {group, set_env}
  98. ].
  99. groups() ->
  100. Tests = [
  101. check_raw_status,
  102. check_status,
  103. chunked_response,
  104. echo_body,
  105. echo_body_max_length,
  106. echo_body_qs,
  107. echo_body_qs_max_length,
  108. error_chain_handle_after_reply,
  109. error_chain_handle_before_reply,
  110. error_handle_after_reply,
  111. error_init_after_reply,
  112. error_init_reply_handle_error,
  113. headers_dupe,
  114. http10_chunkless,
  115. http10_hostless,
  116. keepalive_max,
  117. keepalive_nl,
  118. multipart,
  119. nc_rand,
  120. nc_zero,
  121. pipeline,
  122. pipeline_long_polling,
  123. rest_bad_accept,
  124. rest_bad_content_type,
  125. rest_expires,
  126. rest_keepalive,
  127. rest_keepalive_post,
  128. rest_missing_get_callbacks,
  129. rest_missing_put_callbacks,
  130. rest_nodelete,
  131. rest_options_default,
  132. rest_param_all,
  133. rest_patch,
  134. rest_postonly,
  135. rest_resource_etags,
  136. rest_resource_etags_if_none_match,
  137. set_resp_body,
  138. set_resp_header,
  139. set_resp_overwrite,
  140. slowloris,
  141. slowloris2,
  142. static_attribute_etag,
  143. static_function_etag,
  144. static_mimetypes_function,
  145. static_specify_file,
  146. static_specify_file_catchall,
  147. static_test_file,
  148. static_test_file_css,
  149. stream_body_set_resp,
  150. stream_body_set_resp_close,
  151. stream_body_set_resp_chunked,
  152. stream_body_set_resp_chunked10,
  153. te_chunked,
  154. te_chunked_chopped,
  155. te_chunked_delayed,
  156. te_identity
  157. ],
  158. [
  159. {http, [parallel], Tests},
  160. {https, [parallel], Tests},
  161. {http_compress, [parallel], Tests},
  162. {https_compress, [parallel], Tests},
  163. {onrequest, [parallel], [
  164. onrequest,
  165. onrequest_reply
  166. ]},
  167. {onresponse, [parallel], [
  168. onresponse_crash,
  169. onresponse_reply
  170. ]},
  171. {onresponse_capitalize, [parallel], [
  172. onresponse_capitalize
  173. ]},
  174. {set_env, [], [
  175. set_env_dispatch
  176. ]}
  177. ].
  178. init_per_suite(Config) ->
  179. application:start(crypto),
  180. application:start(ranch),
  181. application:start(cowboy),
  182. Config.
  183. end_per_suite(_Config) ->
  184. application:stop(cowboy),
  185. application:stop(ranch),
  186. application:stop(crypto),
  187. ok.
  188. init_per_group(http, Config) ->
  189. Transport = ranch_tcp,
  190. Config1 = init_static_dir(Config),
  191. {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
  192. {env, [{dispatch, init_dispatch(Config1)}]},
  193. {max_keepalive, 50},
  194. {timeout, 500}
  195. ]),
  196. Port = ranch:get_port(http),
  197. {ok, Client} = cowboy_client:init([]),
  198. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  199. {transport, Transport}, {client, Client}|Config1];
  200. init_per_group(https, Config) ->
  201. Transport = ranch_ssl,
  202. {_, Cert, Key} = ct_helper:make_certs(),
  203. Opts = [{cert, Cert}, {key, Key}],
  204. Config1 = init_static_dir(Config),
  205. application:start(public_key),
  206. application:start(ssl),
  207. {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, 0}], [
  208. {env, [{dispatch, init_dispatch(Config1)}]},
  209. {max_keepalive, 50},
  210. {timeout, 500}
  211. ]),
  212. Port = ranch:get_port(https),
  213. {ok, Client} = cowboy_client:init(Opts),
  214. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  215. {transport, Transport}, {client, Client}|Config1];
  216. init_per_group(http_compress, Config) ->
  217. Transport = ranch_tcp,
  218. Config1 = init_static_dir(Config),
  219. {ok, _} = cowboy:start_http(http_compress, 100, [{port, 0}], [
  220. {compress, true},
  221. {env, [{dispatch, init_dispatch(Config1)}]},
  222. {max_keepalive, 50},
  223. {timeout, 500}
  224. ]),
  225. Port = ranch:get_port(http_compress),
  226. {ok, Client} = cowboy_client:init([]),
  227. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  228. {transport, Transport}, {client, Client}|Config1];
  229. init_per_group(https_compress, Config) ->
  230. Transport = ranch_ssl,
  231. {_, Cert, Key} = ct_helper:make_certs(),
  232. Opts = [{cert, Cert}, {key, Key}],
  233. Config1 = init_static_dir(Config),
  234. application:start(public_key),
  235. application:start(ssl),
  236. {ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, 0}], [
  237. {compress, true},
  238. {env, [{dispatch, init_dispatch(Config1)}]},
  239. {max_keepalive, 50},
  240. {timeout, 500}
  241. ]),
  242. Port = ranch:get_port(https_compress),
  243. {ok, Client} = cowboy_client:init(Opts),
  244. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  245. {transport, Transport}, {client, Client}|Config1];
  246. init_per_group(onrequest, Config) ->
  247. Transport = ranch_tcp,
  248. {ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
  249. {env, [{dispatch, init_dispatch(Config)}]},
  250. {max_keepalive, 50},
  251. {onrequest, fun onrequest_hook/1},
  252. {timeout, 500}
  253. ]),
  254. Port = ranch:get_port(onrequest),
  255. {ok, Client} = cowboy_client:init([]),
  256. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  257. {transport, Transport}, {client, Client}|Config];
  258. init_per_group(onresponse, Config) ->
  259. Transport = ranch_tcp,
  260. {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
  261. {env, [{dispatch, init_dispatch(Config)}]},
  262. {max_keepalive, 50},
  263. {onresponse, fun onresponse_hook/4},
  264. {timeout, 500}
  265. ]),
  266. Port = ranch:get_port(onresponse),
  267. {ok, Client} = cowboy_client:init([]),
  268. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  269. {transport, Transport}, {client, Client}|Config];
  270. init_per_group(onresponse_capitalize, Config) ->
  271. Transport = ranch_tcp,
  272. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
  273. {env, [{dispatch, init_dispatch(Config)}]},
  274. {max_keepalive, 50},
  275. {onresponse, fun onresponse_capitalize_hook/4},
  276. {timeout, 500}
  277. ]),
  278. Port = ranch:get_port(onresponse_capitalize),
  279. {ok, Client} = cowboy_client:init([]),
  280. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  281. {transport, Transport}, {client, Client}|Config];
  282. init_per_group(set_env, Config) ->
  283. Transport = ranch_tcp,
  284. {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
  285. {env, [{dispatch, []}]},
  286. {max_keepalive, 50},
  287. {timeout, 500}
  288. ]),
  289. Port = ranch:get_port(set_env),
  290. {ok, Client} = cowboy_client:init([]),
  291. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  292. {transport, Transport}, {client, Client}|Config].
  293. end_per_group(Group, Config) when Group =:= https; Group =:= https_compress ->
  294. cowboy:stop_listener(https),
  295. application:stop(ssl),
  296. application:stop(public_key),
  297. end_static_dir(Config),
  298. ok;
  299. end_per_group(Group, Config) when Group =:= http; Group =:= http_compress ->
  300. cowboy:stop_listener(http),
  301. end_static_dir(Config);
  302. end_per_group(Name, _) ->
  303. cowboy:stop_listener(Name),
  304. ok.
  305. %% Dispatch configuration.
  306. init_dispatch(Config) ->
  307. cowboy_router:compile([
  308. {"localhost", [
  309. {"/chunked_response", http_chunked, []},
  310. {"/init_shutdown", http_init_shutdown, []},
  311. {"/long_polling", http_long_polling, []},
  312. {"/headers/dupe", http_handler,
  313. [{headers, [{<<"connection">>, <<"close">>}]}]},
  314. {"/set_resp/header", http_set_resp,
  315. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  316. {"/set_resp/overwrite", http_set_resp,
  317. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  318. {"/set_resp/body", http_set_resp,
  319. [{body, <<"A flameless dance does not equal a cycle">>}]},
  320. {"/stream_body/set_resp", http_stream_body,
  321. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  322. {"/stream_body/set_resp_close",
  323. http_stream_body, [
  324. {reply, set_resp_close},
  325. {body, <<"stream_body_set_resp_close">>}]},
  326. {"/stream_body/set_resp_chunked",
  327. http_stream_body, [
  328. {reply, set_resp_chunked},
  329. {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
  330. {"/static/[...]", cowboy_static,
  331. [{directory, ?config(static_dir, Config)},
  332. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  333. {"/static_mimetypes_function/[...]", cowboy_static,
  334. [{directory, ?config(static_dir, Config)},
  335. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  336. [<<"text/html">>] end, data}}]},
  337. {"/handler_errors", http_errors, []},
  338. {"/static_attribute_etag/[...]", cowboy_static,
  339. [{directory, ?config(static_dir, Config)},
  340. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  341. {"/static_function_etag/[...]", cowboy_static,
  342. [{directory, ?config(static_dir, Config)},
  343. {etag, {fun static_function_etag/2, etag_data}}]},
  344. {"/static_specify_file/[...]", cowboy_static,
  345. [{directory, ?config(static_dir, Config)},
  346. {mimetypes, [{<<".css">>, [<<"text/css">>]}]},
  347. {file, <<"test_file.css">>}]},
  348. {"/multipart", http_multipart, []},
  349. {"/echo/body", http_echo_body, []},
  350. {"/echo/body_qs", http_body_qs, []},
  351. {"/param_all", rest_param_all, []},
  352. {"/bad_accept", rest_simple_resource, []},
  353. {"/bad_content_type", rest_patch_resource, []},
  354. {"/simple", rest_simple_resource, []},
  355. {"/forbidden_post", rest_forbidden_resource, [true]},
  356. {"/simple_post", rest_forbidden_resource, [false]},
  357. {"/missing_get_callbacks", rest_missing_callbacks, []},
  358. {"/missing_put_callbacks", rest_missing_callbacks, []},
  359. {"/nodelete", rest_nodelete_resource, []},
  360. {"/postonly", rest_postonly_resource, []},
  361. {"/patch", rest_patch_resource, []},
  362. {"/resetags", rest_resource_etags, []},
  363. {"/rest_expires", rest_expires, []},
  364. {"/rest_empty_resource", rest_empty_resource, []},
  365. {"/loop_recv", http_loop_recv, []},
  366. {"/loop_timeout", http_loop_timeout, []},
  367. {"/", http_handler, []}
  368. ]}
  369. ]).
  370. init_static_dir(Config) ->
  371. Dir = filename:join(?config(priv_dir, Config), "static"),
  372. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  373. ok = file:make_dir(Dir),
  374. ok = file:write_file(Level1("test_file"), "test_file\n"),
  375. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  376. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  377. ok = file:change_mode(Level1("test_noread"), 8#0333),
  378. ok = file:write_file(Level1("test.html"), "test.html\n"),
  379. ok = file:make_dir(Level1("test_dir")),
  380. [{static_dir, Dir}|Config].
  381. end_static_dir(Config) ->
  382. Dir = ?config(static_dir, Config),
  383. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  384. ok = file:delete(Level1("test_file")),
  385. ok = file:delete(Level1("test_file.css")),
  386. ok = file:delete(Level1("test_noread")),
  387. ok = file:delete(Level1("test.html")),
  388. ok = file:del_dir(Level1("test_dir")),
  389. ok = file:del_dir(Dir),
  390. Config.
  391. %% Convenience functions.
  392. quick_raw(Data, Config) ->
  393. Client = ?config(client, Config),
  394. Transport = ?config(transport, Config),
  395. {ok, Client2} = cowboy_client:connect(
  396. Transport, "localhost", ?config(port, Config), Client),
  397. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  398. case cowboy_client:response(Client3) of
  399. {ok, Status, _, _} -> Status;
  400. {error, _} -> closed
  401. end.
  402. build_url(Path, Config) ->
  403. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  404. {port, Port} = lists:keyfind(port, 1, Config),
  405. PortBin = list_to_binary(integer_to_list(Port)),
  406. PathBin = list_to_binary(Path),
  407. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  408. quick_get(URL, Config) ->
  409. Client = ?config(client, Config),
  410. {ok, Client2} = cowboy_client:request(<<"GET">>,
  411. build_url(URL, Config), Client),
  412. {ok, Status, _, _} = cowboy_client:response(Client2),
  413. Status.
  414. body_to_chunks(_, <<>>, Acc) ->
  415. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  416. body_to_chunks(ChunkSize, Body, Acc) ->
  417. BodySize = byte_size(Body),
  418. ChunkSize2 = case BodySize < ChunkSize of
  419. true -> BodySize;
  420. false -> ChunkSize
  421. end,
  422. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  423. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  424. body_to_chunks(ChunkSize, Rest,
  425. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  426. %% Tests.
  427. check_raw_status(Config) ->
  428. Huge = [$0 || _ <- lists:seq(1, 5000)],
  429. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  430. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  431. ResponsePacket =
  432. "HTTP/1.0 302 Found\r
  433. Location: http://www.google.co.il/\r
  434. Cache-Control: private\r
  435. Content-Type: text/html; charset=UTF-8\r
  436. 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
  437. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  438. Server: gws\r
  439. Content-Length: 221\r
  440. X-XSS-Protection: 1; mode=block\r
  441. X-Frame-Options: SAMEORIGIN\r
  442. \r
  443. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  444. <TITLE>302 Moved</TITLE></HEAD><BODY>
  445. <H1>302 Moved</H1>
  446. The document has moved
  447. <A HREF=\"http://www.google.co.il/\">here</A>.
  448. </BODY></HTML>",
  449. Tests = [
  450. {102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  451. "Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>},
  452. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  453. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  454. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  455. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  456. {200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n"
  457. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  458. {400, "\n"},
  459. {400, "Garbage\r\n\r\n"},
  460. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  461. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  462. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  463. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  464. {505, ResponsePacket},
  465. {408, "GET / HTTP/1.1\r\n"},
  466. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  467. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  468. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  469. {414, Huge},
  470. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  471. {500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
  472. "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
  473. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  474. {closed, ""},
  475. {closed, "\r\n"},
  476. {closed, "\r\n\r\n"},
  477. {closed, "GET / HTTP/1.1"}
  478. ],
  479. _ = [{Status, Packet} = begin
  480. Ret = quick_raw(Packet, Config),
  481. {Ret, Packet}
  482. end || {Status, Packet} <- Tests],
  483. ok.
  484. check_status(Config) ->
  485. Tests = [
  486. {102, "/long_polling"},
  487. {200, "/"},
  488. {200, "/simple"},
  489. {204, "/loop_timeout"},
  490. {400, "/static/%2f"},
  491. {400, "/static/%2e"},
  492. {400, "/static/%2e%2e"},
  493. {403, "/static/test_dir"},
  494. {403, "/static/test_dir/"},
  495. {403, "/static/test_noread"},
  496. {404, "/not/found"},
  497. {404, "/static/not_found"},
  498. {500, "/handler_errors?case=handler_before_reply"},
  499. {500, "/handler_errors?case=init_before_reply"},
  500. {666, "/init_shutdown"}
  501. ],
  502. _ = [{Status, URL} = begin
  503. Ret = quick_get(URL, Config),
  504. {Ret, URL}
  505. end || {Status, URL} <- Tests].
  506. chunked_response(Config) ->
  507. Client = ?config(client, Config),
  508. {ok, Client2} = cowboy_client:request(<<"GET">>,
  509. build_url("/chunked_response", Config), Client),
  510. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  511. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  512. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  513. {ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>}
  514. = Transport:recv(Socket, 44, 1000),
  515. {error, closed} = cowboy_client:response(Client3).
  516. %% Check if sending requests whose size is around the MTU breaks something.
  517. echo_body(Config) ->
  518. Client = ?config(client, Config),
  519. {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
  520. _ = [begin
  521. Body = list_to_binary(lists:duplicate(Size, $a)),
  522. {ok, Client2} = cowboy_client:request(<<"POST">>,
  523. build_url("/echo/body", Config),
  524. [{<<"connection">>, <<"close">>}],
  525. Body, Client),
  526. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  527. {ok, Body, _} = cowboy_client:response_body(Client3)
  528. end || Size <- lists:seq(MTU - 500, MTU)],
  529. ok.
  530. %% Check if sending request whose size is bigger than 1000000 bytes causes 413
  531. echo_body_max_length(Config) ->
  532. Client = ?config(client, Config),
  533. Body = <<$a:8000008>>,
  534. {ok, Client2} = cowboy_client:request(<<"POST">>,
  535. build_url("/echo/body", Config),
  536. [{<<"connection">>, <<"close">>}],
  537. Body, Client),
  538. {ok, 413, _, _} = cowboy_client:response(Client2).
  539. % check if body_qs echo's back results
  540. echo_body_qs(Config) ->
  541. Client = ?config(client, Config),
  542. Body = <<"echo=67890">>,
  543. {ok, Client2} = cowboy_client:request(<<"POST">>,
  544. build_url("/echo/body_qs", Config),
  545. [{<<"connection">>, <<"close">>}],
  546. Body, Client),
  547. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  548. {ok, <<"67890">>, _} = cowboy_client:response_body(Client3).
  549. %% Check if sending request whose size is bigger 16000 bytes causes 413
  550. echo_body_qs_max_length(Config) ->
  551. Client = ?config(client, Config),
  552. DefaultMaxBodyQsLength = 16000,
  553. % subtract "echo=" minus 1 byte from max to hit the limit
  554. Bits = (DefaultMaxBodyQsLength - 4) * 8,
  555. AppendedBody = <<$a:Bits>>,
  556. Body = <<"echo=", AppendedBody/binary>>,
  557. {ok, Client2} = cowboy_client:request(<<"POST">>,
  558. build_url("/echo/body_qs", Config),
  559. [{<<"connection">>, <<"close">>}],
  560. Body, Client),
  561. {ok, 413, _, _} = cowboy_client:response(Client2).
  562. error_chain_handle_after_reply(Config) ->
  563. Client = ?config(client, Config),
  564. {ok, Client2} = cowboy_client:request(<<"GET">>,
  565. build_url("/", Config), Client),
  566. {ok, Client3} = cowboy_client:request(<<"GET">>,
  567. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  568. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  569. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  570. {error, closed} = cowboy_client:response(Client5).
  571. error_chain_handle_before_reply(Config) ->
  572. Client = ?config(client, Config),
  573. {ok, Client2} = cowboy_client:request(<<"GET">>,
  574. build_url("/", Config), Client),
  575. {ok, Client3} = cowboy_client:request(<<"GET">>,
  576. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  577. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  578. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  579. {error, closed} = cowboy_client:response(Client5).
  580. error_handle_after_reply(Config) ->
  581. Client = ?config(client, Config),
  582. {ok, Client2} = cowboy_client:request(<<"GET">>,
  583. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  584. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  585. {error, closed} = cowboy_client:response(Client3).
  586. error_init_after_reply(Config) ->
  587. Client = ?config(client, Config),
  588. {ok, Client2} = cowboy_client:request(<<"GET">>,
  589. build_url("/handler_errors?case=init_after_reply", Config), Client),
  590. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  591. {error, closed} = cowboy_client:response(Client3).
  592. error_init_reply_handle_error(Config) ->
  593. Client = ?config(client, Config),
  594. {ok, Client2} = cowboy_client:request(<<"GET">>,
  595. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  596. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  597. {error, closed} = cowboy_client:response(Client3).
  598. headers_dupe(Config) ->
  599. Client = ?config(client, Config),
  600. {ok, Client2} = cowboy_client:request(<<"GET">>,
  601. build_url("/headers/dupe", Config), Client),
  602. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  603. {<<"connection">>, <<"close">>}
  604. = lists:keyfind(<<"connection">>, 1, Headers),
  605. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  606. 1 = length(Connections),
  607. {error, closed} = cowboy_client:response(Client3).
  608. http10_chunkless(Config) ->
  609. Client = ?config(client, Config),
  610. Transport = ?config(transport, Config),
  611. {ok, Client2} = cowboy_client:connect(
  612. Transport, "localhost", ?config(port, Config), Client),
  613. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  614. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  615. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  616. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  617. %% Hack: we just try to get 28 bytes and compare.
  618. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  619. Buffer = element(7, Client4),
  620. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  621. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  622. _ -> Buffer
  623. end,
  624. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  625. http10_hostless(Config) ->
  626. Port10 = ?config(port, Config) + 10,
  627. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  628. ranch:start_listener(Name, 5,
  629. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  630. cowboy_protocol, [
  631. {env, [{dispatch, cowboy_router:compile([
  632. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  633. {max_keepalive, 50},
  634. {timeout, 500}]
  635. ),
  636. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  637. [{port, Port10}|Config]),
  638. cowboy:stop_listener(http10).
  639. keepalive_max(Config) ->
  640. Client = ?config(client, Config),
  641. URL = build_url("/", Config),
  642. ok = keepalive_max_loop(Client, URL, 50).
  643. keepalive_max_loop(Client, _, 0) ->
  644. {error, closed} = cowboy_client:response(Client),
  645. ok;
  646. keepalive_max_loop(Client, URL, N) ->
  647. Headers = [{<<"connection">>, <<"keep-alive">>}],
  648. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  649. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  650. Expected = case N of
  651. 1 -> <<"close">>;
  652. N -> <<"keep-alive">>
  653. end,
  654. {<<"connection">>, Expected}
  655. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  656. keepalive_max_loop(Client3, URL, N - 1).
  657. keepalive_nl(Config) ->
  658. Client = ?config(client, Config),
  659. URL = build_url("/", Config),
  660. ok = keepalive_nl_loop(Client, URL, 10).
  661. keepalive_nl_loop(Client, _, 0) ->
  662. {error, closed} = cowboy_client:response(Client),
  663. ok;
  664. keepalive_nl_loop(Client, URL, N) ->
  665. Headers = [{<<"connection">>, <<"keep-alive">>}],
  666. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  667. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  668. {<<"connection">>, <<"keep-alive">>}
  669. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  670. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  671. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  672. keepalive_nl_loop(Client3, URL, N - 1).
  673. multipart(Config) ->
  674. Client = ?config(client, Config),
  675. Body = <<
  676. "This is a preamble."
  677. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  678. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  679. "\r\n--OHai--"
  680. "This is an epiloque."
  681. >>,
  682. {ok, Client2} = cowboy_client:request(<<"POST">>,
  683. build_url("/multipart", Config),
  684. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  685. Body, Client),
  686. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  687. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  688. Parts = binary_to_term(RespBody),
  689. Parts = [
  690. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  691. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  692. ].
  693. nc_reqs(Config, Input) ->
  694. Cat = os:find_executable("cat"),
  695. Nc = os:find_executable("nc"),
  696. case {Cat, Nc} of
  697. {false, _} ->
  698. {skip, {notfound, cat}};
  699. {_, false} ->
  700. {skip, {notfound, nc}};
  701. _Good ->
  702. %% Throw garbage at the server then check if it's still up.
  703. {port, Port} = lists:keyfind(port, 1, Config),
  704. StrPort = integer_to_list(Port),
  705. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  706. || _ <- lists:seq(1, 100)],
  707. 200 = quick_get("/", Config)
  708. end.
  709. nc_rand(Config) ->
  710. nc_reqs(Config, "/dev/urandom").
  711. nc_zero(Config) ->
  712. nc_reqs(Config, "/dev/zero").
  713. onrequest(Config) ->
  714. Client = ?config(client, Config),
  715. {ok, Client2} = cowboy_client:request(<<"GET">>,
  716. build_url("/", Config), Client),
  717. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  718. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  719. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  720. onrequest_reply(Config) ->
  721. Client = ?config(client, Config),
  722. {ok, Client2} = cowboy_client:request(<<"GET">>,
  723. build_url("/?reply=1", Config), Client),
  724. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  725. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  726. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  727. %% Hook for the above onrequest tests.
  728. onrequest_hook(Req) ->
  729. case cowboy_req:qs_val(<<"reply">>, Req) of
  730. {undefined, Req2} ->
  731. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  732. {_, Req2} ->
  733. {ok, Req3} = cowboy_req:reply(
  734. 200, [], <<"replied!">>, Req2),
  735. Req3
  736. end.
  737. onresponse_capitalize(Config) ->
  738. Client = ?config(client, Config),
  739. {ok, Client2} = cowboy_client:request(<<"GET">>,
  740. build_url("/", Config), Client),
  741. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  742. {ok, Data} = Transport:recv(Socket, 0, 1000),
  743. false = nomatch =:= binary:match(Data, <<"Content-Length">>).
  744. %% Hook for the above onresponse_capitalize test.
  745. onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  746. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  747. || {N, V} <- Headers],
  748. {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
  749. Req2.
  750. onresponse_crash(Config) ->
  751. Client = ?config(client, Config),
  752. {ok, Client2} = cowboy_client:request(<<"GET">>,
  753. build_url("/handler_errors?case=init_before_reply", Config), Client),
  754. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  755. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  756. onresponse_reply(Config) ->
  757. Client = ?config(client, Config),
  758. {ok, Client2} = cowboy_client:request(<<"GET">>,
  759. build_url("/", Config), Client),
  760. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  761. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  762. %% Make sure we don't get the body initially sent.
  763. {error, closed} = cowboy_client:response_body(Client3).
  764. %% Hook for the above onresponse tests.
  765. onresponse_hook(_, Headers, _, Req) ->
  766. {ok, Req2} = cowboy_req:reply(
  767. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  768. Req2.
  769. pipeline(Config) ->
  770. Client = ?config(client, Config),
  771. {ok, Client2} = cowboy_client:request(<<"GET">>,
  772. build_url("/", Config), Client),
  773. {ok, Client3} = cowboy_client:request(<<"GET">>,
  774. build_url("/", Config), Client2),
  775. {ok, Client4} = cowboy_client:request(<<"GET">>,
  776. build_url("/", Config), Client3),
  777. {ok, Client5} = cowboy_client:request(<<"GET">>,
  778. build_url("/", Config), Client4),
  779. {ok, Client6} = cowboy_client:request(<<"GET">>,
  780. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  781. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  782. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  783. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  784. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  785. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  786. {error, closed} = cowboy_client:response(Client11).
  787. pipeline_long_polling(Config) ->
  788. Client = ?config(client, Config),
  789. {ok, Client2} = cowboy_client:request(<<"GET">>,
  790. build_url("/long_polling", Config), Client),
  791. {ok, Client3} = cowboy_client:request(<<"GET">>,
  792. build_url("/long_polling", Config), Client2),
  793. {ok, 102, _, Client4} = cowboy_client:response(Client3),
  794. {ok, 102, _, Client5} = cowboy_client:response(Client4),
  795. {error, closed} = cowboy_client:response(Client5).
  796. rest_param_all(Config) ->
  797. Client = ?config(client, Config),
  798. URL = build_url("/param_all", Config),
  799. % Accept without param
  800. {ok, Client2} = cowboy_client:request(<<"GET">>, URL,
  801. [{<<"accept">>, <<"text/plain">>}], Client),
  802. Client3 = check_response(Client2, <<"[]">>),
  803. % Accept with param
  804. {ok, Client4} = cowboy_client:request(<<"GET">>, URL,
  805. [{<<"accept">>, <<"text/plain;level=1">>}], Client3),
  806. Client5 = check_response(Client4, <<"level=1">>),
  807. % Accept with param and quality
  808. {ok, Client6} = cowboy_client:request(<<"GET">>, URL,
  809. [{<<"accept">>,
  810. <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}],
  811. Client5),
  812. Client7 = check_response(Client6, <<"level=1">>),
  813. {ok, Client8} = cowboy_client:request(<<"GET">>, URL,
  814. [{<<"accept">>,
  815. <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}],
  816. Client7),
  817. Client9 = check_response(Client8, <<"level=2">>),
  818. % Without Accept
  819. {ok, Client10} = cowboy_client:request(<<"GET">>, URL, [], Client9),
  820. Client11 = check_response(Client10, <<"'*'">>),
  821. % Content-Type without param
  822. {ok, Client12} = cowboy_client:request(<<"PUT">>, URL,
  823. [{<<"content-type">>, <<"text/plain">>}], Client11),
  824. {ok, 204, _, Client13} = cowboy_client:response(Client12),
  825. % Content-Type with param
  826. {ok, Client14} = cowboy_client:request(<<"PUT">>, URL,
  827. [{<<"content-type">>, <<"text/plain; charset=utf-8">>}], Client13),
  828. {ok, 204, _, _} = cowboy_client:response(Client14).
  829. check_response(Client, Body) ->
  830. {ok, 200, _, Client2} = cowboy_client:response(Client),
  831. {ok, Body, Client3} = cowboy_client:response_body(Client2),
  832. Client3.
  833. rest_bad_accept(Config) ->
  834. Client = ?config(client, Config),
  835. {ok, Client2} = cowboy_client:request(<<"GET">>,
  836. build_url("/bad_accept", Config),
  837. [{<<"accept">>, <<"1">>}],
  838. Client),
  839. {ok, 400, _, _} = cowboy_client:response(Client2).
  840. rest_bad_content_type(Config) ->
  841. Client = ?config(client, Config),
  842. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  843. build_url("/bad_content_type", Config),
  844. [{<<"content-type">>, <<"text/plain, text/html">>}],
  845. <<"Whatever">>, Client),
  846. {ok, 415, _, _} = cowboy_client:response(Client2).
  847. rest_expires(Config) ->
  848. Client = ?config(client, Config),
  849. {ok, Client2} = cowboy_client:request(<<"GET">>,
  850. build_url("/rest_expires", Config), Client),
  851. {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
  852. {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
  853. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
  854. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  855. ok.
  856. rest_keepalive(Config) ->
  857. Client = ?config(client, Config),
  858. URL = build_url("/simple", Config),
  859. ok = rest_keepalive_loop(Client, URL, 10).
  860. rest_keepalive_loop(_, _, 0) ->
  861. ok;
  862. rest_keepalive_loop(Client, URL, N) ->
  863. Headers = [{<<"connection">>, <<"keep-alive">>}],
  864. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  865. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  866. {<<"connection">>, <<"keep-alive">>}
  867. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  868. rest_keepalive_loop(Client3, URL, N - 1).
  869. rest_keepalive_post(Config) ->
  870. Client = ?config(client, Config),
  871. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  872. rest_keepalive_post_loop(_, _, _, 0) ->
  873. ok;
  874. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  875. Headers = [
  876. {<<"connection">>, <<"keep-alive">>},
  877. {<<"content-type">>, <<"text/plain">>}
  878. ],
  879. {ok, Client2} = cowboy_client:request(<<"POST">>,
  880. build_url("/simple_post", Config), Headers, "12345", Client),
  881. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  882. {<<"connection">>, <<"keep-alive">>}
  883. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  884. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  885. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  886. Headers = [
  887. {<<"connection">>, <<"keep-alive">>},
  888. {<<"content-type">>, <<"text/plain">>}
  889. ],
  890. {ok, Client2} = cowboy_client:request(<<"POST">>,
  891. build_url("/forbidden_post", Config), Headers, "12345", Client),
  892. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  893. {<<"connection">>, <<"keep-alive">>}
  894. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  895. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  896. rest_missing_get_callbacks(Config) ->
  897. Client = ?config(client, Config),
  898. {ok, Client2} = cowboy_client:request(<<"GET">>,
  899. build_url("/missing_get_callbacks", Config), Client),
  900. {ok, 500, _, _} = cowboy_client:response(Client2).
  901. rest_missing_put_callbacks(Config) ->
  902. Client = ?config(client, Config),
  903. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  904. build_url("/missing_put_callbacks", Config),
  905. [{<<"content-type">>, <<"application/json">>}],
  906. <<"{}">>, Client),
  907. {ok, 500, _, _} = cowboy_client:response(Client2).
  908. rest_nodelete(Config) ->
  909. Client = ?config(client, Config),
  910. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  911. build_url("/nodelete", Config), Client),
  912. {ok, 500, _, _} = cowboy_client:response(Client2).
  913. rest_options_default(Config) ->
  914. Client = ?config(client, Config),
  915. {ok, Client2} = cowboy_client:request(<<"OPTIONS">>,
  916. build_url("/rest_empty_resource", Config), Client),
  917. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  918. {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers).
  919. rest_patch(Config) ->
  920. Tests = [
  921. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  922. {422, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  923. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  924. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  925. ],
  926. Client = ?config(client, Config),
  927. _ = [begin
  928. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  929. build_url("/patch", Config), Headers, Body, Client),
  930. {ok, Status, _, _} = cowboy_client:response(Client2),
  931. ok
  932. end || {Status, Headers, Body} <- Tests].
  933. rest_postonly(Config) ->
  934. Client = ?config(client, Config),
  935. Headers = [
  936. {<<"content-type">>, <<"text/plain">>}
  937. ],
  938. {ok, Client2} = cowboy_client:request(<<"POST">>,
  939. build_url("/postonly", Config), Headers, "12345", Client),
  940. {ok, 204, _, _} = cowboy_client:response(Client2).
  941. rest_resource_get_etag(Config, Type) ->
  942. rest_resource_get_etag(Config, Type, []).
  943. rest_resource_get_etag(Config, Type, Headers) ->
  944. Client = ?config(client, Config),
  945. {ok, Client2} = cowboy_client:request(<<"GET">>,
  946. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  947. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  948. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  949. false -> {Status, false};
  950. {<<"etag">>, ETag} -> {Status, ETag}
  951. end.
  952. rest_resource_etags(Config) ->
  953. Tests = [
  954. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  955. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  956. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  957. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  958. {500, false, "binary-strong-unquoted"},
  959. {500, false, "binary-weak-unquoted"}
  960. ],
  961. _ = [{Status, ETag, Type} = begin
  962. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  963. {Ret, RespETag, Type}
  964. end || {Status, ETag, Type} <- Tests].
  965. rest_resource_etags_if_none_match(Config) ->
  966. Tests = [
  967. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  968. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  969. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  970. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  971. ],
  972. _ = [{Status, Type} = begin
  973. {Ret, _} = rest_resource_get_etag(Config, Type,
  974. [{<<"if-none-match">>, ETag}]),
  975. {Ret, Type}
  976. end || {Status, ETag, Type} <- Tests].
  977. set_env_dispatch(Config) ->
  978. Client = ?config(client, Config),
  979. {ok, Client2} = cowboy_client:request(<<"GET">>,
  980. build_url("/", Config), Client),
  981. {ok, 400, _, _} = cowboy_client:response(Client2),
  982. ok = cowboy:set_env(set_env, dispatch,
  983. cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
  984. {ok, Client3} = cowboy_client:request(<<"GET">>,
  985. build_url("/", Config), Client),
  986. {ok, 200, _, _} = cowboy_client:response(Client3).
  987. set_resp_body(Config) ->
  988. Client = ?config(client, Config),
  989. {ok, Client2} = cowboy_client:request(<<"GET">>,
  990. build_url("/set_resp/body", Config), Client),
  991. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  992. {ok, <<"A flameless dance does not equal a cycle">>, _}
  993. = cowboy_client:response_body(Client3).
  994. set_resp_header(Config) ->
  995. Client = ?config(client, Config),
  996. {ok, Client2} = cowboy_client:request(<<"GET">>,
  997. build_url("/set_resp/header", Config), Client),
  998. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  999. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  1000. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  1001. set_resp_overwrite(Config) ->
  1002. Client = ?config(client, Config),
  1003. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1004. build_url("/set_resp/overwrite", Config), Client),
  1005. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1006. {<<"server">>, <<"DesireDrive/1.0">>}
  1007. = lists:keyfind(<<"server">>, 1, Headers).
  1008. slowloris(Config) ->
  1009. Client = ?config(client, Config),
  1010. Transport = ?config(transport, Config),
  1011. {ok, Client2} = cowboy_client:connect(
  1012. Transport, "localhost", ?config(port, Config), Client),
  1013. try
  1014. [begin
  1015. {ok, _} = cowboy_client:raw_request([C], Client2),
  1016. receive after 25 -> ok end
  1017. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  1018. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  1019. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  1020. error(failure)
  1021. catch error:{badmatch, _} ->
  1022. ok
  1023. end.
  1024. slowloris2(Config) ->
  1025. Client = ?config(client, Config),
  1026. Transport = ?config(transport, Config),
  1027. {ok, Client2} = cowboy_client:connect(
  1028. Transport, "localhost", ?config(port, Config), Client),
  1029. {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
  1030. receive after 300 -> ok end,
  1031. {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
  1032. receive after 300 -> ok end,
  1033. {ok, 408, _, _} = cowboy_client:response(Client2).
  1034. static_attribute_etag(Config) ->
  1035. Client = ?config(client, Config),
  1036. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1037. build_url("/static_attribute_etag/test.html", Config), Client),
  1038. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1039. build_url("/static_attribute_etag/test.html", Config), Client2),
  1040. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  1041. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  1042. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  1043. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  1044. false = ETag1 =:= undefined,
  1045. ETag1 = ETag2.
  1046. static_function_etag(Config) ->
  1047. Client = ?config(client, Config),
  1048. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1049. build_url("/static_function_etag/test.html", Config), Client),
  1050. {ok, Client3} = cowboy_client:request(<<"GET">>,
  1051. build_url("/static_function_etag/test.html", Config), Client2),
  1052. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  1053. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  1054. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  1055. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  1056. false = ETag1 =:= undefined,
  1057. ETag1 = ETag2.
  1058. %% Callback function for generating the ETag for the above test.
  1059. static_function_etag(Arguments, etag_data) ->
  1060. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  1061. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  1062. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  1063. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  1064. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  1065. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  1066. {strong, iolist_to_binary(Checksum)}.
  1067. static_mimetypes_function(Config) ->
  1068. Client = ?config(client, Config),
  1069. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1070. build_url("/static_mimetypes_function/test.html", Config), Client),
  1071. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1072. {<<"content-type">>, <<"text/html">>}
  1073. = lists:keyfind(<<"content-type">>, 1, Headers).
  1074. static_specify_file(Config) ->
  1075. Client = ?config(client, Config),
  1076. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1077. build_url("/static_specify_file", Config), Client),
  1078. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1079. {<<"content-type">>, <<"text/css">>}
  1080. = lists:keyfind(<<"content-type">>, 1, Headers),
  1081. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  1082. static_specify_file_catchall(Config) ->
  1083. Client = ?config(client, Config),
  1084. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1085. build_url("/static_specify_file/none", Config), Client),
  1086. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1087. {<<"content-type">>, <<"text/css">>}
  1088. = lists:keyfind(<<"content-type">>, 1, Headers),
  1089. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  1090. static_test_file(Config) ->
  1091. Client = ?config(client, Config),
  1092. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1093. build_url("/static/test_file", Config), Client),
  1094. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1095. {<<"content-type">>, <<"application/octet-stream">>}
  1096. = lists:keyfind(<<"content-type">>, 1, Headers).
  1097. static_test_file_css(Config) ->
  1098. Client = ?config(client, Config),
  1099. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1100. build_url("/static/test_file.css", Config), Client),
  1101. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  1102. {<<"content-type">>, <<"text/css">>}
  1103. = lists:keyfind(<<"content-type">>, 1, Headers).
  1104. stream_body_set_resp(Config) ->
  1105. Client = ?config(client, Config),
  1106. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1107. build_url("/stream_body/set_resp", Config), Client),
  1108. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1109. {ok, <<"stream_body_set_resp">>, _}
  1110. = cowboy_client:response_body(Client3).
  1111. stream_body_set_resp_close(Config) ->
  1112. Client = ?config(client, Config),
  1113. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1114. build_url("/stream_body/set_resp_close", Config), Client),
  1115. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1116. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  1117. case element(7, Client3) of
  1118. <<"stream_body_set_resp_close">> ->
  1119. ok;
  1120. Buffer ->
  1121. {ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000),
  1122. <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
  1123. ok
  1124. end,
  1125. {error, closed} = Transport:recv(Socket, 0, 1000).
  1126. stream_body_set_resp_chunked(Config) ->
  1127. Client = ?config(client, Config),
  1128. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1129. build_url("/stream_body/set_resp_chunked", Config), Client),
  1130. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  1131. {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  1132. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  1133. case element(7, Client3) of
  1134. <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">> ->
  1135. ok;
  1136. Buffer ->
  1137. {ok, Rest} = Transport:recv(Socket, 44 - byte_size(Buffer), 1000),
  1138. <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">>
  1139. = <<Buffer/binary, Rest/binary>>,
  1140. ok
  1141. end.
  1142. stream_body_set_resp_chunked10(Config) ->
  1143. Client = ?config(client, Config),
  1144. Transport = ?config(transport, Config),
  1145. {ok, Client2} = cowboy_client:connect(
  1146. Transport, "localhost", ?config(port, Config), Client),
  1147. Data = ["GET /stream_body/set_resp_chunked HTTP/1.0\r\n",
  1148. "Host: localhost\r\n\r\n"],
  1149. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  1150. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  1151. false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  1152. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  1153. case element(7, Client4) of
  1154. <<"stream_body_set_resp_chunked">> ->
  1155. ok;
  1156. Buffer ->
  1157. {ok, Rest} = Transport:recv(Socket, 28 - byte_size(Buffer), 1000),
  1158. <<"stream_body_set_resp_chunked">>
  1159. = <<Buffer/binary, Rest/binary>>,
  1160. ok
  1161. end,
  1162. {error, closed} = Transport:recv(Socket, 0, 1000).
  1163. te_chunked(Config) ->
  1164. Client = ?config(client, Config),
  1165. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1166. Chunks = body_to_chunks(50, Body, []),
  1167. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1168. build_url("/echo/body", Config),
  1169. [{<<"transfer-encoding">>, <<"chunked">>}],
  1170. Chunks, Client),
  1171. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1172. {ok, Body, _} = cowboy_client:response_body(Client3).
  1173. te_chunked_chopped(Config) ->
  1174. Client = ?config(client, Config),
  1175. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1176. Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
  1177. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1178. build_url("/echo/body", Config),
  1179. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1180. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1181. _ = [begin
  1182. ok = Transport:send(Socket, << C >>),
  1183. ok = timer:sleep(10)
  1184. end || << C >> <= Body2],
  1185. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1186. {ok, Body, _} = cowboy_client:response_body(Client3).
  1187. te_chunked_delayed(Config) ->
  1188. Client = ?config(client, Config),
  1189. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1190. Chunks = body_to_chunks(50, Body, []),
  1191. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1192. build_url("/echo/body", Config),
  1193. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1194. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1195. _ = [begin
  1196. ok = Transport:send(Socket, Chunk),
  1197. ok = timer:sleep(10)
  1198. end || Chunk <- Chunks],
  1199. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1200. {ok, Body, _} = cowboy_client:response_body(Client3).
  1201. te_identity(Config) ->
  1202. Client = ?config(client, Config),
  1203. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1204. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1205. build_url("/echo/body", Config), [], Body, Client),
  1206. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1207. {ok, Body, _} = cowboy_client:response_body(Client3).