Browse Source

Use Transport:sendfile/2 from Ranch

This removes a bunch of unneeded code, including code that was
made for R14 which we don't support anymore.

Note that the dependency on Ranch was updated, so you will need
to update Ranch for the new code to work.
Loïc Hoguin 12 years ago
parent
commit
72b26c6d0c
2 changed files with 2 additions and 50 deletions
  1. 1 1
      rebar.config
  2. 1 49
      src/cowboy_static.erl

+ 1 - 1
rebar.config

@@ -1,6 +1,6 @@
 {deps, [
 	{ranch, "0\\.4\\.0.*", {git, "git://github.com/extend/ranch.git",
-		"cd099983b1b807b87fa050d1e4ff0a13aba25b49"}}
+		"fb7ed3807620f7534c617789e7347192838a419a"}}
 ]}.
 {erl_opts, [
 %%	bin_opt_info,

+ 1 - 49
src/cowboy_static.erl

@@ -322,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);