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/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 // large enough to hold the encoded chunk. | 267 // large enough to hold the encoded chunk. |
268 request_body_read_buf_ = | 268 request_body_read_buf_ = |
269 new SeekableIOBuffer(kRequestBodyBufferSize - kChunkHeaderFooterSize); | 269 new SeekableIOBuffer(kRequestBodyBufferSize - kChunkHeaderFooterSize); |
270 } else { | 270 } else { |
271 // No need to encode request body, just send the raw data. | 271 // No need to encode request body, just send the raw data. |
272 request_body_read_buf_ = request_body_send_buf_; | 272 request_body_read_buf_ = request_body_send_buf_; |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
276 io_state_ = STATE_SEND_HEADERS; | 276 io_state_ = STATE_SEND_HEADERS; |
277 | |
278 // If we have a small request body, then we'll merge with the headers into a | 277 // If we have a small request body, then we'll merge with the headers into a |
279 // single write. | 278 // single write. |
280 bool did_merge = false; | 279 bool did_merge = false; |
281 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) { | 280 if (ShouldMergeRequestHeadersAndBody(request, request_->upload_data_stream)) { |
282 int merged_size = static_cast<int>( | 281 int merged_size = static_cast<int>( |
283 request_headers_length_ + request_->upload_data_stream->size()); | 282 request_headers_length_ + request_->upload_data_stream->size()); |
284 scoped_refptr<IOBuffer> merged_request_headers_and_body( | 283 scoped_refptr<IOBuffer> merged_request_headers_and_body( |
285 new IOBuffer(merged_size)); | 284 new IOBuffer(merged_size)); |
286 // We'll repurpose |request_headers_| to store the merged headers and | 285 // We'll repurpose |request_headers_| to store the merged headers and |
287 // body. | 286 // body. |
288 request_headers_ = new DrainableIOBuffer( | 287 request_headers_ = new DrainableIOBuffer( |
289 merged_request_headers_and_body.get(), merged_size); | 288 merged_request_headers_and_body.get(), merged_size); |
290 | |
291 memcpy(request_headers_->data(), request.data(), request_headers_length_); | 289 memcpy(request_headers_->data(), request.data(), request_headers_length_); |
292 request_headers_->DidConsume(request_headers_length_); | 290 request_headers_->DidConsume(request_headers_length_); |
293 | 291 |
294 uint64_t todo = request_->upload_data_stream->size(); | 292 uint64_t todo = request_->upload_data_stream->size(); |
295 while (todo) { | 293 while (todo) { |
296 int consumed = request_->upload_data_stream->Read( | 294 int consumed = request_->upload_data_stream->Read( |
297 request_headers_.get(), static_cast<int>(todo), CompletionCallback()); | 295 request_headers_.get(), static_cast<int>(todo), CompletionCallback()); |
298 DCHECK_GT(consumed, 0); // Read() won't fail if not chunked. | 296 // Read() must succeed synchronously if not chunked and in memory. |
| 297 DCHECK_GT(consumed, 0); |
299 request_headers_->DidConsume(consumed); | 298 request_headers_->DidConsume(consumed); |
300 todo -= consumed; | 299 todo -= consumed; |
301 } | 300 } |
302 DCHECK(request_->upload_data_stream->IsEOF()); | 301 DCHECK(request_->upload_data_stream->IsEOF()); |
303 // Reset the offset, so the buffer can be read from the beginning. | 302 // Reset the offset, so the buffer can be read from the beginning. |
304 request_headers_->SetOffset(0); | 303 request_headers_->SetOffset(0); |
305 did_merge = true; | 304 did_merge = true; |
306 | 305 |
307 net_log_.AddEvent( | 306 net_log_.AddEvent( |
308 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY, | 307 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY, |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 sent_bytes_ += result; | 542 sent_bytes_ += result; |
544 request_body_send_buf_->DidConsume(result); | 543 request_body_send_buf_->DidConsume(result); |
545 | 544 |
546 io_state_ = STATE_SEND_BODY; | 545 io_state_ = STATE_SEND_BODY; |
547 return OK; | 546 return OK; |
548 } | 547 } |
549 | 548 |
550 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) { | 549 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) { |
551 // |result| is the result of read from the request body from the last call to | 550 // |result| is the result of read from the request body from the last call to |
552 // DoSendBody(). | 551 // DoSendBody(). |
553 DCHECK_GE(result, 0); // There won't be errors. | 552 if (result < 0) |
| 553 return result; |
554 | 554 |
555 // Chunked data needs to be encoded. | 555 // Chunked data needs to be encoded. |
556 if (request_->upload_data_stream->is_chunked()) { | 556 if (request_->upload_data_stream->is_chunked()) { |
557 if (result == 0) { // Reached the end. | 557 if (result == 0) { // Reached the end. |
558 DCHECK(request_->upload_data_stream->IsEOF()); | 558 DCHECK(request_->upload_data_stream->IsEOF()); |
559 sent_last_chunk_ = true; | 559 sent_last_chunk_ = true; |
560 } | 560 } |
561 // Encode the buffer as 1 chunk. | 561 // Encode the buffer as 1 chunk. |
562 const base::StringPiece payload(request_body_read_buf_->data(), result); | 562 const base::StringPiece payload(request_body_read_buf_->data(), result); |
563 request_body_send_buf_->Clear(); | 563 request_body_send_buf_->Clear(); |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 } | 1180 } |
1181 | 1181 |
1182 void HttpStreamParser::ValidateStatusLine(const std::string& status_line) { | 1182 void HttpStreamParser::ValidateStatusLine(const std::string& status_line) { |
1183 HttpStatusLineValidator::StatusLineStatus status = | 1183 HttpStatusLineValidator::StatusLineStatus status = |
1184 HttpStatusLineValidator::ValidateStatusLine(status_line); | 1184 HttpStatusLineValidator::ValidateStatusLine(status_line); |
1185 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status, | 1185 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status, |
1186 HttpStatusLineValidator::STATUS_LINE_MAX); | 1186 HttpStatusLineValidator::STATUS_LINE_MAX); |
1187 } | 1187 } |
1188 | 1188 |
1189 } // namespace net | 1189 } // namespace net |
OLD | NEW |