chromium/third_party/blink/renderer/platform/network/http_parsers.cc

/*
 * Copyright (C) 2006 Alexey Proskuryakov ([email protected])
 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 * Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
 * Copyright (C) 2009 Google Inc. All rights reserved.
 * Copyright (C) 2011 Apple 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.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "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 OR ITS 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/network/http_parsers.h"

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

#include "base/containers/flat_map.h"
#include "base/feature_list.h"
#include "net/http/http_content_disposition.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/content_security_policy/content_security_policy.h"
#include "services/network/public/cpp/no_vary_search_header_parser.h"
#include "services/network/public/cpp/parsed_headers.h"
#include "services/network/public/cpp/timing_allow_origin_parser.h"
#include "services/network/public/mojom/no_vary_search.mojom-blink-forward.h"
#include "services/network/public/mojom/no_vary_search.mojom-blink.h"
#include "services/network/public/mojom/parsed_headers.mojom-blink.h"
#include "services/network/public/mojom/supports_loading_mode.mojom-blink.h"
#include "services/network/public/mojom/timing_allow_origin.mojom-blink.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/mime_util/mime_util.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include "third_party/blink/renderer/platform/network/header_field_tokenizer.h"
#include "third_party/blink/renderer/platform/network/http_names.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/date_math.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/text/character_names.h"
#include "third_party/blink/renderer/platform/wtf/text/parsing_utilities.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"

// We would like finding a way to convert from/to blink type automatically.
// The following attempt has been withdrawn:
// https://chromium-review.googlesource.com/c/chromium/src/+/2126933/7
//
// Note: nesting these helpers inside network::mojom bypasses warnings from
// audit_non_blink_style.py, as well as saving a bunch of typing to qualify the
// types below.
namespace network {
namespace mojom {

// When adding a new conversion, define a new `ConvertToBlink` overload to map
// the non-Blink type (passing by value for primitive types or passing by const
// reference otherwise). The generic converters for container types relies on
// the presence of `ConvertToBlink` overloads to determine the correct return
// type.

// ===== Identity converters =====
// Converts where the input type and output type are identical(-ish).
uint8_t ConvertToBlink(uint8_t in) {}

// Note: for identity enum conversions, there should be `static_assert`s that
// the input enumerator and the output enumerator define matching values.
blink::CSPDirectiveName ConvertToBlink(CSPDirectiveName name) {}

// `in` is a Mojo enum type, which is type aliased to the same underlying type
// by both the non-Blink Mojo variant and the Blink Mojo variant.
blink::WebClientHintsType ConvertToBlink(WebClientHintsType in) {}

blink::LoadingMode ConvertToBlink(LoadingMode in) {}

// ===== Converters for other basic Blink types =====
String ConvertToBlink(const std::string& in) {}

String ConvertToBlink(const std::optional<std::string>& in) {}

::blink::KURL ConvertToBlink(const GURL& in) {}

scoped_refptr<const ::blink::SecurityOrigin> ConvertToBlink(
    const url::Origin& in) {}

// ====== Generic container converters =====
template <
    typename InElement,
    typename OutElement = decltype(ConvertToBlink(std::declval<InElement>()))>
Vector<OutElement> ConvertToBlink(const std::vector<InElement>& in) {}

template <typename InKey,
          typename InValue,
          typename OutKey = decltype(ConvertToBlink(std::declval<InKey>())),
          typename OutValue = decltype(ConvertToBlink(std::declval<InValue>()))>
HashMap<OutKey, OutValue> ConvertToBlink(
    const base::flat_map<InKey, InValue>& in) {}

// ===== Converters from non-Blink to Blink variant of Mojo structs =====
blink::CSPSourcePtr ConvertToBlink(const CSPSourcePtr& in) {}

blink::CSPHashSourcePtr ConvertToBlink(const CSPHashSourcePtr& in) {}

blink::CSPSourceListPtr ConvertToBlink(const CSPSourceListPtr& source_list) {}

blink::ContentSecurityPolicyHeaderPtr ConvertToBlink(
    const ContentSecurityPolicyHeaderPtr& in) {}

blink::CSPTrustedTypesPtr ConvertToBlink(const CSPTrustedTypesPtr& in) {}

blink::ContentSecurityPolicyPtr ConvertToBlink(
    const ContentSecurityPolicyPtr& in) {}

blink::AllowCSPFromHeaderValuePtr ConvertToBlink(
    const AllowCSPFromHeaderValuePtr& allow_csp_from) {}

blink::LinkHeaderPtr ConvertToBlink(const LinkHeaderPtr& in) {}

blink::TimingAllowOriginPtr ConvertToBlink(const TimingAllowOriginPtr& in) {}

blink::NoVarySearchWithParseErrorPtr ConvertToBlink(
    const NoVarySearchWithParseErrorPtr& in) {}

blink::ParsedHeadersPtr ConvertToBlink(const ParsedHeadersPtr& in) {}

}  // namespace mojom
}  // namespace network

namespace blink {

namespace {

const Vector<AtomicString>& ReplaceHeaders() {}

bool IsWhitespace(UChar chr) {}

// true if there is more to parse, after incrementing pos past whitespace.
// Note: Might return pos == str.length()
// if |matcher| is nullptr, isWhitespace() is used.
inline bool SkipWhiteSpace(const String& str,
                           unsigned& pos,
                           WTF::CharacterMatchFunctionPtr matcher = nullptr) {}

template <typename CharType>
inline bool IsASCIILowerAlphaOrDigit(CharType c) {}

template <typename CharType>
inline bool IsASCIILowerAlphaOrDigitOrHyphen(CharType c) {}

// Parse a number with ignoring trailing [0-9.].
// Returns false if the source contains invalid characters.
bool ParseRefreshTime(const String& source, base::TimeDelta& delay) {}

}  // namespace

bool IsValidHTTPHeaderValue(const String& name) {}

// See RFC 7230, Section 3.2.6.
bool IsValidHTTPToken(const String& characters) {}

bool IsContentDispositionAttachment(const String& content_disposition) {}

// https://html.spec.whatwg.org/C/#attr-meta-http-equiv-refresh
bool ParseHTTPRefresh(const String& refresh,
                      WTF::CharacterMatchFunctionPtr matcher,
                      base::TimeDelta& delay,
                      String& url) {}

std::optional<base::Time> ParseDate(const String& value) {}

AtomicString ExtractMIMETypeFromMediaType(const AtomicString& media_type) {}

bool IsHTTPTabOrSpace(UChar c) {}

// https://mimesniff.spec.whatwg.org/#minimize-a-supported-mime-type
// Note that `mime_type` should already have been stripped of parameters by
// `ExtractMIMETypeFromMediaType`.
AtomicString MinimizedMIMEType(const AtomicString& mime_type) {}

ContentTypeOptionsDisposition ParseContentTypeOptionsHeader(
    const String& value) {}

static bool IsCacheHeaderSeparator(UChar c) {}

static bool IsControlCharacter(UChar c) {}

static inline String TrimToNextSeparator(const String& str) {}

static void ParseCacheHeader(const String& header,
                             Vector<std::pair<String, String>>& result) {}

CacheControlHeader ParseCacheControlDirectives(
    const AtomicString& cache_control_value,
    const AtomicString& pragma_value) {}

void ParseCommaDelimitedHeader(const String& header_value,
                               CommaDelimitedHeaderSet& header_set) {}

bool ParseMultipartHeadersFromBody(const char* bytes,
                                   wtf_size_t size,
                                   ResourceResponse* response,
                                   wtf_size_t* end) {}

bool ParseMultipartFormHeadersFromBody(const char* bytes,
                                       wtf_size_t size,
                                       HTTPHeaderMap* header_fields,
                                       wtf_size_t* end) {}

bool ParseContentRangeHeaderFor206(const String& content_range,
                                   int64_t* first_byte_position,
                                   int64_t* last_byte_position,
                                   int64_t* instance_length) {}

std::unique_ptr<ServerTimingHeaderVector> ParseServerTimingHeader(
    const String& headerValue) {}

// This function is simply calling network::ParseHeaders and convert from/to
// blink types. It is used for navigation requests served by a ServiceWorker. It
// is tested by FetchResponseDataTest.ContentSecurityPolicy.
network::mojom::blink::ParsedHeadersPtr ParseHeaders(const String& raw_headers,
                                                     const KURL& url) {}

// This function is simply calling network::ParseContentSecurityPolicies and
// converting from/to blink types.
Vector<network::mojom::blink::ContentSecurityPolicyPtr>
ParseContentSecurityPolicies(
    const String& raw_policies,
    network::mojom::blink::ContentSecurityPolicyType type,
    network::mojom::blink::ContentSecurityPolicySource source,
    const KURL& base_url) {}

// This function is simply calling network::ParseContentSecurityPolicies and
// converting from/to blink types.
Vector<network::mojom::blink::ContentSecurityPolicyPtr>
ParseContentSecurityPolicies(
    const String& raw_policies,
    network::mojom::blink::ContentSecurityPolicyType type,
    network::mojom::blink::ContentSecurityPolicySource source,
    const SecurityOrigin& self_origin) {}

Vector<network::mojom::blink::ContentSecurityPolicyPtr>
ParseContentSecurityPolicyHeaders(
    const ContentSecurityPolicyResponseHeaders& headers) {}

network::mojom::blink::TimingAllowOriginPtr ParseTimingAllowOrigin(
    const String& header_value) {}

network::mojom::blink::NoVarySearchWithParseErrorPtr ParseNoVarySearch(
    const String& header_value) {}

String GetNoVarySearchHintConsoleMessage(
    const network::mojom::NoVarySearchParseError& error) {}
}  // namespace blink