Permalink
Browse files

speedup cowboy_rest by introducing faster rfc2109 conversion

  • Loading branch information...
dvv committed Nov 1, 2012
1 parent 5525369 commit ede5f9f2a8c5d50ba90008090888b519232a2d87
Showing with 19 additions and 4 deletions.
  1. +16 −1 src/cowboy_clock.erl
  2. +2 −2 src/cowboy_rest.erl
  3. +1 −1 src/cowboy_static.erl
View
@@ -25,7 +25,7 @@
-export([start_link/0]).
-export([stop/0]).
-export([rfc1123/0]).
--export([rfc2109/1]).
+-export([rfc2109/1, rfc2109_fast/1]).
%% gen_server.
-export([init/1]).
@@ -106,6 +106,21 @@ rfc2109(LocalTime) ->
MinBin/binary, ":",
SecBin/binary, " GMT">>.
+%% @doc Return the current date and time formatted according to RFC-2109.
+%%
+%% This format is used in the <em>set-cookie</em> header sent with
+%% HTTP responses.
+-spec rfc2109_fast(calendar:datetime()) -> binary().
+rfc2109_fast(DateTime) ->
+ {{Year, Month, Day}, {Hour, Minute, Second}} = DateTime,
+ DoW = calendar:day_of_the_week(Year, Month, Day),
+ list_to_binary(io_lib:format(
+ "~s, ~2..0B ~s ~4..0B ~2..0B:~2..0B:~2..0B GMT", [
+ weekday(DoW),
+ Day, month(Month), Year,
+ Hour, Minute, Second
+ ])).
+
%% gen_server.
%% @private
View
@@ -765,7 +765,7 @@ set_resp_body(Req, State=#state{content_type_a={_Type, Fun}}) ->
LastModified when is_atom(LastModified) ->
Req4 = Req3;
LastModified ->
- LastModifiedStr = httpd_util:rfc1123_date(LastModified),
+ LastModifiedStr = cowboy_clock:rfc2109_fast(LastModified),
Req4 = cowboy_req:set_resp_header(
<<"Last-Modified">>, LastModifiedStr, Req3)
end,
@@ -810,7 +810,7 @@ set_resp_expires(Req, State) ->
Expires when is_atom(Expires) ->
{Req2, State2};
Expires ->
- ExpiresStr = httpd_util:rfc1123_date(Expires),
+ ExpiresStr = cowboy_clock:rfc2109_fast(Expires),
Req3 = cowboy_req:set_resp_header(
<<"Expires">>, ExpiresStr, Req2),
{Req3, State2}
View
@@ -238,7 +238,7 @@ rest_init(Req, Opts) ->
etag_fun=ETagFunction};
ok ->
Filepath1 = join_paths(Directory1, Filepath),
- Fileinfo = file:read_file_info(Filepath1),
+ Fileinfo = file:read_file_info(Filepath1, [{time, universal}]),
#state{filepath=Filepath1, fileinfo=Fileinfo, mimetypes=Mimetypes1,
etag_fun=ETagFunction}
end,

0 comments on commit ede5f9f

Please sign in to comment.