1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/*
* Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include <string>
#include <string_view>
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/memory/scoped_refptr.h"
#include "net/http/structured_headers.h"
#include "net/ssl/ssl_info.h"
#include "services/network/public/cpp/cors/cors.h"
#include "services/network/public/mojom/fetch_api.mojom-blink.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/timing/resource_timing.mojom-blink.h"
#include "third_party/blink/public/platform/web_url_response.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_load_timing.h"
#include "third_party/blink/renderer/platform/loader/fetch/service_worker_router_info.h"
#include "third_party/blink/renderer/platform/loader/unencoded_digest.h"
#include "third_party/blink/renderer/platform/network/http_names.h"
#include "third_party/blink/renderer/platform/network/http_parsers.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/strcat.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
namespace blink {
namespace {
constexpr auto kSupportedContentEncodingValues =
base::MakeFixedFlatSet<std::string_view>({
"br",
"dcb",
"dcz",
"deflate",
"gzip",
"zstd",
});
template <typename Interface>
Vector<Interface> IsolatedCopy(const Vector<Interface>& src) {
Vector<Interface> result;
result.reserve(src.size());
for (const auto& timestamp : src) {
result.push_back(timestamp.IsolatedCopy());
}
return result;
}
} // namespace
ResourceResponse::ResourceResponse()
: was_cached_(false),
connection_reused_(false),
is_null_(true),
have_parsed_age_header_(false),
have_parsed_date_header_(false),
have_parsed_expires_header_(false),
have_parsed_last_modified_header_(false),
has_major_certificate_errors_(false),
has_range_requested_(false),
timing_allow_passed_(false),
was_fetched_via_spdy_(false),
was_fetched_via_service_worker_(false),
did_service_worker_navigation_preload_(false),
did_use_shared_dictionary_(false),
async_revalidation_requested_(false),
is_signed_exchange_inner_response_(false),
is_web_bundle_inner_response_(false),
was_in_prefetch_cache_(false),
was_cookie_in_request_(false),
network_accessed_(false),
from_archive_(false),
was_alternate_protocol_available_(false),
was_alpn_negotiated_(false),
has_authorization_covered_by_wildcard_on_preflight_(false),
is_validated_(false),
request_include_credentials_(true),
should_use_source_hash_for_js_code_cache_(false) {}
ResourceResponse::ResourceResponse(const KURL& current_request_url)
: ResourceResponse() {
SetCurrentRequestUrl(current_request_url);
}
ResourceResponse::ResourceResponse(const ResourceResponse&) = default;
ResourceResponse& ResourceResponse::operator=(const ResourceResponse&) =
default;
ResourceResponse::~ResourceResponse() = default;
bool ResourceResponse::IsHTTP() const {
return current_request_url_.ProtocolIsInHTTPFamily();
}
bool ResourceResponse::ShouldPopulateResourceTiming() const {
return IsHTTP() || is_web_bundle_inner_response_;
}
const KURL& ResourceResponse::CurrentRequestUrl() const {
return current_request_url_;
}
void ResourceResponse::SetCurrentRequestUrl(const KURL& url) {
is_null_ = false;
current_request_url_ = url;
}
KURL ResourceResponse::ResponseUrl() const {
// Ideally ResourceResponse would have a |url_list_| to match Fetch
// specification's URL list concept
// (https://fetch.spec.whatwg.org/#concept-response-url-list), and its
// last element would be returned here.
//
// Instead it has |url_list_via_service_worker_| which is only populated when
// the response came from a service worker, and that response was not created
// through `new Response()`. Use it when available.
if (!url_list_via_service_worker_.empty()) {
DCHECK(WasFetchedViaServiceWorker());
return url_list_via_service_worker_.back();
}
// Otherwise, use the current request URL. This is OK because the Fetch
// specification's "main fetch" algorithm[1] sets the response URL list to the
// request's URL list when the list isn't present. That step can't be
// implemented now because there is no |url_list_| memeber, but effectively
// the same thing happens by returning CurrentRequestUrl() here.
//
// [1] "If internalResponse’s URL list is empty, then set it to a clone of
// request’s URL list." at
// https://fetch.spec.whatwg.org/#ref-for-concept-response-url-list%E2%91%A4
return CurrentRequestUrl();
}
bool ResourceResponse::IsServiceWorkerPassThrough() const {
return cache_storage_cache_name_.empty() &&
!url_list_via_service_worker_.empty() &&
ResponseUrl() == CurrentRequestUrl();
}
const AtomicString& ResourceResponse::MimeType() const {
return mime_type_;
}
void ResourceResponse::SetMimeType(const AtomicString& mime_type) {
is_null_ = false;
// FIXME: MIME type is determined by HTTP Content-Type header. We should
// update the header, so that it doesn't disagree with m_mimeType.
mime_type_ = mime_type;
}
int64_t ResourceResponse::ExpectedContentLength() const {
return expected_content_length_;
}
void ResourceResponse::SetExpectedContentLength(
int64_t expected_content_length) {
is_null_ = false;
// FIXME: Content length is determined by HTTP Content-Length header. We
// should update the header, so that it doesn't disagree with
// m_expectedContentLength.
expected_content_length_ = expected_content_length;
}
const AtomicString& ResourceResponse::TextEncodingName() const {
return text_encoding_name_;
}
void ResourceResponse::SetTextEncodingName(const AtomicString& encoding_name) {
is_null_ = false;
// FIXME: Text encoding is determined by HTTP Content-Type header. We should
// update the header, so that it doesn't disagree with m_textEncodingName.
text_encoding_name_ = encoding_name;
}
int ResourceResponse::HttpStatusCode() const {
return http_status_code_;
}
void ResourceResponse::SetHttpStatusCode(int status_code) {
http_status_code_ = status_code;
}
const AtomicString& ResourceResponse::HttpStatusText() const {
return http_status_text_;
}
void ResourceResponse::SetHttpStatusText(const AtomicString& status_text) {
http_status_text_ = status_text;
}
const AtomicString& ResourceResponse::HttpHeaderField(
const AtomicString& name) const {
return http_header_fields_.Get(name);
}
void ResourceResponse::UpdateHeaderParsedState(const AtomicString& name) {
if (EqualIgnoringASCIICase(name, http_names::kLowerAge)) {
have_parsed_age_header_ = false;
} else if (EqualIgnoringASCIICase(name, http_names::kLowerCacheControl) ||
EqualIgnoringASCIICase(name, http_names::kLowerPragma)) {
cache_control_header_ = CacheControlHeader();
} else if (EqualIgnoringASCIICase(name, http_names::kLowerDate)) {
have_parsed_date_header_ = false;
} else if (EqualIgnoringASCIICase(name, http_names::kLowerExpires)) {
have_parsed_expires_header_ = false;
} else if (EqualIgnoringASCIICase(name, http_names::kLowerLastModified)) {
have_parsed_last_modified_header_ = false;
}
}
void ResourceResponse::SetSSLInfo(const net::SSLInfo& ssl_info) {
DCHECK_NE(security_style_, SecurityStyle::kUnknown);
DCHECK_NE(security_style_, SecurityStyle::kNeutral);
ssl_info_ = ssl_info;
}
void ResourceResponse::SetServiceWorkerRouterInfo(
scoped_refptr<ServiceWorkerRouterInfo> value) {
service_worker_router_info_ = std::move(value);
}
bool ResourceResponse::IsCorsSameOrigin() const {
return network::cors::IsCorsSameOriginResponseType(response_type_);
}
bool ResourceResponse::IsCorsCrossOrigin() const {
return network::cors::IsCorsCrossOriginResponseType(response_type_);
}
void ResourceResponse::SetHttpHeaderField(const AtomicString& name,
const AtomicString& value) {
UpdateHeaderParsedState(name);
http_header_fields_.Set(name, value);
}
void ResourceResponse::AddHttpHeaderField(const AtomicString& name,
const AtomicString& value) {
UpdateHeaderParsedState(name);
HTTPHeaderMap::AddResult result = http_header_fields_.Add(name, value);
if (!result.is_new_entry) {
String new_value = WTF::StrCat({result.stored_value->value, ", ", value});
result.stored_value->value = AtomicString(new_value);
}
}
void ResourceResponse::AddHttpHeaderFieldWithMultipleValues(
const AtomicString& name,
const Vector<AtomicString>& values) {
if (values.empty())
return;
UpdateHeaderParsedState(name);
StringBuilder value_builder;
const auto it = http_header_fields_.Find(name);
if (it != http_header_fields_.end())
value_builder.Append(it->value);
for (const auto& value : values) {
if (!value_builder.empty())
value_builder.Append(", ");
value_builder.Append(value);
}
http_header_fields_.Set(name, value_builder.ToAtomicString());
}
void ResourceResponse::ClearHttpHeaderField(const AtomicString& name) {
http_header_fields_.Remove(name);
}
const HTTPHeaderMap& ResourceResponse::HttpHeaderFields() const {
return http_header_fields_;
}
bool ResourceResponse::CacheControlContainsNoCache() const {
if (!cache_control_header_.parsed) {
cache_control_header_ = ParseCacheControlDirectives(
http_header_fields_.Get(http_names::kLowerCacheControl),
http_header_fields_.Get(http_names::kLowerPragma));
}
return cache_control_header_.contains_no_cache;
}
bool ResourceResponse::CacheControlContainsNoStore() const {
if (!cache_control_header_.parsed) {
cache_control_header_ = ParseCacheControlDirectives(
http_header_fields_.Get(http_names::kLowerCacheControl),
http_header_fields_.Get(http_names::kLowerPragma));
}
return cache_control_header_.contains_no_store;
}
bool ResourceResponse::CacheControlContainsMustRevalidate() const {
if (!cache_control_header_.parsed) {
cache_control_header_ = ParseCacheControlDirectives(
http_header_fields_.Get(http_names::kLowerCacheControl),
http_header_fields_.Get(http_names::kLowerPragma));
}
return cache_control_header_.contains_must_revalidate;
}
bool ResourceResponse::HasCacheValidatorFields() const {
return !http_header_fields_.Get(http_names::kLowerLastModified).empty() ||
!http_header_fields_.Get(http_names::kLowerETag).empty();
}
std::optional<base::TimeDelta> ResourceResponse::CacheControlMaxAge() const {
if (!cache_control_header_.parsed) {
cache_control_header_ = ParseCacheControlDirectives(
http_header_fields_.Get(http_names::kLowerCacheControl),
http_header_fields_.Get(http_names::kLowerPragma));
}
return cache_control_header_.max_age;
}
base::TimeDelta ResourceResponse::CacheControlStaleWhileRevalidate() const {
if (!cache_control_header_.parsed) {
cache_control_header_ = ParseCacheControlDirectives(
http_header_fields_.Get(http_names::kLowerCacheControl),
http_header_fields_.Get(http_names::kLowerPragma));
}
if (!cache_control_header_.stale_while_revalidate ||
cache_control_header_.stale_while_revalidate.value() <
base::TimeDelta()) {
return base::TimeDelta();
}
return cache_control_header_.stale_while_revalidate.value();
}
static std::optional<base::Time> ParseDateValueInHeader(
const HTTPHeaderMap& headers,
const AtomicString& header_name,
UseCounter& use_counter) {
const AtomicString& header_value = headers.Get(header_name);
if (header_value.empty())
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:
//
// > A cache recipient MUST interpret invalid date formats, especially the
// > value "0", as representing a time in the past (i.e., "already expired").
if (header_name == http_names::kLowerExpires && header_value == "0") {
return base::Time::Min();
}
// This handles all date formats required by RFC2616:
// Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
// Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
// Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
std::optional<base::Time> date = ParseDate(header_value, use_counter);
if (date && date.value().is_max())
return std::nullopt;
return date;
}
std::optional<base::Time> ResourceResponse::Date(
UseCounter& use_counter) const {
if (!have_parsed_date_header_) {
date_ = ParseDateValueInHeader(http_header_fields_, http_names::kLowerDate,
use_counter);
have_parsed_date_header_ = true;
}
return date_;
}
std::optional<base::TimeDelta> ResourceResponse::Age() const {
if (!have_parsed_age_header_) {
const AtomicString& header_value =
http_header_fields_.Get(http_names::kLowerAge);
bool ok;
double seconds = header_value.ToDouble(&ok);
if (!ok) {
age_ = std::nullopt;
} else {
age_ = base::Seconds(seconds);
}
have_parsed_age_header_ = true;
}
return age_;
}
std::optional<base::Time> ResourceResponse::Expires(
UseCounter& use_counter) const {
if (!have_parsed_expires_header_) {
expires_ = ParseDateValueInHeader(http_header_fields_,
http_names::kLowerExpires, use_counter);
have_parsed_expires_header_ = true;
}
return expires_;
}
std::optional<base::Time> ResourceResponse::LastModified(
UseCounter& use_counter) const {
if (!have_parsed_last_modified_header_) {
last_modified_ = ParseDateValueInHeader(
http_header_fields_, http_names::kLowerLastModified, use_counter);
have_parsed_last_modified_header_ = true;
}
return last_modified_;
}
std::optional<UnencodedDigest> ResourceResponse::UnencodedDigest(
const FeatureContext* feature_context) const {
if (!RuntimeEnabledFeatures::UnencodedDigestEnabled(feature_context)) {
return std::nullopt;
}
return UnencodedDigest::Create(HttpHeaderFields());
}
bool ResourceResponse::IsAttachment() const {
static const char kAttachmentString[] = "attachment";
String value = http_header_fields_.Get(http_names::kContentDisposition);
wtf_size_t loc = value.find(';');
if (loc != kNotFound)
value = value.Left(loc);
value = value.StripWhiteSpace();
return EqualIgnoringASCIICase(value, kAttachmentString);
}
AtomicString ResourceResponse::HttpContentType() const {
return ExtractMIMETypeFromMediaType(
HttpHeaderField(http_names::kContentType).LowerASCII());
}
AtomicString ResourceResponse::GetFilteredHttpContentEncoding() const {
String content_encoding =
HttpHeaderField(http_names::kContentEncoding).LowerASCII();
if (content_encoding.IsNull() || content_encoding.empty()) {
return g_empty_atom;
}
if (kSupportedContentEncodingValues.contains(content_encoding.Ascii())) {
return AtomicString(content_encoding);
}
if (content_encoding.find(',') != kNotFound) {
return AtomicString("multiple");
}
return AtomicString("unknown");
}
bool ResourceResponse::WasCached() const {
return was_cached_;
}
void ResourceResponse::SetWasCached(bool value) {
was_cached_ = value;
}
bool ResourceResponse::ConnectionReused() const {
return connection_reused_;
}
void ResourceResponse::SetConnectionReused(bool connection_reused) {
connection_reused_ = connection_reused;
}
unsigned ResourceResponse::ConnectionID() const {
return connection_id_;
}
void ResourceResponse::SetConnectionID(unsigned connection_id) {
connection_id_ = connection_id;
}
ResourceLoadTiming* ResourceResponse::GetResourceLoadTiming() const {
return resource_load_timing_.get();
}
void ResourceResponse::SetResourceLoadTiming(
scoped_refptr<ResourceLoadTiming> resource_load_timing) {
resource_load_timing_ = std::move(resource_load_timing);
}
AtomicString ResourceResponse::ConnectionInfoString() const {
std::string_view connection_info_string =
net::HttpConnectionInfoToString(connection_info_);
return AtomicString(base::as_byte_span(connection_info_string));
}
mojom::blink::CacheState ResourceResponse::CacheState() const {
return is_validated_
? mojom::blink::CacheState::kValidated
: (!encoded_data_length_ ? mojom::blink::CacheState::kLocal
: mojom::blink::CacheState::kNone);
}
void ResourceResponse::SetIsValidated(bool is_validated) {
is_validated_ = is_validated;
}
void ResourceResponse::SetEncodedDataLength(int64_t value) {
encoded_data_length_ = value;
}
void ResourceResponse::SetEncodedBodyLength(uint64_t value) {
encoded_body_length_ = value;
}
void ResourceResponse::SetDecodedBodyLength(int64_t value) {
decoded_body_length_ = value;
}
network::mojom::CrossOriginEmbedderPolicyValue
ResourceResponse::GetCrossOriginEmbedderPolicy() const {
const std::string value =
HttpHeaderField(http_names::kLowerCrossOriginEmbedderPolicy).Utf8();
using Item = net::structured_headers::Item;
const auto item = net::structured_headers::ParseItem(value);
if (!item || item->item.Type() != Item::kTokenType) {
return network::mojom::CrossOriginEmbedderPolicyValue::kNone;
}
if (item->item.GetString() == "require-corp") {
return network::mojom::CrossOriginEmbedderPolicyValue::kRequireCorp;
} else if (item->item.GetString() == "credentialless") {
return network::mojom::CrossOriginEmbedderPolicyValue::kCredentialless;
} else {
return network::mojom::CrossOriginEmbedderPolicyValue::kNone;
}
}
STATIC_ASSERT_ENUM(WebURLResponse::kHTTPVersionUnknown,
ResourceResponse::kHTTPVersionUnknown);
STATIC_ASSERT_ENUM(WebURLResponse::kHTTPVersion_0_9,
ResourceResponse::kHTTPVersion_0_9);
STATIC_ASSERT_ENUM(WebURLResponse::kHTTPVersion_1_0,
ResourceResponse::kHTTPVersion_1_0);
STATIC_ASSERT_ENUM(WebURLResponse::kHTTPVersion_1_1,
ResourceResponse::kHTTPVersion_1_1);
STATIC_ASSERT_ENUM(WebURLResponse::kHTTPVersion_2_0,
ResourceResponse::kHTTPVersion_2_0);
} // namespace blink
|