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_encoder.h"
9 #include "base/logging.h"
10 #include "net/spdy/hpack_header_table.h"
11 #include "net/spdy/hpack_huffman_table.h"
12 #include "net/spdy/hpack_output_stream.h"
16 using base::StringPiece
;
19 HpackEncoder::HpackEncoder(const HpackHuffmanTable
& table
)
21 allow_huffman_compression_(true),
22 huffman_table_(table
),
24 total_char_counts_(NULL
) {}
26 HpackEncoder::~HpackEncoder() {}
28 bool HpackEncoder::EncodeHeaderSet(const std::map
<string
, string
>& header_set
,
30 // Separate header set into pseudo-headers and regular headers.
31 Representations pseudo_headers
;
32 Representations regular_headers
;
33 for (std::map
<string
, string
>::const_iterator it
= header_set
.begin();
34 it
!= header_set
.end(); ++it
) {
35 if (it
->first
== "cookie") {
36 // Note that there can only be one "cookie" header, because header_set is
38 CookieToCrumbs(*it
, ®ular_headers
);
39 } else if (it
->first
[0] == kPseudoHeaderPrefix
) {
40 DecomposeRepresentation(*it
, &pseudo_headers
);
42 DecomposeRepresentation(*it
, ®ular_headers
);
46 // Encode pseudo-headers.
47 for (Representations::const_iterator it
= pseudo_headers
.begin();
48 it
!= pseudo_headers
.end(); ++it
) {
49 const HpackEntry
* entry
=
50 header_table_
.GetByNameAndValue(it
->first
, it
->second
);
54 if (it
->first
== ":authority") {
55 // :authority is always present and rarely changes, and has moderate
56 // length, therefore it makes a lot of sense to index (insert in the
58 EmitIndexedLiteral(*it
);
60 // Most common pseudo-header fields are represented in the static table,
61 // while uncommon ones are small, so do not index them.
62 EmitNonIndexedLiteral(*it
);
67 // Encode regular headers.
68 for (Representations::const_iterator it
= regular_headers
.begin();
69 it
!= regular_headers
.end(); ++it
) {
70 const HpackEntry
* entry
=
71 header_table_
.GetByNameAndValue(it
->first
, it
->second
);
75 EmitIndexedLiteral(*it
);
79 output_stream_
.TakeString(output
);
83 bool HpackEncoder::EncodeHeaderSetWithoutCompression(
84 const std::map
<string
, string
>& header_set
,
87 allow_huffman_compression_
= false;
88 for (std::map
<string
, string
>::const_iterator it
= header_set
.begin();
89 it
!= header_set
.end(); ++it
) {
90 // Note that cookies are not crumbled in this case.
91 EmitNonIndexedLiteral(*it
);
93 allow_huffman_compression_
= true;
94 output_stream_
.TakeString(output
);
98 void HpackEncoder::EmitIndex(const HpackEntry
* entry
) {
99 output_stream_
.AppendPrefix(kIndexedOpcode
);
100 output_stream_
.AppendUint32(header_table_
.IndexOf(entry
));
103 void HpackEncoder::EmitIndexedLiteral(const Representation
& representation
) {
104 output_stream_
.AppendPrefix(kLiteralIncrementalIndexOpcode
);
105 EmitLiteral(representation
);
106 header_table_
.TryAddEntry(representation
.first
, representation
.second
);
109 void HpackEncoder::EmitNonIndexedLiteral(
110 const Representation
& representation
) {
111 output_stream_
.AppendPrefix(kLiteralNoIndexOpcode
);
112 output_stream_
.AppendUint32(0);
113 EmitString(representation
.first
);
114 EmitString(representation
.second
);
117 void HpackEncoder::EmitLiteral(const Representation
& representation
) {
118 const HpackEntry
* name_entry
= header_table_
.GetByName(representation
.first
);
119 if (name_entry
!= NULL
) {
120 output_stream_
.AppendUint32(header_table_
.IndexOf(name_entry
));
122 output_stream_
.AppendUint32(0);
123 EmitString(representation
.first
);
125 EmitString(representation
.second
);
128 void HpackEncoder::EmitString(StringPiece str
) {
129 size_t encoded_size
= (!allow_huffman_compression_
? str
.size()
130 : huffman_table_
.EncodedSize(str
));
131 if (encoded_size
< str
.size()) {
132 output_stream_
.AppendPrefix(kStringLiteralHuffmanEncoded
);
133 output_stream_
.AppendUint32(encoded_size
);
134 huffman_table_
.EncodeString(str
, &output_stream_
);
136 output_stream_
.AppendPrefix(kStringLiteralIdentityEncoded
);
137 output_stream_
.AppendUint32(str
.size());
138 output_stream_
.AppendBytes(str
);
140 UpdateCharacterCounts(str
);
143 void HpackEncoder::SetCharCountsStorage(std::vector
<size_t>* char_counts
,
144 size_t* total_char_counts
) {
145 CHECK_LE(256u, char_counts
->size());
146 char_counts_
= char_counts
;
147 total_char_counts_
= total_char_counts
;
150 void HpackEncoder::UpdateCharacterCounts(base::StringPiece str
) {
151 if (char_counts_
== NULL
|| total_char_counts_
== NULL
) {
154 for (StringPiece::const_iterator it
= str
.begin(); it
!= str
.end(); ++it
) {
155 ++(*char_counts_
)[static_cast<uint8
>(*it
)];
157 (*total_char_counts_
) += str
.size();
161 void HpackEncoder::CookieToCrumbs(const Representation
& cookie
,
162 Representations
* out
) {
163 size_t prior_size
= out
->size();
165 // See Section 8.1.2.5. "Compressing the Cookie Header Field" in the HTTP/2
166 // specification at https://tools.ietf.org/html/draft-ietf-httpbis-http2-14.
167 // Cookie values are split into individually-encoded HPACK representations.
168 for (size_t pos
= 0;;) {
169 size_t end
= cookie
.second
.find(";", pos
);
171 if (end
== StringPiece::npos
) {
172 out
->push_back(make_pair(
174 cookie
.second
.substr(pos
)));
177 out
->push_back(make_pair(
179 cookie
.second
.substr(pos
, end
- pos
)));
181 // Consume next space if present.
183 if (pos
!= cookie
.second
.size() && cookie
.second
[pos
] == ' ') {
187 // Sort crumbs and remove duplicates.
188 std::sort(out
->begin() + prior_size
, out
->end());
189 out
->erase(std::unique(out
->begin() + prior_size
, out
->end()),
194 void HpackEncoder::DecomposeRepresentation(const Representation
& header_field
,
195 Representations
* out
) {
198 while (end
!= StringPiece::npos
) {
199 end
= header_field
.second
.find('\0', pos
);
200 out
->push_back(make_pair(header_field
.first
,
201 header_field
.second
.substr(pos
, end
- pos
)));