Rewrite a few HttpResponseHeaders helpers to use std::optional
std::optional<T> is more ergonomic than using a bool and an out-parameter, and is no less efficient. (I'll migrate GetNormalizedHeader in a followup, since that has lots of callsites.) Change-Id: I744ff190542e6c954508362db32c0d30049b7f0d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5800710 Reviewed-by: Bo Liu <boliu@chromium.org> Reviewed-by: Ted Choc <tedchoc@chromium.org> Commit-Queue: Chris Fredrickson <cfredric@chromium.org> Reviewed-by: mmenke <mmenke@chromium.org> Auto-Submit: Chris Fredrickson <cfredric@chromium.org> Cr-Commit-Position: refs/heads/main@{#1344297}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
ca2e89f36d
commit
c78123f23d
components
embedder_support
android
variations
service
content/browser
net
@@ -378,11 +378,8 @@ void AndroidStreamReaderURLLoader::SetCookies() {
|
||||
const std::string_view kSetCookieHeader("Set-Cookie");
|
||||
|
||||
if (response_head_->headers->HasHeader(kSetCookieHeader)) {
|
||||
base::Time response_date;
|
||||
std::optional<base::Time> server_time = std::nullopt;
|
||||
if (response_head_->headers->GetDateValue(&response_date)) {
|
||||
server_time = std::make_optional(response_date);
|
||||
}
|
||||
std::optional<base::Time> server_time =
|
||||
response_head_->headers->GetDateValue();
|
||||
|
||||
std::string cookie_string;
|
||||
size_t iter = 0;
|
||||
|
@@ -7,6 +7,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -850,14 +851,14 @@ void VariationsService::OnSimpleLoaderComplete(
|
||||
DCHECK(headers);
|
||||
DCHECK(response_body);
|
||||
|
||||
base::Time response_date;
|
||||
if (headers->GetDateValue(&response_date)) {
|
||||
DCHECK(!response_date.is_null());
|
||||
std::optional<base::Time> response_date = headers->GetDateValue();
|
||||
if (response_date) {
|
||||
DCHECK(!response_date->is_null());
|
||||
|
||||
const base::TimeDelta latency = now - last_request_started_time_;
|
||||
client_->GetNetworkTimeTracker()->UpdateNetworkTime(
|
||||
response_date, base::Seconds(kServerTimeResolutionInSeconds), latency,
|
||||
now);
|
||||
response_date.value(), base::Seconds(kServerTimeResolutionInSeconds),
|
||||
latency, now);
|
||||
}
|
||||
|
||||
if (response_code == net::HTTP_NOT_MODIFIED) {
|
||||
@@ -869,7 +870,7 @@ void VariationsService::OnSimpleLoaderComplete(
|
||||
// seed, even when running in safe mode, so it's appropriate to always
|
||||
// modify the latest seed's date.
|
||||
field_trial_creator_.seed_store()->UpdateSeedDateAndLogDayChange(
|
||||
response_date);
|
||||
response_date.value_or(base::Time()));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -889,8 +890,8 @@ void VariationsService::OnSimpleLoaderComplete(
|
||||
std::string signature = GetHeaderValue(headers.get(), "X-Seed-Signature");
|
||||
std::string country_code = GetHeaderValue(headers.get(), "X-Country");
|
||||
StoreSeed(std::move(*response_body), std::move(signature),
|
||||
std::move(country_code), response_date, is_delta_compressed,
|
||||
is_gzip_compressed);
|
||||
std::move(country_code), response_date.value_or(base::Time()),
|
||||
is_delta_compressed, is_gzip_compressed);
|
||||
}
|
||||
|
||||
bool VariationsService::MaybeRetryOverHTTP() {
|
||||
|
@@ -10,6 +10,7 @@
|
||||
#include "content/browser/devtools/devtools_url_loader_interceptor.h"
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
|
||||
#include "base/barrier_closure.h"
|
||||
@@ -1287,10 +1288,7 @@ void InterceptionJob::ProcessSetCookies(const net::HttpResponseHeaders& headers,
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<net::CanonicalCookie>> cookies;
|
||||
base::Time response_date;
|
||||
std::optional<base::Time> server_time = std::nullopt;
|
||||
if (headers.GetDateValue(&response_date))
|
||||
server_time = std::make_optional(response_date);
|
||||
std::optional<base::Time> server_time = headers.GetDateValue();
|
||||
base::Time now = base::Time::Now();
|
||||
|
||||
const std::string_view name("Set-Cookie");
|
||||
|
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "base/check.h"
|
||||
@@ -278,8 +279,13 @@ constexpr base::TimeDelta ServiceWorkerVersion::kStopWorkerTimeout;
|
||||
ServiceWorkerVersion::MainScriptResponse::MainScriptResponse(
|
||||
const network::mojom::URLResponseHead& response_head) {
|
||||
response_time = response_head.response_time;
|
||||
if (response_head.headers)
|
||||
response_head.headers->GetLastModifiedValue(&last_modified);
|
||||
if (response_head.headers) {
|
||||
std::optional<base::Time> value =
|
||||
response_head.headers->GetLastModifiedValue();
|
||||
if (value) {
|
||||
last_modified = value.value();
|
||||
}
|
||||
}
|
||||
headers = response_head.headers;
|
||||
if (response_head.ssl_info.has_value())
|
||||
ssl_info = response_head.ssl_info.value();
|
||||
|
@@ -12,6 +12,7 @@
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
@@ -907,9 +908,8 @@ size_t HttpResponseHeaders::FindHeader(size_t from,
|
||||
return std::string::npos;
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetCacheControlDirective(
|
||||
std::string_view directive,
|
||||
base::TimeDelta* result) const {
|
||||
std::optional<base::TimeDelta> HttpResponseHeaders::GetCacheControlDirective(
|
||||
std::string_view directive) const {
|
||||
static constexpr std::string_view name("cache-control");
|
||||
std::string value;
|
||||
|
||||
@@ -945,11 +945,10 @@ bool HttpResponseHeaders::GetCacheControlDirective(
|
||||
// string. For the overflow case we use
|
||||
// base::TimeDelta::FiniteMax().InSeconds().
|
||||
seconds = std::min(seconds, base::TimeDelta::FiniteMax().InSeconds());
|
||||
*result = base::Seconds(seconds);
|
||||
return true;
|
||||
return base::Seconds(seconds);
|
||||
}
|
||||
|
||||
return false;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void HttpResponseHeaders::AddHeader(std::string::const_iterator name_begin,
|
||||
@@ -1209,28 +1208,30 @@ HttpResponseHeaders::GetFreshnessLifetimes(const Time& response_time) const {
|
||||
// Cache-Control directive must_revalidate overrides stale-while-revalidate.
|
||||
bool must_revalidate = HasHeaderValue("cache-control", "must-revalidate");
|
||||
|
||||
if (must_revalidate || !GetStaleWhileRevalidateValue(&lifetimes.staleness)) {
|
||||
DCHECK_EQ(base::TimeDelta(), lifetimes.staleness);
|
||||
}
|
||||
lifetimes.staleness =
|
||||
must_revalidate
|
||||
? base::TimeDelta()
|
||||
: GetStaleWhileRevalidateValue().value_or(base::TimeDelta());
|
||||
|
||||
// NOTE: "Cache-Control: max-age" overrides Expires, so we only check the
|
||||
// Expires header after checking for max-age in GetFreshnessLifetimes. This
|
||||
// is important since "Expires: <date in the past>" means not fresh, but
|
||||
// it should not trump a max-age value.
|
||||
if (GetMaxAgeValue(&lifetimes.freshness))
|
||||
std::optional<base::TimeDelta> max_age_value = GetMaxAgeValue();
|
||||
if (max_age_value) {
|
||||
lifetimes.freshness = max_age_value.value();
|
||||
return lifetimes;
|
||||
}
|
||||
|
||||
// If there is no Date header, then assume that the server response was
|
||||
// generated at the time when we received the response.
|
||||
Time date_value;
|
||||
if (!GetDateValue(&date_value))
|
||||
date_value = response_time;
|
||||
Time date_value = GetDateValue().value_or(response_time);
|
||||
|
||||
Time expires_value;
|
||||
if (GetExpiresValue(&expires_value)) {
|
||||
std::optional<Time> expires_value = GetExpiresValue();
|
||||
if (expires_value) {
|
||||
// The expires value can be a date in the past!
|
||||
if (expires_value > date_value) {
|
||||
lifetimes.freshness = expires_value - date_value;
|
||||
lifetimes.freshness = expires_value.value() - date_value;
|
||||
return lifetimes;
|
||||
}
|
||||
|
||||
@@ -1267,11 +1268,11 @@ HttpResponseHeaders::GetFreshnessLifetimes(const Time& response_time) const {
|
||||
response_code_ == HTTP_PARTIAL_CONTENT) &&
|
||||
!must_revalidate) {
|
||||
// TODO(darin): Implement a smarter heuristic.
|
||||
Time last_modified_value;
|
||||
if (GetLastModifiedValue(&last_modified_value)) {
|
||||
std::optional<Time> last_modified_value = GetLastModifiedValue();
|
||||
if (last_modified_value) {
|
||||
// The last-modified value can be a date in the future!
|
||||
if (last_modified_value <= date_value) {
|
||||
lifetimes.freshness = (date_value - last_modified_value) / 10;
|
||||
if (last_modified_value.value() <= date_value) {
|
||||
lifetimes.freshness = (date_value - last_modified_value.value()) / 10;
|
||||
return lifetimes;
|
||||
}
|
||||
}
|
||||
@@ -1343,14 +1344,10 @@ base::TimeDelta HttpResponseHeaders::GetCurrentAge(
|
||||
const Time& current_time) const {
|
||||
// If there is no Date header, then assume that the server response was
|
||||
// generated at the time when we received the response.
|
||||
Time date_value;
|
||||
if (!GetDateValue(&date_value))
|
||||
date_value = response_time;
|
||||
Time date_value = GetDateValue().value_or(response_time);
|
||||
|
||||
// If there is no Age header, then assume age is zero. GetAgeValue does not
|
||||
// modify its out param if the value does not exist.
|
||||
base::TimeDelta age_value;
|
||||
GetAgeValue(&age_value);
|
||||
// If there is no Age header, then assume age is zero.
|
||||
base::TimeDelta age_value = GetAgeValue().value_or(base::TimeDelta());
|
||||
|
||||
base::TimeDelta apparent_age =
|
||||
std::max(base::TimeDelta(), response_time - date_value);
|
||||
@@ -1364,14 +1361,14 @@ base::TimeDelta HttpResponseHeaders::GetCurrentAge(
|
||||
return current_age;
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetMaxAgeValue(base::TimeDelta* result) const {
|
||||
return GetCacheControlDirective("max-age", result);
|
||||
std::optional<base::TimeDelta> HttpResponseHeaders::GetMaxAgeValue() const {
|
||||
return GetCacheControlDirective("max-age");
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetAgeValue(base::TimeDelta* result) const {
|
||||
std::optional<base::TimeDelta> HttpResponseHeaders::GetAgeValue() const {
|
||||
std::string value;
|
||||
if (!EnumerateHeader(nullptr, "Age", &value))
|
||||
return false;
|
||||
return std::nullopt;
|
||||
|
||||
// Parse the delta-seconds as 1*DIGIT.
|
||||
uint32_t seconds;
|
||||
@@ -1383,36 +1380,35 @@ bool HttpResponseHeaders::GetAgeValue(base::TimeDelta* result) const {
|
||||
// caches should transmit values that overflow.
|
||||
seconds = std::numeric_limits<decltype(seconds)>::max();
|
||||
} else {
|
||||
return false;
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
*result = base::Seconds(seconds);
|
||||
return true;
|
||||
return base::Seconds(seconds);
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetDateValue(Time* result) const {
|
||||
return GetTimeValuedHeader("Date", result);
|
||||
std::optional<Time> HttpResponseHeaders::GetDateValue() const {
|
||||
return GetTimeValuedHeader("Date");
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetLastModifiedValue(Time* result) const {
|
||||
return GetTimeValuedHeader("Last-Modified", result);
|
||||
std::optional<Time> HttpResponseHeaders::GetLastModifiedValue() const {
|
||||
return GetTimeValuedHeader("Last-Modified");
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetExpiresValue(Time* result) const {
|
||||
return GetTimeValuedHeader("Expires", result);
|
||||
std::optional<Time> HttpResponseHeaders::GetExpiresValue() const {
|
||||
return GetTimeValuedHeader("Expires");
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetStaleWhileRevalidateValue(
|
||||
base::TimeDelta* result) const {
|
||||
return GetCacheControlDirective("stale-while-revalidate", result);
|
||||
std::optional<base::TimeDelta>
|
||||
HttpResponseHeaders::GetStaleWhileRevalidateValue() const {
|
||||
return GetCacheControlDirective("stale-while-revalidate");
|
||||
}
|
||||
|
||||
bool HttpResponseHeaders::GetTimeValuedHeader(const std::string& name,
|
||||
Time* result) const {
|
||||
std::optional<Time> HttpResponseHeaders::GetTimeValuedHeader(
|
||||
const std::string& name) const {
|
||||
std::string value;
|
||||
if (!EnumerateHeader(nullptr, name, &value))
|
||||
return false;
|
||||
return std::nullopt;
|
||||
|
||||
// In case of parsing the Expires header value, an invalid string 0 should be
|
||||
// treated as expired according to the RFC 9111 section 5.3 as below:
|
||||
@@ -1422,8 +1418,7 @@ bool HttpResponseHeaders::GetTimeValuedHeader(const std::string& name,
|
||||
if (base::FeatureList::IsEnabled(
|
||||
features::kTreatHTTPExpiresHeaderValueZeroAsExpired) &&
|
||||
name == "Expires" && value == "0") {
|
||||
*result = Time::Min();
|
||||
return true;
|
||||
return Time::Min();
|
||||
}
|
||||
|
||||
// When parsing HTTP dates it's beneficial to default to GMT because:
|
||||
@@ -1433,11 +1428,14 @@ bool HttpResponseHeaders::GetTimeValuedHeader(const std::string& name,
|
||||
// (crbug.com/135131) this better matches our cookie expiration
|
||||
// time parser which ignores timezone specifiers and assumes GMT.
|
||||
// 4. This is exactly what Firefox does.
|
||||
// TODO(pauljensen): The ideal solution would be to return false if the
|
||||
// timezone could not be understood so as to avoid makeing other calculations
|
||||
// TODO(pauljensen): The ideal solution would be to return std::nullopt if the
|
||||
// timezone could not be understood so as to avoid making other calculations
|
||||
// based on an incorrect time. This would require modifying the time
|
||||
// library or duplicating the code. (http://crbug.com/158327)
|
||||
return Time::FromUTCString(value.c_str(), result);
|
||||
Time result;
|
||||
return Time::FromUTCString(value.c_str(), &result)
|
||||
? std::make_optional(result)
|
||||
: std::nullopt;
|
||||
}
|
||||
|
||||
// We accept the first value of "close" or "keep-alive" in a Connection or
|
||||
|
@@ -8,6 +8,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_set>
|
||||
@@ -350,19 +351,19 @@ class NET_EXPORT HttpResponseHeaders
|
||||
const base::Time& response_time,
|
||||
const base::Time& current_time) const;
|
||||
|
||||
// The following methods extract values from the response headers. If a
|
||||
// value is not present, or is invalid, then false is returned. Otherwise,
|
||||
// true is returned and the out param is assigned to the corresponding value.
|
||||
bool GetMaxAgeValue(base::TimeDelta* value) const;
|
||||
bool GetAgeValue(base::TimeDelta* value) const;
|
||||
bool GetDateValue(base::Time* value) const;
|
||||
bool GetLastModifiedValue(base::Time* value) const;
|
||||
bool GetExpiresValue(base::Time* value) const;
|
||||
bool GetStaleWhileRevalidateValue(base::TimeDelta* value) const;
|
||||
// The following methods extract values from the response headers. If a value
|
||||
// is not present, or is invalid, then std::nullopt is returned. Otherwise,
|
||||
// the value is returned directly.
|
||||
std::optional<base::TimeDelta> GetMaxAgeValue() const;
|
||||
std::optional<base::TimeDelta> GetAgeValue() const;
|
||||
std::optional<base::Time> GetDateValue() const;
|
||||
std::optional<base::Time> GetLastModifiedValue() const;
|
||||
std::optional<base::Time> GetExpiresValue() const;
|
||||
std::optional<base::TimeDelta> GetStaleWhileRevalidateValue() const;
|
||||
|
||||
// Extracts the time value of a particular header. This method looks for the
|
||||
// first matching header value and parses its value as a HTTP-date.
|
||||
bool GetTimeValuedHeader(const std::string& name, base::Time* result) const;
|
||||
std::optional<base::Time> GetTimeValuedHeader(const std::string& name) const;
|
||||
|
||||
// Determines if this response indicates a keep-alive connection.
|
||||
bool IsKeepAlive() const;
|
||||
@@ -466,10 +467,9 @@ class NET_EXPORT HttpResponseHeaders
|
||||
size_t FindHeader(size_t from, std::string_view name) const;
|
||||
|
||||
// Search the Cache-Control header for a directive matching |directive|. If
|
||||
// present, treat its value as a time offset in seconds, write it to |result|,
|
||||
// and return true.
|
||||
bool GetCacheControlDirective(std::string_view directive,
|
||||
base::TimeDelta* result) const;
|
||||
// present, treat its value as a time offset in seconds.
|
||||
std::optional<base::TimeDelta> GetCacheControlDirective(
|
||||
std::string_view directive) const;
|
||||
|
||||
// Add header->value pair(s) to our list. The value will be split into
|
||||
// multiple values if it contains unquoted commas. If `contains_commas` is
|
||||
|
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <unordered_set>
|
||||
|
||||
@@ -20,6 +21,7 @@
|
||||
#include "net/http/http_byte_range.h"
|
||||
#include "net/http/http_response_headers_test_util.h"
|
||||
#include "net/http/http_util.h"
|
||||
#include "testing/gmock/include/gmock/gmock.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
#if !BUILDFLAG(CRONET_BUILD)
|
||||
@@ -68,32 +70,27 @@ class HttpResponseHeadersCacheControlTest : public HttpResponseHeadersTest {
|
||||
|
||||
const scoped_refptr<HttpResponseHeaders>& headers() { return headers_; }
|
||||
|
||||
// Return a pointer to a TimeDelta object. For use when the value doesn't
|
||||
// matter.
|
||||
TimeDelta* TimeDeltaPointer() { return &delta_; }
|
||||
|
||||
// Get the max-age value. This should only be used in tests where a valid
|
||||
// max-age parameter is expected to be present.
|
||||
TimeDelta GetMaxAgeValue() {
|
||||
DCHECK(headers_.get()) << "Call InitializeHeadersWithCacheControl() first";
|
||||
TimeDelta max_age_value;
|
||||
EXPECT_TRUE(headers()->GetMaxAgeValue(&max_age_value));
|
||||
return max_age_value;
|
||||
std::optional<TimeDelta> max_age_value = headers()->GetMaxAgeValue();
|
||||
EXPECT_TRUE(max_age_value);
|
||||
return max_age_value.value();
|
||||
}
|
||||
|
||||
// Get the stale-while-revalidate value. This should only be used in tests
|
||||
// where a valid max-age parameter is expected to be present.
|
||||
TimeDelta GetStaleWhileRevalidateValue() {
|
||||
DCHECK(headers_.get()) << "Call InitializeHeadersWithCacheControl() first";
|
||||
TimeDelta stale_while_revalidate_value;
|
||||
EXPECT_TRUE(
|
||||
headers()->GetStaleWhileRevalidateValue(&stale_while_revalidate_value));
|
||||
return stale_while_revalidate_value;
|
||||
std::optional<TimeDelta> stale_while_revalidate_value =
|
||||
headers()->GetStaleWhileRevalidateValue();
|
||||
EXPECT_TRUE(stale_while_revalidate_value);
|
||||
return stale_while_revalidate_value.value();
|
||||
}
|
||||
|
||||
private:
|
||||
scoped_refptr<HttpResponseHeaders> headers_;
|
||||
TimeDelta delta_;
|
||||
};
|
||||
|
||||
class CommonHttpResponseHeadersTest
|
||||
@@ -646,19 +643,16 @@ TEST(HttpResponseHeadersTest, DefaultDateToGMT) {
|
||||
ASSERT_TRUE(base::Time::FromString("Tue, 07 Aug 2007 23:10:55 GMT",
|
||||
&expected_value));
|
||||
|
||||
base::Time value;
|
||||
// When the timezone is missing, GMT is a good guess as its what RFC2616
|
||||
// requires.
|
||||
EXPECT_TRUE(parsed->GetDateValue(&value));
|
||||
EXPECT_EQ(expected_value, value);
|
||||
EXPECT_EQ(expected_value, parsed->GetDateValue());
|
||||
// If GMT is missing but an RFC822-conforming one is present, use that.
|
||||
EXPECT_TRUE(parsed->GetLastModifiedValue(&value));
|
||||
EXPECT_EQ(expected_value, value);
|
||||
EXPECT_EQ(expected_value, parsed->GetLastModifiedValue());
|
||||
// If an unknown timezone is present, treat like a missing timezone and
|
||||
// default to GMT. The only example of a web server not specifying "GMT"
|
||||
// used "UTC" which is equivalent to GMT.
|
||||
if (parsed->GetExpiresValue(&value))
|
||||
EXPECT_EQ(expected_value, value);
|
||||
EXPECT_THAT(parsed->GetExpiresValue(),
|
||||
testing::AnyOf(std::nullopt, expected_value));
|
||||
}
|
||||
|
||||
TEST(HttpResponseHeadersTest, GetAgeValue10) {
|
||||
@@ -667,9 +661,7 @@ TEST(HttpResponseHeadersTest, GetAgeValue10) {
|
||||
"Age: 10\n";
|
||||
HeadersToRaw(&headers);
|
||||
auto parsed = base::MakeRefCounted<HttpResponseHeaders>(headers);
|
||||
base::TimeDelta age;
|
||||
ASSERT_TRUE(parsed->GetAgeValue(&age));
|
||||
EXPECT_EQ(10, age.InSeconds());
|
||||
EXPECT_EQ(base::Seconds(10), parsed->GetAgeValue());
|
||||
}
|
||||
|
||||
TEST(HttpResponseHeadersTest, GetAgeValue0) {
|
||||
@@ -678,9 +670,7 @@ TEST(HttpResponseHeadersTest, GetAgeValue0) {
|
||||
"Age: 0\n";
|
||||
HeadersToRaw(&headers);
|
||||
auto parsed = base::MakeRefCounted<HttpResponseHeaders>(headers);
|
||||
base::TimeDelta age;
|
||||
ASSERT_TRUE(parsed->GetAgeValue(&age));
|
||||
EXPECT_EQ(0, age.InSeconds());
|
||||
EXPECT_EQ(base::TimeDelta(), parsed->GetAgeValue());
|
||||
}
|
||||
|
||||
TEST(HttpResponseHeadersTest, GetAgeValueBogus) {
|
||||
@@ -689,8 +679,7 @@ TEST(HttpResponseHeadersTest, GetAgeValueBogus) {
|
||||
"Age: donkey\n";
|
||||
HeadersToRaw(&headers);
|
||||
auto parsed = base::MakeRefCounted<HttpResponseHeaders>(headers);
|
||||
base::TimeDelta age;
|
||||
ASSERT_FALSE(parsed->GetAgeValue(&age));
|
||||
EXPECT_FALSE(parsed->GetAgeValue());
|
||||
}
|
||||
|
||||
TEST(HttpResponseHeadersTest, GetAgeValueNegative) {
|
||||
@@ -699,8 +688,7 @@ TEST(HttpResponseHeadersTest, GetAgeValueNegative) {
|
||||
"Age: -10\n";
|
||||
HeadersToRaw(&headers);
|
||||
auto parsed = base::MakeRefCounted<HttpResponseHeaders>(headers);
|
||||
base::TimeDelta age;
|
||||
ASSERT_FALSE(parsed->GetAgeValue(&age));
|
||||
EXPECT_FALSE(parsed->GetAgeValue());
|
||||
}
|
||||
|
||||
TEST(HttpResponseHeadersTest, GetAgeValueLeadingPlus) {
|
||||
@@ -709,8 +697,7 @@ TEST(HttpResponseHeadersTest, GetAgeValueLeadingPlus) {
|
||||
"Age: +10\n";
|
||||
HeadersToRaw(&headers);
|
||||
auto parsed = base::MakeRefCounted<HttpResponseHeaders>(headers);
|
||||
base::TimeDelta age;
|
||||
ASSERT_FALSE(parsed->GetAgeValue(&age));
|
||||
EXPECT_FALSE(parsed->GetAgeValue());
|
||||
}
|
||||
|
||||
TEST(HttpResponseHeadersTest, GetAgeValueOverflow) {
|
||||
@@ -719,11 +706,10 @@ TEST(HttpResponseHeadersTest, GetAgeValueOverflow) {
|
||||
"Age: 999999999999999999999999999999999999999999\n";
|
||||
HeadersToRaw(&headers);
|
||||
auto parsed = base::MakeRefCounted<HttpResponseHeaders>(headers);
|
||||
base::TimeDelta age;
|
||||
ASSERT_TRUE(parsed->GetAgeValue(&age));
|
||||
|
||||
// Should have saturated to 2^32 - 1.
|
||||
EXPECT_EQ(static_cast<int64_t>(0xFFFFFFFFL), age.InSeconds());
|
||||
EXPECT_EQ(base::Seconds(static_cast<int64_t>(0xFFFFFFFFL)),
|
||||
parsed->GetAgeValue());
|
||||
}
|
||||
|
||||
struct ContentTypeTestData {
|
||||
@@ -2282,33 +2268,33 @@ INSTANTIATE_TEST_SUITE_P(HttpResponseHeaders,
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest, AbsentMaxAgeReturnsFalse) {
|
||||
InitializeHeadersWithCacheControl("nocache");
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeWithNoParameterRejected) {
|
||||
InitializeHeadersWithCacheControl("max-age=,private");
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeWithSpaceParameterRejected) {
|
||||
InitializeHeadersWithCacheControl("max-age= ,private");
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeWithInterimSpaceIsRejected) {
|
||||
InitializeHeadersWithCacheControl("max-age=1 2");
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeWithMinusSignIsRejected) {
|
||||
InitializeHeadersWithCacheControl("max-age=-7");
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest,
|
||||
MaxAgeWithSpaceBeforeEqualsIsRejected) {
|
||||
InitializeHeadersWithCacheControl("max-age = 7");
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest,
|
||||
@@ -2361,7 +2347,7 @@ TEST_P(MaxAgeEdgeCasesTest, MaxAgeEdgeCases) {
|
||||
EXPECT_EQ(test.expected_seconds.value(), GetMaxAgeValue().InSeconds())
|
||||
<< " for max-age=" << test.max_age_string;
|
||||
} else {
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetMaxAgeValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2391,19 +2377,19 @@ INSTANTIATE_TEST_SUITE_P(HttpResponseHeadersCacheControl,
|
||||
TEST_F(HttpResponseHeadersCacheControlTest,
|
||||
AbsentStaleWhileRevalidateReturnsFalse) {
|
||||
InitializeHeadersWithCacheControl("max-age=3600");
|
||||
EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest,
|
||||
StaleWhileRevalidateWithoutValueRejected) {
|
||||
InitializeHeadersWithCacheControl("max-age=3600,stale-while-revalidate=");
|
||||
EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest,
|
||||
StaleWhileRevalidateWithInvalidValueIgnored) {
|
||||
InitializeHeadersWithCacheControl("max-age=3600,stale-while-revalidate=true");
|
||||
EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue(TimeDeltaPointer()));
|
||||
EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue());
|
||||
}
|
||||
|
||||
TEST_F(HttpResponseHeadersCacheControlTest, StaleWhileRevalidateValueReturned) {
|
||||
|
@@ -1043,10 +1043,7 @@ void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
|
||||
clear_site_data_prevents_cookies_from_being_stored = true;
|
||||
}
|
||||
|
||||
base::Time response_date;
|
||||
std::optional<base::Time> server_time = std::nullopt;
|
||||
if (GetResponseHeaders()->GetDateValue(&response_date))
|
||||
server_time = std::make_optional(response_date);
|
||||
std::optional<base::Time> server_time = GetResponseHeaders()->GetDateValue();
|
||||
|
||||
bool force_ignore_site_for_cookies =
|
||||
request_->force_ignore_site_for_cookies();
|
||||
|
Reference in New Issue
Block a user