Skip to content

Instantly share code, notes, and snippets.

@zmack
Created August 12, 2014 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmack/c86b468f84c07bc61a9e to your computer and use it in GitHub Desktop.
Save zmack/c86b468f84c07bc61a9e to your computer and use it in GitHub Desktop.
IgnoreHashMismatch
Index: net/instaweb/rewriter/rewrite_context.cc
===================================================================
--- net/instaweb/rewriter/rewrite_context.cc (revision 4146)
+++ net/instaweb/rewriter/rewrite_context.cc (working copy)
@@ -962,7 +962,7 @@
handler_->Message(
kInfo, "Deadline exceeded for rewrite of resource %s with %s.",
input->url().c_str(), rewrite_context_->id());
- FetchFallbackDoneImpl(input->contents(), input->response_headers());
+ FetchFallbackDoneImpl(input->contents(), input->response_headers(), true);
}
// We need to be careful not to leak metadata. So only add it when
@@ -1056,7 +1056,7 @@
rewrite_context_->output_partition(0);
CHECK(cached_result != NULL);
rewrite_context_->FixFetchFallbackHeaders(*cached_result,
- response_headers);
+ response_headers, false);
// Use the most conservative Cache-Control considering all inputs.
// Note that this is needed because FixFetchFallbackHeaders might
// actually relax things a bit if the input was no-cache.
@@ -1095,19 +1095,20 @@
return;
}
- FetchFallbackDoneImpl(contents, headers);
+ FetchFallbackDoneImpl(contents, headers, false);
}
// Backend for FetchFallbackCacheDone, but can be also invoked
// for main rewrite when background rewrite is detached.
void FetchFallbackDoneImpl(const StringPiece& contents,
- const ResponseHeaders* headers) {
+ const ResponseHeaders* headers, bool deadline_exceeded) {
async_fetch_->response_headers()->CopyFrom(*headers);
CHECK_EQ(1, rewrite_context_->num_output_partitions());
const CachedResult* cached_result = rewrite_context_->output_partition(0);
CHECK(cached_result != NULL);
+
rewrite_context_->FixFetchFallbackHeaders(*cached_result,
- async_fetch_->response_headers());
+ async_fetch_->response_headers(), deadline_exceeded);
// Use the most conservative Cache-Control considering all inputs.
ApplyInputCacheControl(async_fetch_->response_headers());
if (!detached_) {
@@ -2992,11 +2993,15 @@
}
void RewriteContext::FixFetchFallbackHeaders(
- const CachedResult& cached_result, ResponseHeaders* headers) {
+ const CachedResult& cached_result, ResponseHeaders* headers,
+ bool deadline_exceeded) {
if (headers->Sanitize()) {
headers->ComputeCaching();
}
+ bool shorten_cache_length = true;
+
+
// In the case of a resource fetch with hash mismatch, we will not have
// inputs, so fix headers based on the metadata. As we do not consider
// FILE_BASED inputs here, if all inputs are FILE_BASED, the TTL will be the
@@ -3017,14 +3022,20 @@
}
}
- // Shorten cache length, and prevent proxies caching this, as it's under
- // the "wrong" URL.
- headers->SetDateAndCaching(
- headers->date_ms(),
- std::min(min_cache_expiry_time_ms - headers->date_ms(),
- headers->implicit_cache_ttl_ms()),
- ",private");
- headers->RemoveAll(HttpAttributes::kEtag);
+ if (Options()->ignore_hash_mismatch()) {
+ shorten_cache_length = deadline_exceeded;
+ }
+
+ if (shorten_cache_length) {
+ // Shorten cache length, and prevent proxies caching this, as it's under
+ // the "wrong" URL.
+ headers->SetDateAndCaching(
+ headers->date_ms(),
+ std::min(min_cache_expiry_time_ms - headers->date_ms(),
+ headers->implicit_cache_ttl_ms()),
+ ",private");
+ headers->RemoveAll(HttpAttributes::kEtag);
+ }
headers->ComputeCaching();
}
Index: net/instaweb/rewriter/in_place_rewrite_context.cc
===================================================================
--- net/instaweb/rewriter/in_place_rewrite_context.cc (revision 4146)
+++ net/instaweb/rewriter/in_place_rewrite_context.cc (working copy)
@@ -377,7 +377,8 @@
}
void InPlaceRewriteContext::FixFetchFallbackHeaders(
- const CachedResult& cached_result, ResponseHeaders* headers) {
+ const CachedResult& cached_result, ResponseHeaders* headers,
+ bool deadline_exceeded) {
if (is_rewritten_) {
if (!rewritten_hash_.empty()) {
headers->Replace(HttpAttributes::kEtag, HTTPCache::FormatEtag(StrCat(
Index: net/instaweb/rewriter/rewrite_options.cc
===================================================================
--- net/instaweb/rewriter/rewrite_options.cc (revision 4146)
+++ net/instaweb/rewriter/rewrite_options.cc (working copy)
@@ -136,6 +136,8 @@
"ForbidAllDisabledFilters";
const char RewriteOptions::kHideRefererUsingMeta[] = "HideRefererUsingMeta";
const char RewriteOptions::kIdleFlushTimeMs[] = "IdleFlushTimeMs";
+const char RewriteOptions::kIgnoreHashMismatch[] =
+ "IgnoreHashMismatch";
const char RewriteOptions::kImageInlineMaxBytes[] = "ImageInlineMaxBytes";
const char RewriteOptions::kImageJpegNumProgressiveScans[] =
"ImageJpegNumProgressiveScans";
@@ -1324,6 +1326,15 @@
"Add query-params with configuration adjustments to rewritten "
"URLs.");
AddBaseProperty(
+ false, &RewriteOptions::ignore_hash_mismatch_,
+ "ihm",
+ kIgnoreHashMismatch,
+ kDirectoryScope,
+ "When serving a request for a .pagespeed. URL with the wrong hash, allow "
+ "return the max TTL for everything but assets that go over the generation "
+ "timeout");
+
+ AddBaseProperty(
false, &RewriteOptions::in_place_rewriting_enabled_, "ipro",
kInPlaceResourceOptimization,
kDirectoryScope,
Index: net/instaweb/rewriter/rewrite_options_test.cc
===================================================================
--- net/instaweb/rewriter/rewrite_options_test.cc (revision 4146)
+++ net/instaweb/rewriter/rewrite_options_test.cc (working copy)
@@ -893,6 +893,7 @@
PassLookupOptionByName(RewriteOptions::kForbidAllDisabledFilters);
PassLookupOptionByName(RewriteOptions::kHideRefererUsingMeta);
PassLookupOptionByName(RewriteOptions::kIdleFlushTimeMs);
+ PassLookupOptionByName(RewriteOptions::kIgnoreHashMismatch);
PassLookupOptionByName(RewriteOptions::kImageInlineMaxBytes);
PassLookupOptionByName(RewriteOptions::kImageJpegNumProgressiveScans);
PassLookupOptionByName(RewriteOptions::
Index: net/instaweb/rewriter/public/rewrite_context.h
===================================================================
--- net/instaweb/rewriter/public/rewrite_context.h (revision 4146)
+++ net/instaweb/rewriter/public/rewrite_context.h (working copy)
@@ -553,7 +553,7 @@
// be cached much. By default we strip Set-Cookie* headers and Etags, and
// convert Cache-Control headers to private, max-age=300.
virtual void FixFetchFallbackHeaders(const CachedResult& cached_result,
- ResponseHeaders* headers);
+ ResponseHeaders* headers, bool deadline_exceeded);
// Callback once the fetch is done. This calls Driver()->FetchComplete() if
// notify_driver_on_fetch_done is true.
Index: net/instaweb/rewriter/public/in_place_rewrite_context.h
===================================================================
--- net/instaweb/rewriter/public/in_place_rewrite_context.h (revision 4146)
+++ net/instaweb/rewriter/public/in_place_rewrite_context.h (working copy)
@@ -118,7 +118,8 @@
void StartFetchReconstructionParent();
// Implements RewriteContext::FixFetchFallbackHeaders().
virtual void FixFetchFallbackHeaders(const CachedResult& cached_result,
- ResponseHeaders* headers);
+ ResponseHeaders* headers,
+ bool deadline_exceeded);
// Implements RewriteContext::FetchTryFallback().
virtual void FetchTryFallback(const GoogleString& url,
const StringPiece& hash);
Index: net/instaweb/rewriter/public/rewrite_options.h
===================================================================
--- net/instaweb/rewriter/public/rewrite_options.h (revision 4146)
+++ net/instaweb/rewriter/public/rewrite_options.h (working copy)
@@ -258,6 +258,7 @@
static const char kForbidAllDisabledFilters[];
static const char kHideRefererUsingMeta[];
static const char kIdleFlushTimeMs[];
+ static const char kIgnoreHashMismatch[];
static const char kImageInlineMaxBytes[];
static const char kImageJpegNumProgressiveScans[];
static const char kImageJpegNumProgressiveScansForSmallScreens[];
@@ -1335,6 +1336,15 @@
set_option(x, &preserve_url_relativity_);
}
+ void set_ignore_hash_mismatch(bool x) {
+ set_option(x, &ignore_hash_mismatch_);
+ }
+
+ bool ignore_hash_mismatch() const {
+ return ignore_hash_mismatch_.value();
+ }
+
+
// Returns false if there is an entry in url_cache_invalidation_entries_ with
// its timestamp_ms > time_ms and url matches the url_pattern. Else, return
// true.
@@ -3375,6 +3385,10 @@
// can be reconstructed on servers without the same configuration file.
Option<bool> add_options_to_urls_;
+ // If this option is enabled, hash mismatches are ignored, and
+ // the max ttl is returned for all but newly-generated assets
+ Option<bool> ignore_hash_mismatch_;
+
// Should in-place-resource-optimization(IPRO) be enabled?
Option<bool> in_place_rewriting_enabled_;
// Optimize before responding in in-place flow?
Index: net/instaweb/rewriter/rewrite_context.cc
===================================================================
--- net/instaweb/rewriter/rewrite_context.cc (revision 4142)
+++ net/instaweb/rewriter/rewrite_context.cc (working copy)
@@ -966,7 +966,7 @@
handler_->Message(
kInfo, "Deadline exceeded for rewrite of resource %s with %s.",
input->url().c_str(), rewrite_context_->id());
- FetchFallbackDoneImpl(input->contents(), input->response_headers());
+ FetchFallbackDoneImpl(input->contents(), input->response_headers(), true);
}
// We need to be careful not to leak metadata. So only add it when
@@ -1060,7 +1060,7 @@
rewrite_context_->output_partition(0);
CHECK(cached_result != NULL);
rewrite_context_->FixFetchFallbackHeaders(*cached_result,
- response_headers);
+ response_headers, false);
// Use the most conservative Cache-Control considering all inputs.
// Note that this is needed because FixFetchFallbackHeaders might
// actually relax things a bit if the input was no-cache.
@@ -1099,19 +1099,20 @@
return;
}
- FetchFallbackDoneImpl(contents, headers);
+ FetchFallbackDoneImpl(contents, headers, false);
}
// Backend for FetchFallbackCacheDone, but can be also invoked
// for main rewrite when background rewrite is detached.
void FetchFallbackDoneImpl(const StringPiece& contents,
- const ResponseHeaders* headers) {
+ const ResponseHeaders* headers, bool deadline_exceeded) {
async_fetch_->response_headers()->CopyFrom(*headers);
CHECK_EQ(1, rewrite_context_->num_output_partitions());
const CachedResult* cached_result = rewrite_context_->output_partition(0);
CHECK(cached_result != NULL);
+
rewrite_context_->FixFetchFallbackHeaders(*cached_result,
- async_fetch_->response_headers());
+ async_fetch_->response_headers(), deadline_exceeded);
// Use the most conservative Cache-Control considering all inputs.
ApplyInputCacheControl(async_fetch_->response_headers());
if (!detached_) {
@@ -3038,12 +3039,14 @@
}
void RewriteContext::FixFetchFallbackHeaders(
- const CachedResult& cached_result, ResponseHeaders* headers) {
+ const CachedResult& cached_result, ResponseHeaders* headers,
+ bool deadline_exceeded) {
if (headers->Sanitize()) {
headers->ComputeCaching();
}
const char* cache_control_suffix = "";
+ bool shorten_cache_length = false;
// In the case of a resource fetch with hash mismatch, we will not have
// inputs, so fix headers based on the metadata. As we do not consider
@@ -3066,7 +3069,16 @@
}
}
int64 ttl_ms = min_cache_expiry_time_ms - date_ms;
- if (!Options()->publicly_cache_mismatched_hashes_experimental()) {
+
+ if (Options()->ignore_hash_mismatch()) {
+ shorten_cache_length = deadline_exceeded &&
+ !Options()->publicly_cache_mismatched_hashes_experimental();
+ } else {
+ shorten_cache_length =
+ !Options()->publicly_cache_mismatched_hashes_experimental();
+ }
+
+ if (shorten_cache_length) {
// Shorten cache length, and prevent proxies caching this, as it's under
// the "wrong" URL.
cache_control_suffix = ",private";
Index: net/instaweb/rewriter/in_place_rewrite_context.cc
===================================================================
--- net/instaweb/rewriter/in_place_rewrite_context.cc (revision 4142)
+++ net/instaweb/rewriter/in_place_rewrite_context.cc (working copy)
@@ -385,7 +385,8 @@
}
void InPlaceRewriteContext::FixFetchFallbackHeaders(
- const CachedResult& cached_result, ResponseHeaders* headers) {
+ const CachedResult& cached_result, ResponseHeaders* headers,
+ bool deadline_exceeded) {
if (is_rewritten_) {
if (!rewritten_hash_.empty()) {
headers->Replace(HttpAttributes::kEtag, HTTPCache::FormatEtag(StrCat(
Index: net/instaweb/rewriter/rewrite_options.cc
===================================================================
--- net/instaweb/rewriter/rewrite_options.cc (revision 4142)
+++ net/instaweb/rewriter/rewrite_options.cc (working copy)
@@ -138,6 +138,8 @@
"ForbidAllDisabledFilters";
const char RewriteOptions::kHideRefererUsingMeta[] = "HideRefererUsingMeta";
const char RewriteOptions::kIdleFlushTimeMs[] = "IdleFlushTimeMs";
+const char RewriteOptions::kIgnoreHashMismatch[] =
+ "IgnoreHashMismatch";
const char RewriteOptions::kImageInlineMaxBytes[] = "ImageInlineMaxBytes";
const char RewriteOptions::kImageJpegNumProgressiveScans[] =
"ImageJpegNumProgressiveScans";
@@ -1341,6 +1343,15 @@
"public caching based on the origin TTL.", false);
AddBaseProperty(
+ false, &RewriteOptions::ignore_hash_mismatch_,
+ "ihm",
+ kIgnoreHashMismatch,
+ kDirectoryScope,
+ "When serving a request for a .pagespeed. URL with the wrong hash, allow "
+ "return the max TTL for everything but assets that go over the generation "
+ "timeout", false);
+
+ AddBaseProperty(
false, &RewriteOptions::in_place_rewriting_enabled_, "ipro",
kInPlaceResourceOptimization,
kDirectoryScope,
Index: net/instaweb/rewriter/rewrite_options_test.cc
===================================================================
--- net/instaweb/rewriter/rewrite_options_test.cc (revision 4142)
+++ net/instaweb/rewriter/rewrite_options_test.cc (working copy)
@@ -884,6 +884,7 @@
RewriteOptions::kForbidAllDisabledFilters,
RewriteOptions::kHideRefererUsingMeta,
RewriteOptions::kIdleFlushTimeMs,
+ RewriteOptions::kIgnoreHashMismatch,
RewriteOptions::kImageInlineMaxBytes,
RewriteOptions::kImageJpegNumProgressiveScans,
RewriteOptions::kImageJpegNumProgressiveScansForSmallScreens,
Index: net/instaweb/rewriter/public/rewrite_context.h
===================================================================
--- net/instaweb/rewriter/public/rewrite_context.h (revision 4142)
+++ net/instaweb/rewriter/public/rewrite_context.h (working copy)
@@ -553,7 +553,7 @@
// be cached much. By default we strip Set-Cookie* headers and Etags, and
// convert Cache-Control headers to private, max-age=300.
virtual void FixFetchFallbackHeaders(const CachedResult& cached_result,
- ResponseHeaders* headers);
+ ResponseHeaders* headers, bool deadline_exceeded);
// Callback once the fetch is done. This calls Driver()->FetchComplete() if
// notify_driver_on_fetch_done is true.
Index: net/instaweb/rewriter/public/in_place_rewrite_context.h
===================================================================
--- net/instaweb/rewriter/public/in_place_rewrite_context.h (revision 4142)
+++ net/instaweb/rewriter/public/in_place_rewrite_context.h (working copy)
@@ -121,7 +121,8 @@
void StartFetchReconstructionParent();
// Implements RewriteContext::FixFetchFallbackHeaders().
virtual void FixFetchFallbackHeaders(const CachedResult& cached_result,
- ResponseHeaders* headers);
+ ResponseHeaders* headers,
+ bool deadline_exceeded);
// Implements RewriteContext::FetchTryFallback().
virtual void FetchTryFallback(const GoogleString& url,
const StringPiece& hash);
Index: net/instaweb/rewriter/public/rewrite_options.h
===================================================================
--- net/instaweb/rewriter/public/rewrite_options.h (revision 4142)
+++ net/instaweb/rewriter/public/rewrite_options.h (working copy)
@@ -262,6 +262,7 @@
static const char kForbidAllDisabledFilters[];
static const char kHideRefererUsingMeta[];
static const char kIdleFlushTimeMs[];
+ static const char kIgnoreHashMismatch[];
static const char kImageInlineMaxBytes[];
static const char kImageJpegNumProgressiveScans[];
static const char kImageJpegNumProgressiveScansForSmallScreens[];
@@ -1515,6 +1516,14 @@
return add_options_to_urls_.value();
}
+ void set_ignore_hash_mismatch(bool x) {
+ set_option(x, &ignore_hash_mismatch_);
+ }
+
+ bool ignore_hash_mismatch() const {
+ return ignore_hash_mismatch_.value();
+ }
+
void set_publicly_cache_mismatched_hashes_experimental(bool x) {
set_option(x, &publicly_cache_mismatched_hashes_experimental_);
}
@@ -3422,6 +3431,10 @@
// in proxies.
Option<bool> publicly_cache_mismatched_hashes_experimental_;
+ // If this option is enabled, hash mismatches are ignored, and
+ // the max ttl is returned for all but newly-generated assets
+ Option<bool> ignore_hash_mismatch_;
+
// Should in-place-resource-optimization(IPRO) be enabled?
Option<bool> in_place_rewriting_enabled_;
// Optimize before responding in in-place flow?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment