-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to handle WebTransport subprotocol negotiation headers.
Based on ietf-wg-webtrans/draft-ietf-webtrans-http3#144 PiperOrigin-RevId: 583029408
- Loading branch information
1 parent
8cbf649
commit a20f97d
Showing
8 changed files
with
214 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "quiche/web_transport/web_transport_headers.h" | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/str_cat.h" | ||
#include "absl/strings/str_join.h" | ||
#include "absl/strings/string_view.h" | ||
#include "absl/types/span.h" | ||
#include "quiche/common/structured_headers.h" | ||
|
||
namespace webtransport { | ||
|
||
using ::quiche::structured_headers::ItemTypeToString; | ||
using ::quiche::structured_headers::List; | ||
using ::quiche::structured_headers::ParameterizedItem; | ||
using ::quiche::structured_headers::ParameterizedMember; | ||
|
||
absl::StatusOr<std::vector<std::string>> ParseSubprotocolRequestHeader( | ||
absl::string_view value) { | ||
std::optional<List> parsed = quiche::structured_headers::ParseList(value); | ||
if (!parsed.has_value()) { | ||
return absl::InvalidArgumentError( | ||
"Failed to parse the header as an sf-list"); | ||
} | ||
|
||
std::vector<std::string> result; | ||
result.reserve(parsed->size()); | ||
for (ParameterizedMember& member : *parsed) { | ||
if (member.member_is_inner_list || member.member.size() != 1) { | ||
return absl::InvalidArgumentError( | ||
"Expected all members to be tokens, found a nested list instead"); | ||
} | ||
ParameterizedItem& item = member.member[0]; | ||
if (!item.item.is_token()) { | ||
return absl::InvalidArgumentError( | ||
absl::StrCat("Expected all members to be tokens, found ", | ||
ItemTypeToString(item.item.Type()), " instead")); | ||
} | ||
result.push_back(std::move(item).item.TakeString()); | ||
} | ||
return result; | ||
} | ||
|
||
absl::StatusOr<std::string> SerializeSubprotocolRequestHeader( | ||
absl::Span<const std::string> subprotocols) { | ||
// Serialize tokens manually via a simple StrJoin call; this lets us provide | ||
// better error messages, and is probably more efficient too. | ||
for (const std::string& token : subprotocols) { | ||
if (!quiche::structured_headers::IsValidToken(token)) { | ||
return absl::InvalidArgumentError(absl::StrCat("Invalid token: ", token)); | ||
} | ||
} | ||
return absl::StrJoin(subprotocols, ", "); | ||
} | ||
|
||
} // namespace webtransport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2023 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef QUICHE_WEB_TRANSPORT_WEB_TRANSPORT_HEADERS_H_ | ||
#define QUICHE_WEB_TRANSPORT_WEB_TRANSPORT_HEADERS_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "absl/strings/string_view.h" | ||
#include "absl/types/span.h" | ||
#include "quiche/common/platform/api/quiche_export.h" | ||
|
||
namespace webtransport { | ||
|
||
inline constexpr absl::string_view kSubprotocolRequestHeader = | ||
"WebTransport-Subprotocols-Available"; | ||
inline constexpr absl::string_view kSubprotocolResponseHeader = | ||
"WebTransport-Subprotocol"; | ||
|
||
QUICHE_EXPORT absl::StatusOr<std::vector<std::string>> | ||
ParseSubprotocolRequestHeader(absl::string_view value); | ||
QUICHE_EXPORT absl::StatusOr<std::string> SerializeSubprotocolRequestHeader( | ||
absl::Span<const std::string> subprotocols); | ||
|
||
} // namespace webtransport | ||
|
||
#endif // QUICHE_WEB_TRANSPORT_WEB_TRANSPORT_HEADERS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2023 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "quiche/web_transport/web_transport_headers.h" | ||
|
||
#include "absl/status/status.h" | ||
#include "quiche/common/platform/api/quiche_test.h" | ||
#include "quiche/common/test_tools/quiche_test_utils.h" | ||
|
||
namespace webtransport { | ||
namespace { | ||
|
||
using ::quiche::test::IsOkAndHolds; | ||
using ::quiche::test::StatusIs; | ||
using ::testing::ElementsAre; | ||
using ::testing::HasSubstr; | ||
|
||
TEST(WebTransportHeaders, ParseSubprotocolRequestHeader) { | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("test"), | ||
IsOkAndHolds(ElementsAre("test"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("moqt-draft01, moqt-draft02"), | ||
IsOkAndHolds(ElementsAre("moqt-draft01", "moqt-draft02"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("moqt-draft01; a=b, moqt-draft02"), | ||
IsOkAndHolds(ElementsAre("moqt-draft01", "moqt-draft02"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("moqt-draft01, moqt-draft02; a=b"), | ||
IsOkAndHolds(ElementsAre("moqt-draft01", "moqt-draft02"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("\"test\""), | ||
StatusIs(absl::StatusCode::kInvalidArgument, | ||
HasSubstr("found string instead"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("42"), | ||
StatusIs(absl::StatusCode::kInvalidArgument, | ||
HasSubstr("found integer instead"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("a, (b)"), | ||
StatusIs(absl::StatusCode::kInvalidArgument, | ||
HasSubstr("found a nested list instead"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("a, (b c)"), | ||
StatusIs(absl::StatusCode::kInvalidArgument, | ||
HasSubstr("found a nested list instead"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("foo, ?1, bar"), | ||
StatusIs(absl::StatusCode::kInvalidArgument, | ||
HasSubstr("found boolean instead"))); | ||
EXPECT_THAT(ParseSubprotocolRequestHeader("(a"), | ||
StatusIs(absl::StatusCode::kInvalidArgument, | ||
HasSubstr("parse the header as an sf-list"))); | ||
} | ||
|
||
TEST(WebTransportHeaders, SerializeSubprotocolRequestHeader) { | ||
EXPECT_THAT(SerializeSubprotocolRequestHeader({"test"}), | ||
IsOkAndHolds("test")); | ||
EXPECT_THAT(SerializeSubprotocolRequestHeader({"foo", "bar"}), | ||
IsOkAndHolds("foo, bar")); | ||
EXPECT_THAT(SerializeSubprotocolRequestHeader({"moqt-draft01", "a/b/c"}), | ||
IsOkAndHolds("moqt-draft01, a/b/c")); | ||
EXPECT_THAT( | ||
SerializeSubprotocolRequestHeader({"abcd", "0123", "efgh"}), | ||
StatusIs(absl::StatusCode::kInvalidArgument, "Invalid token: 0123")); | ||
} | ||
|
||
} // namespace | ||
} // namespace webtransport |