| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/hpack_encoder.h" | 5 #include "net/spdy/hpack_encoder.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/spdy/hpack_header_table.h" | 10 #include "net/spdy/hpack_header_table.h" |
| 11 #include "net/spdy/hpack_huffman_table.h" | 11 #include "net/spdy/hpack_huffman_table.h" |
| 12 #include "net/spdy/hpack_output_stream.h" | 12 #include "net/spdy/hpack_output_stream.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 using base::StringPiece; | 16 using base::StringPiece; |
| 17 using std::string; | 17 using std::string; |
| 18 | 18 |
| 19 HpackEncoder::HpackEncoder(const HpackHuffmanTable& table) | 19 HpackEncoder::HpackEncoder(const HpackHuffmanTable& table) |
| 20 : output_stream_(), | 20 : output_stream_(), |
| 21 allow_huffman_compression_(true), | 21 allow_huffman_compression_(true), |
| 22 huffman_table_(table), | 22 huffman_table_(table), |
| 23 char_counts_(NULL), | 23 char_counts_(NULL), |
| 24 total_char_counts_(NULL) {} | 24 total_char_counts_(NULL) {} |
| 25 | 25 |
| 26 HpackEncoder::~HpackEncoder() {} | 26 HpackEncoder::~HpackEncoder() {} |
| 27 | 27 |
| 28 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set, | 28 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set, |
| 29 string* output) { | 29 string* output) { |
| 30 // Flatten & crumble headers into an ordered list of representations. | 30 // Separate header set into pseudo-headers and regular headers. |
| 31 Representations full_set; | 31 Representations pseudo_headers; |
| 32 Representations regular_headers; |
| 32 for (std::map<string, string>::const_iterator it = header_set.begin(); | 33 for (std::map<string, string>::const_iterator it = header_set.begin(); |
| 33 it != header_set.end(); ++it) { | 34 it != header_set.end(); ++it) { |
| 34 if (it->first == "cookie") { | 35 if (it->first == "cookie") { |
| 35 // |CookieToCrumbs()| produces ordered crumbs. | 36 // Note that there can only be one "cookie" header, because header_set is |
| 36 CookieToCrumbs(*it, &full_set); | 37 // a map. |
| 38 CookieToCrumbs(*it, ®ular_headers); |
| 39 } else if (it->first[0] == kPseudoHeaderPrefix) { |
| 40 pseudo_headers.push_back(make_pair( |
| 41 StringPiece(it->first), StringPiece(it->second))); |
| 37 } else { | 42 } else { |
| 38 // Note std::map guarantees representations are ordered. | 43 regular_headers.push_back(make_pair( |
| 39 full_set.push_back(make_pair( | |
| 40 StringPiece(it->first), StringPiece(it->second))); | 44 StringPiece(it->first), StringPiece(it->second))); |
| 41 } | 45 } |
| 42 } | 46 } |
| 43 | 47 |
| 44 // Walk this ordered list and encode entries. | 48 // Encode pseudo-headers. |
| 45 for (Representations::const_iterator it = full_set.begin(); | 49 for (Representations::const_iterator it = pseudo_headers.begin(); |
| 46 it != full_set.end(); ++it) { | 50 it != pseudo_headers.end(); ++it) { |
| 47 HpackEntry* entry = header_table_.GetByNameAndValue(it->first, it->second); | 51 HpackEntry* entry = header_table_.GetByNameAndValue(it->first, it->second); |
| 48 if (entry != NULL) { | 52 if (entry != NULL) { |
| 49 EmitIndex(entry); | 53 EmitIndex(entry); |
| 50 } else { | 54 } else { |
| 51 // TODO(bnc): if another entry in the header table is about to be evicted | 55 if (it->first == ":authority") { |
| 52 // but it appears in the header list, emit that by index first. | 56 // :authority is always present and rarely changes, and has moderate |
| 53 EmitIndexedLiteral(*it); | 57 // length, therefore it makes a lot of sense to index (insert in the |
| 58 // header table). |
| 59 EmitIndexedLiteral(*it); |
| 60 } else { |
| 61 // Most common pseudo-header fields are represented in the static table, |
| 62 // while uncommon ones are small, so do not index them. |
| 63 EmitNonIndexedLiteral(*it); |
| 64 } |
| 54 } | 65 } |
| 55 } | 66 } |
| 56 | 67 |
| 68 // Encode regular headers that are already in the header table first, |
| 69 // save the rest into another vector. This way we avoid evicting an entry |
| 70 // from the header table before it can be used. |
| 71 Representations literal_headers; |
| 72 for (Representations::const_iterator it = regular_headers.begin(); |
| 73 it != regular_headers.end(); ++it) { |
| 74 HpackEntry* entry = header_table_.GetByNameAndValue(it->first, it->second); |
| 75 if (entry != NULL) { |
| 76 EmitIndex(entry); |
| 77 } else { |
| 78 literal_headers.push_back(*it); |
| 79 } |
| 80 } |
| 81 |
| 82 // Encode the remaining header fields, while inserting them in the header |
| 83 // table. |
| 84 for (Representations::const_iterator it = literal_headers.begin(); |
| 85 it != literal_headers.end(); ++it) { |
| 86 EmitIndexedLiteral(*it); |
| 87 } |
| 88 |
| 57 output_stream_.TakeString(output); | 89 output_stream_.TakeString(output); |
| 58 return true; | 90 return true; |
| 59 } | 91 } |
| 60 | 92 |
| 61 bool HpackEncoder::EncodeHeaderSetWithoutCompression( | 93 bool HpackEncoder::EncodeHeaderSetWithoutCompression( |
| 62 const std::map<string, string>& header_set, | 94 const std::map<string, string>& header_set, |
| 63 string* output) { | 95 string* output) { |
| 64 | 96 |
| 65 allow_huffman_compression_ = false; | 97 allow_huffman_compression_ = false; |
| 66 for (std::map<string, string>::const_iterator it = header_set.begin(); | 98 for (std::map<string, string>::const_iterator it = header_set.begin(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 ++(*char_counts_)[static_cast<uint8>(*it)]; | 165 ++(*char_counts_)[static_cast<uint8>(*it)]; |
| 134 } | 166 } |
| 135 (*total_char_counts_) += str.size(); | 167 (*total_char_counts_) += str.size(); |
| 136 } | 168 } |
| 137 | 169 |
| 138 // static | 170 // static |
| 139 void HpackEncoder::CookieToCrumbs(const Representation& cookie, | 171 void HpackEncoder::CookieToCrumbs(const Representation& cookie, |
| 140 Representations* out) { | 172 Representations* out) { |
| 141 size_t prior_size = out->size(); | 173 size_t prior_size = out->size(); |
| 142 | 174 |
| 143 // See Section 8.1.3.4 "Compressing the Cookie Header Field" in the HTTP/2 | 175 // See Section 8.1.2.5. "Compressing the Cookie Header Field" in the HTTP/2 |
| 144 // specification at http://tools.ietf.org/html/draft-ietf-httpbis-http2-11 | 176 // specification at https://tools.ietf.org/html/draft-ietf-httpbis-http2-14. |
| 145 // Cookie values are split into individually-encoded HPACK representations. | 177 // Cookie values are split into individually-encoded HPACK representations. |
| 146 for (size_t pos = 0;;) { | 178 for (size_t pos = 0;;) { |
| 147 size_t end = cookie.second.find(";", pos); | 179 size_t end = cookie.second.find(";", pos); |
| 148 | 180 |
| 149 if (end == StringPiece::npos) { | 181 if (end == StringPiece::npos) { |
| 150 out->push_back(make_pair( | 182 out->push_back(make_pair( |
| 151 cookie.first, | 183 cookie.first, |
| 152 cookie.second.substr(pos))); | 184 cookie.second.substr(pos))); |
| 153 break; | 185 break; |
| 154 } | 186 } |
| 155 out->push_back(make_pair( | 187 out->push_back(make_pair( |
| 156 cookie.first, | 188 cookie.first, |
| 157 cookie.second.substr(pos, end - pos))); | 189 cookie.second.substr(pos, end - pos))); |
| 158 | 190 |
| 159 // Consume next space if present. | 191 // Consume next space if present. |
| 160 pos = end + 1; | 192 pos = end + 1; |
| 161 if (pos != cookie.second.size() && cookie.second[pos] == ' ') { | 193 if (pos != cookie.second.size() && cookie.second[pos] == ' ') { |
| 162 pos++; | 194 pos++; |
| 163 } | 195 } |
| 164 } | 196 } |
| 165 // Sort crumbs and remove duplicates. | 197 // Sort crumbs and remove duplicates. |
| 166 std::sort(out->begin() + prior_size, out->end()); | 198 std::sort(out->begin() + prior_size, out->end()); |
| 167 out->erase(std::unique(out->begin() + prior_size, out->end()), | 199 out->erase(std::unique(out->begin() + prior_size, out->end()), |
| 168 out->end()); | 200 out->end()); |
| 169 } | 201 } |
| 170 | 202 |
| 171 } // namespace net | 203 } // namespace net |
| OLD | NEW |