static_handler_SUITE.erl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. {404, Headers, _} = do_get(config(prefix, Config) ++ "/not.found", Config),
  347. false = lists:keyfind(<<"content-type">>, 1, Headers),
  348. ok.
  349. dir_error_dot(Config) ->
  350. doc("Try to get a file named '.'."),
  351. {403, _, _} = do_get(config(prefix, Config) ++ "/.", Config),
  352. ok.
  353. dir_error_dot_urlencoded(Config) ->
  354. doc("Try to get a file named '.' percent encoded."),
  355. {403, _, _} = do_get(config(prefix, Config) ++ "/%2e", Config),
  356. ok.
  357. dir_error_dotdot(Config) ->
  358. doc("Try to get a file named '..'."),
  359. {404, _, _} = do_get(config(prefix, Config) ++ "/..", Config),
  360. ok.
  361. dir_error_dotdot_urlencoded(Config) ->
  362. doc("Try to get a file named '..' percent encoded."),
  363. {404, _, _} = do_get(config(prefix, Config) ++ "/%2e%2e", Config),
  364. ok.
  365. dir_error_empty(Config) ->
  366. doc("Try to get the configured directory."),
  367. {403, _, _} = do_get(config(prefix, Config) ++ "", Config),
  368. ok.
  369. dir_error_slash(Config) ->
  370. %% I know the description isn't that good considering / has a meaning in URIs.
  371. doc("Try to get a file named '/'."),
  372. {403, _, _} = do_get(config(prefix, Config) ++ "//", Config),
  373. ok.
  374. dir_error_slash_urlencoded(Config) ->
  375. doc("Try to get a file named '/' percent encoded."),
  376. {404, _, _} = do_get(config(prefix, Config) ++ "/%2f", Config),
  377. ok.
  378. dir_error_slash_urlencoded_dotdot_file(Config) ->
  379. doc("Try to use a percent encoded slash to access an existing file."),
  380. {200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config),
  381. {404, _, _} = do_get(config(prefix, Config) ++ "/directory%2f../style.css", Config),
  382. ok.
  383. dir_error_unreadable(Config) ->
  384. doc("Try to get a file that can't be read."),
  385. {403, _, _} = do_get(config(prefix, Config) ++ "/unreadable", Config),
  386. ok.
  387. dir_html(Config) ->
  388. doc("Get a .html file."),
  389. {200, Headers, <<"<html><body>Hello!</body></html>\n">>}
  390. = do_get(config(prefix, Config) ++ "/index.html", Config),
  391. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  392. ok.
  393. dir_large_file(Config) ->
  394. doc("Get a large file."),
  395. ConnPid = gun_open(Config),
  396. Ref = gun:get(ConnPid, config(prefix, Config) ++ "/large.bin",
  397. [{<<"accept-encoding">>, <<"gzip">>}]),
  398. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  399. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
  400. Size = 32*1024*1024,
  401. {ok, Size} = do_dir_large_file(ConnPid, Ref, 0),
  402. ok.
  403. do_dir_large_file(ConnPid, Ref, N) ->
  404. receive
  405. {gun_data, ConnPid, Ref, nofin, Data} ->
  406. do_dir_large_file(ConnPid, Ref, N + byte_size(Data));
  407. {gun_data, ConnPid, Ref, fin, Data} ->
  408. {ok, N + byte_size(Data)};
  409. {gun_error, ConnPid, Ref, Reason} ->
  410. {error, Reason};
  411. {gun_error, ConnPid, Reason} ->
  412. {error, Reason}
  413. after 5000 ->
  414. {error, timeout}
  415. end.
  416. dir_text(Config) ->
  417. doc("Get a .txt file. The extension is unknown by default."),
  418. {200, Headers, <<"Timeless space.\n">>}
  419. = do_get(config(prefix, Config) ++ "/plain.txt", Config),
  420. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  421. ok.
  422. dir_unknown(Config) ->
  423. doc("Get a file with no extension."),
  424. {200, Headers, <<"File with no extension.\n">>}
  425. = do_get(config(prefix, Config) ++ "/unknown", Config),
  426. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  427. ok.
  428. etag_crash(Config) ->
  429. doc("Get a file with a crashing etag function."),
  430. {500, _, _} = do_get("/etag/crash", Config),
  431. ok.
  432. etag_custom(Config) ->
  433. doc("Get a file with custom Etag function and make sure it is used."),
  434. {200, Headers, _} = do_get("/etag/custom", Config),
  435. {_, <<"\"etag\"">>} = lists:keyfind(<<"etag">>, 1, Headers),
  436. ok.
  437. etag_default(Config) ->
  438. doc("Get a file twice and make sure the Etag matches."),
  439. {200, Headers1, _} = do_get("/dir/style.css", Config),
  440. {200, Headers2, _} = do_get("/dir/style.css", Config),
  441. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers1),
  442. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers2),
  443. ok.
  444. etag_default_change(Config) ->
  445. doc("Get a file, modify it, get it again and make sure the Etag doesn't match."),
  446. {200, Headers1, _} = do_get("/dir/index.html", Config),
  447. {_, Etag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  448. ok = file:change_time(config(static_dir, Config) ++ "/index.html",
  449. {{config(port, Config), 1, 1}, {1, 1, 1}}),
  450. {200, Headers2, _} = do_get("/dir/index.html", Config),
  451. {_, Etag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  452. true = Etag1 =/= Etag2,
  453. ok.
  454. etag_disable(Config) ->
  455. doc("Get a file with disabled Etag and make sure no Etag is provided."),
  456. {200, Headers, _} = do_get("/etag/disable", Config),
  457. false = lists:keyfind(<<"etag">>, 1, Headers),
  458. ok.
  459. file(Config) ->
  460. doc("Get a file with hardcoded route."),
  461. {200, Headers, <<"body{color:red}\n">>} = do_get("/file/style.css", Config),
  462. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  463. ok.
  464. if_match(Config) ->
  465. doc("Get a file with If-Match matching."),
  466. {200, _, _} = do_get("/etag/custom", [
  467. {<<"if-match">>, <<"\"etag\"">>}
  468. ], Config),
  469. ok.
  470. if_match_fail(Config) ->
  471. doc("Get a file with If-Match not matching."),
  472. {412, _, _} = do_get("/etag/custom", [
  473. {<<"if-match">>, <<"\"invalid\"">>}
  474. ], Config),
  475. ok.
  476. if_match_invalid(Config) ->
  477. doc("Try to get a file with an invalid If-Match header."),
  478. {400, _, _} = do_get("/etag/custom", [
  479. {<<"if-match">>, <<"bad input">>}
  480. ], Config),
  481. ok.
  482. if_match_list(Config) ->
  483. doc("Get a file with If-Match matching."),
  484. {200, _, _} = do_get("/etag/custom", [
  485. {<<"if-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  486. ], Config),
  487. ok.
  488. if_match_list_fail(Config) ->
  489. doc("Get a file with If-Match not matching."),
  490. {412, _, _} = do_get("/etag/custom", [
  491. {<<"if-match">>, <<"\"invalid\", W/\"etag\", \"cowboy\"">>}
  492. ], Config),
  493. ok.
  494. if_match_weak(Config) ->
  495. doc("Try to get a file with a weak If-Match header."),
  496. {412, _, _} = do_get("/etag/custom", [
  497. {<<"if-match">>, <<"W/\"etag\"">>}
  498. ], Config),
  499. ok.
  500. if_match_wildcard(Config) ->
  501. doc("Get a file with a wildcard If-Match."),
  502. {200, _, _} = do_get("/etag/custom", [
  503. {<<"if-match">>, <<"*">>}
  504. ], Config),
  505. ok.
  506. if_modified_since(Config) ->
  507. doc("Get a file with If-Modified-Since in the past."),
  508. {200, _, _} = do_get("/etag/custom", [
  509. {<<"if-modified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  510. ], Config),
  511. ok.
  512. if_modified_since_fail(Config) ->
  513. doc("Get a file with If-Modified-Since equal to file modification time."),
  514. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  515. {304, _, _} = do_get("/etag/custom", [
  516. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)}
  517. ], Config),
  518. ok.
  519. if_modified_since_future(Config) ->
  520. doc("Get a file with If-Modified-Since in the future."),
  521. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  522. {200, _, _} = do_get("/etag/custom", [
  523. {<<"if-modified-since">>, [
  524. <<"Sat, 29 Oct ">>,
  525. integer_to_binary(Year + 1),
  526. <<" 19:43:31 GMT">>]}
  527. ], Config),
  528. ok.
  529. if_modified_since_if_none_match(Config) ->
  530. doc("Get a file with both If-Modified-Since and If-None-Match headers."
  531. "If-None-Match takes precedence and If-Modified-Since is ignored. (RFC7232 3.3)"),
  532. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  533. {200, _, _} = do_get("/etag/custom", [
  534. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)},
  535. {<<"if-none-match">>, <<"\"not-etag\"">>}
  536. ], Config),
  537. ok.
  538. if_modified_since_invalid(Config) ->
  539. doc("Get a file with an invalid If-Modified-Since header."),
  540. {200, _, _} = do_get("/etag/custom", [
  541. {<<"if-modified-since">>, <<"\"not a date\"">>}
  542. ], Config),
  543. ok.
  544. if_none_match(Config) ->
  545. doc("Get a file with If-None-Match not matching."),
  546. {200, _, _} = do_get("/etag/custom", [
  547. {<<"if-none-match">>, <<"\"not-etag\"">>}
  548. ], Config),
  549. ok.
  550. if_none_match_fail(Config) ->
  551. doc("Get a file with If-None-Match matching."),
  552. {304, _, _} = do_get("/etag/custom", [
  553. {<<"if-none-match">>, <<"\"etag\"">>}
  554. ], Config),
  555. ok.
  556. if_none_match_invalid(Config) ->
  557. doc("Try to get a file with an invalid If-None-Match header."),
  558. {400, _, _} = do_get("/etag/custom", [
  559. {<<"if-none-match">>, <<"bad input">>}
  560. ], Config),
  561. ok.
  562. if_none_match_list(Config) ->
  563. doc("Get a file with If-None-Match not matching."),
  564. {200, _, _} = do_get("/etag/custom", [
  565. {<<"if-none-match">>, <<"\"invalid\", W/\"not-etag\", \"cowboy\"">>}
  566. ], Config),
  567. ok.
  568. if_none_match_list_fail(Config) ->
  569. doc("Get a file with If-None-Match matching."),
  570. {304, _, _} = do_get("/etag/custom", [
  571. {<<"if-none-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  572. ], Config),
  573. ok.
  574. if_none_match_weak(Config) ->
  575. doc("Try to get a file with a weak If-None-Match header matching."),
  576. {304, _, _} = do_get("/etag/custom", [
  577. {<<"if-none-match">>, <<"W/\"etag\"">>}
  578. ], Config),
  579. ok.
  580. if_none_match_wildcard(Config) ->
  581. doc("Try to get a file with a wildcard If-None-Match."),
  582. {304, _, _} = do_get("/etag/custom", [
  583. {<<"if-none-match">>, <<"*">>}
  584. ], Config),
  585. ok.
  586. if_unmodified_since(Config) ->
  587. doc("Get a file with If-Unmodified-Since equal to file modification time."),
  588. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  589. {200, _, _} = do_get("/etag/custom", [
  590. {<<"if-unmodified-since">>, httpd_util:rfc1123_date(LastModified)}
  591. ], Config),
  592. ok.
  593. if_unmodified_since_fail(Config) ->
  594. doc("Get a file with If-Unmodified-Since in the past."),
  595. {412, _, _} = do_get("/etag/custom", [
  596. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  597. ], Config),
  598. ok.
  599. if_unmodified_since_future(Config) ->
  600. doc("Get a file with If-Unmodified-Since in the future."),
  601. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  602. {200, _, _} = do_get("/etag/custom", [
  603. {<<"if-unmodified-since">>, [
  604. <<"Sat, 29 Oct ">>,
  605. integer_to_binary(Year + 1),
  606. <<" 19:43:31 GMT">>]}
  607. ], Config),
  608. ok.
  609. if_unmodified_since_if_match(Config) ->
  610. doc("Get a file with both If-Unmodified-Since and If-Match headers."
  611. "If-Match takes precedence and If-Unmodified-Since is ignored. (RFC7232 3.4)"),
  612. {200, _, _} = do_get("/etag/custom", [
  613. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>},
  614. {<<"if-match">>, <<"\"etag\"">>}
  615. ], Config),
  616. ok.
  617. if_unmodified_since_invalid(Config) ->
  618. doc("Get a file with an invalid If-Unmodified-Since header."),
  619. {200, _, _} = do_get("/etag/custom", [
  620. {<<"if-unmodified-since">>, <<"\"not a date\"">>}
  621. ], Config),
  622. ok.
  623. index_file(Config) ->
  624. doc("Get an index file."),
  625. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index", Config),
  626. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  627. ok.
  628. index_file_slash(Config) ->
  629. doc("Get an index file with extra slash."),
  630. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index/", Config),
  631. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  632. ok.
  633. last_modified(Config) ->
  634. doc("Get a file, modify it, get it again and make sure Last-Modified changes."),
  635. {200, Headers1, _} = do_get("/dir/file.cowboy", Config),
  636. {_, LastModified1} = lists:keyfind(<<"last-modified">>, 1, Headers1),
  637. ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy",
  638. {{config(port, Config), 1, 1}, {1, 1, 1}}),
  639. {200, Headers2, _} = do_get("/dir/file.cowboy", Config),
  640. {_, LastModified2} = lists:keyfind(<<"last-modified">>, 1, Headers2),
  641. true = LastModified1 =/= LastModified2,
  642. ok.
  643. mime_all_cowboy(Config) ->
  644. doc("Get a .cowboy file. The extension is unknown."),
  645. {200, Headers, _} = do_get("/mime/all/file.cowboy", Config),
  646. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  647. ok.
  648. mime_all_css(Config) ->
  649. doc("Get a .css file."),
  650. {200, Headers, _} = do_get("/mime/all/style.css", Config),
  651. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  652. ok.
  653. mime_all_txt(Config) ->
  654. doc("Get a .txt file."),
  655. {200, Headers, _} = do_get("/mime/all/plain.txt", Config),
  656. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  657. ok.
  658. mime_crash(Config) ->
  659. doc("Get a file with a crashing mimetype function."),
  660. {500, _, _} = do_get("/mime/crash/style.css", Config),
  661. ok.
  662. mime_custom_cowboy(Config) ->
  663. doc("Get a .cowboy file."),
  664. {200, Headers, _} = do_get("/mime/custom/file.cowboy", Config),
  665. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  666. ok.
  667. mime_custom_css(Config) ->
  668. doc("Get a .css file. The extension is unknown."),
  669. {200, Headers, _} = do_get("/mime/custom/style.css", Config),
  670. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  671. ok.
  672. mime_custom_txt(Config) ->
  673. doc("Get a .txt file."),
  674. {200, Headers, _} = do_get("/mime/custom/plain.txt", Config),
  675. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  676. ok.
  677. mime_hardcode_binary(Config) ->
  678. doc("Get a .cowboy file with hardcoded route and media type in binary form."),
  679. {200, Headers, _} = do_get("/mime/hardcode/binary-form", Config),
  680. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  681. ok.
  682. mime_hardcode_tuple(Config) ->
  683. doc("Get a .cowboy file with hardcoded route and media type in tuple form."),
  684. {200, Headers, _} = do_get("/mime/hardcode/tuple-form", Config),
  685. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  686. ok.
  687. charset_crash(Config) ->
  688. doc("Get a file with a crashing charset function."),
  689. {500, _, _} = do_get("/charset/crash/style.css", Config),
  690. ok.
  691. charset_custom_cowboy(Config) ->
  692. doc("Get a .cowboy file."),
  693. {200, Headers, _} = do_get("/charset/custom/file.cowboy", Config),
  694. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  695. ok.
  696. charset_custom_css(Config) ->
  697. doc("Get a .css file."),
  698. {200, Headers, _} = do_get("/charset/custom/style.css", Config),
  699. {_, <<"text/css; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  700. ok.
  701. charset_custom_html(Config) ->
  702. doc("Get a .html file."),
  703. {200, Headers, _} = do_get("/charset/custom/index.html", Config),
  704. {_, <<"text/html; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  705. ok.
  706. charset_hardcode_binary(Config) ->
  707. doc("Get a .html file with hardcoded route and charset."),
  708. {200, Headers, _} = do_get("/charset/hardcode", Config),
  709. {_, <<"text/html; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  710. ok.
  711. priv_dir_in_ez_archive(Config) ->
  712. doc("Get a file from a priv_dir stored in Erlang application .ez archive."),
  713. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_dir/index.html", Config),
  714. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  715. ok.
  716. priv_file(Config) ->
  717. doc("Get a file with hardcoded route."),
  718. {200, Headers, <<"body{color:red}\n">>} = do_get("/priv_file/style.css", Config),
  719. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  720. ok.
  721. priv_file_in_ez_archive(Config) ->
  722. doc("Get a file stored in Erlang application .ez archive."),
  723. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_file/index.html", Config),
  724. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  725. ok.
  726. range_request(Config) ->
  727. doc("Confirm that range requests are enabled."),
  728. {206, Headers, <<"less space.\n">>} = do_get("/dir/plain.txt",
  729. [{<<"range">>, <<"bytes=4-">>}], Config),
  730. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  731. {_, <<"bytes 4-15/16">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  732. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  733. ok.
  734. unicode_basic_latin(Config) ->
  735. doc("Get a file with non-urlencoded characters from Unicode Basic Latin block."),
  736. _ = [case do_get("/char/" ++ [C], Config) of
  737. {200, _, << C >>} -> ok;
  738. Error -> exit({error, C, Error})
  739. end || C <-
  740. %% Excluding the dot which has a special meaning in URLs
  741. %% when they are the only content in a path segment,
  742. %% and is tested as part of filenames in other test cases.
  743. "abcdefghijklmnopqrstuvwxyz"
  744. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  745. "0123456789"
  746. ":@-_~!$&'()*+,;="
  747. ],
  748. ok.
  749. unicode_basic_error(Config) ->
  750. doc("Try to get a file with invalid non-urlencoded characters from Unicode Basic Latin block."),
  751. Exclude = case config(protocol, Config) of
  752. %% Some characters trigger different errors in HTTP/1.1
  753. %% because they are used for the protocol.
  754. %%
  755. %% # and ? indicate fragment and query components
  756. %% and are therefore not part of the path.
  757. http -> "\r\s#?";
  758. http2 -> "#?"
  759. end,
  760. _ = [case do_get("/char/" ++ [C], Config) of
  761. {400, _, _} -> ok;
  762. Error -> exit({error, C, Error})
  763. end || C <- (config(chars, Config) -- Exclude) --
  764. "abcdefghijklmnopqrstuvwxyz"
  765. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  766. "0123456789"
  767. ":@-_~!$&'()*+,;="
  768. ],
  769. ok.
  770. unicode_basic_latin_urlencoded(Config) ->
  771. doc("Get a file with urlencoded characters from Unicode Basic Latin block."),
  772. _ = [case do_get(lists:flatten(["/char/%", io_lib:format("~2.16.0b", [C])]), Config) of
  773. {200, _, << C >>} -> ok;
  774. Error -> exit({error, C, Error})
  775. end || C <- config(chars, Config)],
  776. ok.
  777. unknown_option(Config) ->
  778. doc("Get a file configured with unknown extra options."),
  779. {200, Headers, <<"body{color:red}\n">>} = do_get("/unknown/option", Config),
  780. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  781. ok.