OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SPDY_HPACK_ENTRY_H_ | 5 #ifndef NET_SPDY_HPACK_ENTRY_H_ |
6 #define NET_SPDY_HPACK_ENTRY_H_ | 6 #define NET_SPDY_HPACK_ENTRY_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <deque> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | |
12 | 11 |
13 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
14 #include "base/macros.h" | 13 #include "base/macros.h" |
15 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
16 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
17 | 16 |
18 namespace net { | |
19 | |
20 // All section references below are to | 17 // All section references below are to |
21 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06 | 18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06 |
22 | 19 |
| 20 namespace net { |
| 21 |
23 // A structure for an entry in the header table (3.1.2) and the | 22 // A structure for an entry in the header table (3.1.2) and the |
24 // reference set (3.1.3). This structure also keeps track of how many | 23 // reference set (3.1.3). |
25 // times the entry has been 'touched', which is useful for both | |
26 // encoding and decoding. | |
27 class NET_EXPORT_PRIVATE HpackEntry { | 24 class NET_EXPORT_PRIVATE HpackEntry { |
28 public: | 25 public: |
29 // The constant amount added to name().size() and value().size() to | 26 // The constant amount added to name().size() and value().size() to |
30 // get the size of an HpackEntry as defined in 3.3.1. | 27 // get the size of an HpackEntry as defined in 3.3.1. |
31 static const uint32 kSizeOverhead; | 28 static const size_t kSizeOverhead; |
32 | 29 |
33 // The constant returned by touch_count() if an entry hasn't been | 30 // Implements a total ordering of HpackEntry on name(), value(), then Index() |
34 // touched (which is distinct from an entry having a touch count of | 31 // ascending. Note that Index() may change over the lifetime of an HpackEntry, |
35 // 0). | 32 // but the relative Index() order of two entries will not. This comparator is |
| 33 // composed with the 'lookup' HpackEntry constructor to allow for efficient |
| 34 // lower-bounding of matching entries. |
| 35 struct NET_EXPORT_PRIVATE Comparator { |
| 36 bool operator() (const HpackEntry* lhs, const HpackEntry* rhs) const; |
| 37 }; |
| 38 typedef std::set<HpackEntry*, Comparator> OrderedSet; |
| 39 |
| 40 // Creates an entry. Preconditions: |
| 41 // - |is_static| captures whether this entry is a member of the static |
| 42 // or dynamic header table. |
| 43 // - |insertion_index| is this entry's index in the total set of entries ever |
| 44 // inserted into the header table (including static entries). |
| 45 // - |total_table_insertions_or_current_size| references an externally- |
| 46 // updated count of either the total number of header insertions (if |
| 47 // !|is_static|), or the current size of the header table (if |is_static|). |
36 // | 48 // |
37 // TODO(akalin): The distinction between untouched and having a | 49 // The combination of |is_static|, |insertion_index|, and |
38 // touch count of 0 is confusing. Think of a better way to represent | 50 // |total_table_insertions_or_current_size| allows an HpackEntry to determine |
39 // this state. | 51 // its current table index in O(1) time. |
40 static const uint32 kUntouched; | 52 HpackEntry(base::StringPiece name, |
| 53 base::StringPiece value, |
| 54 bool is_static, |
| 55 size_t insertion_index, |
| 56 const size_t* total_table_insertions_or_current_size); |
| 57 |
| 58 // Create a 'lookup' entry (only) suitable for querying a HpackEntrySet. The |
| 59 // instance Index() always returns 0, and will lower-bound all entries |
| 60 // matching |name| & |value| in an OrderedSet. |
| 61 HpackEntry(base::StringPiece name, base::StringPiece value); |
41 | 62 |
42 // Creates an entry with empty name a value. Only defined so that | 63 // Creates an entry with empty name a value. Only defined so that |
43 // entries can be stored in STL containers. | 64 // entries can be stored in STL containers. |
44 HpackEntry(); | 65 HpackEntry(); |
45 | 66 |
46 // Creates an entry with a copy is made of the given name and value. | 67 ~HpackEntry(); |
47 // | |
48 // TODO(akalin): Add option to not make a copy (for static table | |
49 // entries). | |
50 HpackEntry(base::StringPiece name, base::StringPiece value); | |
51 | 68 |
52 // Copy constructor and assignment operator welcome. | 69 const std::string& name() const { return name_; } |
| 70 const std::string& value() const { return value_; } |
53 | 71 |
54 // The name() and value() StringPieces have the same lifetime as | 72 // Returns whether this entry is a member of the static (as opposed to |
55 // this entry. | 73 // dynamic) table. |
| 74 bool IsStatic() const { return is_static_; } |
56 | 75 |
57 base::StringPiece name() const { return base::StringPiece(name_); } | 76 // Returns and sets the state of the entry, or zero if never set. |
58 base::StringPiece value() const { return base::StringPiece(value_); } | 77 // The semantics of |state| are specific to the encoder or decoder. |
| 78 uint8 state() const { return state_; } |
| 79 void set_state(uint8 state) { state_ = state; } |
59 | 80 |
60 // Returns whether or not this entry is in the reference set. | 81 // Returns the entry's current index in the header table. |
61 bool IsReferenced() const; | 82 size_t Index() const; |
62 | 83 |
63 // Returns how many touches this entry has, or kUntouched if this | 84 // Returns the size of an entry as defined in 3.3.1. |
64 // entry hasn't been touched at all. The meaning of the touch count | 85 static size_t Size(base::StringPiece name, base::StringPiece value); |
65 // is defined by whatever is calling | |
66 // AddTouchCount()/ClearTouchCount() (i.e., the encoder or decoder). | |
67 uint32 TouchCount() const; | |
68 | |
69 // Returns the size of an entry as defined in 3.3.1. The returned | |
70 // value may not necessarily fit in 32 bits. | |
71 size_t Size() const; | 86 size_t Size() const; |
72 | 87 |
73 std::string GetDebugString() const; | 88 std::string GetDebugString() const; |
74 | 89 |
75 // Returns whether this entry has the same name, value, referenced | |
76 // state, and touch count as the given one. | |
77 bool Equals(const HpackEntry& other) const; | |
78 | |
79 void SetReferenced(bool referenced); | |
80 | |
81 // Adds the given number of touches to this entry (see | |
82 // TouchCount()). The total number of touches must not exceed 2^31 - | |
83 // 2. It is guaranteed that this entry's touch count will not equal | |
84 // kUntouched after this function is called (even if touch_count == | |
85 // 0). | |
86 void AddTouches(uint32 additional_touch_count); | |
87 | |
88 // Sets the touch count of this entry to kUntouched. | |
89 void ClearTouches(); | |
90 | |
91 private: | 90 private: |
| 91 // TODO(jgraettinger): Reduce copies, possibly via SpdyPinnableBufferPiece. |
92 std::string name_; | 92 std::string name_; |
93 std::string value_; | 93 std::string value_; |
94 | 94 |
95 // The high bit stores 'referenced' and the rest stores the touch | 95 bool is_static_; |
96 // count. | 96 uint8 state_; |
97 uint32 referenced_and_touch_count_; | 97 |
| 98 // The entry's index in the total set of entries ever inserted into the header |
| 99 // table. |
| 100 size_t insertion_index_; |
| 101 |
| 102 // If |is_static_|, references the current size of the headers table. |
| 103 // Else, references the total number of header insertions which have occurred. |
| 104 const size_t* total_insertions_or_size_; |
98 }; | 105 }; |
99 | 106 |
100 } // namespace net | 107 } // namespace net |
101 | 108 |
102 #endif // NET_SPDY_HPACK_ENTRY_H_ | 109 #endif // NET_SPDY_HPACK_ENTRY_H_ |
OLD | NEW |