| 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_response_body_drainer.h" | 5 #include "net/http/http_response_body_drainer.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/http/http_network_session.h" | 11 #include "net/http/http_network_session.h" |
| 12 #include "net/http/http_stream_base.h" | 12 #include "net/http/http_stream_base.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 HttpResponseBodyDrainer::HttpResponseBodyDrainer(HttpStreamBase* stream) | 16 HttpResponseBodyDrainer::HttpResponseBodyDrainer(HttpStreamBase* stream) |
| 17 : read_size_(0), | 17 : stream_(stream), |
| 18 stream_(stream), | |
| 19 next_state_(STATE_NONE), | 18 next_state_(STATE_NONE), |
| 20 total_read_(0), | 19 total_read_(0), |
| 21 session_(NULL) {} | 20 session_(NULL) {} |
| 22 | 21 |
| 23 HttpResponseBodyDrainer::~HttpResponseBodyDrainer() {} | 22 HttpResponseBodyDrainer::~HttpResponseBodyDrainer() {} |
| 24 | 23 |
| 25 void HttpResponseBodyDrainer::Start(HttpNetworkSession* session) { | 24 void HttpResponseBodyDrainer::Start(HttpNetworkSession* session) { |
| 26 StartWithSize(session, kDrainBodyBufferSize); | 25 read_buf_ = new IOBuffer(kDrainBodyBufferSize); |
| 27 } | |
| 28 | |
| 29 void HttpResponseBodyDrainer::StartWithSize(HttpNetworkSession* session, | |
| 30 int num_bytes_to_drain) { | |
| 31 DCHECK_LE(0, num_bytes_to_drain); | |
| 32 // TODO(simonjam): Consider raising this limit if we're pipelining. If we have | |
| 33 // a bunch of responses in the pipeline, we should be less willing to give up | |
| 34 // while draining. | |
| 35 if (num_bytes_to_drain > kDrainBodyBufferSize) { | |
| 36 Finish(ERR_RESPONSE_BODY_TOO_BIG_TO_DRAIN); | |
| 37 return; | |
| 38 } else if (num_bytes_to_drain == 0) { | |
| 39 Finish(OK); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 read_size_ = num_bytes_to_drain; | |
| 44 read_buf_ = new IOBuffer(read_size_); | |
| 45 next_state_ = STATE_DRAIN_RESPONSE_BODY; | 26 next_state_ = STATE_DRAIN_RESPONSE_BODY; |
| 46 int rv = DoLoop(OK); | 27 int rv = DoLoop(OK); |
| 47 | 28 |
| 48 if (rv == ERR_IO_PENDING) { | 29 if (rv == ERR_IO_PENDING) { |
| 49 timer_.Start(FROM_HERE, | 30 timer_.Start(FROM_HERE, |
| 50 base::TimeDelta::FromSeconds(kTimeoutInSeconds), | 31 base::TimeDelta::FromSeconds(kTimeoutInSeconds), |
| 51 this, | 32 this, |
| 52 &HttpResponseBodyDrainer::OnTimerFired); | 33 &HttpResponseBodyDrainer::OnTimerFired); |
| 53 session_ = session; | 34 session_ = session; |
| 54 session->AddResponseDrainer(this); | 35 session->AddResponseDrainer(this); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 62 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 82 | 63 |
| 83 return rv; | 64 return rv; |
| 84 } | 65 } |
| 85 | 66 |
| 86 int HttpResponseBodyDrainer::DoDrainResponseBody() { | 67 int HttpResponseBodyDrainer::DoDrainResponseBody() { |
| 87 next_state_ = STATE_DRAIN_RESPONSE_BODY_COMPLETE; | 68 next_state_ = STATE_DRAIN_RESPONSE_BODY_COMPLETE; |
| 88 | 69 |
| 89 return stream_->ReadResponseBody( | 70 return stream_->ReadResponseBody( |
| 90 read_buf_.get(), | 71 read_buf_.get(), |
| 91 read_size_ - total_read_, | 72 kDrainBodyBufferSize - total_read_, |
| 92 base::Bind(&HttpResponseBodyDrainer::OnIOComplete, | 73 base::Bind(&HttpResponseBodyDrainer::OnIOComplete, |
| 93 base::Unretained(this))); | 74 base::Unretained(this))); |
| 94 } | 75 } |
| 95 | 76 |
| 96 int HttpResponseBodyDrainer::DoDrainResponseBodyComplete(int result) { | 77 int HttpResponseBodyDrainer::DoDrainResponseBodyComplete(int result) { |
| 97 DCHECK_NE(ERR_IO_PENDING, result); | 78 DCHECK_NE(ERR_IO_PENDING, result); |
| 98 | 79 |
| 99 if (result < 0) | 80 if (result < 0) |
| 100 return result; | 81 return result; |
| 101 | 82 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 stream_->Close(true /* no keep-alive */); | 117 stream_->Close(true /* no keep-alive */); |
| 137 } else { | 118 } else { |
| 138 DCHECK_EQ(OK, result); | 119 DCHECK_EQ(OK, result); |
| 139 stream_->Close(false /* keep-alive */); | 120 stream_->Close(false /* keep-alive */); |
| 140 } | 121 } |
| 141 | 122 |
| 142 delete this; | 123 delete this; |
| 143 } | 124 } |
| 144 | 125 |
| 145 } // namespace net | 126 } // namespace net |
| OLD | NEW |