1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_SPDY_SPDY_HTTP_UTILS_H_ 6 #define NET_SPDY_SPDY_HTTP_UTILS_H_ 7 8 #include <optional> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/types/expected.h" 12 #include "net/base/net_export.h" 13 #include "net/base/request_priority.h" 14 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h" 15 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_framer.h" 16 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h" 17 #include "url/gurl.h" 18 19 namespace net { 20 21 class HttpResponseInfo; 22 struct HttpRequestInfo; 23 class HttpRequestHeaders; 24 class HttpResponseHeaders; 25 26 // HTTP Extensible Priorities header (in lowercase HTTP2/3). 27 // RFC 9218. 28 NET_EXPORT extern const char* const kHttp2PriorityHeader; 29 30 // Convert a spdy::Http2HeaderBlock into an HttpResponseInfo with some checks. 31 // `headers` input parameter with the spdy::Http2HeaderBlock. 32 // `response` output parameter for the HttpResponseInfo. 33 // Returns OK if successfully converted. An error is returned if the 34 // spdy::Http2HeaderBlock is incomplete (e.g. missing 'status' or 'version') or 35 // checks fail. 36 NET_EXPORT int SpdyHeadersToHttpResponse(const spdy::Http2HeaderBlock& headers, 37 HttpResponseInfo* response); 38 39 // Converts a spdy::Http2HeaderBlock object into an HttpResponseHeaders object 40 // by creating a string with embedded nul bytes instead of newlines and then 41 // parsing it to the HttpResponseHeaders constructor to be parsed. Exposed for 42 // testing. 43 // TODO(https://crbug.com/1485670): Remove this once it is no longer needed. 44 NET_EXPORT_PRIVATE base::expected<scoped_refptr<HttpResponseHeaders>, int> 45 SpdyHeadersToHttpResponseHeadersUsingRawString( 46 const spdy::Http2HeaderBlock& headers); 47 48 // Converts a spdy::Http2HeaderBlock object into an HttpResponseHeaders object 49 // by using the HttpResponseHeaders::Builder API. Exposed for testing. 50 // TODO(https://crbug.com/1485670): Merge this back into 51 // SpdyHeadersToHttpResponse() when 52 // SpdyHeadersToHttpResponseHeadersUsingRawString() is removed. 53 NET_EXPORT_PRIVATE base::expected<scoped_refptr<HttpResponseHeaders>, int> 54 SpdyHeadersToHttpResponseHeadersUsingBuilder( 55 const spdy::Http2HeaderBlock& headers); 56 57 // Create a spdy::Http2HeaderBlock from HttpRequestInfo and 58 // HttpRequestHeaders. 59 NET_EXPORT void CreateSpdyHeadersFromHttpRequest( 60 const HttpRequestInfo& info, 61 std::optional<RequestPriority> priority, 62 const HttpRequestHeaders& request_headers, 63 spdy::Http2HeaderBlock* headers); 64 65 // Create a spdy::Http2HeaderBlock from HttpRequestInfo and 66 // HttpRequestHeaders, with the given protocol for extended CONNECT. 67 // The request's method must be `CONNECT`. 68 NET_EXPORT void CreateSpdyHeadersFromHttpRequestForExtendedConnect( 69 const HttpRequestInfo& info, 70 std::optional<RequestPriority> priority, 71 const std::string& ext_connect_protocol, 72 const HttpRequestHeaders& request_headers, 73 spdy::Http2HeaderBlock* headers); 74 75 // Create a spdy::Http2HeaderBlock from HttpRequestInfo and HttpRequestHeaders 76 // for a WebSockets over HTTP/2 request. 77 NET_EXPORT void CreateSpdyHeadersFromHttpRequestForWebSocket( 78 const GURL& url, 79 const HttpRequestHeaders& request_headers, 80 spdy::Http2HeaderBlock* headers); 81 82 // Create HttpRequestHeaders from spdy::Http2HeaderBlock. 83 NET_EXPORT void ConvertHeaderBlockToHttpRequestHeaders( 84 const spdy::Http2HeaderBlock& spdy_headers, 85 HttpRequestHeaders* http_headers); 86 87 NET_EXPORT spdy::SpdyPriority ConvertRequestPriorityToSpdyPriority( 88 RequestPriority priority); 89 90 NET_EXPORT RequestPriority 91 ConvertSpdyPriorityToRequestPriority(spdy::SpdyPriority priority); 92 93 } // namespace net 94 95 #endif // NET_SPDY_SPDY_HTTP_UTILS_H_ 96