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" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 std::string header_value = Trim(header_line.substr( | 127 std::string header_value = Trim(header_line.substr( |
128 delimiter_pos + 1, | 128 delimiter_pos + 1, |
129 header_line.size() - delimiter_pos - 1)); | 129 header_line.size() - delimiter_pos - 1)); |
130 http_request_->headers[header_name] = header_value; | 130 http_request_->headers[header_name] = header_value; |
131 } | 131 } |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 // Headers done. Is any content data attached to the request? | 135 // Headers done. Is any content data attached to the request? |
136 declared_content_length_ = 0; | 136 declared_content_length_ = 0; |
137 if (http_request_->headers.count("Content-Length") > 0) { | 137 if (http_request_->headers.count("Transfer-Encoding") > 0) { |
mmenke
2015/02/12 15:34:30
Suggest flipping the order - ignoring a Content-Le
xunjieli
2015/02/12 15:58:40
Done.
| |
138 if (http_request_->headers["Transfer-Encoding"] == "chunked") { | |
139 http_request_->has_content = true; | |
140 chunked_decoder_.reset(new HttpChunkedDecoder()); | |
141 state_ = STATE_CONTENT; | |
142 return WAITING; | |
143 } | |
144 } else if (http_request_->headers.count("Content-Length") > 0) { | |
138 http_request_->has_content = true; | 145 http_request_->has_content = true; |
139 const bool success = base::StringToSizeT( | 146 const bool success = base::StringToSizeT( |
140 http_request_->headers["Content-Length"], | 147 http_request_->headers["Content-Length"], |
141 &declared_content_length_); | 148 &declared_content_length_); |
142 DCHECK(success) << "Malformed Content-Length header's value."; | 149 DCHECK(success) << "Malformed Content-Length header's value."; |
143 } | 150 } |
144 if (declared_content_length_ == 0) { | 151 if (declared_content_length_ == 0) { |
145 // No content data, so parsing is finished. | 152 // No content data, so parsing is finished. |
146 state_ = STATE_ACCEPTED; | 153 state_ = STATE_ACCEPTED; |
147 return ACCEPTED; | 154 return ACCEPTED; |
148 } | 155 } |
149 | 156 |
150 // The request has not yet been parsed yet, content data is still to be | 157 // The request has not yet been parsed yet, content data is still to be |
151 // processed. | 158 // processed. |
152 state_ = STATE_CONTENT; | 159 state_ = STATE_CONTENT; |
153 return WAITING; | 160 return WAITING; |
154 } | 161 } |
155 | 162 |
156 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { | 163 HttpRequestParser::ParseResult HttpRequestParser::ParseContent() { |
157 const size_t available_bytes = buffer_.size() - buffer_position_; | 164 const size_t available_bytes = buffer_.size() - buffer_position_; |
165 if (chunked_decoder_.get()) { | |
mmenke
2015/02/12 15:34:30
nit: The ".get()" isn't needed
xunjieli
2015/02/12 15:58:40
Done.
| |
166 std::string result; | |
167 result.append(buffer_.data() + buffer_position_, available_bytes); | |
168 int bytes_written = chunked_decoder_->FilterBuf( | |
169 &result[0], static_cast<int>(result.size())); | |
mmenke
2015/02/12 15:34:30
Why do we need result? Can't we just use:
bytes_
xunjieli
2015/02/12 15:58:40
When I tried "chunked_decoder_->FilterBuf(buffer_.
mmenke
2015/02/12 16:17:44
That's because buffer_ is a string (I had assumed
| |
170 if (bytes_written > 0 && chunked_decoder_->reached_eof()) { | |
171 buffer_position_ += bytes_written; | |
mmenke
2015/02/12 15:34:30
This should be outside of the if block. The fact
xunjieli
2015/02/12 15:58:40
It looks like FilterBuf can only parse chunked dat
mmenke
2015/02/12 16:17:44
You're misreading the code. FilterBuf is a stream
| |
172 http_request_->content.append(result.data(), bytes_written); | |
173 state_ = STATE_ACCEPTED; | |
174 return ACCEPTED; | |
175 } | |
176 state_ = STATE_CONTENT; | |
177 return WAITING; | |
178 } | |
179 | |
158 const size_t fetch_bytes = std::min( | 180 const size_t fetch_bytes = std::min( |
159 available_bytes, | 181 available_bytes, |
160 declared_content_length_ - http_request_->content.size()); | 182 declared_content_length_ - http_request_->content.size()); |
161 http_request_->content.append(buffer_.data() + buffer_position_, | 183 http_request_->content.append(buffer_.data() + buffer_position_, |
162 fetch_bytes); | 184 fetch_bytes); |
163 buffer_position_ += fetch_bytes; | 185 buffer_position_ += fetch_bytes; |
164 | 186 |
165 if (declared_content_length_ == http_request_->content.size()) { | 187 if (declared_content_length_ == http_request_->content.size()) { |
166 state_ = STATE_ACCEPTED; | 188 state_ = STATE_ACCEPTED; |
167 return ACCEPTED; | 189 return ACCEPTED; |
(...skipping 30 matching lines...) Expand all Loading... | |
198 return METHOD_DELETE; | 220 return METHOD_DELETE; |
199 } else if (token == "patch") { | 221 } else if (token == "patch") { |
200 return METHOD_PATCH; | 222 return METHOD_PATCH; |
201 } | 223 } |
202 NOTREACHED() << "Method not implemented: " << token; | 224 NOTREACHED() << "Method not implemented: " << token; |
203 return METHOD_UNKNOWN; | 225 return METHOD_UNKNOWN; |
204 } | 226 } |
205 | 227 |
206 } // namespace test_server | 228 } // namespace test_server |
207 } // namespace net | 229 } // namespace net |
OLD | NEW |