xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_entry.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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_SPDY_CORE_HPACK_HPACK_ENTRY_H_
6 #define QUICHE_SPDY_CORE_HPACK_HPACK_ENTRY_H_
7 
8 #include <cstddef>
9 #include <string>
10 #include <utility>
11 
12 #include "absl/strings/string_view.h"
13 #include "quiche/common/platform/api/quiche_export.h"
14 
15 // All section references below are to
16 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08
17 
18 namespace spdy {
19 
20 // The constant amount added to name().size() and value().size() to
21 // get the size of an HpackEntry as defined in 5.1.
22 inline constexpr size_t kHpackEntrySizeOverhead = 32;
23 
24 // A structure for looking up entries in the static and dynamic tables.
25 struct QUICHE_EXPORT HpackLookupEntry {
26   absl::string_view name;
27   absl::string_view value;
28 
29   bool operator==(const HpackLookupEntry& other) const {
30     return name == other.name && value == other.value;
31   }
32 
33   // Abseil hashing framework extension according to absl/hash/hash.h:
34   template <typename H>
AbslHashValueHpackLookupEntry35   friend H AbslHashValue(H h, const HpackLookupEntry& entry) {
36     return H::combine(std::move(h), entry.name, entry.value);
37   }
38 };
39 
40 // A structure for an entry in the static table (3.3.1)
41 // and the header table (3.3.2).
42 class QUICHE_EXPORT HpackEntry {
43  public:
44   HpackEntry(std::string name, std::string value);
45 
46   // Make HpackEntry non-copyable to make sure it is always moved.
47   HpackEntry(const HpackEntry&) = delete;
48   HpackEntry& operator=(const HpackEntry&) = delete;
49 
50   HpackEntry(HpackEntry&&) = default;
51   HpackEntry& operator=(HpackEntry&&) = default;
52 
53   // Getters for std::string members traditionally return const std::string&.
54   // However, HpackHeaderTable uses string_view as keys in the maps
55   // static_name_index_ and dynamic_name_index_.  If HpackEntry::name() returned
56   // const std::string&, then
57   //   dynamic_name_index_.insert(std::make_pair(entry.name(), index));
58   // would silently create a dangling reference: make_pair infers type from the
59   // return type of entry.name() and silently creates a temporary string copy.
60   // Insert creates a string_view that points to this copy, which then
61   // immediately goes out of scope and gets destroyed.  While this is quite easy
62   // to avoid, for example, by explicitly specifying type as a template
63   // parameter to make_pair, returning string_view here is less error-prone.
name()64   absl::string_view name() const { return name_; }
value()65   absl::string_view value() const { return value_; }
66 
67   // Returns the size of an entry as defined in 5.1.
68   static size_t Size(absl::string_view name, absl::string_view value);
69   size_t Size() const;
70 
71   std::string GetDebugString() const;
72 
73  private:
74   std::string name_;
75   std::string value_;
76 };
77 
78 }  // namespace spdy
79 
80 #endif  // QUICHE_SPDY_CORE_HPACK_HPACK_ENTRY_H_
81