Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eric/190716 to your computer and use it in GitHub Desktop.
Save eric/190716 to your computer and use it in GitHub Desktop.
From 656f4c678b0c5fb7f40b758bf4d8a8cf884f2ce9 Mon Sep 17 00:00:00 2001
From: Eric Lindvall <[email protected]>
Date: Mon, 21 Sep 2009 18:33:53 -0700
Subject: [PATCH] Pass cache headers on to S3 and back to client.
This should allow varnish to serve the body of most requests after checking
freshness with the authoritative source.
---
app/metal/hostess.rb | 15 ++++++++++++++-
1 files changed, 14 insertions(+), 1 deletions(-)
diff --git a/app/metal/hostess.rb b/app/metal/hostess.rb
index 1c89d36..db81d16 100644
--- a/app/metal/hostess.rb
+++ b/app/metal/hostess.rb
@@ -13,7 +13,20 @@ class Hostess < Sinatra::Default
if redirect
redirect File.join("http://s3.amazonaws.com", VaultObject.current_bucket, request.path_info)
else
- VaultObject.value(request.path_info)
+ # Query S3
+ result = VaultObject.value(request.path_info,
+ :if_modified_since => env['HTTP_IF_MODIFIED_SINCE'],
+ :if_none_match => env['HTTP_IF_NONE_MATCH'])
+
+ # These should raise a 304 if either of them match
+ last_modified(result.response['last-modified']) if result.response['last-modified']
+ etag(result.response['etag']) if result.response['etag']
+
+ # If we got a 304 back, let's give it back to the client
+ halt 304 if result.response.code == 304
+
+ # Otherwise return the result back
+ result
end
end
end
--
1.6.3.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment