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 "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "net/spdy/hpack_output_stream.h" | 8 #include "net/spdy/hpack_output_stream.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 | 11 |
12 using base::StringPiece; | 12 using base::StringPiece; |
13 using std::string; | 13 using std::string; |
14 | 14 |
15 HpackEncoder::HpackEncoder() | 15 HpackEncoder::HpackEncoder() |
16 : max_string_literal_size_(kDefaultMaxStringLiteralSize) {} | 16 : max_string_literal_size_(kDefaultMaxStringLiteralSize), |
| 17 char_counts_(NULL), |
| 18 total_char_counts_(NULL) { |
| 19 } |
17 | 20 |
18 HpackEncoder::~HpackEncoder() {} | 21 HpackEncoder::~HpackEncoder() {} |
19 | 22 |
20 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set, | 23 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set, |
21 string* output) { | 24 string* output) { |
22 // TOOD(jgraettinger): Do more sophisticated encoding. | 25 // TOOD(jgraettinger): Do more sophisticated encoding. |
23 HpackOutputStream output_stream(max_string_literal_size_); | 26 HpackOutputStream output_stream(max_string_literal_size_); |
24 for (std::map<string, string>::const_iterator it = header_set.begin(); | 27 for (std::map<string, string>::const_iterator it = header_set.begin(); |
25 it != header_set.end(); ++it) { | 28 it != header_set.end(); ++it) { |
26 // TODO(jgraettinger): HTTP/2 requires strict lowercasing of headers, | 29 // TODO(jgraettinger): HTTP/2 requires strict lowercasing of headers, |
27 // and the permissiveness here isn't wanted. Back this out in upstream. | 30 // and the permissiveness here isn't wanted. Back this out in upstream. |
28 if (LowerCaseEqualsASCII(it->first, "cookie")) { | 31 if (LowerCaseEqualsASCII(it->first, "cookie")) { |
29 std::vector<StringPiece> crumbs; | 32 std::vector<StringPiece> crumbs; |
30 CookieToCrumbs(it->second, &crumbs); | 33 CookieToCrumbs(it->second, &crumbs); |
31 for (size_t i = 0; i != crumbs.size(); i++) { | 34 for (size_t i = 0; i != crumbs.size(); i++) { |
32 if (!output_stream.AppendLiteralHeaderNoIndexingWithName( | 35 if (!output_stream.AppendLiteralHeaderNoIndexingWithName( |
33 it->first, crumbs[i])) { | 36 it->first, crumbs[i])) { |
34 return false; | 37 return false; |
35 } | 38 } |
36 } | 39 } |
37 } else if (!output_stream.AppendLiteralHeaderNoIndexingWithName( | 40 } else if (!output_stream.AppendLiteralHeaderNoIndexingWithName( |
38 it->first, it->second)) { | 41 it->first, it->second)) { |
39 return false; | 42 return false; |
40 } | 43 } |
| 44 UpdateCharacterCounts(it->first); |
| 45 UpdateCharacterCounts(it->second); |
41 } | 46 } |
42 output_stream.TakeString(output); | 47 output_stream.TakeString(output); |
43 return true; | 48 return true; |
44 } | 49 } |
45 | 50 |
| 51 void HpackEncoder::SetCharCountsStorage(std::vector<size_t>* char_counts, |
| 52 size_t* total_char_counts) { |
| 53 CHECK_LE(256u, char_counts->size()); |
| 54 char_counts_ = char_counts; |
| 55 total_char_counts_ = total_char_counts; |
| 56 } |
| 57 |
46 void HpackEncoder::CookieToCrumbs(StringPiece cookie, | 58 void HpackEncoder::CookieToCrumbs(StringPiece cookie, |
47 std::vector<StringPiece>* out) { | 59 std::vector<StringPiece>* out) { |
48 out->clear(); | 60 out->clear(); |
49 for (size_t pos = 0;;) { | 61 for (size_t pos = 0;;) { |
50 size_t end = cookie.find(';', pos); | 62 size_t end = cookie.find(';', pos); |
51 | 63 |
52 if (end == StringPiece::npos) { | 64 if (end == StringPiece::npos) { |
53 out->push_back(cookie.substr(pos)); | 65 out->push_back(cookie.substr(pos)); |
54 return; | 66 return; |
55 } | 67 } |
56 out->push_back(cookie.substr(pos, end - pos)); | 68 out->push_back(cookie.substr(pos, end - pos)); |
57 | 69 |
58 // Consume next space if present. | 70 // Consume next space if present. |
59 pos = end + 1; | 71 pos = end + 1; |
60 if (pos != cookie.size() && cookie[pos] == ' ') { | 72 if (pos != cookie.size() && cookie[pos] == ' ') { |
61 pos++; | 73 pos++; |
62 } | 74 } |
63 } | 75 } |
64 } | 76 } |
65 | 77 |
| 78 void HpackEncoder::UpdateCharacterCounts(base::StringPiece str) { |
| 79 if (char_counts_ == NULL || total_char_counts_ == NULL) { |
| 80 return; |
| 81 } |
| 82 for (StringPiece::const_iterator it = str.begin(); it != str.end(); ++it) { |
| 83 ++(*char_counts_)[static_cast<uint8>(*it)]; |
| 84 } |
| 85 (*total_char_counts_) += str.size(); |
| 86 } |
| 87 |
66 } // namespace net | 88 } // namespace net |
OLD | NEW |