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 #include "net/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 // Check to see if we're done reading. | 523 // Check to see if we're done reading. |
524 if (IsResponseBodyComplete()) | 524 if (IsResponseBodyComplete()) |
525 return 0; | 525 return 0; |
526 | 526 |
527 DCHECK_EQ(0, read_buf_->offset()); | 527 DCHECK_EQ(0, read_buf_->offset()); |
528 return connection_->socket()->Read(user_read_buf_, user_read_buf_len_, | 528 return connection_->socket()->Read(user_read_buf_, user_read_buf_len_, |
529 &io_callback_); | 529 &io_callback_); |
530 } | 530 } |
531 | 531 |
532 int HttpStreamParser::DoReadBodyComplete(int result) { | 532 int HttpStreamParser::DoReadBodyComplete(int result) { |
533 // If we didn't get a Content-Length and aren't using a chunked encoding, | 533 // When the connection is closed, there are numerous ways to interpret it. |
534 // the only way to signal the end of a stream is to close the connection, | 534 // |
535 // so we don't treat that as an error, though in some cases we may not | 535 // - If a Content-Length header is present and the body contains exactly that |
536 // have completely received the resource. | 536 // number of bytes at connection close, the response is successful. |
537 if (result == 0 && !IsResponseBodyComplete() && CanFindEndOfResponse()) | 537 // |
538 result = ERR_CONNECTION_CLOSED; | 538 // - If a Content-Length header is present and the body contains fewer bytes |
| 539 // than promised by the header at connection close, it may indicate that |
| 540 // the connection was closed prematurely, or it may indicate that the |
| 541 // server sent an invalid Content-Length header. Unfortunately, the invalid |
| 542 // Content-Length header case does occur in practice and other browsers are |
| 543 // tolerant of it, so this is reported as an ERR_CONTENT_LENGTH_MISMATCH |
| 544 // rather than an ERR_CONNECTION_CLOSE. |
| 545 // |
| 546 // - If chunked encoding is used and the terminating chunk has been processed |
| 547 // when the connection is closed, the response is successful. |
| 548 // |
| 549 // - If chunked encoding is used and the terminating chunk has not been |
| 550 // processed when the connection is closed, it may indicate that the |
| 551 // connection was closed prematurely or it may indicate that the server |
| 552 // sent an invalid chunked encoding. We choose to treat it as |
| 553 // an invalid chunked encoding. |
| 554 // |
| 555 // - If a Content-Length is not present and chunked encoding is not used, |
| 556 // connection close is the only way to signal that the response is |
| 557 // complete. Unfortunately, this also means that there is no way to detect |
| 558 // early close of a connection. No error is returned. |
| 559 if (result == 0 && !IsResponseBodyComplete() && CanFindEndOfResponse()) { |
| 560 if (chunked_decoder_.get()) |
| 561 result = ERR_INVALID_CHUNKED_ENCODING; |
| 562 else |
| 563 result = ERR_CONTENT_LENGTH_MISMATCH; |
| 564 } |
539 | 565 |
540 // Filter incoming data if appropriate. FilterBuf may return an error. | 566 // Filter incoming data if appropriate. FilterBuf may return an error. |
541 if (result > 0 && chunked_decoder_.get()) { | 567 if (result > 0 && chunked_decoder_.get()) { |
542 result = chunked_decoder_->FilterBuf(user_read_buf_->data(), result); | 568 result = chunked_decoder_->FilterBuf(user_read_buf_->data(), result); |
543 if (result == 0 && !chunked_decoder_->reached_eof()) { | 569 if (result == 0 && !chunked_decoder_->reached_eof()) { |
544 // Don't signal completion of the Read call yet or else it'll look like | 570 // Don't signal completion of the Read call yet or else it'll look like |
545 // we received end-of-file. Wait for more data. | 571 // we received end-of-file. Wait for more data. |
546 io_state_ = STATE_READ_BODY; | 572 io_state_ = STATE_READ_BODY; |
547 return OK; | 573 return OK; |
548 } | 574 } |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 void HttpStreamParser::GetSSLCertRequestInfo( | 788 void HttpStreamParser::GetSSLCertRequestInfo( |
763 SSLCertRequestInfo* cert_request_info) { | 789 SSLCertRequestInfo* cert_request_info) { |
764 if (request_->url.SchemeIs("https") && connection_->socket()) { | 790 if (request_->url.SchemeIs("https") && connection_->socket()) { |
765 SSLClientSocket* ssl_socket = | 791 SSLClientSocket* ssl_socket = |
766 static_cast<SSLClientSocket*>(connection_->socket()); | 792 static_cast<SSLClientSocket*>(connection_->socket()); |
767 ssl_socket->GetSSLCertRequestInfo(cert_request_info); | 793 ssl_socket->GetSSLCertRequestInfo(cert_request_info); |
768 } | 794 } |
769 } | 795 } |
770 | 796 |
771 } // namespace net | 797 } // namespace net |
OLD | NEW |