aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http_rest.erl31
-rw-r--r--src/cowboy_http_static.erl34
2 files changed, 37 insertions, 28 deletions
diff --git a/src/cowboy_http_rest.erl b/src/cowboy_http_rest.erl
index ee67b36..8bce1d4 100644
--- a/src/cowboy_http_rest.erl
+++ b/src/cowboy_http_rest.erl
@@ -41,7 +41,7 @@
charset_a :: undefined | binary(),
%% Cached resource calls.
- etag :: undefined | no_call | binary(),
+ etag :: undefined | no_call | {strong | weak, binary()},
last_modified :: undefined | no_call | calendar:datetime(),
expires :: undefined | no_call | calendar:datetime()
}).
@@ -487,14 +487,10 @@ if_match_exists(Req, State) ->
if_match(Req, State, EtagsList) ->
{Etag, Req2, State2} = generate_etag(Req, State),
- case Etag of
- no_call ->
- precondition_failed(Req2, State2);
- Etag ->
- case lists:member(Etag, EtagsList) of
- true -> if_unmodified_since_exists(Req2, State2);
- false -> precondition_failed(Req2, State2)
- end
+ case lists:member(Etag, EtagsList) of
+ true -> if_unmodified_since_exists(Req2, State2);
+ %% Etag may be `undefined' which cannot be a member.
+ false -> precondition_failed(Req2, State2)
end.
if_match_musnt_exist(Req, State) ->
@@ -534,7 +530,7 @@ if_none_match_exists(Req, State) ->
if_none_match(Req, State, EtagsList) ->
{Etag, Req2, State2} = generate_etag(Req, State),
case Etag of
- no_call ->
+ undefined ->
precondition_failed(Req2, State2);
Etag ->
case lists:member(Etag, EtagsList) of
@@ -810,10 +806,14 @@ set_resp_etag(Req, State) ->
{Req2, State2};
Etag ->
{ok, Req3} = cowboy_http_req:set_resp_header(
- <<"Etag">>, Etag, Req2),
+ <<"ETag">>, encode_etag(Etag), Req2),
{Req3, State2}
end.
+-spec encode_etag({strong | weak, binary()}) -> iolist().
+encode_etag({strong, Etag}) -> [$",Etag,$"];
+encode_etag({weak, Etag}) -> ["W/\"",Etag,$"].
+
set_resp_expires(Req, State) ->
{Expires, Req2, State2} = expires(Req, State),
case Expires of
@@ -834,6 +834,15 @@ generate_etag(Req, State=#state{etag=undefined}) ->
case call(Req, State, generate_etag) of
no_call ->
{undefined, Req, State#state{etag=no_call}};
+ %% Previously the return value from the generate_etag/2 callback was set
+ %% as the value of the ETag header in the response. Therefore the only
+ %% valid return type was `binary()'. If a handler returns a `binary()'
+ %% it must be mapped to the expected type or it'll always fail to
+ %% compare equal to any entity tags present in the request headers.
+ %% @todo Remove support for binary return values after 0.6.
+ {Etag, Req2, HandlerState} when is_binary(Etag) ->
+ [Etag2] = cowboy_http:entity_tag_match(Etag),
+ {Etag2, Req2, State#state{handler_state=HandlerState, etag=Etag2}};
{Etag, Req2, HandlerState} ->
{Etag, Req2, State#state{handler_state=HandlerState, etag=Etag}}
end;
diff --git a/src/cowboy_http_static.erl b/src/cowboy_http_static.erl
index 0ee996a..007cd16 100644
--- a/src/cowboy_http_static.erl
+++ b/src/cowboy_http_static.erl
@@ -96,15 +96,16 @@
%%
%% The default behaviour can be overridden to generate an ETag header based on
%% a combination of the file path, file size, inode and mtime values. If the
-%% option value is a list of attribute names tagged with `attributes' a hex
-%% encoded CRC32 checksum of the attribute values are used as the ETag header
-%% value.
+%% option value is a non-empty list of attribute names tagged with `attributes'
+%% a hex encoded checksum of each attribute specified is included in the value
+%% of the the ETag header. If the list of attribute names is empty no ETag
+%% header is generated.
%%
%% If a strong ETag is required a user defined function for generating the
%% header value can be supplied. The function must accept a proplist of the
%% file attributes as the first argument and a second argument containing any
-%% additional data that the function requires. The function must return a
-%% `binary()' or `undefined'.
+%% additional data that the function requires. The function must return a term
+%% of the type `{weak | strong, binary()}' or `undefined'.
%%
%% ==== Examples ====
%% ```
@@ -130,7 +131,7 @@
%% {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
%% ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
%% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
-%% iolist_to_binary(Checksum).
+%% {strong, iolist_to_binary(Checksum)}.
%% '''
-module(cowboy_http_static).
@@ -161,7 +162,8 @@
filepath :: binary() | error,
fileinfo :: {ok, #file_info{}} | {error, _} | error,
mimetypes :: {fun((binary(), T) -> [mimedef()]), T} | undefined,
- etag_fun :: {fun(([etagarg()], T) -> undefined | binary()), T}}).
+ etag_fun :: {fun(([etagarg()], T) ->
+ undefined | {strong | weak, binary()}), T}}).
%% @private Upgrade from HTTP handler to REST handler.
@@ -183,8 +185,9 @@ rest_init(Req, Opts) ->
ETagFunction = case proplists:get_value(etag, Opts) of
default -> {fun no_etag_function/2, undefined};
undefined -> {fun no_etag_function/2, undefined};
+ {attributes, []} -> {fun no_etag_function/2, undefined};
{attributes, Attrs} -> {fun attr_etag_function/2, Attrs};
- {_, _}=EtagFunction1 -> EtagFunction1
+ {_, _}=ETagFunction1 -> ETagFunction1
end,
{Filepath, Req1} = cowboy_http_req:path_info(Req),
State = case check_path(Filepath) of
@@ -411,16 +414,13 @@ no_etag_function(_Args, undefined) ->
%% @private A simple alternative is to send an ETag based on file attributes.
-type fileattr() :: filepath | filesize | mtime | inode.
--spec attr_etag_function([etagarg()], [fileattr()]) -> binary().
+-spec attr_etag_function([etagarg()], [fileattr()]) -> {strong, binary()}.
attr_etag_function(Args, Attrs) ->
- attr_etag_function(Args, Attrs, []).
-
--spec attr_etag_function([etagarg()], [fileattr()], [binary()]) -> binary().
-attr_etag_function(_Args, [], Acc) ->
- list_to_binary(integer_to_list(erlang:crc32(Acc), 16));
-attr_etag_function(Args, [H|T], Acc) ->
- {_, Value} = lists:keyfind(H, 1, Args),
- attr_etag_function(Args, T, [term_to_binary(Value)|Acc]).
+ [[_|H]|T] = [begin
+ {_,Pair} = {_,{_,_}} = {Attr,lists:keyfind(Attr, 1, Args)},
+ [$-|integer_to_list(erlang:phash2(Pair, 1 bsl 32), 16)]
+ end || Attr <- Attrs],
+ {strong, list_to_binary([H|T])}.
-ifdef(TEST).