xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/http/quic_header_list.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2016 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_QUIC_HEADER_LIST_H_
6 #define QUICHE_QUIC_CORE_HTTP_QUIC_HEADER_LIST_H_
7 
8 #include <algorithm>
9 #include <functional>
10 #include <string>
11 #include <utility>
12 
13 #include "absl/strings/string_view.h"
14 #include "quiche/quic/platform/api/quic_bug_tracker.h"
15 #include "quiche/quic/platform/api/quic_export.h"
16 #include "quiche/common/quiche_circular_deque.h"
17 #include "quiche/spdy/core/spdy_headers_handler_interface.h"
18 
19 namespace quic {
20 
21 // A simple class that accumulates header pairs
22 class QUICHE_EXPORT QuicHeaderList : public spdy::SpdyHeadersHandlerInterface {
23  public:
24   using ListType =
25       quiche::QuicheCircularDeque<std::pair<std::string, std::string>>;
26   using value_type = ListType::value_type;
27   using const_iterator = ListType::const_iterator;
28 
29   QuicHeaderList();
30   QuicHeaderList(QuicHeaderList&& other);
31   QuicHeaderList(const QuicHeaderList& other);
32   QuicHeaderList& operator=(QuicHeaderList&& other);
33   QuicHeaderList& operator=(const QuicHeaderList& other);
34   ~QuicHeaderList() override;
35 
36   // From SpdyHeadersHandlerInteface.
37   void OnHeaderBlockStart() override;
38   void OnHeader(absl::string_view name, absl::string_view value) override;
39   void OnHeaderBlockEnd(size_t uncompressed_header_bytes,
40                         size_t compressed_header_bytes) override;
41 
42   void Clear();
43 
begin()44   const_iterator begin() const { return header_list_.begin(); }
end()45   const_iterator end() const { return header_list_.end(); }
46 
empty()47   bool empty() const { return header_list_.empty(); }
uncompressed_header_bytes()48   size_t uncompressed_header_bytes() const {
49     return uncompressed_header_bytes_;
50   }
compressed_header_bytes()51   size_t compressed_header_bytes() const { return compressed_header_bytes_; }
52 
53   // Deprecated.  TODO(b/145909215): remove.
set_max_header_list_size(size_t max_header_list_size)54   void set_max_header_list_size(size_t max_header_list_size) {
55     max_header_list_size_ = max_header_list_size;
56   }
57 
58   std::string DebugString() const;
59 
60  private:
61   quiche::QuicheCircularDeque<std::pair<std::string, std::string>> header_list_;
62 
63   // The limit on the size of the header list (defined by spec as name + value +
64   // overhead for each header field). Headers over this limit will not be
65   // buffered, and the list will be cleared upon OnHeaderBlockEnd.
66   size_t max_header_list_size_;
67 
68   // Defined per the spec as the size of all header fields with an additional
69   // overhead for each field.
70   size_t current_header_list_size_;
71 
72   // TODO(dahollings) Are these fields necessary?
73   size_t uncompressed_header_bytes_;
74   size_t compressed_header_bytes_;
75 };
76 
77 inline bool operator==(const QuicHeaderList& l1, const QuicHeaderList& l2) {
78   auto pred = [](const std::pair<std::string, std::string>& p1,
79                  const std::pair<std::string, std::string>& p2) {
80     return p1.first == p2.first && p1.second == p2.second;
81   };
82   return std::equal(l1.begin(), l1.end(), l2.begin(), pred);
83 }
84 
85 }  // namespace quic
86 
87 #endif  // QUICHE_QUIC_CORE_HTTP_QUIC_HEADER_LIST_H_
88