We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / spdy / hpack_encoder.cc
blobfe95593f53b757f6aeaff63b0c695d9f0d4c30ad
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"
7 #include <algorithm>
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"
14 namespace net {
16 using base::StringPiece;
17 using std::string;
19 HpackEncoder::HpackEncoder(const HpackHuffmanTable& table)
20 : output_stream_(),
21 allow_huffman_compression_(true),
22 huffman_table_(table),
23 char_counts_(NULL),
24 total_char_counts_(NULL) {}
26 HpackEncoder::~HpackEncoder() {}
28 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set,
29 string* output) {
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
37 // a map.
38 CookieToCrumbs(*it, &regular_headers);
39 } else if (it->first[0] == kPseudoHeaderPrefix) {
40 DecomposeRepresentation(*it, &pseudo_headers);
41 } else {
42 DecomposeRepresentation(*it, &regular_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);
51 if (entry != NULL) {
52 EmitIndex(entry);
53 } else {
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
57 // header table).
58 EmitIndexedLiteral(*it);
59 } else {
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);
72 if (entry != NULL) {
73 EmitIndex(entry);
74 } else {
75 EmitIndexedLiteral(*it);
79 output_stream_.TakeString(output);
80 return true;
83 bool HpackEncoder::EncodeHeaderSetWithoutCompression(
84 const std::map<string, string>& header_set,
85 string* output) {
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);
95 return true;
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));
121 } else {
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_);
135 } else {
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) {
152 return;
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();
160 // static
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(std::make_pair(cookie.first, cookie.second.substr(pos)));
173 break;
175 out->push_back(
176 std::make_pair(cookie.first, cookie.second.substr(pos, end - pos)));
178 // Consume next space if present.
179 pos = end + 1;
180 if (pos != cookie.second.size() && cookie.second[pos] == ' ') {
181 pos++;
184 // Sort crumbs and remove duplicates.
185 std::sort(out->begin() + prior_size, out->end());
186 out->erase(std::unique(out->begin() + prior_size, out->end()),
187 out->end());
190 // static
191 void HpackEncoder::DecomposeRepresentation(const Representation& header_field,
192 Representations* out) {
193 size_t pos = 0;
194 size_t end = 0;
195 while (end != StringPiece::npos) {
196 end = header_field.second.find('\0', pos);
197 out->push_back(std::make_pair(header_field.first,
198 header_field.second.substr(pos, end - pos)));
199 pos = end + 1;
203 } // namespace net