| 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 <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 16 | 16 |
| 17 // All section references below are to | 17 // All section references below are to |
| 18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-07 | 18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-07 |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 // 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 |
| 23 // reference set (3.1.3). | 23 // reference set (3.1.3). |
| 24 class NET_EXPORT_PRIVATE HpackEntry { | 24 class NET_EXPORT_PRIVATE HpackEntry { |
| 25 public: | 25 public: |
| 26 // The constant amount added to name().size() and value().size() to | 26 // The constant amount added to name().size() and value().size() to |
| 27 // 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. |
| 28 static const size_t kSizeOverhead; | 28 static const size_t kSizeOverhead; |
| 29 | 29 |
| 30 // Implements a total ordering of HpackEntry on name(), value(), then Index() | |
| 31 // ascending. Note that Index() may change over the lifetime of an HpackEntry, | |
| 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: | 30 // Creates an entry. Preconditions: |
| 41 // - |is_static| captures whether this entry is a member of the static | 31 // - |is_static| captures whether this entry is a member of the static |
| 42 // or dynamic header table. | 32 // or dynamic header table. |
| 43 // - |insertion_index| is this entry's index in the total set of entries ever | 33 // - |insertion_index| is this entry's index in the total set of entries ever |
| 44 // inserted into the header table (including static entries). | 34 // 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|). | |
| 48 // | 35 // |
| 49 // The combination of |is_static|, |insertion_index|, and | 36 // The combination of |is_static| and |insertion_index| allows an |
| 50 // |total_table_insertions_or_current_size| allows an HpackEntry to determine | 37 // HpackEntryTable to determine the index of an HpackEntry in O(1) time. |
| 51 // its current table index in O(1) time. | |
| 52 HpackEntry(base::StringPiece name, | 38 HpackEntry(base::StringPiece name, |
| 53 base::StringPiece value, | 39 base::StringPiece value, |
| 54 bool is_static, | 40 bool is_static, |
| 55 size_t insertion_index, | 41 size_t insertion_index); |
| 56 const size_t* total_table_insertions_or_current_size); | |
| 57 | 42 |
| 58 // Create a 'lookup' entry (only) suitable for querying a HpackEntrySet. The | 43 // Create a 'lookup' entry (only) suitable for querying a HpackEntrySet. The |
| 59 // instance Index() always returns 0, and will lower-bound all entries | 44 // instance InsertionIndex() always returns 0 and IsLookup() returns true. |
| 60 // matching |name| & |value| in an OrderedSet. | |
| 61 HpackEntry(base::StringPiece name, base::StringPiece value); | 45 HpackEntry(base::StringPiece name, base::StringPiece value); |
| 62 | 46 |
| 63 // Creates an entry with empty name a value. Only defined so that | 47 // Creates an entry with empty name and value. Only defined so that |
| 64 // entries can be stored in STL containers. | 48 // entries can be stored in STL containers. |
| 65 HpackEntry(); | 49 HpackEntry(); |
| 66 | 50 |
| 67 ~HpackEntry(); | 51 ~HpackEntry(); |
| 68 | 52 |
| 69 const std::string& name() const { return name_; } | 53 const std::string& name() const { return name_; } |
| 70 const std::string& value() const { return value_; } | 54 const std::string& value() const { return value_; } |
| 71 | 55 |
| 72 // Returns whether this entry is a member of the static (as opposed to | 56 // Returns whether this entry is a member of the static (as opposed to |
| 73 // dynamic) table. | 57 // dynamic) table. |
| 74 bool IsStatic() const { return is_static_; } | 58 bool IsStatic() const { return type_ == STATIC; } |
| 59 |
| 60 // Returns whether this entry is a lookup-only entry. |
| 61 bool IsLookup() const { return type_ == LOOKUP; } |
| 75 | 62 |
| 76 // Returns and sets the state of the entry, or zero if never set. | 63 // Returns and sets the state of the entry, or zero if never set. |
| 77 // The semantics of |state| are specific to the encoder or decoder. | 64 // The semantics of |state| are specific to the encoder or decoder. |
| 78 uint8 state() const { return state_; } | 65 uint8 state() const { return state_; } |
| 79 void set_state(uint8 state) { state_ = state; } | 66 void set_state(uint8 state) { state_ = state; } |
| 80 | 67 |
| 81 // Returns the entry's current index in the header table. | 68 // Used to compute the entry's index in the header table. |
| 82 size_t Index() const; | 69 size_t InsertionIndex() const { return insertion_index_; } |
| 83 | 70 |
| 84 // Returns the size of an entry as defined in 3.3.1. | 71 // Returns the size of an entry as defined in 3.3.1. |
| 85 static size_t Size(base::StringPiece name, base::StringPiece value); | 72 static size_t Size(base::StringPiece name, base::StringPiece value); |
| 86 size_t Size() const; | 73 size_t Size() const; |
| 87 | 74 |
| 88 std::string GetDebugString() const; | 75 std::string GetDebugString() const; |
| 89 | 76 |
| 90 private: | 77 private: |
| 78 enum EntryType { |
| 79 LOOKUP, |
| 80 DYNAMIC, |
| 81 STATIC, |
| 82 }; |
| 83 |
| 91 // TODO(jgraettinger): Reduce copies, possibly via SpdyPinnableBufferPiece. | 84 // TODO(jgraettinger): Reduce copies, possibly via SpdyPinnableBufferPiece. |
| 92 std::string name_; | 85 std::string name_; |
| 93 std::string value_; | 86 std::string value_; |
| 94 | 87 |
| 95 bool is_static_; | |
| 96 uint8 state_; | |
| 97 | |
| 98 // The entry's index in the total set of entries ever inserted into the header | 88 // The entry's index in the total set of entries ever inserted into the header |
| 99 // table. | 89 // table. |
| 100 size_t insertion_index_; | 90 size_t insertion_index_; |
| 101 | 91 |
| 102 // If |is_static_|, references the current size of the headers table. | 92 uint8 state_; |
| 103 // Else, references the total number of header insertions which have occurred. | 93 EntryType type_; |
| 104 const size_t* total_insertions_or_size_; | |
| 105 }; | 94 }; |
| 106 | 95 |
| 107 } // namespace net | 96 } // namespace net |
| 108 | 97 |
| 109 #endif // NET_SPDY_HPACK_ENTRY_H_ | 98 #endif // NET_SPDY_HPACK_ENTRY_H_ |
| OLD | NEW |