OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Derived from: | 5 // Derived from: |
6 // mozilla/netwerk/protocol/http/src/nsHttpChunkedDecoder.cpp | 6 // mozilla/netwerk/protocol/http/src/nsHttpChunkedDecoder.cpp |
7 // The license block is: | 7 // The license block is: |
8 /* ***** BEGIN LICENSE BLOCK ***** | 8 /* ***** BEGIN LICENSE BLOCK ***** |
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
10 * | 10 * |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 : chunk_remaining_(0), | 62 : chunk_remaining_(0), |
63 chunk_terminator_remaining_(false), | 63 chunk_terminator_remaining_(false), |
64 reached_last_chunk_(false), | 64 reached_last_chunk_(false), |
65 reached_eof_(false), | 65 reached_eof_(false), |
66 bytes_after_eof_(0) { | 66 bytes_after_eof_(0) { |
67 } | 67 } |
68 | 68 |
69 int HttpChunkedDecoder::FilterBuf(char* buf, int buf_len) { | 69 int HttpChunkedDecoder::FilterBuf(char* buf, int buf_len) { |
70 int result = 0; | 70 int result = 0; |
71 | 71 |
72 while (buf_len) { | 72 while (buf_len > 0) { |
73 if (chunk_remaining_) { | 73 if (chunk_remaining_ > 0) { |
74 int num = std::min(chunk_remaining_, buf_len); | 74 // Since |chunk_remaining_| is positive and |buf_len| an int, the minimum |
| 75 // of the two must be an int. |
| 76 int num = static_cast<int>( |
| 77 std::min(chunk_remaining_, static_cast<int64_t>(buf_len))); |
75 | 78 |
76 buf_len -= num; | 79 buf_len -= num; |
77 chunk_remaining_ -= num; | 80 chunk_remaining_ -= num; |
78 | 81 |
79 result += num; | 82 result += num; |
80 buf += num; | 83 buf += num; |
81 | 84 |
82 // After each chunk's data there should be a CRLF | 85 // After each chunk's data there should be a CRLF. |
83 if (!chunk_remaining_) | 86 if (chunk_remaining_ == 0) |
84 chunk_terminator_remaining_ = true; | 87 chunk_terminator_remaining_ = true; |
85 continue; | 88 continue; |
86 } else if (reached_eof_) { | 89 } else if (reached_eof_) { |
87 bytes_after_eof_ += buf_len; | 90 bytes_after_eof_ += buf_len; |
88 break; // Done! | 91 break; // Done! |
89 } | 92 } |
90 | 93 |
91 int bytes_consumed = ScanForChunkRemaining(buf, buf_len); | 94 int bytes_consumed = ScanForChunkRemaining(buf, buf_len); |
92 if (bytes_consumed < 0) | 95 if (bytes_consumed < 0) |
93 return bytes_consumed; // Error | 96 return bytes_consumed; // Error |
94 | 97 |
95 buf_len -= bytes_consumed; | 98 buf_len -= bytes_consumed; |
96 if (buf_len) | 99 if (buf_len > 0) |
97 memmove(buf, buf + bytes_consumed, buf_len); | 100 memmove(buf, buf + bytes_consumed, buf_len); |
98 } | 101 } |
99 | 102 |
100 return result; | 103 return result; |
101 } | 104 } |
102 | 105 |
103 int HttpChunkedDecoder::ScanForChunkRemaining(const char* buf, int buf_len) { | 106 int HttpChunkedDecoder::ScanForChunkRemaining(const char* buf, int buf_len) { |
104 DCHECK_EQ(0, chunk_remaining_); | 107 DCHECK_EQ(0, chunk_remaining_); |
105 DCHECK_GT(buf_len, 0); | 108 DCHECK_GT(buf_len, 0); |
106 | 109 |
107 int bytes_consumed = 0; | 110 int bytes_consumed = 0; |
108 | 111 |
109 size_t index_of_lf = base::StringPiece(buf, buf_len).find('\n'); | 112 size_t index_of_lf = base::StringPiece(buf, buf_len).find('\n'); |
110 if (index_of_lf != base::StringPiece::npos) { | 113 if (index_of_lf != base::StringPiece::npos) { |
111 buf_len = static_cast<int>(index_of_lf); | 114 buf_len = static_cast<int>(index_of_lf); |
112 if (buf_len && buf[buf_len - 1] == '\r') // Eliminate a preceding CR. | 115 if (buf_len && buf[buf_len - 1] == '\r') // Eliminate a preceding CR. |
113 buf_len--; | 116 buf_len--; |
114 bytes_consumed = static_cast<int>(index_of_lf) + 1; | 117 bytes_consumed = static_cast<int>(index_of_lf) + 1; |
115 | 118 |
116 // Make buf point to the full line buffer to parse. | 119 // Make buf point to the full line buffer to parse. |
117 if (!line_buf_.empty()) { | 120 if (!line_buf_.empty()) { |
118 line_buf_.append(buf, buf_len); | 121 line_buf_.append(buf, buf_len); |
119 buf = line_buf_.data(); | 122 buf = line_buf_.data(); |
120 buf_len = static_cast<int>(line_buf_.size()); | 123 buf_len = static_cast<int>(line_buf_.size()); |
121 } | 124 } |
122 | 125 |
123 if (reached_last_chunk_) { | 126 if (reached_last_chunk_) { |
124 if (buf_len) | 127 if (buf_len > 0) |
125 DVLOG(1) << "ignoring http trailer"; | 128 DVLOG(1) << "ignoring http trailer"; |
126 else | 129 else |
127 reached_eof_ = true; | 130 reached_eof_ = true; |
128 } else if (chunk_terminator_remaining_) { | 131 } else if (chunk_terminator_remaining_) { |
129 if (buf_len) { | 132 if (buf_len > 0) { |
130 DLOG(ERROR) << "chunk data not terminated properly"; | 133 DLOG(ERROR) << "chunk data not terminated properly"; |
131 return ERR_INVALID_CHUNKED_ENCODING; | 134 return ERR_INVALID_CHUNKED_ENCODING; |
132 } | 135 } |
133 chunk_terminator_remaining_ = false; | 136 chunk_terminator_remaining_ = false; |
134 } else if (buf_len) { | 137 } else if (buf_len > 0) { |
135 // Ignore any chunk-extensions. | 138 // Ignore any chunk-extensions. |
136 size_t index_of_semicolon = base::StringPiece(buf, buf_len).find(';'); | 139 size_t index_of_semicolon = base::StringPiece(buf, buf_len).find(';'); |
137 if (index_of_semicolon != base::StringPiece::npos) | 140 if (index_of_semicolon != base::StringPiece::npos) |
138 buf_len = static_cast<int>(index_of_semicolon); | 141 buf_len = static_cast<int>(index_of_semicolon); |
139 | 142 |
140 if (!ParseChunkSize(buf, buf_len, &chunk_remaining_)) { | 143 if (!ParseChunkSize(buf, buf_len, &chunk_remaining_)) { |
141 DLOG(ERROR) << "Failed parsing HEX from: " << | 144 DLOG(ERROR) << "Failed parsing HEX from: " << |
142 std::string(buf, buf_len); | 145 std::string(buf, buf_len); |
143 return ERR_INVALID_CHUNKED_ENCODING; | 146 return ERR_INVALID_CHUNKED_ENCODING; |
144 } | 147 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // RFC 7230: ^\X+$ | 185 // RFC 7230: ^\X+$ |
183 // IE7: ^\X+[^\X]*$ | 186 // IE7: ^\X+[^\X]*$ |
184 // Safari 3.1: ^[\t\r ]*\X+[\t ]*$ | 187 // Safari 3.1: ^[\t\r ]*\X+[\t ]*$ |
185 // Firefox 3: ^[\t\f\v\r ]*[+]?(0x)?\X+[^\X]*$ | 188 // Firefox 3: ^[\t\f\v\r ]*[+]?(0x)?\X+[^\X]*$ |
186 // Opera 9.51: ^[\t\f\v ]*[+]?(0x)?\X+[^\X]*$ | 189 // Opera 9.51: ^[\t\f\v ]*[+]?(0x)?\X+[^\X]*$ |
187 // | 190 // |
188 // Our strategy is to be as strict as possible, while not breaking | 191 // Our strategy is to be as strict as possible, while not breaking |
189 // known sites. | 192 // known sites. |
190 // | 193 // |
191 // Us: ^\X+[ ]*$ | 194 // Us: ^\X+[ ]*$ |
192 bool HttpChunkedDecoder::ParseChunkSize(const char* start, int len, int* out) { | 195 bool HttpChunkedDecoder::ParseChunkSize(const char* start, |
| 196 int len, |
| 197 int64_t* out) { |
193 DCHECK_GE(len, 0); | 198 DCHECK_GE(len, 0); |
194 | 199 |
195 // Strip trailing spaces | 200 // Strip trailing spaces |
196 while (len && start[len - 1] == ' ') | 201 while (len > 0 && start[len - 1] == ' ') |
197 len--; | 202 len--; |
198 | 203 |
199 // Be more restrictive than HexStringToInt; | 204 // Be more restrictive than HexStringToInt64; |
200 // don't allow inputs with leading "-", "+", "0x", "0X" | 205 // don't allow inputs with leading "-", "+", "0x", "0X" |
201 base::StringPiece chunk_size(start, len); | 206 base::StringPiece chunk_size(start, len); |
202 if (chunk_size.find_first_not_of("0123456789abcdefABCDEF") | 207 if (chunk_size.find_first_not_of("0123456789abcdefABCDEF") |
203 != base::StringPiece::npos) { | 208 != base::StringPiece::npos) { |
204 return false; | 209 return false; |
205 } | 210 } |
206 | 211 |
207 int parsed_number; | 212 int64_t parsed_number; |
208 bool ok = base::HexStringToInt(chunk_size, &parsed_number); | 213 bool ok = base::HexStringToInt64(chunk_size, &parsed_number); |
209 if (ok && parsed_number >= 0) { | 214 if (ok && parsed_number >= 0) { |
210 *out = parsed_number; | 215 *out = parsed_number; |
211 return true; | 216 return true; |
212 } | 217 } |
213 return false; | 218 return false; |
214 } | 219 } |
215 | 220 |
216 } // namespace net | 221 } // namespace net |
OLD | NEW |