|
9 | 9 | #include <proxygen/lib/http/webtransport/HTTPWebTransport.h>
|
10 | 10 |
|
11 | 11 | #include <proxygen/lib/http/HTTPMessage.h>
|
12 |
| -#include <proxygen/lib/http/structuredheaders/StructuredHeadersDecoder.h> |
13 |
| -#include <proxygen/lib/http/structuredheaders/StructuredHeadersEncoder.h> |
14 | 12 |
|
15 | 13 | namespace proxygen {
|
16 | 14 |
|
17 |
| -using WTProtocolError = HTTPWebTransport::WTProtocolError; |
18 |
| - |
19 | 15 | /*static*/ bool HTTPWebTransport::isConnectMessage(const HTTPMessage& msg) {
|
20 | 16 | constexpr std::string_view kWebTransport{"webtransport"};
|
21 | 17 | return msg.isRequest() && msg.getMethod() == proxygen::HTTPMethod::CONNECT &&
|
22 | 18 | msg.getUpgradeProtocol() && *msg.getUpgradeProtocol() == kWebTransport;
|
23 | 19 | }
|
24 | 20 |
|
25 |
| -/*static*/ void HTTPWebTransport::setWTAvailableProtocols( |
26 |
| - HTTPMessage& msg, const std::vector<std::string>& protocols) { |
27 |
| - std::vector<StructuredHeaders::StructuredHeaderItem> items; |
28 |
| - items.reserve(protocols.size()); |
29 |
| - for (const auto& protocol : protocols) { |
30 |
| - items.emplace_back(StructuredHeaderItem::Type::STRING, protocol); |
31 |
| - } |
32 |
| - |
33 |
| - if (!items.empty()) { |
34 |
| - StructuredHeadersEncoder encoder; |
35 |
| - encoder.encodeList(items); |
36 |
| - msg.getHeaders().set(headers::kWTAvailableProtocols, encoder.get()); |
37 |
| - } |
38 |
| -} |
39 |
| - |
40 |
| -/*static*/ void HTTPWebTransport::setWTProtocol(HTTPMessage& msg, |
41 |
| - std::string protocol) { |
42 |
| - StructuredHeadersEncoder encoder; |
43 |
| - StructuredHeaderItem item(StructuredHeaderItem::Type::STRING, protocol); |
44 |
| - encoder.encodeItem(item); |
45 |
| - msg.getHeaders().set(headers::kWTProtocol, encoder.get()); |
46 |
| -} |
47 |
| - |
48 |
| -/*static*/ folly::Expected<std::vector<std::string>, WTProtocolError> |
49 |
| -HTTPWebTransport::getWTAvailableProtocols(const HTTPMessage& msg) { |
50 |
| - auto header = msg.getHeaders().combine(headers::kWTAvailableProtocols); |
51 |
| - if (header.empty()) { |
52 |
| - return folly::makeUnexpected(WTProtocolError::HeaderMissing); |
53 |
| - } |
54 |
| - |
55 |
| - StructuredHeadersDecoder decoder(header); |
56 |
| - std::vector<StructuredHeaderItem> list; |
57 |
| - |
58 |
| - if (decoder.decodeList(list) != DecodeError::OK) { |
59 |
| - return folly::makeUnexpected(WTProtocolError::ParseFailed); |
60 |
| - } |
61 |
| - |
62 |
| - std::vector<std::string> protocols; |
63 |
| - for (const auto& item : list) { |
64 |
| - if (item.tag != StructuredHeaders::StructuredHeaderItem::Type::STRING) { |
65 |
| - return folly::makeUnexpected(WTProtocolError::ParseFailed); |
66 |
| - } |
67 |
| - protocols.emplace_back(item.get<std::string>()); |
68 |
| - } |
69 |
| - |
70 |
| - if (protocols.empty()) { |
71 |
| - return folly::makeUnexpected(WTProtocolError::EmptyList); |
72 |
| - } |
73 |
| - |
74 |
| - return protocols; |
75 |
| -} |
76 |
| - |
77 |
| -/*static*/ folly::Expected<std::string, WTProtocolError> |
78 |
| -HTTPWebTransport::getWTProtocol(const HTTPMessage& msg) { |
79 |
| - auto header = msg.getHeaders().getSingleOrEmpty(headers::kWTProtocol); |
80 |
| - if (header.empty()) { |
81 |
| - return folly::makeUnexpected(WTProtocolError::HeaderMissing); |
82 |
| - } |
83 |
| - |
84 |
| - StructuredHeadersDecoder decoder(header); |
85 |
| - StructuredHeaderItem item; |
86 |
| - |
87 |
| - if (decoder.decodeItem(item) != DecodeError::OK) { |
88 |
| - return folly::makeUnexpected(WTProtocolError::ParseFailed); |
89 |
| - } |
90 |
| - if (item.tag != StructuredHeaders::StructuredHeaderItem::Type::STRING) { |
91 |
| - return folly::makeUnexpected(WTProtocolError::ParseFailed); |
92 |
| - } |
93 |
| - |
94 |
| - return item.get<std::string>(); |
95 |
| -} |
96 |
| - |
97 |
| -/*static*/ folly::Optional<std::string> HTTPWebTransport::negotiateWTProtocol( |
98 |
| - const std::vector<std::string>& wtAvailableProtocols, |
99 |
| - const std::vector<std::string>& supportedProtocols) { |
100 |
| - for (const auto& protocol : supportedProtocols) { |
101 |
| - if (std::find(wtAvailableProtocols.begin(), |
102 |
| - wtAvailableProtocols.end(), |
103 |
| - protocol) != wtAvailableProtocols.end()) { |
104 |
| - return protocol; |
105 |
| - } |
106 |
| - } |
107 |
| - |
108 |
| - return folly::none; |
109 |
| -} |
110 |
| - |
111 | 21 | }; // namespace proxygen
|
0 commit comments