cowboy_static.erl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. %% Copyright (c) 2011, Magnus Klaar <magnus.klaar@gmail.com>
  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. %% @doc Static resource handler.
  15. %%
  16. %% This built in HTTP handler provides a simple file serving capability for
  17. %% cowboy applications. It should be considered an experimental feature because
  18. %% of it's dependency on the experimental REST handler. It's recommended to be
  19. %% used for small or temporary environments where it is not preferrable to set
  20. %% up a second server just to serve files.
  21. %%
  22. %% If this handler is used the Erlang node running the cowboy application must
  23. %% be configured to use an async thread pool. This is configured by adding the
  24. %% `+A $POOL_SIZE' argument to the `erl' command used to start the node. See
  25. %% <a href="http://erlang.org/pipermail/erlang-bugs/2012-January/002720.html">
  26. %% this reply</a> from the OTP team to erlang-bugs
  27. %%
  28. %% == Base configuration ==
  29. %%
  30. %% The handler must be configured with a request path prefix to serve files
  31. %% under and the path to a directory to read files from. The request path prefix
  32. %% is defined in the path pattern of the cowboy dispatch rule for the handler.
  33. %% The request path pattern must end with a `...' token.
  34. %% The directory path can be set to either an absolute or relative path in the
  35. %% form of a list or binary string representation of a file system path. A list
  36. %% of binary path segments, as is used throughout cowboy, is also a valid
  37. %% directory path.
  38. %%
  39. %% The directory path can also be set to a relative path within the `priv/'
  40. %% directory of an application. This is configured by setting the value of the
  41. %% directory option to a tuple of the form `{priv_dir, Application, Relpath}'.
  42. %%
  43. %% ==== Examples ====
  44. %% ```
  45. %% %% Serve files from /var/www/ under http://example.com/static/
  46. %% {[<<"static">>, '...'], cowboy_static,
  47. %% [{directory, "/var/www"}]}
  48. %%
  49. %% %% Serve files from the current working directory under http://example.com/static/
  50. %% {[<<"static">>, '...'], cowboy_static,
  51. %% [{directory, <<"./">>}]}
  52. %%
  53. %% %% Serve files from cowboy/priv/www under http://example.com/
  54. %% {['...'], cowboy_static,
  55. %% [{directory, {priv_dir, cowboy, [<<"www">>]}}]}
  56. %% '''
  57. %%
  58. %% == Content type configuration ==
  59. %%
  60. %% By default the content type of all static resources will be set to
  61. %% `application/octet-stream'. This can be overriden by supplying a list
  62. %% of filename extension to mimetypes pairs in the `mimetypes' option.
  63. %% The filename extension should be a binary string including the leading dot.
  64. %% The mimetypes must be of a type that the `cowboy_rest' protocol can
  65. %% handle.
  66. %%
  67. %% The <a href="https://github.com/spawngrid/mimetypes">spawngrid/mimetypes</a>
  68. %% application, or an arbitrary function accepting the path to the file being
  69. %% served, can also be used to generate the list of content types for a static
  70. %% file resource. The function used must accept an additional argument after
  71. %% the file path argument.
  72. %%
  73. %% ==== Example ====
  74. %% ```
  75. %% %% Use a static list of content types.
  76. %% {[<<"static">>, '...'], cowboy_static,
  77. %% [{directory, {priv_dir, cowboy, []}},
  78. %% {mimetypes, [
  79. %% {<<".css">>, [<<"text/css">>]},
  80. %% {<<".js">>, [<<"application/javascript">>]}]}]}
  81. %%
  82. %% %% Use the default database in the mimetypes application.
  83. %% {[<<"static">>, '...'], cowboy_static,
  84. %% [{directory, {priv_dir, cowboy, []}},
  85. %% {mimetypes, {fun mimetypes:path_to_mimes/2, default}}]}
  86. %% '''
  87. %%
  88. %% == ETag Header Function ==
  89. %%
  90. %% The default behaviour of the static file handler is to not generate ETag
  91. %% headers. This is because generating ETag headers based on file metadata
  92. %% causes different servers in a cluster to generate different ETag values for
  93. %% the same file unless the metadata is also synced. Generating strong ETags
  94. %% based on the contents of a file is currently out of scope for this module.
  95. %%
  96. %% The default behaviour can be overridden to generate an ETag header based on
  97. %% a combination of the file path, file size, inode and mtime values. If the
  98. %% option value is a non-empty list of attribute names tagged with `attributes'
  99. %% a hex encoded checksum of each attribute specified is included in the value
  100. %% of the the ETag header. If the list of attribute names is empty no ETag
  101. %% header is generated.
  102. %%
  103. %% If a strong ETag is required a user defined function for generating the
  104. %% header value can be supplied. The function must accept a proplist of the
  105. %% file attributes as the first argument and a second argument containing any
  106. %% additional data that the function requires. The function must return a term
  107. %% of the type `{weak | strong, binary()}' or `undefined'.
  108. %%
  109. %% ==== Examples ====
  110. %% ```
  111. %% %% A value of default is equal to not specifying the option.
  112. %% {[<<"static">>, '...'], cowboy_static,
  113. %% [{directory, {priv_dir, cowboy, []}},
  114. %% {etag, default}]}
  115. %%
  116. %% %% Use all avaliable ETag function arguments to generate a header value.
  117. %% {[<<"static">>, '...'], cowboy_static,
  118. %% [{directory, {priv_dir, cowboy, []}},
  119. %% {etag, {attributes, [filepath, filesize, inode, mtime]}}]}
  120. %%
  121. %% %% Use a user defined function to generate a strong ETag header value.
  122. %% {[<<"static">>, '...'], cowboy_static,
  123. %% [{directory, {priv_dir, cowboy, []}},
  124. %% {etag, {fun generate_strong_etag/2, strong_etag_extra}}]}
  125. %%
  126. %% generate_strong_etag(Arguments, strong_etag_extra) ->
  127. %% {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  128. %% {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  129. %% {_, _INode} = lists:keyfind(inode, 1, Arguments),
  130. %% {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  131. %% ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  132. %% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  133. %% {strong, iolist_to_binary(Checksum)}.
  134. %% '''
  135. %%
  136. %% == File configuration ==
  137. %%
  138. %% If the file system path being served does not share a common suffix with
  139. %% the request path it is possible to override the file path using the `file'
  140. %% option. The value of this option is expected to be a relative path within
  141. %% the static file directory specified using the `directory' option.
  142. %% The path must be in the form of a list or binary string representation of a
  143. %% file system path. A list of binary path segments, as is used throughout
  144. %% cowboy, is also a valid.
  145. %%
  146. %% When the `file' option is used the same file will be served for all requests
  147. %% matching the cowboy dispatch fule for the handler. It is not necessary to
  148. %% end the request path pattern with a `...' token because the request path
  149. %% will not be used to determine which file to serve from the static directory.
  150. %%
  151. %% === Examples ===
  152. %%
  153. %% ```
  154. %% %% Serve cowboy/priv/www/index.html as http://example.com/
  155. %% {[], cowboy_static,
  156. %% [{directory, {priv_dir, cowboy, [<<"www">>]}}
  157. %% {file, <<"index.html">>}]}
  158. %%
  159. %% %% Serve cowboy/priv/www/page.html under http://example.com/*/page
  160. %% {['_', <<"page">>], cowboy_static,
  161. %% [{directory, {priv_dir, cowboy, [<<"www">>]}}
  162. %% {file, <<"page.html">>}]}.
  163. %%
  164. %% %% Always serve cowboy/priv/www/other.html under http://example.com/other
  165. %% {[<<"other">>, '...'], cowboy_static,
  166. %% [{directory, {priv_dir, cowboy, [<<"www">>]}}
  167. %% {file, "other.html"}]}
  168. %% '''
  169. -module(cowboy_static).
  170. %% include files
  171. -include_lib("kernel/include/file.hrl").
  172. %% cowboy_protocol callbacks
  173. -export([init/3]).
  174. %% cowboy_rest callbacks
  175. -export([rest_init/2]).
  176. -export([allowed_methods/2]).
  177. -export([malformed_request/2]).
  178. -export([resource_exists/2]).
  179. -export([forbidden/2]).
  180. -export([last_modified/2]).
  181. -export([generate_etag/2]).
  182. -export([content_types_provided/2]).
  183. -export([file_contents/2]).
  184. %% internal
  185. -export([path_to_mimetypes/2]).
  186. %% types
  187. -type dirpath() :: string() | binary() | [binary()].
  188. -type dirspec() :: dirpath() | {priv, atom(), dirpath()}.
  189. -type mimedef() :: {binary(), binary(), [{binary(), binary()}]}.
  190. -type etagarg() :: {filepath, binary()} | {mtime, calendar:datetime()}
  191. | {inode, non_neg_integer()} | {filesize, non_neg_integer()}.
  192. %% handler state
  193. -record(state, {
  194. filepath :: binary() | error,
  195. fileinfo :: {ok, #file_info{}} | {error, _} | error,
  196. mimetypes :: {fun((binary(), T) -> [mimedef()]), T} | undefined,
  197. etag_fun :: {fun(([etagarg()], T) ->
  198. undefined | {strong | weak, binary()}), T}}).
  199. %% @private Upgrade from HTTP handler to REST handler.
  200. init({_Transport, http}, _Req, _Opts) ->
  201. {upgrade, protocol, cowboy_rest}.
  202. %% @private Set up initial state of REST handler.
  203. -spec rest_init(Req, list()) -> {ok, Req, #state{}} when Req::cowboy_req:req().
  204. rest_init(Req, Opts) ->
  205. Directory = proplists:get_value(directory, Opts),
  206. Directory1 = directory_path(Directory),
  207. Mimetypes = proplists:get_value(mimetypes, Opts, []),
  208. Mimetypes1 = case Mimetypes of
  209. {{M, F}, E} -> {fun M:F/2, E};
  210. {_, _} -> Mimetypes;
  211. [] -> {fun path_to_mimetypes/2, []};
  212. [_|_] -> {fun path_to_mimetypes/2, Mimetypes}
  213. end,
  214. ETagFunction = case proplists:get_value(etag, Opts) of
  215. default -> {fun no_etag_function/2, undefined};
  216. undefined -> {fun no_etag_function/2, undefined};
  217. {attributes, []} -> {fun no_etag_function/2, undefined};
  218. {attributes, Attrs} -> {fun attr_etag_function/2, Attrs};
  219. {_, _}=ETagFunction1 -> ETagFunction1
  220. end,
  221. {Filepath, Req1} = case lists:keyfind(file, 1, Opts) of
  222. {_, Filepath2} -> {filepath_path(Filepath2), Req};
  223. false -> cowboy_req:path_info(Req)
  224. end,
  225. State = case check_path(Filepath) of
  226. error ->
  227. #state{filepath=error, fileinfo=error, mimetypes=undefined,
  228. etag_fun=ETagFunction};
  229. ok ->
  230. Filepath1 = join_paths(Directory1, Filepath),
  231. Fileinfo = file:read_file_info(Filepath1),
  232. #state{filepath=Filepath1, fileinfo=Fileinfo, mimetypes=Mimetypes1,
  233. etag_fun=ETagFunction}
  234. end,
  235. {ok, Req1, State}.
  236. %% @private Only allow GET and HEAD requests on files.
  237. -spec allowed_methods(Req, #state{})
  238. -> {[binary()], Req, #state{}} when Req::cowboy_req:req().
  239. allowed_methods(Req, State) ->
  240. {[<<"GET">>, <<"HEAD">>], Req, State}.
  241. %% @private
  242. -spec malformed_request(Req, #state{})
  243. -> {boolean(), Req, #state{}} when Req::cowboy_req:req().
  244. malformed_request(Req, #state{filepath=error}=State) ->
  245. {true, Req, State};
  246. malformed_request(Req, State) ->
  247. {false, Req, State}.
  248. %% @private Check if the resource exists under the document root.
  249. -spec resource_exists(Req, #state{})
  250. -> {boolean(), Req, #state{}} when Req::cowboy_req:req().
  251. resource_exists(Req, #state{fileinfo={error, _}}=State) ->
  252. {false, Req, State};
  253. resource_exists(Req, #state{fileinfo={ok, Fileinfo}}=State) ->
  254. {Fileinfo#file_info.type =:= regular, Req, State}.
  255. %% @private
  256. %% Access to a file resource is forbidden if it exists and the local node does
  257. %% not have permission to read it. Directory listings are always forbidden.
  258. -spec forbidden(Req, #state{})
  259. -> {boolean(), Req, #state{}} when Req::cowboy_req:req().
  260. forbidden(Req, #state{fileinfo={_, #file_info{type=directory}}}=State) ->
  261. {true, Req, State};
  262. forbidden(Req, #state{fileinfo={error, eacces}}=State) ->
  263. {true, Req, State};
  264. forbidden(Req, #state{fileinfo={error, _}}=State) ->
  265. {false, Req, State};
  266. forbidden(Req, #state{fileinfo={ok, #file_info{access=Access}}}=State) ->
  267. {not (Access =:= read orelse Access =:= read_write), Req, State}.
  268. %% @private Read the time a file system system object was last modified.
  269. -spec last_modified(Req, #state{})
  270. -> {calendar:datetime(), Req, #state{}} when Req::cowboy_req:req().
  271. last_modified(Req, #state{fileinfo={ok, #file_info{mtime=Modified}}}=State) ->
  272. {Modified, Req, State}.
  273. %% @private Generate the ETag header value for this file.
  274. %% The ETag header value is only generated if the resource is a file that
  275. %% exists in document root.
  276. -spec generate_etag(Req, #state{})
  277. -> {undefined | binary(), Req, #state{}} when Req::cowboy_req:req().
  278. generate_etag(Req, #state{fileinfo={_, #file_info{type=regular, inode=INode,
  279. mtime=Modified, size=Filesize}}, filepath=Filepath,
  280. etag_fun={ETagFun, ETagData}}=State) ->
  281. ETagArgs = [
  282. {filepath, Filepath}, {filesize, Filesize},
  283. {inode, INode}, {mtime, Modified}],
  284. {ETagFun(ETagArgs, ETagData), Req, State};
  285. generate_etag(Req, State) ->
  286. {undefined, Req, State}.
  287. %% @private Return the content type of a file.
  288. -spec content_types_provided(cowboy_req:req(), #state{}) -> tuple().
  289. content_types_provided(Req, #state{filepath=Filepath,
  290. mimetypes={MimetypesFun, MimetypesData}}=State) ->
  291. Mimetypes = [{T, file_contents}
  292. || T <- MimetypesFun(Filepath, MimetypesData)],
  293. {Mimetypes, Req, State}.
  294. %% @private Return a function that writes a file directly to the socket.
  295. -spec file_contents(cowboy_req:req(), #state{}) -> tuple().
  296. file_contents(Req, #state{filepath=Filepath,
  297. fileinfo={ok, #file_info{size=Filesize}}}=State) ->
  298. Writefile = fun(Socket, Transport) ->
  299. {ok, _} = Transport:sendfile(Socket, Filepath),
  300. ok
  301. end,
  302. {{stream, Filesize, Writefile}, Req, State}.
  303. -spec directory_path(dirspec()) -> dirpath().
  304. directory_path({priv_dir, App, []}) ->
  305. priv_dir_path(App);
  306. directory_path({priv_dir, App, [H|_]=Path}) when is_integer(H) ->
  307. filename:join(priv_dir_path(App), Path);
  308. directory_path({priv_dir, App, [H|_]=Path}) when is_binary(H) ->
  309. filename:join(filename:split(priv_dir_path(App)) ++ Path);
  310. directory_path({priv_dir, App, Path}) when is_binary(Path) ->
  311. filename:join(priv_dir_path(App), Path);
  312. directory_path(Path) ->
  313. Path.
  314. %% @private Ensure that a file path is of the same type as a request path.
  315. -spec filepath_path(dirpath()) -> Path::[binary()].
  316. filepath_path([H|_]=Path) when is_integer(H) ->
  317. filename:split(list_to_binary(Path));
  318. filepath_path(Path) when is_binary(Path) ->
  319. filename:split(Path);
  320. filepath_path([H|_]=Path) when is_binary(H) ->
  321. Path.
  322. %% @private Validate a request path for unsafe characters.
  323. %% There is no way to escape special characters in a filesystem path.
  324. -spec check_path(Path::[binary()]) -> ok | error.
  325. check_path([]) -> ok;
  326. check_path([<<"">>|_T]) -> error;
  327. check_path([<<".">>|_T]) -> error;
  328. check_path([<<"..">>|_T]) -> error;
  329. check_path([H|T]) ->
  330. case binary:match(H, <<"/">>) of
  331. {_, _} -> error;
  332. nomatch -> check_path(T)
  333. end.
  334. %% @private Join the the directory and request paths.
  335. -spec join_paths(dirpath(), [binary()]) -> binary().
  336. join_paths([H|_]=Dirpath, Filepath) when is_integer(H) ->
  337. filename:join(filename:split(Dirpath) ++ Filepath);
  338. join_paths([H|_]=Dirpath, Filepath) when is_binary(H) ->
  339. filename:join(Dirpath ++ Filepath);
  340. join_paths(Dirpath, Filepath) when is_binary(Dirpath) ->
  341. filename:join([Dirpath] ++ Filepath);
  342. join_paths([], Filepath) ->
  343. filename:join(Filepath).
  344. %% @private Return the path to the priv/ directory of an application.
  345. -spec priv_dir_path(atom()) -> string().
  346. priv_dir_path(App) ->
  347. case code:priv_dir(App) of
  348. {error, bad_name} -> priv_dir_mod(App);
  349. Dir -> Dir
  350. end.
  351. -spec priv_dir_mod(atom()) -> string().
  352. priv_dir_mod(Mod) ->
  353. case code:which(Mod) of
  354. File when not is_list(File) -> "../priv";
  355. File -> filename:join([filename:dirname(File),"../priv"])
  356. end.
  357. %% @private Use application/octet-stream as the default mimetype.
  358. %% If a list of extension - mimetype pairs are provided as the mimetypes
  359. %% an attempt to find the mimetype using the file extension. If no match
  360. %% is found the default mimetype is returned.
  361. -spec path_to_mimetypes(binary(), [{binary(), [mimedef()]}]) ->
  362. [mimedef()].
  363. path_to_mimetypes(Filepath, Extensions) when is_binary(Filepath) ->
  364. Ext = filename:extension(Filepath),
  365. case Ext of
  366. <<>> -> default_mimetype();
  367. _Ext -> path_to_mimetypes_(Ext, Extensions)
  368. end.
  369. -spec path_to_mimetypes_(binary(), [{binary(), [mimedef()]}]) -> [mimedef()].
  370. path_to_mimetypes_(Ext, Extensions) ->
  371. case lists:keyfind(cowboy_bstr:to_lower(Ext), 1, Extensions) of
  372. {_, MTs} -> MTs;
  373. _Unknown -> default_mimetype()
  374. end.
  375. -spec default_mimetype() -> [mimedef()].
  376. default_mimetype() ->
  377. [{<<"application">>, <<"octet-stream">>, []}].
  378. %% @private Do not send ETag headers in the default configuration.
  379. -spec no_etag_function([etagarg()], undefined) -> undefined.
  380. no_etag_function(_Args, undefined) ->
  381. undefined.
  382. %% @private A simple alternative is to send an ETag based on file attributes.
  383. -type fileattr() :: filepath | filesize | mtime | inode.
  384. -spec attr_etag_function([etagarg()], [fileattr()]) -> {strong, binary()}.
  385. attr_etag_function(Args, Attrs) ->
  386. [[_|H]|T] = [begin
  387. {_,Pair} = {_,{_,_}} = {Attr,lists:keyfind(Attr, 1, Args)},
  388. [$-|integer_to_list(erlang:phash2(Pair, 1 bsl 32), 16)]
  389. end || Attr <- Attrs],
  390. {strong, list_to_binary([H|T])}.
  391. -ifdef(TEST).
  392. -include_lib("eunit/include/eunit.hrl").
  393. -define(_eq(E, I), ?_assertEqual(E, I)).
  394. check_path_test_() ->
  395. C = fun check_path/1,
  396. [?_eq(error, C([<<>>])),
  397. ?_eq(ok, C([<<"abc">>])),
  398. ?_eq(error, C([<<".">>])),
  399. ?_eq(error, C([<<"..">>])),
  400. ?_eq(error, C([<<"/">>]))
  401. ].
  402. join_paths_test_() ->
  403. P = fun join_paths/2,
  404. [?_eq(<<"a">>, P([], [<<"a">>])),
  405. ?_eq(<<"a/b/c">>, P(<<"a/b">>, [<<"c">>])),
  406. ?_eq(<<"a/b/c">>, P("a/b", [<<"c">>])),
  407. ?_eq(<<"a/b/c">>, P([<<"a">>, <<"b">>], [<<"c">>]))
  408. ].
  409. directory_path_test_() ->
  410. P = fun directory_path/1,
  411. PL = fun(I) -> length(filename:split(P(I))) end,
  412. Base = PL({priv_dir, cowboy, []}),
  413. [?_eq(Base + 1, PL({priv_dir, cowboy, "a"})),
  414. ?_eq(Base + 1, PL({priv_dir, cowboy, <<"a">>})),
  415. ?_eq(Base + 1, PL({priv_dir, cowboy, [<<"a">>]})),
  416. ?_eq(Base + 2, PL({priv_dir, cowboy, "a/b"})),
  417. ?_eq(Base + 2, PL({priv_dir, cowboy, <<"a/b">>})),
  418. ?_eq(Base + 2, PL({priv_dir, cowboy, [<<"a">>, <<"b">>]})),
  419. ?_eq("a/b", P("a/b"))
  420. ].
  421. filepath_path_test_() ->
  422. P = fun filepath_path/1,
  423. [?_eq([<<"a">>], P("a")),
  424. ?_eq([<<"a">>], P(<<"a">>)),
  425. ?_eq([<<"a">>], P([<<"a">>])),
  426. ?_eq([<<"a">>, <<"b">>], P("a/b")),
  427. ?_eq([<<"a">>, <<"b">>], P(<<"a/b">>)),
  428. ?_eq([<<"a">>, <<"b">>], P([<<"a">>, <<"b">>]))
  429. ].
  430. -endif.