static_handler_SUITE.erl 31 KB

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