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 | 17 |
18 HpackEncoder::~HpackEncoder() {} | 18 HpackEncoder::~HpackEncoder() {} |
19 | 19 |
20 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set, | 20 bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set, |
21 string* output) { | 21 string* output) { |
22 // TOOD(jgraettinger): Do more sophisticated encoding. | 22 // TOOD(jgraettinger): Do more sophisticated encoding. |
23 HpackOutputStream output_stream(max_string_literal_size_); | 23 HpackOutputStream output_stream; |
24 for (std::map<string, string>::const_iterator it = header_set.begin(); | 24 for (std::map<string, string>::const_iterator it = header_set.begin(); |
25 it != header_set.end(); ++it) { | 25 it != header_set.end(); ++it) { |
26 // TODO(jgraettinger): HTTP/2 requires strict lowercasing of headers, | 26 // TODO(jgraettinger): HTTP/2 requires strict lowercasing of headers, |
27 // and the permissiveness here isn't wanted. Back this out in upstream. | 27 // and the permissiveness here isn't wanted. Back this out in upstream. |
28 if (LowerCaseEqualsASCII(it->first, "cookie")) { | 28 if (LowerCaseEqualsASCII(it->first, "cookie")) { |
29 std::vector<StringPiece> crumbs; | 29 std::vector<StringPiece> crumbs; |
30 CookieToCrumbs(it->second, &crumbs); | 30 CookieToCrumbs(it->second, &crumbs); |
31 for (size_t i = 0; i != crumbs.size(); i++) { | 31 for (size_t i = 0; i != crumbs.size(); i++) { |
32 if (!output_stream.AppendLiteralHeaderNoIndexingWithName( | 32 EmitNonIndexedLiteral(it->first, crumbs[i], &output_stream); |
33 it->first, crumbs[i])) { | |
34 return false; | |
35 } | |
36 } | 33 } |
37 } else if (!output_stream.AppendLiteralHeaderNoIndexingWithName( | 34 } else { |
38 it->first, it->second)) { | 35 EmitNonIndexedLiteral(it->first, it->second, &output_stream); |
39 return false; | |
40 } | 36 } |
41 } | 37 } |
42 output_stream.TakeString(output); | 38 output_stream.TakeString(output); |
43 return true; | 39 return true; |
44 } | 40 } |
45 | 41 |
| 42 void HpackEncoder::EmitNonIndexedLiteral(StringPiece name, |
| 43 StringPiece value, |
| 44 HpackOutputStream* output_stream) { |
| 45 output_stream->AppendPrefix(kLiteralNoIndexOpcode); |
| 46 output_stream->AppendUint32(0u); |
| 47 output_stream->AppendPrefix(kStringLiteralIdentityEncoded); |
| 48 output_stream->AppendUint32(name.size()); |
| 49 output_stream->AppendBytes(name); |
| 50 output_stream->AppendPrefix(kStringLiteralIdentityEncoded); |
| 51 output_stream->AppendUint32(value.size()); |
| 52 output_stream->AppendBytes(value); |
| 53 } |
| 54 |
46 void HpackEncoder::CookieToCrumbs(StringPiece cookie, | 55 void HpackEncoder::CookieToCrumbs(StringPiece cookie, |
47 std::vector<StringPiece>* out) { | 56 std::vector<StringPiece>* out) { |
48 out->clear(); | 57 out->clear(); |
49 for (size_t pos = 0;;) { | 58 for (size_t pos = 0;;) { |
50 size_t end = cookie.find(';', pos); | 59 size_t end = cookie.find(';', pos); |
51 | 60 |
52 if (end == StringPiece::npos) { | 61 if (end == StringPiece::npos) { |
53 out->push_back(cookie.substr(pos)); | 62 out->push_back(cookie.substr(pos)); |
54 return; | 63 return; |
55 } | 64 } |
56 out->push_back(cookie.substr(pos, end - pos)); | 65 out->push_back(cookie.substr(pos, end - pos)); |
57 | 66 |
58 // Consume next space if present. | 67 // Consume next space if present. |
59 pos = end + 1; | 68 pos = end + 1; |
60 if (pos != cookie.size() && cookie[pos] == ' ') { | 69 if (pos != cookie.size() && cookie[pos] == ' ') { |
61 pos++; | 70 pos++; |
62 } | 71 } |
63 } | 72 } |
64 } | 73 } |
65 | 74 |
66 } // namespace net | 75 } // namespace net |
OLD | NEW |