static_handler_SUITE.erl 32 KB

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