Skip to content

Commit

Permalink
speedup cowboy_rest by introducing faster rfc2109 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dvv committed Nov 2, 2012
1 parent 5525369 commit ede5f9f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/cowboy_clock.erl
Expand Up @@ -25,7 +25,7 @@
-export([start_link/0]). -export([start_link/0]).
-export([stop/0]). -export([stop/0]).
-export([rfc1123/0]). -export([rfc1123/0]).
-export([rfc2109/1]). -export([rfc2109/1, rfc2109_fast/1]).


%% gen_server. %% gen_server.
-export([init/1]). -export([init/1]).
Expand Down Expand Up @@ -106,6 +106,21 @@ rfc2109(LocalTime) ->
MinBin/binary, ":", MinBin/binary, ":",
SecBin/binary, " GMT">>. 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. %% gen_server.


%% @private %% @private
Expand Down
4 changes: 2 additions & 2 deletions src/cowboy_rest.erl
Expand Up @@ -765,7 +765,7 @@ set_resp_body(Req, State=#state{content_type_a={_Type, Fun}}) ->
LastModified when is_atom(LastModified) -> LastModified when is_atom(LastModified) ->
Req4 = Req3; Req4 = Req3;
LastModified -> LastModified ->
LastModifiedStr = httpd_util:rfc1123_date(LastModified), LastModifiedStr = cowboy_clock:rfc2109_fast(LastModified),
Req4 = cowboy_req:set_resp_header( Req4 = cowboy_req:set_resp_header(
<<"Last-Modified">>, LastModifiedStr, Req3) <<"Last-Modified">>, LastModifiedStr, Req3)
end, end,
Expand Down Expand Up @@ -810,7 +810,7 @@ set_resp_expires(Req, State) ->
Expires when is_atom(Expires) -> Expires when is_atom(Expires) ->
{Req2, State2}; {Req2, State2};
Expires -> Expires ->
ExpiresStr = httpd_util:rfc1123_date(Expires), ExpiresStr = cowboy_clock:rfc2109_fast(Expires),
Req3 = cowboy_req:set_resp_header( Req3 = cowboy_req:set_resp_header(
<<"Expires">>, ExpiresStr, Req2), <<"Expires">>, ExpiresStr, Req2),
{Req3, State2} {Req3, State2}
Expand Down
2 changes: 1 addition & 1 deletion src/cowboy_static.erl
Expand Up @@ -238,7 +238,7 @@ rest_init(Req, Opts) ->
etag_fun=ETagFunction}; etag_fun=ETagFunction};
ok -> ok ->
Filepath1 = join_paths(Directory1, Filepath), 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, #state{filepath=Filepath1, fileinfo=Fileinfo, mimetypes=Mimetypes1,
etag_fun=ETagFunction} etag_fun=ETagFunction}
end, end,
Expand Down

0 comments on commit ede5f9f

Please sign in to comment.