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 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 io_state_ = STATE_SEND_HEADERS_COMPLETE; | 467 io_state_ = STATE_SEND_HEADERS_COMPLETE; |
468 return connection_->socket() | 468 return connection_->socket() |
469 ->Write(request_headers_.get(), bytes_remaining, io_callback_); | 469 ->Write(request_headers_.get(), bytes_remaining, io_callback_); |
470 } | 470 } |
471 | 471 |
472 int HttpStreamParser::DoSendHeadersComplete(int result) { | 472 int HttpStreamParser::DoSendHeadersComplete(int result) { |
473 if (result < 0) { | 473 if (result < 0) { |
474 // In the unlikely case that the headers and body were merged, all the | 474 // In the unlikely case that the headers and body were merged, all the |
475 // the headers were sent, but not all of the body way, and |result| is | 475 // the headers were sent, but not all of the body way, and |result| is |
476 // an error that this should try reading after, stash the error for now and | 476 // an error that this should try reading after, stash the error for now and |
477 // act like the request was successfully sent. | 477 // act like the request was successfully sent. |
mmenke
2016/07/11 15:15:35
I'd suggest adding a new state, DO_SEND_REQUEST_CO
mmenke
2016/07/11 15:15:35
Missed a spot.
maksims (do not use this acc)
2016/07/12 10:49:25
Do you like it now?
| |
478 if (request_headers_->BytesConsumed() >= request_headers_length_ && | 478 if (request_headers_->BytesConsumed() >= request_headers_length_ && |
479 ShouldTryReadingOnUploadError(result)) { | 479 ShouldTryReadingOnUploadError(result)) { |
480 upload_error_ = result; | 480 upload_error_ = result; |
481 return OK; | 481 return OK; |
482 } | 482 } |
483 return result; | 483 return result; |
484 } | 484 } |
485 | 485 |
486 sent_bytes_ += result; | 486 sent_bytes_ += result; |
487 request_headers_->DidConsume(result); | 487 request_headers_->DidConsume(result); |
(...skipping 11 matching lines...) Expand all Loading... | |
499 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY, | 499 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY, |
500 base::Bind(&NetLogSendRequestBodyCallback, | 500 base::Bind(&NetLogSendRequestBodyCallback, |
501 request_->upload_data_stream->size(), | 501 request_->upload_data_stream->size(), |
502 request_->upload_data_stream->is_chunked(), | 502 request_->upload_data_stream->is_chunked(), |
503 false /* not merged */)); | 503 false /* not merged */)); |
504 io_state_ = STATE_SEND_BODY; | 504 io_state_ = STATE_SEND_BODY; |
505 return OK; | 505 return OK; |
506 } | 506 } |
507 | 507 |
508 // Finished sending the request. | 508 // Finished sending the request. |
509 FlushBuffers(); | |
509 return OK; | 510 return OK; |
510 } | 511 } |
511 | 512 |
512 int HttpStreamParser::DoSendBody() { | 513 int HttpStreamParser::DoSendBody() { |
513 if (request_body_send_buf_->BytesRemaining() > 0) { | 514 if (request_body_send_buf_->BytesRemaining() > 0) { |
514 io_state_ = STATE_SEND_BODY_COMPLETE; | 515 io_state_ = STATE_SEND_BODY_COMPLETE; |
515 return connection_->socket() | 516 return connection_->socket() |
516 ->Write(request_body_send_buf_.get(), | 517 ->Write(request_body_send_buf_.get(), |
517 request_body_send_buf_->BytesRemaining(), | 518 request_body_send_buf_->BytesRemaining(), |
518 io_callback_); | 519 io_callback_); |
519 } | 520 } |
520 | 521 |
521 if (request_->upload_data_stream->is_chunked() && sent_last_chunk_) { | 522 if (request_->upload_data_stream->is_chunked() && sent_last_chunk_) { |
522 // Finished sending the request. | 523 // Finished sending the request. |
524 FlushBuffers(); | |
523 return OK; | 525 return OK; |
524 } | 526 } |
525 | 527 |
526 request_body_read_buf_->Clear(); | 528 request_body_read_buf_->Clear(); |
527 io_state_ = STATE_SEND_REQUEST_READ_BODY_COMPLETE; | 529 io_state_ = STATE_SEND_REQUEST_READ_BODY_COMPLETE; |
528 return request_->upload_data_stream->Read(request_body_read_buf_.get(), | 530 return request_->upload_data_stream->Read(request_body_read_buf_.get(), |
529 request_body_read_buf_->capacity(), | 531 request_body_read_buf_->capacity(), |
530 io_callback_); | 532 io_callback_); |
531 } | 533 } |
532 | 534 |
(...skipping 11 matching lines...) Expand all Loading... | |
544 sent_bytes_ += result; | 546 sent_bytes_ += result; |
545 request_body_send_buf_->DidConsume(result); | 547 request_body_send_buf_->DidConsume(result); |
546 | 548 |
547 io_state_ = STATE_SEND_BODY; | 549 io_state_ = STATE_SEND_BODY; |
548 return OK; | 550 return OK; |
549 } | 551 } |
550 | 552 |
551 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) { | 553 int HttpStreamParser::DoSendRequestReadBodyComplete(int result) { |
552 // |result| is the result of read from the request body from the last call to | 554 // |result| is the result of read from the request body from the last call to |
553 // DoSendBody(). | 555 // DoSendBody(). |
554 if (result < 0) | 556 if (result < 0) { |
557 FlushBuffers(); | |
555 return result; | 558 return result; |
559 } | |
556 | 560 |
557 // Chunked data needs to be encoded. | 561 // Chunked data needs to be encoded. |
558 if (request_->upload_data_stream->is_chunked()) { | 562 if (request_->upload_data_stream->is_chunked()) { |
559 if (result == 0) { // Reached the end. | 563 if (result == 0) { // Reached the end. |
560 DCHECK(request_->upload_data_stream->IsEOF()); | 564 DCHECK(request_->upload_data_stream->IsEOF()); |
561 sent_last_chunk_ = true; | 565 sent_last_chunk_ = true; |
562 } | 566 } |
563 // Encode the buffer as 1 chunk. | 567 // Encode the buffer as 1 chunk. |
564 const base::StringPiece payload(request_body_read_buf_->data(), result); | 568 const base::StringPiece payload(request_body_read_buf_->data(), result); |
565 request_body_send_buf_->Clear(); | 569 request_body_send_buf_->Clear(); |
566 result = EncodeChunk(payload, | 570 result = EncodeChunk(payload, |
567 request_body_send_buf_->data(), | 571 request_body_send_buf_->data(), |
568 request_body_send_buf_->capacity()); | 572 request_body_send_buf_->capacity()); |
569 } | 573 } |
570 | 574 |
571 if (result == 0) { // Reached the end. | 575 if (result == 0) { // Reached the end. |
572 // Reaching EOF means we can finish sending request body unless the data is | 576 // Reaching EOF means we can finish sending request body unless the data is |
573 // chunked. (i.e. No need to send the terminal chunk.) | 577 // chunked. (i.e. No need to send the terminal chunk.) |
574 DCHECK(request_->upload_data_stream->IsEOF()); | 578 DCHECK(request_->upload_data_stream->IsEOF()); |
575 DCHECK(!request_->upload_data_stream->is_chunked()); | 579 DCHECK(!request_->upload_data_stream->is_chunked()); |
576 // Finished sending the request. | 580 // Finished sending the request. |
581 FlushBuffers(); | |
577 } else if (result > 0) { | 582 } else if (result > 0) { |
578 request_body_send_buf_->DidAppend(result); | 583 request_body_send_buf_->DidAppend(result); |
579 result = 0; | 584 result = 0; |
580 io_state_ = STATE_SEND_BODY; | 585 io_state_ = STATE_SEND_BODY; |
581 } | 586 } |
582 return result; | 587 return result; |
583 } | 588 } |
584 | 589 |
585 int HttpStreamParser::DoReadHeaders() { | 590 int HttpStreamParser::DoReadHeaders() { |
586 io_state_ = STATE_READ_HEADERS_COMPLETE; | 591 io_state_ = STATE_READ_HEADERS_COMPLETE; |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1181 return false; | 1186 return false; |
1182 } | 1187 } |
1183 | 1188 |
1184 void HttpStreamParser::ValidateStatusLine(const std::string& status_line) { | 1189 void HttpStreamParser::ValidateStatusLine(const std::string& status_line) { |
1185 HttpStatusLineValidator::StatusLineStatus status = | 1190 HttpStatusLineValidator::StatusLineStatus status = |
1186 HttpStatusLineValidator::ValidateStatusLine(status_line); | 1191 HttpStatusLineValidator::ValidateStatusLine(status_line); |
1187 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status, | 1192 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status, |
1188 HttpStatusLineValidator::STATUS_LINE_MAX); | 1193 HttpStatusLineValidator::STATUS_LINE_MAX); |
1189 } | 1194 } |
1190 | 1195 |
1196 void HttpStreamParser::FlushBuffers() { | |
1197 if (request_headers_) | |
mmenke
2016/07/11 15:15:35
None of these ifs are necessary - refptrs have ide
| |
1198 request_headers_ = nullptr; | |
1199 if (request_body_send_buf_) | |
1200 request_body_send_buf_ = nullptr; | |
1201 if (request_body_read_buf_) | |
1202 request_body_read_buf_ = nullptr; | |
1203 } | |
1204 | |
1191 } // namespace net | 1205 } // namespace net |
OLD | NEW |