OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.h" | 12 #include "net/http/http_stream.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 HttpResponseBodyDrainer::HttpResponseBodyDrainer(HttpStream* stream) | 16 HttpResponseBodyDrainer::HttpResponseBodyDrainer(HttpStream* stream) |
17 : stream_(stream), | 17 : stream_(stream), |
18 next_state_(STATE_NONE), | 18 next_state_(STATE_NONE), |
19 total_read_(0), | 19 total_read_(0), |
20 ALLOW_THIS_IN_INITIALIZER_LIST( | 20 ALLOW_THIS_IN_INITIALIZER_LIST( |
21 io_callback_(this, &HttpResponseBodyDrainer::OnIOComplete)), | 21 io_callback_(this, &HttpResponseBodyDrainer::OnIOComplete)), |
22 user_callback_(NULL), | 22 user_callback_(NULL), |
23 session_(NULL) {} | 23 session_(NULL) {} |
24 | 24 |
25 HttpResponseBodyDrainer::~HttpResponseBodyDrainer() {} | 25 HttpResponseBodyDrainer::~HttpResponseBodyDrainer() {} |
26 | 26 |
27 void HttpResponseBodyDrainer::Start(HttpNetworkSession* session) { | 27 void HttpResponseBodyDrainer::Start(HttpNetworkSession* session) { |
28 read_buf_ = new IOBuffer(kDrainBodyBufferSize); | 28 StartWithSize(session, kDrainBodyBufferSize); |
29 } | |
30 | |
31 void HttpResponseBodyDrainer::StartWithSize(HttpNetworkSession* session, | |
32 int num_bytes_to_drain) { | |
33 DCHECK_LE(0, num_bytes_to_drain); | |
34 if (num_bytes_to_drain > kDrainBodyBufferSize) { | |
mmenke
2011/11/28 18:39:10
Just thinking out loud: A larger minimum may be b
James Simonsen
2011/11/29 23:57:28
Good point. I added a TODO.
| |
35 Finish(ERR_RESPONSE_BODY_TOO_BIG_TO_DRAIN); | |
36 return; | |
37 } else if (num_bytes_to_drain == 0) { | |
38 Finish(OK); | |
39 return; | |
40 } | |
41 | |
42 read_size_ = num_bytes_to_drain; | |
43 read_buf_ = new IOBuffer(read_size_); | |
29 next_state_ = STATE_DRAIN_RESPONSE_BODY; | 44 next_state_ = STATE_DRAIN_RESPONSE_BODY; |
30 int rv = DoLoop(OK); | 45 int rv = DoLoop(OK); |
31 | 46 |
32 if (rv == ERR_IO_PENDING) { | 47 if (rv == ERR_IO_PENDING) { |
33 timer_.Start(FROM_HERE, | 48 timer_.Start(FROM_HERE, |
34 base::TimeDelta::FromSeconds(kTimeoutInSeconds), | 49 base::TimeDelta::FromSeconds(kTimeoutInSeconds), |
35 this, | 50 this, |
36 &HttpResponseBodyDrainer::OnTimerFired); | 51 &HttpResponseBodyDrainer::OnTimerFired); |
37 session_ = session; | 52 session_ = session; |
38 session->AddResponseDrainer(this); | 53 session->AddResponseDrainer(this); |
(...skipping 25 matching lines...) Expand all Loading... | |
64 } | 79 } |
65 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 80 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
66 | 81 |
67 return rv; | 82 return rv; |
68 } | 83 } |
69 | 84 |
70 int HttpResponseBodyDrainer::DoDrainResponseBody() { | 85 int HttpResponseBodyDrainer::DoDrainResponseBody() { |
71 next_state_ = STATE_DRAIN_RESPONSE_BODY_COMPLETE; | 86 next_state_ = STATE_DRAIN_RESPONSE_BODY_COMPLETE; |
72 | 87 |
73 return stream_->ReadResponseBody( | 88 return stream_->ReadResponseBody( |
74 read_buf_, kDrainBodyBufferSize - total_read_, | 89 read_buf_, read_size_ - total_read_, |
75 &io_callback_); | 90 &io_callback_); |
76 } | 91 } |
77 | 92 |
78 int HttpResponseBodyDrainer::DoDrainResponseBodyComplete(int result) { | 93 int HttpResponseBodyDrainer::DoDrainResponseBodyComplete(int result) { |
79 DCHECK_NE(ERR_IO_PENDING, result); | 94 DCHECK_NE(ERR_IO_PENDING, result); |
80 | 95 |
81 if (result < 0) | 96 if (result < 0) |
82 return result; | 97 return result; |
83 | 98 |
84 if (result == 0) | 99 if (result == 0) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 stream_->Close(true /* no keep-alive */); | 133 stream_->Close(true /* no keep-alive */); |
119 } else { | 134 } else { |
120 DCHECK_EQ(OK, result); | 135 DCHECK_EQ(OK, result); |
121 stream_->Close(false /* keep-alive */); | 136 stream_->Close(false /* keep-alive */); |
122 } | 137 } |
123 | 138 |
124 delete this; | 139 delete this; |
125 } | 140 } |
126 | 141 |
127 } // namespace net | 142 } // namespace net |
OLD | NEW |