Skip to content

Commit

Permalink
cowboy/mochi/yaws use simple_bridge_util:expires
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed May 31, 2012
1 parent 44c344d commit 65cada2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
20 changes: 12 additions & 8 deletions src/cowboy_bridge_modules/cowboy_response_bridge.erl
Expand Up @@ -49,23 +49,27 @@ build_response(ReqKey, Res) ->
{ok,FinReq};

{file, Path} ->
%% Calculate expire date far into future...
Seconds = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
TenYears = 10 * 365 * 24 * 60 * 60,
Seconds1 = calendar:gregorian_seconds_to_datetime(Seconds + TenYears),
ExpireDate = httpd_util:rfc1123_date(Seconds1),
%% Note: that this entire {file, Path} section should be avoided
%% as much as possible, since this reads the entire file into
%% memory before sending.
%%
%% You want to make sure that cowboy.config is properly set
%% up with paths so that the requests for static files are
%% properly handled by cowboy directly.
%%
%% See https://github.com/nitrogen/nitrogen/blob/master/rel/overlay/cowboy/etc/cowboy.config
%% and
%% https://github.com/choptastic/nitrogen/blob/master/rel/overlay/cowboy/site/src/nitrogen_sup.erl
ExpireDate = simple_bridge_util:expires(years, 10),

[$. | Ext] = filename:extension(Path),
Mimetype = mimetypes:extension(Ext),

%% Create the response telling Mochiweb to serve the file...
Headers = [
{"Expires", ExpireDate},
{"Content-Type",Mimetype}
],

io:format("Serving static file ~p~n",[Path]),

FullPath = filename:join(DocRoot,Path),
{ok, FinReq} = case file:read_file(FullPath) of
{error,enoent} ->
Expand Down
9 changes: 2 additions & 7 deletions src/mochiweb_bridge_modules/mochiweb_response_bridge.erl
Expand Up @@ -34,15 +34,10 @@ build_response({Req, DocRoot}, Res) ->
% Send the mochiweb response...
Req:respond({Code, Headers2, Body});
{file, Path} ->
%% Calculate expire date far into future...
%% This method copied from Evan Miller's implementation
{{Y, _, _}, _} = calendar:local_time(),

ExpireDate = httpd_util:rfc1123_date(),
ExpireDate1 = re:replace(ExpireDate, " \\d\\d\\d\\d ", io_lib:format(" ~4.4.0w ", [Y + 10])),
ExpireDate = simple_bridge_util:expires(years, 10),

%% Create the response telling Mochiweb to serve the file...
Headers = [{"Expires", ExpireDate1}],
Headers = [{"Expires", ExpireDate}],
Req:serve_file(tl(Path), DocRoot, Headers)
end.

Expand Down
12 changes: 11 additions & 1 deletion src/simple_bridge_util.erl
@@ -1,6 +1,9 @@
% vim: ts=4 sw=4 et
-module(simple_bridge_util).
-export([atomize_header/1]).
-export([
atomize_header/1,
expires/2
]).


%% converts a Header to a lower-case, underscored version
Expand All @@ -21,4 +24,11 @@ atomize_header(Header) when is_list(Header) ->
end,
list_to_atom(lists:map(LowerUnderscore,Header)).

%% TODO: Make this flexibile beyond just years
expires(years, Years) when is_integer(Years) ->
%% Calculate expire date far into future...
%% This method copied from Evan Miller's implementation
{{Y, _, _}, _} = calendar:local_time(),

ExpireDate = httpd_util:rfc1123_date(),
_FinalExpiresDate = re:replace(ExpireDate, " \\d\\d\\d\\d ", io_lib:format(" ~4.4.0w ", [Y + Years])).
6 changes: 1 addition & 5 deletions src/yaws_bridge_modules/yaws_response_bridge.erl
Expand Up @@ -43,11 +43,7 @@ build_response(_Arg, Res) ->
%% completely back to Yaws, or 2) how the streamcontent return types work as define in
%% yaws_server:handle_out_reply

%% Calculate expire date far into future...
Seconds = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
TenYears = 10 * 365 * 24 * 60 * 60,
Seconds1 = calendar:gregorian_seconds_to_datetime(Seconds + TenYears),
ExpireDate = httpd_util:rfc1123_date(Seconds1),
ExpireDate = simple_bridge_util:expires(years, 10),

%% Docroot needed to find file in Path
Docroot = yaws_api:arg_docroot(_Arg),
Expand Down

0 comments on commit 65cada2

Please sign in to comment.