Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(725)

Unified Diff: net/http/http_cache_transaction.cc

Issue 391763002: Initial implementation of Chrome-Freshness header. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use PrivacyMode setting to control sending Chrome-Freshness header. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | net/http/http_cache_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/http/http_cache_transaction.cc
diff --git a/net/http/http_cache_transaction.cc b/net/http/http_cache_transaction.cc
index aba792330bf4dcf92e78deeb88ff44ff88fc663f..21ae62ca0f23ef484d6ff8880afcc81285c4b85d 100644
--- a/net/http/http_cache_transaction.cc
+++ b/net/http/http_cache_transaction.cc
@@ -15,13 +15,16 @@
#include "base/bind.h"
#include "base/compiler_specific.h"
+#include "base/format_macros.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/metrics/sparse_histogram.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
@@ -48,6 +51,9 @@ using base::TimeTicks;
namespace {
+// TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
+static const char kFreshnessHeader[] = "Chrome-Freshness";
+
// Stores data relevant to the statistics of writing and reading entire
// certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
// of certificates in the chain that have pending operations in the
@@ -232,6 +238,38 @@ void RecordNoStoreHeaderHistogram(int load_flags,
}
}
+// TODO(ricea): Move this to a more appropriate home. Also, this is closely
+// based on HttpResponseHeaders::GetMaxAgeValue so they should probably share
+// implementation.
+bool GetStaleWhileRevalidateValue(const net::HttpResponseHeaders& headers,
+ TimeDelta* result) {
+ const std::string name = "cache-control";
+ std::string value;
+
+ const char kStaleWhileRevalidatePrefix[] = "stale-while-revalidate=";
+ const size_t kStaleWhileRevalidatePrefixLen =
+ arraysize(kStaleWhileRevalidatePrefix) - 1;
+
+ void* iter = NULL;
+ while (headers.EnumerateHeader(&iter, name, &value)) {
+ if (value.size() > kStaleWhileRevalidatePrefixLen) {
+ if (LowerCaseEqualsASCII(value.begin(),
+ value.begin() + kStaleWhileRevalidatePrefixLen,
+ kStaleWhileRevalidatePrefix)) {
+ int64 seconds;
+ base::StringToInt64(
tyoshino (SeeGerritForStatus) 2014/07/18 02:29:55 handle failure?
Adam Rice 2014/07/18 04:16:51 It looks like the code I copy-and-pasted relied on
+ base::StringPiece(value.begin() + kStaleWhileRevalidatePrefixLen,
+ value.end()),
tyoshino (SeeGerritForStatus) 2014/07/18 02:29:55 indent
Adam Rice 2014/07/18 04:16:51 Done.
+ &seconds);
+ *result = TimeDelta::FromSeconds(seconds);
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
} // namespace
namespace net {
@@ -2257,6 +2295,29 @@ bool HttpCache::Transaction::ConditionalizeRequest() {
bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
!invalid_range_;
+ if (!use_if_range && request_->privacy_mode != PRIVACY_MODE_ENABLED) {
+ // stale-while-revalidate is not useful when we only have a partial response
+ // cached, so don't set the header in that case.
+ // Also don't send the header when DO_NOT_SEND_COOKIES is set, to avoid
tyoshino (SeeGerritForStatus) 2014/07/18 02:29:54 the if-clause is looking at privacy_mode which is
Adam Rice 2014/07/18 04:16:51 Sorry, yes, I found that DO_NOT_SEND_COOKIES was i
+ // accidentally fingerprinting the user.
+ base::TimeDelta stale_while_revalidate;
+ if (GetStaleWhileRevalidateValue(*response_.headers,
+ &stale_while_revalidate)) {
+ base::TimeDelta max_age =
+ response_.headers->GetFreshnessLifetime(response_.response_time);
+ base::TimeDelta current_age = response_.headers->GetCurrentAge(
+ response_.request_time, response_.response_time, Time::Now());
+
+ custom_request_->extra_headers.SetHeader(
+ kFreshnessHeader,
+ base::StringPrintf("max-age=%" PRId64
+ ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
+ max_age.InSeconds(),
+ stale_while_revalidate.InSeconds(),
+ current_age.InSeconds()));
+ }
+ }
+
if (!etag_value.empty()) {
if (use_if_range) {
// We don't want to switch to WRITE mode if we don't have this block of a
« no previous file with comments | « no previous file | net/http/http_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698