cowboy_static.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. %% Copyright (c) 2013-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Magnus Klaar <magnus.klaar@gmail.com>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(cowboy_static).
  16. -export([init/2]).
  17. -export([malformed_request/2]).
  18. -export([forbidden/2]).
  19. -export([content_types_provided/2]).
  20. -export([charsets_provided/2]).
  21. -export([ranges_provided/2]).
  22. -export([resource_exists/2]).
  23. -export([last_modified/2]).
  24. -export([generate_etag/2]).
  25. -export([get_file/2]).
  26. -type extra_charset() :: {charset, module(), function()} | {charset, binary()}.
  27. -type extra_etag() :: {etag, module(), function()} | {etag, false}.
  28. -type extra_mimetypes() :: {mimetypes, module(), function()}
  29. | {mimetypes, binary() | {binary(), binary(), [{binary(), binary()}]}}.
  30. -type extra() :: [extra_charset() | extra_etag() | extra_mimetypes()].
  31. -type opts() :: {file | dir, string() | binary()}
  32. | {file | dir, string() | binary(), extra()}
  33. | {priv_file | priv_dir, atom(), string() | binary()}
  34. | {priv_file | priv_dir, atom(), string() | binary(), extra()}.
  35. -export_type([opts/0]).
  36. -include_lib("kernel/include/file.hrl").
  37. -type state() :: {binary(), {direct | archive, #file_info{}}
  38. | {error, atom()}, extra()}.
  39. %% Resolve the file that will be sent and get its file information.
  40. %% If the handler is configured to manage a directory, check that the
  41. %% requested file is inside the configured directory.
  42. -spec init(Req, opts()) -> {cowboy_rest, Req, error | state()} when Req::cowboy_req:req().
  43. init(Req, {Name, Path}) ->
  44. init_opts(Req, {Name, Path, []});
  45. init(Req, {Name, App, Path})
  46. when Name =:= priv_file; Name =:= priv_dir ->
  47. init_opts(Req, {Name, App, Path, []});
  48. init(Req, Opts) ->
  49. init_opts(Req, Opts).
  50. init_opts(Req, {priv_file, App, Path, Extra}) ->
  51. {PrivPath, HowToAccess} = priv_path(App, Path),
  52. init_info(Req, absname(PrivPath), HowToAccess, Extra);
  53. init_opts(Req, {file, Path, Extra}) ->
  54. init_info(Req, absname(Path), direct, Extra);
  55. init_opts(Req, {priv_dir, App, Path, Extra}) ->
  56. {PrivPath, HowToAccess} = priv_path(App, Path),
  57. init_dir(Req, PrivPath, HowToAccess, Extra);
  58. init_opts(Req, {dir, Path, Extra}) ->
  59. init_dir(Req, Path, direct, Extra).
  60. priv_path(App, Path) ->
  61. case code:priv_dir(App) of
  62. {error, bad_name} ->
  63. error({badarg, "Can't resolve the priv_dir of application "
  64. ++ atom_to_list(App)});
  65. PrivDir when is_list(Path) ->
  66. {
  67. PrivDir ++ "/" ++ Path,
  68. how_to_access_app_priv(PrivDir)
  69. };
  70. PrivDir when is_binary(Path) ->
  71. {
  72. << (list_to_binary(PrivDir))/binary, $/, Path/binary >>,
  73. how_to_access_app_priv(PrivDir)
  74. }
  75. end.
  76. how_to_access_app_priv(PrivDir) ->
  77. %% If the priv directory is not a directory, it must be
  78. %% inside an Erlang application .ez archive. We call
  79. %% how_to_access_app_priv1() to find the corresponding archive.
  80. case filelib:is_dir(PrivDir) of
  81. true -> direct;
  82. false -> how_to_access_app_priv1(PrivDir)
  83. end.
  84. how_to_access_app_priv1(Dir) ->
  85. %% We go "up" by one path component at a time and look for a
  86. %% regular file.
  87. Archive = filename:dirname(Dir),
  88. case Archive of
  89. Dir ->
  90. %% filename:dirname() returned its argument:
  91. %% we reach the root directory. We found no
  92. %% archive so we return 'direct': the given priv
  93. %% directory doesn't exist.
  94. direct;
  95. _ ->
  96. case filelib:is_regular(Archive) of
  97. true -> {archive, Archive};
  98. false -> how_to_access_app_priv1(Archive)
  99. end
  100. end.
  101. absname(Path) when is_list(Path) ->
  102. filename:absname(list_to_binary(Path));
  103. absname(Path) when is_binary(Path) ->
  104. filename:absname(Path).
  105. init_dir(Req, Path, HowToAccess, Extra) when is_list(Path) ->
  106. init_dir(Req, list_to_binary(Path), HowToAccess, Extra);
  107. init_dir(Req, Path, HowToAccess, Extra) ->
  108. Dir = fullpath(filename:absname(Path)),
  109. PathInfo = cowboy_req:path_info(Req),
  110. Filepath = filename:join([Dir|escape_reserved(PathInfo)]),
  111. Len = byte_size(Dir),
  112. case fullpath(Filepath) of
  113. << Dir:Len/binary, $/, _/binary >> ->
  114. init_info(Req, Filepath, HowToAccess, Extra);
  115. << Dir:Len/binary >> ->
  116. init_info(Req, Filepath, HowToAccess, Extra);
  117. _ ->
  118. {cowboy_rest, Req, error}
  119. end.
  120. escape_reserved([]) -> [];
  121. escape_reserved([P|Tail]) -> [escape_reserved(P, <<>>)|escape_reserved(Tail)].
  122. %% We escape the slash found in path segments because
  123. %% a segment corresponds to a directory entry, and
  124. %% therefore those slashes are expected to be part of
  125. %% the directory name.
  126. %%
  127. %% Note that on most systems the slash is prohibited
  128. %% and cannot appear in filenames, which means the
  129. %% requested file will end up being not found.
  130. escape_reserved(<<>>, Acc) ->
  131. Acc;
  132. escape_reserved(<< $/, Rest/bits >>, Acc) ->
  133. escape_reserved(Rest, << Acc/binary, $\\, $/ >>);
  134. escape_reserved(<< C, Rest/bits >>, Acc) ->
  135. escape_reserved(Rest, << Acc/binary, C >>).
  136. fullpath(Path) ->
  137. fullpath(filename:split(Path), []).
  138. fullpath([], Acc) ->
  139. filename:join(lists:reverse(Acc));
  140. fullpath([<<".">>|Tail], Acc) ->
  141. fullpath(Tail, Acc);
  142. fullpath([<<"..">>|Tail], Acc=[_]) ->
  143. fullpath(Tail, Acc);
  144. fullpath([<<"..">>|Tail], [_|Acc]) ->
  145. fullpath(Tail, Acc);
  146. fullpath([Segment|Tail], Acc) ->
  147. fullpath(Tail, [Segment|Acc]).
  148. init_info(Req, Path, HowToAccess, Extra) ->
  149. Info = read_file_info(Path, HowToAccess),
  150. {cowboy_rest, Req, {Path, Info, Extra}}.
  151. read_file_info(Path, direct) ->
  152. case file:read_file_info(Path, [{time, universal}]) of
  153. {ok, Info} -> {direct, Info};
  154. Error -> Error
  155. end;
  156. read_file_info(Path, {archive, Archive}) ->
  157. case file:read_file_info(Archive, [{time, universal}]) of
  158. {ok, ArchiveInfo} ->
  159. %% The Erlang application archive is fine.
  160. %% Now check if the requested file is in that
  161. %% archive. We also need the file_info to merge
  162. %% them with the archive's one.
  163. PathS = binary_to_list(Path),
  164. case erl_prim_loader:read_file_info(PathS) of
  165. {ok, ContainedFileInfo} ->
  166. Info = fix_archived_file_info(
  167. ArchiveInfo,
  168. ContainedFileInfo),
  169. {archive, Info};
  170. error ->
  171. {error, enoent}
  172. end;
  173. Error ->
  174. Error
  175. end.
  176. fix_archived_file_info(ArchiveInfo, ContainedFileInfo) ->
  177. %% We merge the archive and content #file_info because we are
  178. %% interested by the timestamps of the archive, but the type and
  179. %% size of the contained file/directory.
  180. %%
  181. %% We reset the access to 'read', because we won't rewrite the
  182. %% archive.
  183. ArchiveInfo#file_info{
  184. size = ContainedFileInfo#file_info.size,
  185. type = ContainedFileInfo#file_info.type,
  186. access = read
  187. }.
  188. -ifdef(TEST).
  189. fullpath_test_() ->
  190. Tests = [
  191. {<<"/home/cowboy">>, <<"/home/cowboy">>},
  192. {<<"/home/cowboy">>, <<"/home/cowboy/">>},
  193. {<<"/home/cowboy">>, <<"/home/cowboy/./">>},
  194. {<<"/home/cowboy">>, <<"/home/cowboy/./././././.">>},
  195. {<<"/home/cowboy">>, <<"/home/cowboy/abc/..">>},
  196. {<<"/home/cowboy">>, <<"/home/cowboy/abc/../">>},
  197. {<<"/home/cowboy">>, <<"/home/cowboy/abc/./../.">>},
  198. {<<"/">>, <<"/home/cowboy/../../../../../..">>},
  199. {<<"/etc/passwd">>, <<"/home/cowboy/../../etc/passwd">>}
  200. ],
  201. [{P, fun() -> R = fullpath(P) end} || {R, P} <- Tests].
  202. good_path_check_test_() ->
  203. Tests = [
  204. <<"/home/cowboy/file">>,
  205. <<"/home/cowboy/file/">>,
  206. <<"/home/cowboy/./file">>,
  207. <<"/home/cowboy/././././././file">>,
  208. <<"/home/cowboy/abc/../file">>,
  209. <<"/home/cowboy/abc/../file">>,
  210. <<"/home/cowboy/abc/./.././file">>
  211. ],
  212. [{P, fun() ->
  213. case fullpath(P) of
  214. << "/home/cowboy/", _/bits >> -> ok
  215. end
  216. end} || P <- Tests].
  217. bad_path_check_test_() ->
  218. Tests = [
  219. <<"/home/cowboy/../../../../../../file">>,
  220. <<"/home/cowboy/../../etc/passwd">>
  221. ],
  222. [{P, fun() ->
  223. error = case fullpath(P) of
  224. << "/home/cowboy/", _/bits >> -> ok;
  225. _ -> error
  226. end
  227. end} || P <- Tests].
  228. good_path_win32_check_test_() ->
  229. Tests = case os:type() of
  230. {unix, _} ->
  231. [];
  232. {win32, _} ->
  233. [
  234. <<"c:/home/cowboy/file">>,
  235. <<"c:/home/cowboy/file/">>,
  236. <<"c:/home/cowboy/./file">>,
  237. <<"c:/home/cowboy/././././././file">>,
  238. <<"c:/home/cowboy/abc/../file">>,
  239. <<"c:/home/cowboy/abc/../file">>,
  240. <<"c:/home/cowboy/abc/./.././file">>
  241. ]
  242. end,
  243. [{P, fun() ->
  244. case fullpath(P) of
  245. << "c:/home/cowboy/", _/bits >> -> ok
  246. end
  247. end} || P <- Tests].
  248. bad_path_win32_check_test_() ->
  249. Tests = case os:type() of
  250. {unix, _} ->
  251. [];
  252. {win32, _} ->
  253. [
  254. <<"c:/home/cowboy/../../secretfile.bat">>,
  255. <<"c:/home/cowboy/c:/secretfile.bat">>,
  256. <<"c:/home/cowboy/..\\..\\secretfile.bat">>,
  257. <<"c:/home/cowboy/c:\\secretfile.bat">>
  258. ]
  259. end,
  260. [{P, fun() ->
  261. error = case fullpath(P) of
  262. << "c:/home/cowboy/", _/bits >> -> ok;
  263. _ -> error
  264. end
  265. end} || P <- Tests].
  266. -endif.
  267. %% Reject requests that tried to access a file outside
  268. %% the target directory.
  269. -spec malformed_request(Req, State)
  270. -> {boolean(), Req, State}.
  271. malformed_request(Req, State) ->
  272. {State =:= error, Req, State}.
  273. %% Directories, files that can't be accessed at all and
  274. %% files with no read flag are forbidden.
  275. -spec forbidden(Req, State)
  276. -> {boolean(), Req, State}
  277. when State::state().
  278. forbidden(Req, State={_, {_, #file_info{type=directory}}, _}) ->
  279. {true, Req, State};
  280. forbidden(Req, State={_, {error, eacces}, _}) ->
  281. {true, Req, State};
  282. forbidden(Req, State={_, {_, #file_info{access=Access}}, _})
  283. when Access =:= write; Access =:= none ->
  284. {true, Req, State};
  285. forbidden(Req, State) ->
  286. {false, Req, State}.
  287. %% Detect the mimetype of the file.
  288. -spec content_types_provided(Req, State)
  289. -> {[{binary(), get_file}], Req, State}
  290. when State::state().
  291. content_types_provided(Req, State={Path, _, Extra}) when is_list(Extra) ->
  292. case lists:keyfind(mimetypes, 1, Extra) of
  293. false ->
  294. {[{cow_mimetypes:web(Path), get_file}], Req, State};
  295. {mimetypes, Module, Function} ->
  296. {[{Module:Function(Path), get_file}], Req, State};
  297. {mimetypes, Type} ->
  298. {[{Type, get_file}], Req, State}
  299. end.
  300. %% Detect the charset of the file.
  301. -spec charsets_provided(Req, State)
  302. -> {[binary()], Req, State}
  303. when State::state().
  304. charsets_provided(Req, State={Path, _, Extra}) ->
  305. case lists:keyfind(charset, 1, Extra) of
  306. %% We simulate the callback not being exported.
  307. false ->
  308. no_call;
  309. {charset, Module, Function} ->
  310. {[Module:Function(Path)], Req, State};
  311. {charset, Charset} when is_binary(Charset) ->
  312. {[Charset], Req, State}
  313. end.
  314. %% Enable support for range requests.
  315. -spec ranges_provided(Req, State)
  316. -> {[{binary(), auto}], Req, State}
  317. when State::state().
  318. ranges_provided(Req, State) ->
  319. {[{<<"bytes">>, auto}], Req, State}.
  320. %% Assume the resource doesn't exist if it's not a regular file.
  321. -spec resource_exists(Req, State)
  322. -> {boolean(), Req, State}
  323. when State::state().
  324. resource_exists(Req, State={_, {_, #file_info{type=regular}}, _}) ->
  325. {true, Req, State};
  326. resource_exists(Req, State) ->
  327. {false, Req, State}.
  328. %% Generate an etag for the file.
  329. -spec generate_etag(Req, State)
  330. -> {{strong | weak, binary()}, Req, State}
  331. when State::state().
  332. generate_etag(Req, State={Path, {_, #file_info{size=Size, mtime=Mtime}},
  333. Extra}) ->
  334. case lists:keyfind(etag, 1, Extra) of
  335. false ->
  336. {generate_default_etag(Size, Mtime), Req, State};
  337. {etag, Module, Function} ->
  338. {Module:Function(Path, Size, Mtime), Req, State};
  339. {etag, false} ->
  340. {undefined, Req, State}
  341. end.
  342. generate_default_etag(Size, Mtime) ->
  343. {strong, integer_to_binary(erlang:phash2({Size, Mtime}, 16#ffffffff))}.
  344. %% Return the time of last modification of the file.
  345. -spec last_modified(Req, State)
  346. -> {calendar:datetime(), Req, State}
  347. when State::state().
  348. last_modified(Req, State={_, {_, #file_info{mtime=Modified}}, _}) ->
  349. {Modified, Req, State}.
  350. %% Stream the file.
  351. -spec get_file(Req, State)
  352. -> {{sendfile, 0, non_neg_integer(), binary()}, Req, State}
  353. when State::state().
  354. get_file(Req, State={Path, {direct, #file_info{size=Size}}, _}) ->
  355. {{sendfile, 0, Size, Path}, Req, State};
  356. get_file(Req, State={Path, {archive, _}, _}) ->
  357. PathS = binary_to_list(Path),
  358. {ok, Bin, _} = erl_prim_loader:get_file(PathS),
  359. {Bin, Req, State}.