xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/http/spdy_utils.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
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 QUICHE_QUIC_CORE_HTTP_SPDY_UTILS_H_
6 #define QUICHE_QUIC_CORE_HTTP_SPDY_UTILS_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 #include <optional>
11 #include <string>
12 
13 #include "quiche/quic/core/http/http_constants.h"
14 #include "quiche/quic/core/http/quic_header_list.h"
15 #include "quiche/quic/core/quic_packets.h"
16 #include "quiche/quic/platform/api/quic_export.h"
17 #include "quiche/spdy/core/http2_header_block.h"
18 #include "quiche/spdy/core/spdy_alt_svc_wire_format.h"
19 
20 namespace quic {
21 
22 class QUICHE_EXPORT SpdyUtils {
23  public:
24   SpdyUtils() = delete;
25 
26   // Populate |content length| with the value of the content-length header.
27   // Returns true on success, false if parsing fails or content-length header is
28   // missing.
29   static bool ExtractContentLengthFromHeaders(int64_t* content_length,
30                                               spdy::Http2HeaderBlock* headers);
31 
32   // Copies a list of headers to a Http2HeaderBlock.
33   static bool CopyAndValidateHeaders(const QuicHeaderList& header_list,
34                                      int64_t* content_length,
35                                      spdy::Http2HeaderBlock* headers);
36 
37   // Copies a list of headers to a Http2HeaderBlock.
38   // If |expect_final_byte_offset| is true, requires exactly one header field
39   // with key kFinalOffsetHeaderKey and an integer value.
40   // If |expect_final_byte_offset| is false, no kFinalOffsetHeaderKey may be
41   // present.
42   // Returns true if parsing is successful.  Returns false if the presence of
43   // kFinalOffsetHeaderKey does not match the value of
44   // |expect_final_byte_offset|, the kFinalOffsetHeaderKey value cannot be
45   // parsed, any other pseudo-header is present, an empty header key is present,
46   // or a header key contains an uppercase character.
47   static bool CopyAndValidateTrailers(const QuicHeaderList& header_list,
48                                       bool expect_final_byte_offset,
49                                       size_t* final_byte_offset,
50                                       spdy::Http2HeaderBlock* trailers);
51 
52   // Populates the fields of |headers| to make a GET request of |url|,
53   // which must be fully-qualified.
54   static bool PopulateHeaderBlockFromUrl(const std::string url,
55                                          spdy::Http2HeaderBlock* headers);
56 
57   // Returns the advertised QUIC version from the specified alternative service
58   // advertisement, or ParsedQuicVersion::Unsupported() if no supported version
59   // is advertised.
60   static ParsedQuicVersion ExtractQuicVersionFromAltSvcEntry(
61       const spdy::SpdyAltSvcWireFormat::AlternativeService&
62           alternative_service_entry,
63       const ParsedQuicVersionVector& supported_versions);
64 };
65 
66 }  // namespace quic
67 
68 #endif  // QUICHE_QUIC_CORE_HTTP_SPDY_UTILS_H_
69