Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cowboy/mochi/yaws use simple_bridge_util:expires

  • Loading branch information...
commit 65cada2913f4d2bf1911cd86c86cbf58d42c6469 1 parent 44c344d
@choptastic choptastic authored
View
20 src/cowboy_bridge_modules/cowboy_response_bridge.erl
@@ -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} ->
View
9 src/mochiweb_bridge_modules/mochiweb_response_bridge.erl
@@ -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.
View
12 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
@@ -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])).
View
6 src/yaws_bridge_modules/yaws_response_bridge.erl
@@ -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),

0 comments on commit 65cada2

Please sign in to comment.
Something went wrong with that request. Please try again.