Commit 667b0af4 authored by Ian Clelland's avatar Ian Clelland Committed by Commit Bot

Move structured header parser from web_package to a more general location.

Reporting and Document Policy are going to be using SH, and it is more generally
useful as a building block for new features than just Web Packages. This CL
moves it into blink/[public/]common/http, and removes the http_ prefix from
individual files and directories.

Bug: 993790, 1011101
Change-Id: I344e4aad6ebeb6956e441a7ab75478d98d7f60a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1835998Reviewed-by: default avatarKunihiko Sakamoto <ksakamoto@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Commit-Queue: Ian Clelland <iclelland@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702877}
parent 3e63798f
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "content/browser/web_package/signed_exchange_consts.h" #include "content/browser/web_package/signed_exchange_consts.h"
#include "content/browser/web_package/signed_exchange_utils.h" #include "content/browser/web_package/signed_exchange_utils.h"
#include "crypto/sha2.h" #include "crypto/sha2.h"
#include "third_party/blink/public/common/web_package/http_structured_header.h" #include "third_party/blink/public/common/http/structured_header.h"
namespace content { namespace content {
......
...@@ -41,6 +41,7 @@ jumbo_source_set("common") { ...@@ -41,6 +41,7 @@ jumbo_source_set("common") {
"frame/frame_policy.cc", "frame/frame_policy.cc",
"frame/from_ad_state.cc", "frame/from_ad_state.cc",
"frame/user_activation_state.cc", "frame/user_activation_state.cc",
"http/structured_header.cc",
"indexeddb/indexed_db_default_mojom_traits.cc", "indexeddb/indexed_db_default_mojom_traits.cc",
"indexeddb/indexeddb_key.cc", "indexeddb/indexeddb_key.cc",
"indexeddb/indexeddb_key_path.cc", "indexeddb/indexeddb_key_path.cc",
...@@ -85,7 +86,6 @@ jumbo_source_set("common") { ...@@ -85,7 +86,6 @@ jumbo_source_set("common") {
"service_worker/service_worker_type_converters.cc", "service_worker/service_worker_type_converters.cc",
"service_worker/service_worker_utils.cc", "service_worker/service_worker_utils.cc",
"thread_safe_browser_interface_broker_proxy.cc", "thread_safe_browser_interface_broker_proxy.cc",
"web_package/http_structured_header.cc",
"web_package/signed_exchange_consts.cc", "web_package/signed_exchange_consts.cc",
"web_package/signed_exchange_request_matcher.cc", "web_package/signed_exchange_request_matcher.cc",
] ]
...@@ -126,13 +126,13 @@ jumbo_source_set("common") { ...@@ -126,13 +126,13 @@ jumbo_source_set("common") {
fuzzer_test("http_structured_header_fuzzer") { fuzzer_test("http_structured_header_fuzzer") {
sources = [ sources = [
"web_package/http_structured_header_fuzzer.cc", "http/structured_header_fuzzer.cc",
] ]
deps = [ deps = [
":common", ":common",
"//third_party/blink/renderer/platform:blink_fuzzer_test_support", "//third_party/blink/renderer/platform:blink_fuzzer_test_support",
] ]
seed_corpus = "web_package/http_structured_header_corpus" seed_corpus = "http/structured_header_corpus"
} }
test("blink_common_unittests") { test("blink_common_unittests") {
...@@ -158,6 +158,7 @@ jumbo_source_set("common_unittests_sources") { ...@@ -158,6 +158,7 @@ jumbo_source_set("common_unittests_sources") {
"device_memory/approximated_device_memory_unittest.cc", "device_memory/approximated_device_memory_unittest.cc",
"feature_policy/feature_policy_unittest.cc", "feature_policy/feature_policy_unittest.cc",
"frame/user_activation_state_unittest.cc", "frame/user_activation_state_unittest.cc",
"http/structured_header_unittest.cc",
"indexeddb/indexeddb_key_unittest.cc", "indexeddb/indexeddb_key_unittest.cc",
"loader/mime_sniffing_throttle_unittest.cc", "loader/mime_sniffing_throttle_unittest.cc",
"manifest/manifest_icon_selector_unittest.cc", "manifest/manifest_icon_selector_unittest.cc",
...@@ -170,7 +171,6 @@ jumbo_source_set("common_unittests_sources") { ...@@ -170,7 +171,6 @@ jumbo_source_set("common_unittests_sources") {
"origin_trials/trial_token_unittest.cc", "origin_trials/trial_token_unittest.cc",
"origin_trials/trial_token_validator_unittest.cc", "origin_trials/trial_token_validator_unittest.cc",
"test/run_all_unittests.cc", "test/run_all_unittests.cc",
"web_package/http_structured_header_unittest.cc",
"web_package/signed_exchange_request_matcher_unittest.cc", "web_package/signed_exchange_request_matcher_unittest.cc",
] ]
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/public/common/web_package/http_structured_header.h" #include "third_party/blink/public/common/http/structured_header.h"
#include <string> #include <string>
#include <utility> #include <utility>
...@@ -79,7 +79,7 @@ class StructuredHeaderParser { ...@@ -79,7 +79,7 @@ class StructuredHeaderParser {
// byte sequences) are supported, and all types are returned as a string // byte sequences) are supported, and all types are returned as a string
// regardless of the item type. E.g. both 123 (number) and "123" (string) are // regardless of the item type. E.g. both 123 (number) and "123" (string) are
// returned as "123". // returned as "123".
// TODO(ksakamoto): Add support for other types, and return a value with type // TODO(1011101): Add support for other types, and return a value with type
// info. // info.
base::Optional<std::string> ReadItem() { base::Optional<std::string> ReadItem() {
if (input_.empty()) { if (input_.empty()) {
...@@ -289,6 +289,8 @@ class StructuredHeaderParser { ...@@ -289,6 +289,8 @@ class StructuredHeaderParser {
ParameterisedIdentifier::ParameterisedIdentifier( ParameterisedIdentifier::ParameterisedIdentifier(
const ParameterisedIdentifier&) = default; const ParameterisedIdentifier&) = default;
ParameterisedIdentifier& ParameterisedIdentifier::operator=(
const ParameterisedIdentifier&) = default;
ParameterisedIdentifier::ParameterisedIdentifier(const std::string& id, ParameterisedIdentifier::ParameterisedIdentifier(const std::string& id,
const Parameters& ps) const Parameters& ps)
: identifier(id), params(ps) {} : identifier(id), params(ps) {}
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/public/common/web_package/http_structured_header.h" // nogncheck #include "third_party/blink/public/common/http/structured_header.h" // nogncheck
namespace blink { namespace blink {
namespace http_structured_header { namespace http_structured_header {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/public/common/web_package/http_structured_header.h" #include "third_party/blink/public/common/http/structured_header.h"
#include <string> #include <string>
......
...@@ -71,6 +71,7 @@ source_set("headers") { ...@@ -71,6 +71,7 @@ source_set("headers") {
"frame/user_activation_state.h", "frame/user_activation_state.h",
"frame/user_activation_update_source.h", "frame/user_activation_update_source.h",
"frame/user_activation_update_type.h", "frame/user_activation_update_type.h",
"http/structured_header.h",
"indexeddb/indexed_db_default_mojom_traits.h", "indexeddb/indexed_db_default_mojom_traits.h",
"indexeddb/indexeddb_key.h", "indexeddb/indexeddb_key.h",
"indexeddb/indexeddb_key_path.h", "indexeddb/indexeddb_key_path.h",
...@@ -125,7 +126,6 @@ source_set("headers") { ...@@ -125,7 +126,6 @@ source_set("headers") {
"sms/sms_receiver_outcome.h", "sms/sms_receiver_outcome.h",
"user_agent/user_agent_metadata.h", "user_agent/user_agent_metadata.h",
"web_cache/web_cache_resource_type_stats.h", "web_cache/web_cache_resource_type_stats.h",
"web_package/http_structured_header.h",
"web_package/signed_exchange_consts.h", "web_package/signed_exchange_consts.h",
"web_package/signed_exchange_request_matcher.h", "web_package/signed_exchange_request_matcher.h",
] ]
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_PUBLIC_COMMON_WEB_PACKAGE_HTTP_STRUCTURED_HEADER_H_ #ifndef THIRD_PARTY_BLINK_PUBLIC_COMMON_HTTP_STRUCTURED_HEADER_H_
#define THIRD_PARTY_BLINK_PUBLIC_COMMON_WEB_PACKAGE_HTTP_STRUCTURED_HEADER_H_ #define THIRD_PARTY_BLINK_PUBLIC_COMMON_HTTP_STRUCTURED_HEADER_H_
#include <map> #include <map>
#include <string> #include <string>
...@@ -16,6 +16,36 @@ ...@@ -16,6 +16,36 @@
namespace blink { namespace blink {
namespace http_structured_header { namespace http_structured_header {
// This file implements parsing of HTTP structured headers, as defined in
// https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html.
//
// Currently supported data types are:
// Item:
// integer: 123
// string: "abc"
// token: abc
// byte sequence: *YWJj*
// Parameterised list: abc_123;a=1;b=2; cdef_456, ghi;q="9";r="w"
// List-of-lists: "foo";"bar", "baz", "bat"; "one"
//
// Functions are provided to parse each of these, which are intended to be
// called with the complete value of an HTTP header (that is, any
// sub-structure will be handled internally by the parser; the exported
// functions are not intended to be called on partial header strings.) Input
// values should be ASCII byte strings (non-ASCII characters should not be
// present in Structured Header values, and will cause the entire header to fail
// to parse.)
//
// Note that all values of Item type are currently returned as strings. The
// string, token and byte sequence examples above would all be returned by the
// parser as the string "abc". It is not currently possible to determine which
// type of Item was parsed from a given header.
// TODO(1011101): Return values with type information attached.
//
// TODO(1011101): The current parser implementation is of draft #9
// (https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-09) from
// December 2018; this should be brought up to date with the latest changes.
struct BLINK_COMMON_EXPORT ParameterisedIdentifier { struct BLINK_COMMON_EXPORT ParameterisedIdentifier {
using Parameters = std::map<std::string, std::string>; using Parameters = std::map<std::string, std::string>;
...@@ -23,6 +53,7 @@ struct BLINK_COMMON_EXPORT ParameterisedIdentifier { ...@@ -23,6 +53,7 @@ struct BLINK_COMMON_EXPORT ParameterisedIdentifier {
Parameters params; Parameters params;
ParameterisedIdentifier(const ParameterisedIdentifier&); ParameterisedIdentifier(const ParameterisedIdentifier&);
ParameterisedIdentifier& operator=(const ParameterisedIdentifier&);
ParameterisedIdentifier(const std::string&, const Parameters&); ParameterisedIdentifier(const std::string&, const Parameters&);
~ParameterisedIdentifier(); ~ParameterisedIdentifier();
}; };
...@@ -30,14 +61,27 @@ struct BLINK_COMMON_EXPORT ParameterisedIdentifier { ...@@ -30,14 +61,27 @@ struct BLINK_COMMON_EXPORT ParameterisedIdentifier {
using ParameterisedList = std::vector<ParameterisedIdentifier>; using ParameterisedList = std::vector<ParameterisedIdentifier>;
using ListOfLists = std::vector<std::vector<std::string>>; using ListOfLists = std::vector<std::vector<std::string>>;
// Returns the string representation of the header value, if it can be parsed as
// an Item, or nullopt if it cannot. Note that the returned string is not
// guaranteed to have any encoding, as it may have been a Byte Sequence.
BLINK_COMMON_EXPORT base::Optional<std::string> ParseItem( BLINK_COMMON_EXPORT base::Optional<std::string> ParseItem(
const base::StringPiece& str); const base::StringPiece& str);
// Returns the result of parsing the header value as a Parameterised List, if it
// can be parsed as one, or nullopt if it cannot. Note that list items, as well
// as parameter values, will be returned as strings, and that those strings are
// not guaranteed to have any encoding, as they may have been Byte Sequences.
BLINK_COMMON_EXPORT base::Optional<ParameterisedList> ParseParameterisedList( BLINK_COMMON_EXPORT base::Optional<ParameterisedList> ParseParameterisedList(
const base::StringPiece& str); const base::StringPiece& str);
// Returns the result of parsing the header value as a List of Lists, if it can
// be parsed as one, or nullopt if it cannot. Note that inner list items will be
// be returned as strings, and that those strings are not guaranteed to have any
// encoding, as they may have been Byte Sequences.
BLINK_COMMON_EXPORT base::Optional<ListOfLists> ParseListOfLists( BLINK_COMMON_EXPORT base::Optional<ListOfLists> ParseListOfLists(
const base::StringPiece& str); const base::StringPiece& str);
} // namespace http_structured_header } // namespace http_structured_header
} // namespace blink } // namespace blink
#endif // THIRD_PARTY_BLINK_PUBLIC_COMMON_WEB_PACKAGE_HTTP_STRUCTURED_HEADER_H_ #endif // THIRD_PARTY_BLINK_PUBLIC_COMMON_HTTP_STRUCTURED_HEADER_H_
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
#include "third_party/blink/public/common/common_export.h" #include "third_party/blink/public/common/common_export.h"
#include "third_party/blink/public/common/web_package/http_structured_header.h" #include "third_party/blink/public/common/http/structured_header.h"
namespace blink { namespace blink {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment