OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/test/embedded_test_server/http_request.h" | 5 #include "net/test/embedded_test_server/http_request.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "net/http/http_chunked_decoder.h" |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 namespace test_server { | 16 namespace test_server { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. | 20 size_t kRequestSizeLimit = 64 * 1024 * 1024; // 64 mb. |
20 | 21 |
21 // Helper function used to trim tokens in http request headers. | 22 // Helper function used to trim tokens in http request headers. |
22 std::string Trim(const std::string& value) { | 23 std::string Trim(const std::string& value) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 134 } |
134 | 135 |
135 // Headers done. Is any content data attached to the request? | 136 // Headers done. Is any content data attached to the request? |
136 declared_content_length_ = 0; | 137 declared_content_length_ = 0; |
137 if (http_request_->headers.count("Content-Length") > 0) { | 138 if (http_request_->headers.count("Content-Length") > 0) { |
138 http_request_->has_content = true; | 139 http_request_->has_content = true; |
139 const bool success = base::StringToSizeT( | 140 const bool success = base::StringToSizeT( |
140 http_request_->headers["Content-Length"], | 141 http_request_->headers["Content-Length"], |
141 &declared_content_length_); | 142 &declared_content_length_); |
142 DCHECK(success) << "Malformed Content-Length header's value."; | 143 DCHECK(success) << "Malformed Content-Length header's value."; |
| 144 } else if (http_request_->headers.count("Transfer-Encoding") > 0) { |
| 145 if (http_request_->headers["Transfer-Encoding"] == "chunked") { |
| 146 http_request_->has_content = true; |
| 147 chunked_decoder_.reset(new HttpChunkedDecoder()); |
| 148 state_ = STATE_CONTENT; |
| 149 return WAITING; |
| 150 } |
143 } | 151 } |
144 if (declared_content_length_ == 0) { | 152 if (declared_content_length_ == 0) { |
145 // No content data, so parsing is finished. | 153 // No content data, so parsing is finished. |
146 state_ = STATE_ACCEPTED; | 154 state_ = STATE_ACCEPTED; |
147 return ACCEPTED; | 155 return ACCEPTED; |
148 } | 156 } |
149 | 157 |
150 // The request has not yet been parsed yet, content data is still to be | 158 // The request has not yet been parsed yet, content data is still to be |
151 // processed. | 159 // processed. |
152 state_ = STATE_CONTENT; | 160 state_ = STATE_CONTENT; |
153 return WAITING; | 161 return WAITING; |
154 } | 162 } |
155 | 163 |
156 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | 164 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { |
157 const size_t available_bytes = buffer_.size() - buffer_position_; | 165 const size_t available_bytes = buffer_.size() - buffer_position_; |
| 166 if (chunked_decoder_.get()) { |
| 167 int bytes_written = chunked_decoder_->FilterBuf( |
| 168 const_cast<char*>(buffer_.data()) + buffer_position_, available_bytes); |
| 169 http_request_->content.append(buffer_.data() + buffer_position_, |
| 170 bytes_written); |
| 171 |
| 172 if (chunked_decoder_->reached_eof()) { |
| 173 buffer_ = |
| 174 buffer_.substr(buffer_.size() - chunked_decoder_->bytes_after_eof()); |
| 175 buffer_position_ = 0; |
| 176 state_ = STATE_ACCEPTED; |
| 177 return ACCEPTED; |
| 178 } |
| 179 buffer_ = ""; |
| 180 buffer_position_ = 0; |
| 181 state_ = STATE_CONTENT; |
| 182 return WAITING; |
| 183 } |
| 184 |
158 const size_t fetch_bytes = std::min( | 185 const size_t fetch_bytes = std::min( |
159 available_bytes, | 186 available_bytes, |
160 declared_content_length_ - http_request_->content.size()); | 187 declared_content_length_ - http_request_->content.size()); |
161 http_request_->content.append(buffer_.data() + buffer_position_, | 188 http_request_->content.append(buffer_.data() + buffer_position_, |
162 fetch_bytes); | 189 fetch_bytes); |
163 buffer_position_ += fetch_bytes; | 190 buffer_position_ += fetch_bytes; |
164 | 191 |
165 if (declared_content_length_ == http_request_->content.size()) { | 192 if (declared_content_length_ == http_request_->content.size()) { |
166 state_ = STATE_ACCEPTED; | 193 state_ = STATE_ACCEPTED; |
167 return ACCEPTED; | 194 return ACCEPTED; |
(...skipping 30 matching lines...) Expand all Loading... |
198 return METHOD_DELETE; | 225 return METHOD_DELETE; |
199 } else if (token == "patch") { | 226 } else if (token == "patch") { |
200 return METHOD_PATCH; | 227 return METHOD_PATCH; |
201 } | 228 } |
202 NOTREACHED() << "Method not implemented: " << token; | 229 NOTREACHED() << "Method not implemented: " << token; |
203 return METHOD_UNKNOWN; | 230 return METHOD_UNKNOWN; |
204 } | 231 } |
205 | 232 |
206 } // namespace test_server | 233 } // namespace test_server |
207 } // namespace net | 234 } // namespace net |
OLD | NEW |