Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: net/spdy/hpack_encoding_context.cc

Issue 138243003: Implement basic classes for HPACK (HTTP/2 compression) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: And removing tabs from net.gyp... Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #include "net/spdy/hpack_encoding_context.h"
2
3 #include <cstddef>
4
5 #include "base/logging.h"
6 #include "base/macros.h"
7 #include "net/spdy/hpack_entry.h"
8
9 namespace net {
10
11 namespace {
12
13 // An entry in the static table. Must be a POD in order to avoid
14 // static initializers, i.e. no user-defined constructors or
15 // destructors.
16 struct StaticEntry {
17 const char* const name;
18 const size_t name_len;
19 const char* const value;
20 const size_t value_len;
21 };
22
23 // The "constructor" for a StaticEntry that computes the lengths at
24 // compile time.
25 #define STATIC_ENTRY(name, value) \
26 { name, arraysize(name) - 1, value, arraysize(value) - 1 }
27
28 const StaticEntry kStaticTable[] = {
29 STATIC_ENTRY(":authority" , ""), // 0
30 STATIC_ENTRY(":method" , "GET"), // 1
31 STATIC_ENTRY(":method" , "POST"), // 2
32 STATIC_ENTRY(":path" , "/"), // 3
33 STATIC_ENTRY(":path" , "/index.html"), // 4
34 STATIC_ENTRY(":scheme" , "http"), // 5
35 STATIC_ENTRY(":scheme" , "https"), // 6
36 STATIC_ENTRY(":status" , "200"), // 7
37 STATIC_ENTRY(":status" , "500"), // 8
38 STATIC_ENTRY(":status" , "404"), // 9
39 STATIC_ENTRY(":status" , "403"), // 10
40 STATIC_ENTRY(":status" , "400"), // 11
41 STATIC_ENTRY(":status" , "401"), // 12
42 STATIC_ENTRY("accept-charset" , ""), // 13
43 STATIC_ENTRY("accept-encoding" , ""), // 14
44 STATIC_ENTRY("accept-language" , ""), // 15
45 STATIC_ENTRY("accept-ranges" , ""), // 16
46 STATIC_ENTRY("accept" , ""), // 17
47 STATIC_ENTRY("access-control-allow-origin" , ""), // 18
48 STATIC_ENTRY("age" , ""), // 19
49 STATIC_ENTRY("allow" , ""), // 20
50 STATIC_ENTRY("authorization" , ""), // 21
51 STATIC_ENTRY("cache-control" , ""), // 22
52 STATIC_ENTRY("content-disposition" , ""), // 23
53 STATIC_ENTRY("content-encoding" , ""), // 24
54 STATIC_ENTRY("content-language" , ""), // 25
55 STATIC_ENTRY("content-length" , ""), // 26
56 STATIC_ENTRY("content-location" , ""), // 27
57 STATIC_ENTRY("content-range" , ""), // 28
58 STATIC_ENTRY("content-type" , ""), // 29
59 STATIC_ENTRY("cookie" , ""), // 30
60 STATIC_ENTRY("date" , ""), // 31
61 STATIC_ENTRY("etag" , ""), // 32
62 STATIC_ENTRY("expect" , ""), // 33
63 STATIC_ENTRY("expires" , ""), // 34
64 STATIC_ENTRY("from" , ""), // 35
65 STATIC_ENTRY("if-match" , ""), // 36
66 STATIC_ENTRY("if-modified-since" , ""), // 37
67 STATIC_ENTRY("if-none-match" , ""), // 38
68 STATIC_ENTRY("if-range" , ""), // 39
69 STATIC_ENTRY("if-unmodified-since" , ""), // 40
70 STATIC_ENTRY("last-modified" , ""), // 41
71 STATIC_ENTRY("link" , ""), // 42
72 STATIC_ENTRY("location" , ""), // 43
73 STATIC_ENTRY("max-forwards" , ""), // 44
74 STATIC_ENTRY("proxy-authenticate" , ""), // 45
75 STATIC_ENTRY("proxy-authorization" , ""), // 46
76 STATIC_ENTRY("range" , ""), // 47
77 STATIC_ENTRY("referer" , ""), // 48
78 STATIC_ENTRY("refresh" , ""), // 49
79 STATIC_ENTRY("retry-after" , ""), // 50
80 STATIC_ENTRY("server" , ""), // 51
81 STATIC_ENTRY("set-cookie" , ""), // 52
82 STATIC_ENTRY("strict-transport-security" , ""), // 53
83 STATIC_ENTRY("transfer-encoding" , ""), // 54
84 STATIC_ENTRY("user-agent" , ""), // 55
85 STATIC_ENTRY("vary" , ""), // 56
86 STATIC_ENTRY("via" , ""), // 57
87 STATIC_ENTRY("www-authenticate" , ""), // 58
88 };
89
90 #undef STATIC_ENTRY
91
92 const size_t kStaticEntryCount = arraysize(kStaticTable);
93
94 } // namespace
95
96 const uint32 HpackEncodingContext::kUntouched = HpackEntry::kUntouched;
97
98 HpackEncodingContext::HpackEncodingContext() {}
99
100 HpackEncodingContext::~HpackEncodingContext() {}
101
102 uint32 HpackEncodingContext::GetEntryCount() const {
103 return header_table_.GetEntryCount() + kStaticEntryCount;
104 }
105
106 base::StringPiece HpackEncodingContext::GetNameAt(uint32 index) const {
107 CHECK_LT(index, GetEntryCount());
108 if (index >= header_table_.GetEntryCount()) {
109 const StaticEntry& entry =
110 kStaticTable[index - header_table_.GetEntryCount()];
111 return base::StringPiece(entry.name, entry.name_len);
112 }
113 return header_table_.GetEntry(index).name();
114 }
115
116 base::StringPiece HpackEncodingContext::GetValueAt(uint32 index) const {
117 CHECK_LT(index, GetEntryCount());
118 if (index >= header_table_.GetEntryCount()) {
119 const StaticEntry& entry =
120 kStaticTable[index - header_table_.GetEntryCount()];
121 return base::StringPiece(entry.value, entry.value_len);
122 }
123 return header_table_.GetEntry(index).value();
124 }
125
126 bool HpackEncodingContext::IsReferencedAt(uint32 index) const {
127 CHECK_LT(index, GetEntryCount());
128 if (index >= header_table_.GetEntryCount())
129 return false;
130 return header_table_.GetEntry(index).IsReferenced();
131 }
132
133 uint32 HpackEncodingContext::GetTouchCountAt(uint32 index) const {
134 CHECK_LT(index, GetEntryCount());
135 if (index >= header_table_.GetEntryCount())
136 return 0;
137 return header_table_.GetEntry(index).TouchCount();
138 }
139
140 void HpackEncodingContext::SetReferencedAt(uint32 index, bool referenced) {
141 header_table_.GetMutableEntry(index)->SetReferenced(referenced);
142 }
143
144 void HpackEncodingContext::AddTouchesAt(uint32 index, uint32 touch_count) {
145 header_table_.GetMutableEntry(index)->AddTouches(touch_count);
146 }
147
148 void HpackEncodingContext::ClearTouchesAt(uint32 index) {
149 header_table_.GetMutableEntry(index)->ClearTouches();
150 }
151
152 void HpackEncodingContext::SetMaxSize(uint32 max_size) {
153 header_table_.SetMaxSize(max_size);
154 }
155
156 bool HpackEncodingContext::ProcessIndexedHeader(
157 uint32 index,
158 int32* new_index,
159 std::vector<uint32>* removed_referenced_indices) {
160 if (index >= GetEntryCount())
161 return false;
162
163 if (index < header_table_.GetEntryCount()) {
164 *new_index = index;
165 removed_referenced_indices->clear();
166 HpackEntry* entry = header_table_.GetMutableEntry(index);
167 entry->SetReferenced(!entry->IsReferenced());
168 } else {
169 // TODO(akalin): Make HpackEntry know about owned strings and
170 // non-owned strings so that it can potentially avoid copies here.
171 HpackEntry entry(GetNameAt(index), GetValueAt(index));
172
173 header_table_.TryAddEntry(entry, new_index, removed_referenced_indices);
174 if (*new_index >= 0) {
175 header_table_.GetMutableEntry(*new_index)->SetReferenced(true);
176 }
177 }
178 return true;
179 }
180
181 bool HpackEncodingContext::ProcessLiteralHeaderWithIncrementalIndexing(
182 base::StringPiece name,
183 base::StringPiece value,
184 int32* index,
185 std::vector<uint32>* removed_referenced_indices) {
186 HpackEntry entry(name, value);
187 header_table_.TryAddEntry(entry, index, removed_referenced_indices);
188 if (*index >= 0) {
189 header_table_.GetMutableEntry(*index)->SetReferenced(true);
190 }
191 return true;
192 }
193
194 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698