aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_static.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/cowboy_static.erl')
-rw-r--r--src/cowboy_static.erl53
1 files changed, 3 insertions, 50 deletions
diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl
index 724bf33..55d01c7 100644
--- a/src/cowboy_static.erl
+++ b/src/cowboy_static.erl
@@ -158,7 +158,7 @@
%% {file, <<"index.html">>}]}
%%
%% %% Serve cowboy/priv/www/page.html under http://example.com/*/page
-%% {['*', <<"page">>], cowboy_static,
+%% {['_', <<"page">>], cowboy_static,
%% [{directory, {priv_dir, cowboy, [<<"www">>]}}
%% {file, <<"page.html">>}]}.
%%
@@ -217,6 +217,7 @@ rest_init(Req, Opts) ->
Directory1 = directory_path(Directory),
Mimetypes = proplists:get_value(mimetypes, Opts, []),
Mimetypes1 = case Mimetypes of
+ {{M, F}, E} -> {fun M:F/2, E};
{_, _} -> Mimetypes;
[] -> {fun path_to_mimetypes/2, []};
[_|_] -> {fun path_to_mimetypes/2, Mimetypes}
@@ -321,58 +322,10 @@ content_types_provided(Req, #state{filepath=Filepath,
file_contents(Req, #state{filepath=Filepath,
fileinfo={ok, #file_info{size=Filesize}}}=State) ->
{ok, Transport, Socket} = cowboy_req:transport(Req),
- Writefile = content_function(Transport, Socket, Filepath),
+ Writefile = fun() -> Transport:sendfile(Socket, Filepath) end,
{{stream, Filesize, Writefile}, Req, State}.
-%% @private Return a function writing the contents of a file to a socket.
-%% The function returns the number of bytes written to the socket to enable
-%% the calling function to determine if the expected number of bytes were
-%% written to the socket.
--spec content_function(module(), inet:socket(), binary()) ->
- fun(() -> {sent, non_neg_integer()}).
-content_function(Transport, Socket, Filepath) ->
- %% `file:sendfile/2' will only work with the `ranch_tcp'
- %% transport module. SSL or future SPDY transports that require the
- %% content to be encrypted or framed as the content is sent
- %% will use the fallback mechanism.
- case erlang:function_exported(file, sendfile, 2) of
- false ->
- fun() -> sfallback(Transport, Socket, Filepath) end;
- _ when Transport =/= ranch_tcp ->
- fun() -> sfallback(Transport, Socket, Filepath) end;
- true ->
- fun() -> sendfile(Socket, Filepath) end
- end.
-
-
-%% @private Sendfile fallback function.
--spec sfallback(module(), inet:socket(), binary()) -> {sent, non_neg_integer()}.
-sfallback(Transport, Socket, Filepath) ->
- {ok, File} = file:open(Filepath, [read,binary,raw]),
- sfallback(Transport, Socket, File, 0).
-
--spec sfallback(module(), inet:socket(), file:io_device(),
- non_neg_integer()) -> {sent, non_neg_integer()}.
-sfallback(Transport, Socket, File, Sent) ->
- case file:read(File, 16#1FFF) of
- eof ->
- ok = file:close(File),
- {sent, Sent};
- {ok, Bin} ->
- case Transport:send(Socket, Bin) of
- ok -> sfallback(Transport, Socket, File, Sent + byte_size(Bin));
- {error, closed} -> {sent, Sent}
- end
- end.
-
-
-%% @private Wrapper for sendfile function.
--spec sendfile(inet:socket(), binary()) -> {sent, non_neg_integer()}.
-sendfile(Socket, Filepath) ->
- {ok, Sent} = file:sendfile(Filepath, Socket),
- {sent, Sent}.
-
-spec directory_path(dirspec()) -> dirpath().
directory_path({priv_dir, App, []}) ->
priv_dir_path(App);