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