hpack_entry.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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 NET_SPDY_HPACK_ENTRY_H_
6#define NET_SPDY_HPACK_ENTRY_H_
7
8#include <cstddef>
9#include <set>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/macros.h"
14#include "base/strings/string_piece.h"
15#include "net/base/net_export.h"
16
17// All section references below are to
18// http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06
19
20namespace net {
21
22// A structure for an entry in the header table (3.1.2) and the
23// reference set (3.1.3).
24class NET_EXPORT_PRIVATE HpackEntry {
25 public:
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.
28  static const size_t kSizeOverhead;
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:
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|).
48  //
49  // The combination of |is_static|, |insertion_index|, and
50  // |total_table_insertions_or_current_size| allows an HpackEntry to determine
51  // its current table index in O(1) time.
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);
62
63  // Creates an entry with empty name a value. Only defined so that
64  // entries can be stored in STL containers.
65  HpackEntry();
66
67  ~HpackEntry();
68
69  const std::string& name() const { return name_; }
70  const std::string& value() const { return value_; }
71
72  // Returns whether this entry is a member of the static (as opposed to
73  // dynamic) table.
74  bool IsStatic() const { return is_static_; }
75
76  // 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.
78  uint8 state() const { return state_; }
79  void set_state(uint8 state) { state_ = state; }
80
81  // Returns the entry's current index in the header table.
82  size_t Index() const;
83
84  // Returns the size of an entry as defined in 3.3.1.
85  static size_t Size(base::StringPiece name, base::StringPiece value);
86  size_t Size() const;
87
88  std::string GetDebugString() const;
89
90 private:
91  // TODO(jgraettinger): Reduce copies, possibly via SpdyPinnableBufferPiece.
92  std::string name_;
93  std::string value_;
94
95  bool is_static_;
96  uint8 state_;
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_;
105};
106
107}  // namespace net
108
109#endif  // NET_SPDY_HPACK_ENTRY_H_
110