static_handler_SUITE.erl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. %% Copyright (c) 2016-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(static_handler_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. %% ct.
  21. all() ->
  22. cowboy_test:common_all() ++ [
  23. {group, http_no_sendfile},
  24. {group, h2c_no_sendfile}
  25. ].
  26. groups() ->
  27. AllTests = ct_helper:all(?MODULE),
  28. %% The directory tests are shared between dir and priv_dir options.
  29. DirTests = lists:usort([F || {F, 1} <- ?MODULE:module_info(exports),
  30. string:substr(atom_to_list(F), 1, 4) =:= "dir_"
  31. ]),
  32. OtherTests = AllTests -- DirTests,
  33. GroupTests = OtherTests ++ [
  34. {dir, [parallel], DirTests},
  35. {priv_dir, [parallel], DirTests}
  36. ],
  37. [
  38. {http, [parallel], GroupTests},
  39. {https, [parallel], GroupTests},
  40. {h2, [parallel], GroupTests},
  41. {h2c, [parallel], GroupTests},
  42. {http_compress, [parallel], GroupTests},
  43. {https_compress, [parallel], GroupTests},
  44. {h2_compress, [parallel], GroupTests},
  45. {h2c_compress, [parallel], GroupTests},
  46. %% No real need to test sendfile disabled against https or h2.
  47. {http_no_sendfile, [parallel], GroupTests},
  48. {h2c_no_sendfile, [parallel], GroupTests}
  49. ].
  50. init_per_suite(Config) ->
  51. %% Two static folders are created: one in ct_helper's private directory,
  52. %% and one in the test run private directory.
  53. PrivDir = code:priv_dir(ct_helper) ++ "/static",
  54. StaticDir = config(priv_dir, Config) ++ "/static",
  55. ct_helper:create_static_dir(PrivDir),
  56. ct_helper:create_static_dir(StaticDir),
  57. init_large_file(PrivDir ++ "/large.bin"),
  58. init_large_file(StaticDir ++ "/large.bin"),
  59. %% Add a simple Erlang application archive containing one file
  60. %% in its priv directory.
  61. true = code:add_pathz(filename:join(
  62. [config(data_dir, Config), "static_files_app", "ebin"])),
  63. ok = application:load(static_files_app),
  64. %% A special folder contains files of 1 character from 0 to 127.
  65. CharDir = config(priv_dir, Config) ++ "/char",
  66. ok = filelib:ensure_dir(CharDir ++ "/file"),
  67. Chars = lists:flatten([case file:write_file(CharDir ++ [$/, C], [C]) of
  68. ok -> C;
  69. {error, _} -> []
  70. end || C <- lists:seq(0, 127)]),
  71. [{static_dir, StaticDir}, {char_dir, CharDir}, {chars, Chars}|Config].
  72. end_per_suite(Config) ->
  73. %% Special directory.
  74. CharDir = config(char_dir, Config),
  75. _ = [file:delete(CharDir ++ [$/, C]) || C <- lists:seq(0, 127)],
  76. _ = file:del_dir(CharDir),
  77. %% Static directories.
  78. StaticDir = config(static_dir, Config),
  79. PrivDir = code:priv_dir(ct_helper) ++ "/static",
  80. %% This file is not created on Windows.
  81. _ = file:delete(StaticDir ++ "/large.bin"),
  82. _ = file:delete(PrivDir ++ "/large.bin"),
  83. ct_helper:delete_static_dir(StaticDir),
  84. ct_helper:delete_static_dir(PrivDir).
  85. init_per_group(dir, Config) ->
  86. [{prefix, "/dir"}|Config];
  87. init_per_group(priv_dir, Config) ->
  88. [{prefix, "/priv_dir"}|Config];
  89. init_per_group(Name=http_no_sendfile, Config) ->
  90. cowboy_test:init_http(Name, #{
  91. env => #{dispatch => init_dispatch(Config)},
  92. middlewares => [?MODULE, cowboy_router, cowboy_handler],
  93. sendfile => false
  94. }, [{flavor, vanilla}|Config]);
  95. init_per_group(Name=h2c_no_sendfile, Config) ->
  96. Config1 = cowboy_test:init_http(Name, #{
  97. env => #{dispatch => init_dispatch(Config)},
  98. middlewares => [?MODULE, cowboy_router, cowboy_handler],
  99. sendfile => false
  100. }, [{flavor, vanilla}|Config]),
  101. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  102. init_per_group(Name, Config) ->
  103. Config1 = cowboy_test:init_common_groups(Name, Config, ?MODULE),
  104. Opts = ranch:get_protocol_options(Name),
  105. ok = ranch:set_protocol_options(Name, Opts#{
  106. middlewares => [?MODULE, cowboy_router, cowboy_handler]
  107. }),
  108. Config1.
  109. end_per_group(Name, _) ->
  110. cowboy:stop_listener(Name).
  111. %% Large file.
  112. init_large_file(Filename) ->
  113. case os:type() of
  114. {unix, _} ->
  115. "" = os:cmd("truncate -s 32M " ++ Filename),
  116. ok;
  117. {win32, _} ->
  118. ok
  119. end.
  120. %% Routes.
  121. init_dispatch(Config) ->
  122. cowboy_router:compile([{'_', [
  123. {"/priv_dir/[...]", cowboy_static, {priv_dir, ct_helper, "static"}},
  124. {"/dir/[...]", cowboy_static, {dir, config(static_dir, Config)}},
  125. {"/priv_file/style.css", cowboy_static, {priv_file, ct_helper, "static/style.css"}},
  126. {"/file/style.css", cowboy_static, {file, config(static_dir, Config) ++ "/style.css"}},
  127. {"/index", cowboy_static, {file, config(static_dir, Config) ++ "/index.html"}},
  128. {"/mime/all/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  129. [{mimetypes, cow_mimetypes, all}]}},
  130. {"/mime/custom/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  131. [{mimetypes, ?MODULE, do_mime_custom}]}},
  132. {"/mime/crash/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  133. [{mimetypes, ?MODULE, do_mime_crash}]}},
  134. {"/mime/hardcode/binary-form", cowboy_static, {priv_file, ct_helper, "static/file.cowboy",
  135. [{mimetypes, <<"application/vnd.ninenines.cowboy+xml;v=1">>}]}},
  136. {"/mime/hardcode/tuple-form", cowboy_static, {priv_file, ct_helper, "static/file.cowboy",
  137. [{mimetypes, {<<"application">>, <<"vnd.ninenines.cowboy+xml">>, [{<<"v">>, <<"1">>}]}}]}},
  138. {"/charset/custom/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  139. [{charset, ?MODULE, do_charset_custom}]}},
  140. {"/charset/crash/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  141. [{charset, ?MODULE, do_charset_crash}]}},
  142. {"/charset/hardcode/[...]", cowboy_static, {priv_file, ct_helper, "static/index.html",
  143. [{charset, <<"utf-8">>}]}},
  144. {"/etag/custom", cowboy_static, {file, config(static_dir, Config) ++ "/style.css",
  145. [{etag, ?MODULE, do_etag_custom}]}},
  146. {"/etag/crash", cowboy_static, {file, config(static_dir, Config) ++ "/style.css",
  147. [{etag, ?MODULE, do_etag_crash}]}},
  148. {"/etag/disable", cowboy_static, {file, config(static_dir, Config) ++ "/style.css",
  149. [{etag, false}]}},
  150. {"/bad", cowboy_static, bad},
  151. {"/bad/priv_dir/app/[...]", cowboy_static, {priv_dir, bad_app, "static"}},
  152. {"/bad/priv_dir/no-priv/[...]", cowboy_static, {priv_dir, cowboy, "static"}},
  153. {"/bad/priv_dir/path/[...]", cowboy_static, {priv_dir, ct_helper, "bad"}},
  154. {"/bad/priv_dir/route", cowboy_static, {priv_dir, ct_helper, "static"}},
  155. {"/bad/dir/path/[...]", cowboy_static, {dir, "/bad/path"}},
  156. {"/bad/dir/route", cowboy_static, {dir, config(static_dir, Config)}},
  157. {"/bad/priv_file/app", cowboy_static, {priv_file, bad_app, "static/style.css"}},
  158. {"/bad/priv_file/no-priv", cowboy_static, {priv_file, cowboy, "static/style.css"}},
  159. {"/bad/priv_file/path", cowboy_static, {priv_file, ct_helper, "bad/style.css"}},
  160. {"/bad/file/path", cowboy_static, {file, "/bad/path/style.css"}},
  161. {"/bad/options", cowboy_static, {priv_file, ct_helper, "static/style.css", bad}},
  162. {"/bad/options/mime", cowboy_static, {priv_file, ct_helper, "static/style.css", [{mimetypes, bad}]}},
  163. {"/bad/options/charset", cowboy_static, {priv_file, ct_helper, "static/style.css", [{charset, bad}]}},
  164. {"/bad/options/etag", cowboy_static, {priv_file, ct_helper, "static/style.css", [{etag, true}]}},
  165. {"/unknown/option", cowboy_static, {priv_file, ct_helper, "static/style.css", [{bad, option}]}},
  166. {"/char/[...]", cowboy_static, {dir, config(char_dir, Config)}},
  167. {"/ez_priv_file/index.html", cowboy_static, {priv_file, static_files_app, "www/index.html"}},
  168. {"/bad/ez_priv_file/index.php", cowboy_static, {priv_file, static_files_app, "www/index.php"}},
  169. {"/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "www"}},
  170. {"/bad/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "cgi-bin"}}
  171. ]}]).
  172. %% Middleware interface to silence expected errors.
  173. execute(Req=#{path := Path}, Env) ->
  174. case Path of
  175. <<"/bad/priv_dir/app/", _/bits>> -> ct_helper:ignore(cowboy_static, priv_path, 2);
  176. <<"/bad/priv_file/app">> -> ct_helper:ignore(cowboy_static, priv_path, 2);
  177. <<"/bad/priv_dir/route">> -> ct_helper:ignore(cowboy_static, escape_reserved, 1);
  178. <<"/bad/dir/route">> -> ct_helper:ignore(cowboy_static, escape_reserved, 1);
  179. <<"/bad">> -> ct_helper:ignore(cowboy_static, init_opts, 2);
  180. <<"/bad/options">> -> ct_helper:ignore(cowboy_static, content_types_provided, 2);
  181. <<"/bad/options/mime">> -> ct_helper:ignore(cowboy_rest, set_content_type, 2);
  182. <<"/bad/options/etag">> -> ct_helper:ignore(cowboy_static, generate_etag, 2);
  183. _ -> ok
  184. end,
  185. {ok, Req, Env}.
  186. %% Internal functions.
  187. -spec do_charset_crash(_) -> no_return().
  188. do_charset_crash(_) ->
  189. ct_helper_error_h:ignore(?MODULE, do_charset_crash, 1),
  190. exit(crash).
  191. do_charset_custom(Path) ->
  192. case filename:extension(Path) of
  193. <<".cowboy">> -> <<"utf-32">>;
  194. <<".html">> -> <<"utf-16">>;
  195. _ -> <<"utf-8">>
  196. end.
  197. -spec do_etag_crash(_, _, _) -> no_return().
  198. do_etag_crash(_, _, _) ->
  199. ct_helper_error_h:ignore(?MODULE, do_etag_crash, 3),
  200. exit(crash).
  201. do_etag_custom(_, _, _) ->
  202. {strong, <<"etag">>}.
  203. -spec do_mime_crash(_) -> no_return().
  204. do_mime_crash(_) ->
  205. ct_helper_error_h:ignore(?MODULE, do_mime_crash, 1),
  206. exit(crash).
  207. do_mime_custom(Path) ->
  208. case filename:extension(Path) of
  209. <<".cowboy">> -> <<"application/vnd.ninenines.cowboy+xml;v=1">>;
  210. <<".txt">> -> <<"text/plain">>;
  211. _ -> {<<"application">>, <<"octet-stream">>, []}
  212. end.
  213. do_get(Path, Config) ->
  214. do_get(Path, [], Config).
  215. do_get(Path, ReqHeaders, Config) ->
  216. ConnPid = gun_open(Config),
  217. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|ReqHeaders]),
  218. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  219. {ok, Body} = case IsFin of
  220. nofin -> gun:await_body(ConnPid, Ref);
  221. fin -> {ok, <<>>}
  222. end,
  223. gun:close(ConnPid),
  224. {Status, RespHeaders, Body}.
  225. %% Tests.
  226. bad(Config) ->
  227. doc("Bad cowboy_static options: not a tuple."),
  228. {500, _, _} = do_get("/bad", Config),
  229. ok.
  230. bad_dir_path(Config) ->
  231. doc("Bad cowboy_static options: wrong path."),
  232. {404, _, _} = do_get("/bad/dir/path/style.css", Config),
  233. ok.
  234. bad_dir_route(Config) ->
  235. doc("Bad cowboy_static options: missing [...] in route."),
  236. {500, _, _} = do_get("/bad/dir/route", Config),
  237. ok.
  238. bad_file_in_priv_dir_in_ez_archive(Config) ->
  239. doc("Get a missing file from a priv_dir stored in Erlang application .ez archive."),
  240. {404, _, _} = do_get("/ez_priv_dir/index.php", Config),
  241. ok.
  242. bad_file_path(Config) ->
  243. doc("Bad cowboy_static options: wrong path."),
  244. {404, _, _} = do_get("/bad/file/path", Config),
  245. ok.
  246. bad_options(Config) ->
  247. doc("Bad cowboy_static extra options: not a list."),
  248. {500, _, _} = do_get("/bad/options", Config),
  249. ok.
  250. bad_options_etag(Config) ->
  251. doc("Bad cowboy_static extra options: invalid etag option."),
  252. {500, _, _} = do_get("/bad/options/etag", Config),
  253. ok.
  254. bad_options_mime(Config) ->
  255. doc("Bad cowboy_static extra options: invalid mimetypes option."),
  256. {500, _, _} = do_get("/bad/options/mime", Config),
  257. ok.
  258. bad_priv_dir_app(Config) ->
  259. doc("Bad cowboy_static options: wrong application name."),
  260. {500, _, _} = do_get("/bad/priv_dir/app/style.css", Config),
  261. ok.
  262. bad_priv_dir_in_ez_archive(Config) ->
  263. doc("Bad cowboy_static options: priv_dir path missing from Erlang application .ez archive."),
  264. {404, _, _} = do_get("/bad/ez_priv_dir/index.html", Config),
  265. ok.
  266. bad_priv_dir_no_priv(Config) ->
  267. doc("Bad cowboy_static options: application has no priv directory."),
  268. {404, _, _} = do_get("/bad/priv_dir/no-priv/style.css", Config),
  269. ok.
  270. bad_priv_dir_path(Config) ->
  271. doc("Bad cowboy_static options: wrong path."),
  272. {404, _, _} = do_get("/bad/priv_dir/path/style.css", Config),
  273. ok.
  274. bad_priv_dir_route(Config) ->
  275. doc("Bad cowboy_static options: missing [...] in route."),
  276. {500, _, _} = do_get("/bad/priv_dir/route", Config),
  277. ok.
  278. bad_priv_file_app(Config) ->
  279. doc("Bad cowboy_static options: wrong application name."),
  280. {500, _, _} = do_get("/bad/priv_file/app", Config),
  281. ok.
  282. bad_priv_file_in_ez_archive(Config) ->
  283. doc("Bad cowboy_static options: priv_file path missing from Erlang application .ez archive."),
  284. {404, _, _} = do_get("/bad/ez_priv_file/index.php", Config),
  285. ok.
  286. bad_priv_file_no_priv(Config) ->
  287. doc("Bad cowboy_static options: application has no priv directory."),
  288. {404, _, _} = do_get("/bad/priv_file/no-priv", Config),
  289. ok.
  290. bad_priv_file_path(Config) ->
  291. doc("Bad cowboy_static options: wrong path."),
  292. {404, _, _} = do_get("/bad/priv_file/path", Config),
  293. ok.
  294. dir_cowboy(Config) ->
  295. doc("Get a .cowboy file."),
  296. {200, Headers, <<"File with custom extension.\n">>}
  297. = do_get(config(prefix, Config) ++ "/file.cowboy", Config),
  298. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  299. ok.
  300. dir_css(Config) ->
  301. doc("Get a .css file."),
  302. {200, Headers, <<"body{color:red}\n">>}
  303. = do_get(config(prefix, Config) ++ "/style.css", Config),
  304. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  305. ok.
  306. dir_css_urlencoded(Config) ->
  307. doc("Get a .css file with the extension dot urlencoded."),
  308. {200, Headers, <<"body{color:red}\n">>}
  309. = do_get(config(prefix, Config) ++ "/style%2ecss", Config),
  310. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  311. ok.
  312. dir_dot_file(Config) ->
  313. doc("Get a file with extra dot segments in the path."),
  314. %% All these are equivalent.
  315. {200, _, _} = do_get(config(prefix, Config) ++ "/./style.css", Config),
  316. {200, _, _} = do_get(config(prefix, Config) ++ "/././style.css", Config),
  317. {200, _, _} = do_get(config(prefix, Config) ++ "/./././style.css", Config),
  318. {200, _, _} = do_get("/./priv_dir/style.css", Config),
  319. {200, _, _} = do_get("/././priv_dir/style.css", Config),
  320. {200, _, _} = do_get("/./././priv_dir/style.css", Config),
  321. ok.
  322. dir_dotdot_file(Config) ->
  323. doc("Get a file with extra dotdot segments in the path."),
  324. %% All these are equivalent.
  325. {200, _, _} = do_get("/../priv_dir/style.css", Config),
  326. {200, _, _} = do_get("/../../priv_dir/style.css", Config),
  327. {200, _, _} = do_get("/../../../priv_dir/style.css", Config),
  328. {200, _, _} = do_get(config(prefix, Config) ++ "/../priv_dir/style.css", Config),
  329. {200, _, _} = do_get(config(prefix, Config) ++ "/../../priv_dir/style.css", Config),
  330. {200, _, _} = do_get(config(prefix, Config) ++ "/../../../priv_dir/style.css", Config),
  331. {200, _, _} = do_get("/../priv_dir/../priv_dir/style.css", Config),
  332. {200, _, _} = do_get("/../../priv_dir/../../priv_dir/style.css", Config),
  333. {200, _, _} = do_get("/../../../priv_dir/../../../priv_dir/style.css", Config),
  334. %% Try with non-existing segments, which may correspond to real folders.
  335. {200, _, _} = do_get("/anything/../priv_dir/style.css", Config),
  336. {200, _, _} = do_get(config(prefix, Config) ++ "/anything/../style.css", Config),
  337. {200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config),
  338. {200, _, _} = do_get(config(prefix, Config) ++ "/static/../style.css", Config),
  339. %% Try with segments corresponding to real files. It works because
  340. %% URI normalization happens before looking at the filesystem.
  341. {200, _, _} = do_get(config(prefix, Config) ++ "/style.css/../style.css", Config),
  342. {200, _, _} = do_get(config(prefix, Config) ++ "/style.css/../../priv_dir/style.css", Config),
  343. %% Try to fool the server to accept segments corresponding to real folders.
  344. {404, _, _} = do_get(config(prefix, Config) ++ "/../static/style.css", Config),
  345. {404, _, _} = do_get(config(prefix, Config) ++ "/directory/../../static/style.css", Config),
  346. ok.
  347. dir_empty_file(Config) ->
  348. doc("Get an empty .txt file."),
  349. {200, _, <<>>} = do_get(config(prefix, Config) ++ "/empty.txt", Config),
  350. ok.
  351. dir_error_directory(Config) ->
  352. doc("Try to get a directory."),
  353. {403, _, _} = do_get(config(prefix, Config) ++ "/directory", Config),
  354. ok.
  355. dir_error_directory_slash(Config) ->
  356. doc("Try to get a directory with an extra slash in the path."),
  357. {403, _, _} = do_get(config(prefix, Config) ++ "/directory/", Config),
  358. ok.
  359. dir_error_doesnt_exist(Config) ->
  360. doc("Try to get a file that does not exist."),
  361. {404, Headers, _} = do_get(config(prefix, Config) ++ "/not.found", Config),
  362. false = lists:keyfind(<<"content-type">>, 1, Headers),
  363. ok.
  364. dir_error_dot(Config) ->
  365. doc("Try to get a file named '.'."),
  366. {403, _, _} = do_get(config(prefix, Config) ++ "/.", Config),
  367. ok.
  368. dir_error_dot_urlencoded(Config) ->
  369. doc("Try to get a file named '.' percent encoded."),
  370. {403, _, _} = do_get(config(prefix, Config) ++ "/%2e", Config),
  371. ok.
  372. dir_error_dotdot(Config) ->
  373. doc("Try to get a file named '..'."),
  374. {404, _, _} = do_get(config(prefix, Config) ++ "/..", Config),
  375. ok.
  376. dir_error_dotdot_urlencoded(Config) ->
  377. doc("Try to get a file named '..' percent encoded."),
  378. {404, _, _} = do_get(config(prefix, Config) ++ "/%2e%2e", Config),
  379. ok.
  380. dir_error_empty(Config) ->
  381. doc("Try to get the configured directory."),
  382. {403, _, _} = do_get(config(prefix, Config) ++ "", Config),
  383. ok.
  384. dir_error_slash(Config) ->
  385. %% I know the description isn't that good considering / has a meaning in URIs.
  386. doc("Try to get a file named '/'."),
  387. {403, _, _} = do_get(config(prefix, Config) ++ "//", Config),
  388. ok.
  389. dir_error_slash_urlencoded(Config) ->
  390. doc("Try to get a file named '/' percent encoded."),
  391. {404, _, _} = do_get(config(prefix, Config) ++ "/%2f", Config),
  392. ok.
  393. dir_error_slash_urlencoded_dotdot_file(Config) ->
  394. doc("Try to use a percent encoded slash to access an existing file."),
  395. {200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config),
  396. {404, _, _} = do_get(config(prefix, Config) ++ "/directory%2f../style.css", Config),
  397. ok.
  398. dir_error_unreadable(Config) ->
  399. doc("Try to get a file that can't be read."),
  400. {403, _, _} = do_get(config(prefix, Config) ++ "/unreadable", Config),
  401. ok.
  402. dir_html(Config) ->
  403. doc("Get a .html file."),
  404. {200, Headers, <<"<html><body>Hello!</body></html>\n">>}
  405. = do_get(config(prefix, Config) ++ "/index.html", Config),
  406. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  407. ok.
  408. dir_large_file(Config) ->
  409. doc("Get a large file."),
  410. ConnPid = gun_open(Config),
  411. Ref = gun:get(ConnPid, config(prefix, Config) ++ "/large.bin",
  412. [{<<"accept-encoding">>, <<"gzip">>}]),
  413. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  414. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
  415. Size = 32*1024*1024,
  416. {ok, Size} = do_dir_large_file(ConnPid, Ref, 0),
  417. ok.
  418. do_dir_large_file(ConnPid, Ref, N) ->
  419. receive
  420. {gun_data, ConnPid, Ref, nofin, Data} ->
  421. do_dir_large_file(ConnPid, Ref, N + byte_size(Data));
  422. {gun_data, ConnPid, Ref, fin, Data} ->
  423. {ok, N + byte_size(Data)};
  424. {gun_error, ConnPid, Ref, Reason} ->
  425. {error, Reason};
  426. {gun_error, ConnPid, Reason} ->
  427. {error, Reason}
  428. after 5000 ->
  429. {error, timeout}
  430. end.
  431. dir_text(Config) ->
  432. doc("Get a .txt file. The extension is unknown by default."),
  433. {200, Headers, <<"Timeless space.\n">>}
  434. = do_get(config(prefix, Config) ++ "/plain.txt", Config),
  435. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  436. ok.
  437. dir_unknown(Config) ->
  438. doc("Get a file with no extension."),
  439. {200, Headers, <<"File with no extension.\n">>}
  440. = do_get(config(prefix, Config) ++ "/unknown", Config),
  441. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  442. ok.
  443. etag_crash(Config) ->
  444. doc("Get a file with a crashing etag function."),
  445. {500, _, _} = do_get("/etag/crash", Config),
  446. ok.
  447. etag_custom(Config) ->
  448. doc("Get a file with custom Etag function and make sure it is used."),
  449. {200, Headers, _} = do_get("/etag/custom", Config),
  450. {_, <<"\"etag\"">>} = lists:keyfind(<<"etag">>, 1, Headers),
  451. ok.
  452. etag_default(Config) ->
  453. doc("Get a file twice and make sure the Etag matches."),
  454. {200, Headers1, _} = do_get("/dir/style.css", Config),
  455. {200, Headers2, _} = do_get("/dir/style.css", Config),
  456. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers1),
  457. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers2),
  458. ok.
  459. etag_default_change(Config) ->
  460. doc("Get a file, modify it, get it again and make sure the Etag doesn't match."),
  461. {200, Headers1, _} = do_get("/dir/index.html", Config),
  462. {_, Etag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  463. ok = file:change_time(config(static_dir, Config) ++ "/index.html",
  464. {{config(port, Config), 1, 1}, {1, 1, 1}}),
  465. {200, Headers2, _} = do_get("/dir/index.html", Config),
  466. {_, Etag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  467. true = Etag1 =/= Etag2,
  468. ok.
  469. etag_disable(Config) ->
  470. doc("Get a file with disabled Etag and make sure no Etag is provided."),
  471. {200, Headers, _} = do_get("/etag/disable", Config),
  472. false = lists:keyfind(<<"etag">>, 1, Headers),
  473. ok.
  474. file(Config) ->
  475. doc("Get a file with hardcoded route."),
  476. {200, Headers, <<"body{color:red}\n">>} = do_get("/file/style.css", Config),
  477. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  478. ok.
  479. if_match(Config) ->
  480. doc("Get a file with If-Match matching."),
  481. {200, _, _} = do_get("/etag/custom", [
  482. {<<"if-match">>, <<"\"etag\"">>}
  483. ], Config),
  484. ok.
  485. if_match_fail(Config) ->
  486. doc("Get a file with If-Match not matching."),
  487. {412, _, _} = do_get("/etag/custom", [
  488. {<<"if-match">>, <<"\"invalid\"">>}
  489. ], Config),
  490. ok.
  491. if_match_invalid(Config) ->
  492. doc("Try to get a file with an invalid If-Match header."),
  493. {400, _, _} = do_get("/etag/custom", [
  494. {<<"if-match">>, <<"bad input">>}
  495. ], Config),
  496. ok.
  497. if_match_list(Config) ->
  498. doc("Get a file with If-Match matching."),
  499. {200, _, _} = do_get("/etag/custom", [
  500. {<<"if-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  501. ], Config),
  502. ok.
  503. if_match_list_fail(Config) ->
  504. doc("Get a file with If-Match not matching."),
  505. {412, _, _} = do_get("/etag/custom", [
  506. {<<"if-match">>, <<"\"invalid\", W/\"etag\", \"cowboy\"">>}
  507. ], Config),
  508. ok.
  509. if_match_weak(Config) ->
  510. doc("Try to get a file with a weak If-Match header."),
  511. {412, _, _} = do_get("/etag/custom", [
  512. {<<"if-match">>, <<"W/\"etag\"">>}
  513. ], Config),
  514. ok.
  515. if_match_wildcard(Config) ->
  516. doc("Get a file with a wildcard If-Match."),
  517. {200, _, _} = do_get("/etag/custom", [
  518. {<<"if-match">>, <<"*">>}
  519. ], Config),
  520. ok.
  521. if_modified_since(Config) ->
  522. doc("Get a file with If-Modified-Since in the past."),
  523. {200, _, _} = do_get("/etag/custom", [
  524. {<<"if-modified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  525. ], Config),
  526. ok.
  527. if_modified_since_fail(Config) ->
  528. doc("Get a file with If-Modified-Since equal to file modification time."),
  529. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  530. {304, _, _} = do_get("/etag/custom", [
  531. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)}
  532. ], Config),
  533. ok.
  534. if_modified_since_future(Config) ->
  535. doc("Get a file with If-Modified-Since in the future."),
  536. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  537. {200, _, _} = do_get("/etag/custom", [
  538. {<<"if-modified-since">>, [
  539. <<"Sat, 29 Oct ">>,
  540. integer_to_binary(Year + 1),
  541. <<" 19:43:31 GMT">>]}
  542. ], Config),
  543. ok.
  544. if_modified_since_if_none_match(Config) ->
  545. doc("Get a file with both If-Modified-Since and If-None-Match headers."
  546. "If-None-Match takes precedence and If-Modified-Since is ignored. (RFC7232 3.3)"),
  547. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  548. {200, _, _} = do_get("/etag/custom", [
  549. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)},
  550. {<<"if-none-match">>, <<"\"not-etag\"">>}
  551. ], Config),
  552. ok.
  553. if_modified_since_invalid(Config) ->
  554. doc("Get a file with an invalid If-Modified-Since header."),
  555. {200, _, _} = do_get("/etag/custom", [
  556. {<<"if-modified-since">>, <<"\"not a date\"">>}
  557. ], Config),
  558. ok.
  559. if_none_match(Config) ->
  560. doc("Get a file with If-None-Match not matching."),
  561. {200, _, _} = do_get("/etag/custom", [
  562. {<<"if-none-match">>, <<"\"not-etag\"">>}
  563. ], Config),
  564. ok.
  565. if_none_match_fail(Config) ->
  566. doc("Get a file with If-None-Match matching."),
  567. {304, _, _} = do_get("/etag/custom", [
  568. {<<"if-none-match">>, <<"\"etag\"">>}
  569. ], Config),
  570. ok.
  571. if_none_match_invalid(Config) ->
  572. doc("Try to get a file with an invalid If-None-Match header."),
  573. {400, _, _} = do_get("/etag/custom", [
  574. {<<"if-none-match">>, <<"bad input">>}
  575. ], Config),
  576. ok.
  577. if_none_match_list(Config) ->
  578. doc("Get a file with If-None-Match not matching."),
  579. {200, _, _} = do_get("/etag/custom", [
  580. {<<"if-none-match">>, <<"\"invalid\", W/\"not-etag\", \"cowboy\"">>}
  581. ], Config),
  582. ok.
  583. if_none_match_list_fail(Config) ->
  584. doc("Get a file with If-None-Match matching."),
  585. {304, _, _} = do_get("/etag/custom", [
  586. {<<"if-none-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  587. ], Config),
  588. ok.
  589. if_none_match_weak(Config) ->
  590. doc("Try to get a file with a weak If-None-Match header matching."),
  591. {304, _, _} = do_get("/etag/custom", [
  592. {<<"if-none-match">>, <<"W/\"etag\"">>}
  593. ], Config),
  594. ok.
  595. if_none_match_wildcard(Config) ->
  596. doc("Try to get a file with a wildcard If-None-Match."),
  597. {304, _, _} = do_get("/etag/custom", [
  598. {<<"if-none-match">>, <<"*">>}
  599. ], Config),
  600. ok.
  601. if_unmodified_since(Config) ->
  602. doc("Get a file with If-Unmodified-Since equal to file modification time."),
  603. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  604. {200, _, _} = do_get("/etag/custom", [
  605. {<<"if-unmodified-since">>, httpd_util:rfc1123_date(LastModified)}
  606. ], Config),
  607. ok.
  608. if_unmodified_since_fail(Config) ->
  609. doc("Get a file with If-Unmodified-Since in the past."),
  610. {412, _, _} = do_get("/etag/custom", [
  611. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  612. ], Config),
  613. ok.
  614. if_unmodified_since_future(Config) ->
  615. doc("Get a file with If-Unmodified-Since in the future."),
  616. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  617. {200, _, _} = do_get("/etag/custom", [
  618. {<<"if-unmodified-since">>, [
  619. <<"Sat, 29 Oct ">>,
  620. integer_to_binary(Year + 1),
  621. <<" 19:43:31 GMT">>]}
  622. ], Config),
  623. ok.
  624. if_unmodified_since_if_match(Config) ->
  625. doc("Get a file with both If-Unmodified-Since and If-Match headers."
  626. "If-Match takes precedence and If-Unmodified-Since is ignored. (RFC7232 3.4)"),
  627. {200, _, _} = do_get("/etag/custom", [
  628. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>},
  629. {<<"if-match">>, <<"\"etag\"">>}
  630. ], Config),
  631. ok.
  632. if_unmodified_since_invalid(Config) ->
  633. doc("Get a file with an invalid If-Unmodified-Since header."),
  634. {200, _, _} = do_get("/etag/custom", [
  635. {<<"if-unmodified-since">>, <<"\"not a date\"">>}
  636. ], Config),
  637. ok.
  638. index_file(Config) ->
  639. doc("Get an index file."),
  640. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index", Config),
  641. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  642. ok.
  643. index_file_slash(Config) ->
  644. doc("Get an index file with extra slash."),
  645. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index/", Config),
  646. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  647. ok.
  648. last_modified(Config) ->
  649. doc("Get a file, modify it, get it again and make sure Last-Modified changes."),
  650. {200, Headers1, _} = do_get("/dir/file.cowboy", Config),
  651. {_, LastModified1} = lists:keyfind(<<"last-modified">>, 1, Headers1),
  652. ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy",
  653. {{config(port, Config), 1, 1}, {1, 1, 1}}),
  654. {200, Headers2, _} = do_get("/dir/file.cowboy", Config),
  655. {_, LastModified2} = lists:keyfind(<<"last-modified">>, 1, Headers2),
  656. true = LastModified1 =/= LastModified2,
  657. ok.
  658. mime_all_cowboy(Config) ->
  659. doc("Get a .cowboy file. The extension is unknown."),
  660. {200, Headers, _} = do_get("/mime/all/file.cowboy", Config),
  661. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  662. ok.
  663. mime_all_css(Config) ->
  664. doc("Get a .css file."),
  665. {200, Headers, _} = do_get("/mime/all/style.css", Config),
  666. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  667. ok.
  668. mime_all_txt(Config) ->
  669. doc("Get a .txt file."),
  670. {200, Headers, _} = do_get("/mime/all/plain.txt", Config),
  671. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  672. ok.
  673. mime_crash(Config) ->
  674. doc("Get a file with a crashing mimetype function."),
  675. {500, _, _} = do_get("/mime/crash/style.css", Config),
  676. ok.
  677. mime_custom_cowboy(Config) ->
  678. doc("Get a .cowboy file."),
  679. {200, Headers, _} = do_get("/mime/custom/file.cowboy", Config),
  680. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  681. ok.
  682. mime_custom_css(Config) ->
  683. doc("Get a .css file. The extension is unknown."),
  684. {200, Headers, _} = do_get("/mime/custom/style.css", Config),
  685. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  686. ok.
  687. mime_custom_txt(Config) ->
  688. doc("Get a .txt file."),
  689. {200, Headers, _} = do_get("/mime/custom/plain.txt", Config),
  690. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  691. ok.
  692. mime_hardcode_binary(Config) ->
  693. doc("Get a .cowboy file with hardcoded route and media type in binary form."),
  694. {200, Headers, _} = do_get("/mime/hardcode/binary-form", Config),
  695. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  696. ok.
  697. mime_hardcode_tuple(Config) ->
  698. doc("Get a .cowboy file with hardcoded route and media type in tuple form."),
  699. {200, Headers, _} = do_get("/mime/hardcode/tuple-form", Config),
  700. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  701. ok.
  702. charset_crash(Config) ->
  703. doc("Get a file with a crashing charset function."),
  704. {500, _, _} = do_get("/charset/crash/style.css", Config),
  705. ok.
  706. charset_custom_cowboy(Config) ->
  707. doc("Get a .cowboy file."),
  708. {200, Headers, _} = do_get("/charset/custom/file.cowboy", Config),
  709. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  710. ok.
  711. charset_custom_css(Config) ->
  712. doc("Get a .css file."),
  713. {200, Headers, _} = do_get("/charset/custom/style.css", Config),
  714. {_, <<"text/css; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  715. ok.
  716. charset_custom_html(Config) ->
  717. doc("Get a .html file."),
  718. {200, Headers, _} = do_get("/charset/custom/index.html", Config),
  719. {_, <<"text/html; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  720. ok.
  721. charset_hardcode_binary(Config) ->
  722. doc("Get a .html file with hardcoded route and charset."),
  723. {200, Headers, _} = do_get("/charset/hardcode", Config),
  724. {_, <<"text/html; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  725. ok.
  726. priv_dir_in_ez_archive(Config) ->
  727. doc("Get a file from a priv_dir stored in Erlang application .ez archive."),
  728. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_dir/index.html", Config),
  729. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  730. ok.
  731. priv_file(Config) ->
  732. doc("Get a file with hardcoded route."),
  733. {200, Headers, <<"body{color:red}\n">>} = do_get("/priv_file/style.css", Config),
  734. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  735. ok.
  736. priv_file_in_ez_archive(Config) ->
  737. doc("Get a file stored in Erlang application .ez archive."),
  738. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_file/index.html", Config),
  739. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  740. ok.
  741. range_request(Config) ->
  742. doc("Confirm that range requests are enabled."),
  743. {206, Headers, <<"less space.\n">>} = do_get("/dir/plain.txt",
  744. [{<<"range">>, <<"bytes=4-">>}], Config),
  745. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  746. {_, <<"bytes 4-15/16">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  747. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  748. ok.
  749. unicode_basic_latin(Config) ->
  750. doc("Get a file with non-urlencoded characters from Unicode Basic Latin block."),
  751. _ = [case do_get("/char/" ++ [C], Config) of
  752. {200, _, << C >>} -> ok;
  753. Error -> exit({error, C, Error})
  754. end || C <-
  755. %% Excluding the dot which has a special meaning in URLs
  756. %% when they are the only content in a path segment,
  757. %% and is tested as part of filenames in other test cases.
  758. "abcdefghijklmnopqrstuvwxyz"
  759. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  760. "0123456789"
  761. ":@-_~!$&'()*+,;="
  762. ],
  763. ok.
  764. unicode_basic_error(Config) ->
  765. doc("Try to get a file with invalid non-urlencoded characters from Unicode Basic Latin block."),
  766. Exclude = case config(protocol, Config) of
  767. %% Some characters trigger different errors in HTTP/1.1
  768. %% because they are used for the protocol.
  769. %%
  770. %% # and ? indicate fragment and query components
  771. %% and are therefore not part of the path.
  772. http -> "\r\s#?";
  773. http2 -> "#?"
  774. end,
  775. _ = [case do_get("/char/" ++ [C], Config) of
  776. {400, _, _} -> ok;
  777. Error -> exit({error, C, Error})
  778. end || C <- (config(chars, Config) -- Exclude) --
  779. "abcdefghijklmnopqrstuvwxyz"
  780. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  781. "0123456789"
  782. ":@-_~!$&'()*+,;="
  783. ],
  784. ok.
  785. unicode_basic_latin_urlencoded(Config) ->
  786. doc("Get a file with urlencoded characters from Unicode Basic Latin block."),
  787. _ = [case do_get(lists:flatten(["/char/%", io_lib:format("~2.16.0b", [C])]), Config) of
  788. {200, _, << C >>} -> ok;
  789. Error -> exit({error, C, Error})
  790. end || C <- config(chars, Config)],
  791. ok.
  792. unknown_option(Config) ->
  793. doc("Get a file configured with unknown extra options."),
  794. {200, Headers, <<"body{color:red}\n">>} = do_get("/unknown/option", Config),
  795. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  796. ok.