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.
5 #include "net/spdy/hpack_encoding_context.h"
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "net/spdy/hpack_entry.h"
15 using base::StringPiece
;
19 // An entry in the static table. Must be a POD in order to avoid
20 // static initializers, i.e. no user-defined constructors or
23 const char* const name
;
24 const size_t name_len
;
25 const char* const value
;
26 const size_t value_len
;
29 // The "constructor" for a StaticEntry that computes the lengths at
31 #define STATIC_ENTRY(name, value) \
32 { name, arraysize(name) - 1, value, arraysize(value) - 1 }
34 const StaticEntry kStaticTable
[] = {
35 STATIC_ENTRY(":authority" , ""), // 1
36 STATIC_ENTRY(":method" , "GET"), // 2
37 STATIC_ENTRY(":method" , "POST"), // 3
38 STATIC_ENTRY(":path" , "/"), // 4
39 STATIC_ENTRY(":path" , "/index.html"), // 5
40 STATIC_ENTRY(":scheme" , "http"), // 6
41 STATIC_ENTRY(":scheme" , "https"), // 7
42 STATIC_ENTRY(":status" , "200"), // 8
43 STATIC_ENTRY(":status" , "500"), // 9
44 STATIC_ENTRY(":status" , "404"), // 10
45 STATIC_ENTRY(":status" , "403"), // 11
46 STATIC_ENTRY(":status" , "400"), // 12
47 STATIC_ENTRY(":status" , "401"), // 13
48 STATIC_ENTRY("accept-charset" , ""), // 14
49 STATIC_ENTRY("accept-encoding" , ""), // 15
50 STATIC_ENTRY("accept-language" , ""), // 16
51 STATIC_ENTRY("accept-ranges" , ""), // 17
52 STATIC_ENTRY("accept" , ""), // 18
53 STATIC_ENTRY("access-control-allow-origin" , ""), // 19
54 STATIC_ENTRY("age" , ""), // 20
55 STATIC_ENTRY("allow" , ""), // 21
56 STATIC_ENTRY("authorization" , ""), // 22
57 STATIC_ENTRY("cache-control" , ""), // 23
58 STATIC_ENTRY("content-disposition" , ""), // 24
59 STATIC_ENTRY("content-encoding" , ""), // 25
60 STATIC_ENTRY("content-language" , ""), // 26
61 STATIC_ENTRY("content-length" , ""), // 27
62 STATIC_ENTRY("content-location" , ""), // 28
63 STATIC_ENTRY("content-range" , ""), // 29
64 STATIC_ENTRY("content-type" , ""), // 30
65 STATIC_ENTRY("cookie" , ""), // 31
66 STATIC_ENTRY("date" , ""), // 32
67 STATIC_ENTRY("etag" , ""), // 33
68 STATIC_ENTRY("expect" , ""), // 34
69 STATIC_ENTRY("expires" , ""), // 35
70 STATIC_ENTRY("from" , ""), // 36
71 STATIC_ENTRY("host" , ""), // 37
72 STATIC_ENTRY("if-match" , ""), // 38
73 STATIC_ENTRY("if-modified-since" , ""), // 39
74 STATIC_ENTRY("if-none-match" , ""), // 40
75 STATIC_ENTRY("if-range" , ""), // 41
76 STATIC_ENTRY("if-unmodified-since" , ""), // 42
77 STATIC_ENTRY("last-modified" , ""), // 43
78 STATIC_ENTRY("link" , ""), // 44
79 STATIC_ENTRY("location" , ""), // 45
80 STATIC_ENTRY("max-forwards" , ""), // 46
81 STATIC_ENTRY("proxy-authenticate" , ""), // 47
82 STATIC_ENTRY("proxy-authorization" , ""), // 48
83 STATIC_ENTRY("range" , ""), // 49
84 STATIC_ENTRY("referer" , ""), // 50
85 STATIC_ENTRY("refresh" , ""), // 51
86 STATIC_ENTRY("retry-after" , ""), // 52
87 STATIC_ENTRY("server" , ""), // 53
88 STATIC_ENTRY("set-cookie" , ""), // 54
89 STATIC_ENTRY("strict-transport-security" , ""), // 55
90 STATIC_ENTRY("transfer-encoding" , ""), // 56
91 STATIC_ENTRY("user-agent" , ""), // 57
92 STATIC_ENTRY("vary" , ""), // 58
93 STATIC_ENTRY("via" , ""), // 59
94 STATIC_ENTRY("www-authenticate" , ""), // 60
99 const size_t kStaticEntryCount
= arraysize(kStaticTable
);
103 const uint32
HpackEncodingContext::kUntouched
= HpackEntry::kUntouched
;
105 HpackEncodingContext::HpackEncodingContext() {}
107 HpackEncodingContext::~HpackEncodingContext() {}
109 uint32
HpackEncodingContext::GetMutableEntryCount() const {
110 return header_table_
.GetEntryCount();
113 uint32
HpackEncodingContext::GetEntryCount() const {
114 return GetMutableEntryCount() + kStaticEntryCount
;
117 StringPiece
HpackEncodingContext::GetNameAt(uint32 index
) const {
119 CHECK_LE(index
, GetEntryCount());
120 if (index
> header_table_
.GetEntryCount()) {
121 const StaticEntry
& entry
=
122 kStaticTable
[index
- header_table_
.GetEntryCount() - 1];
123 return StringPiece(entry
.name
, entry
.name_len
);
125 return header_table_
.GetEntry(index
).name();
128 StringPiece
HpackEncodingContext::GetValueAt(uint32 index
) const {
130 CHECK_LE(index
, GetEntryCount());
131 if (index
> header_table_
.GetEntryCount()) {
132 const StaticEntry
& entry
=
133 kStaticTable
[index
- header_table_
.GetEntryCount() - 1];
134 return StringPiece(entry
.value
, entry
.value_len
);
136 return header_table_
.GetEntry(index
).value();
139 bool HpackEncodingContext::IsReferencedAt(uint32 index
) const {
141 CHECK_LE(index
, GetEntryCount());
142 if (index
> header_table_
.GetEntryCount())
144 return header_table_
.GetEntry(index
).IsReferenced();
147 uint32
HpackEncodingContext::GetTouchCountAt(uint32 index
) const {
149 CHECK_LE(index
, GetEntryCount());
150 if (index
> header_table_
.GetEntryCount())
152 return header_table_
.GetEntry(index
).TouchCount();
155 void HpackEncodingContext::SetReferencedAt(uint32 index
, bool referenced
) {
156 header_table_
.GetMutableEntry(index
)->SetReferenced(referenced
);
159 void HpackEncodingContext::AddTouchesAt(uint32 index
, uint32 touch_count
) {
160 header_table_
.GetMutableEntry(index
)->AddTouches(touch_count
);
163 void HpackEncodingContext::ClearTouchesAt(uint32 index
) {
164 header_table_
.GetMutableEntry(index
)->ClearTouches();
167 void HpackEncodingContext::SetMaxSize(uint32 max_size
) {
168 header_table_
.SetMaxSize(max_size
);
171 bool HpackEncodingContext::ProcessIndexedHeader(
172 uint32 index_or_zero
,
174 std::vector
<uint32
>* removed_referenced_indices
) {
175 if (index_or_zero
> GetEntryCount())
178 if (index_or_zero
== 0) {
180 removed_referenced_indices
->clear();
181 // Empty the reference set.
182 for (size_t i
= 1; i
<= header_table_
.GetEntryCount(); ++i
) {
183 HpackEntry
* entry
= header_table_
.GetMutableEntry(i
);
184 if (entry
->IsReferenced()) {
185 removed_referenced_indices
->push_back(i
);
186 entry
->SetReferenced(false);
192 uint32 index
= index_or_zero
;
194 if (index
<= header_table_
.GetEntryCount()) {
196 removed_referenced_indices
->clear();
197 HpackEntry
* entry
= header_table_
.GetMutableEntry(index
);
198 entry
->SetReferenced(!entry
->IsReferenced());
200 // TODO(akalin): Make HpackEntry know about owned strings and
201 // non-owned strings so that it can potentially avoid copies here.
202 HpackEntry
entry(GetNameAt(index
), GetValueAt(index
));
204 header_table_
.TryAddEntry(entry
, new_index
, removed_referenced_indices
);
205 if (*new_index
>= 1) {
206 header_table_
.GetMutableEntry(*new_index
)->SetReferenced(true);
212 bool HpackEncodingContext::ProcessLiteralHeaderWithIncrementalIndexing(
216 std::vector
<uint32
>* removed_referenced_indices
) {
217 HpackEntry
entry(name
, value
);
218 header_table_
.TryAddEntry(entry
, index
, removed_referenced_indices
);
220 header_table_
.GetMutableEntry(*index
)->SetReferenced(true);