Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement conditional gets for static resources

  • Loading branch information...
commit ce9020a9cfe4d44b8e47ae96bbbf51176b35d317 1 parent 73704ec
@mitchellh authored
Showing with 26 additions and 2 deletions.
  1. +26 −2 apps/lifeguard/src/lifeguard_web_static.erl
View
28 apps/lifeguard/src/lifeguard_web_static.erl
@@ -4,7 +4,10 @@
-module(lifeguard_web_static).
-export([init/1,
+ allowed_methods/2,
content_types_provided/2,
+ generate_etag/2,
+ last_modified/2,
resource_exists/2,
to_response/2]).
@@ -13,18 +16,35 @@
init(_) ->
{ok, no_context}.
+allowed_methods(ReqData, Context) ->
+ {['GET'], ReqData, Context}.
+
content_types_provided(ReqData, Context) ->
- StaticPath = wrq:disp_path(ReqData),
+ StaticPath = wrq:disp_path(ReqData),
ContentType = webmachine_util:guess_mime(StaticPath),
{[{ContentType, to_response}], ReqData, Context}.
+generate_etag(ReqData, Context) ->
+ StaticPath = wrq:disp_path(ReqData),
+ {ok, Value} = file:read_file(full_resource_path(StaticPath)),
+ ETag = hash_body(Value),
+
+ % Respond with the ETag
+ {ETag, ReqData, Context}.
+
+last_modified(ReqData, Context) ->
+ % Get the last modified time for the static file
+ FullPath = full_resource_path(wrq:disp_path(ReqData)),
+ LastMod = filelib:last_modified(FullPath),
+ {LastMod, ReqData, Context}.
+
resource_exists(ReqData, Context) ->
% Get the static path...
StaticPath = wrq:disp_path(ReqData),
% Expand the file path and check that it exists
FullPath = full_resource_path(StaticPath),
- Exists = filelib:is_regular(FullPath),
+ Exists = filelib:is_regular(FullPath),
{Exists, ReqData, Context}.
to_response(ReqData, Context) ->
@@ -42,3 +62,7 @@ to_response(ReqData, Context) ->
-spec full_resource_path(string()) -> string().
full_resource_path(ShortPath) ->
filename:join([code:priv_dir(lifeguard), "static", ShortPath]).
+
+-spec hash_body(string()) -> string().
+hash_body(Body) ->
+ mochihex:to_hex(binary_to_list(crypto:sha(Body))).
Please sign in to comment.
Something went wrong with that request. Please try again.