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_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 next_state_(STATE_NONE), | 152 next_state_(STATE_NONE), |
153 establishing_tunnel_(false), | 153 establishing_tunnel_(false), |
154 websocket_handshake_stream_base_create_helper_(NULL) { | 154 websocket_handshake_stream_base_create_helper_(NULL) { |
155 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_); | 155 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_); |
156 session->GetNextProtos(&server_ssl_config_.next_protos); | 156 session->GetNextProtos(&server_ssl_config_.next_protos); |
157 proxy_ssl_config_ = server_ssl_config_; | 157 proxy_ssl_config_ = server_ssl_config_; |
158 } | 158 } |
159 | 159 |
160 HttpNetworkTransaction::~HttpNetworkTransaction() { | 160 HttpNetworkTransaction::~HttpNetworkTransaction() { |
161 if (stream_.get()) { | 161 if (stream_.get()) { |
162 HttpResponseHeaders* headers = GetResponseHeaders(); | |
163 // TODO(mbelshe): The stream_ should be able to compute whether or not the | 162 // TODO(mbelshe): The stream_ should be able to compute whether or not the |
164 // stream should be kept alive. No reason to compute here | 163 // stream should be kept alive. No reason to compute here |
165 // and pass it in. | 164 // and pass it in. |
166 bool try_to_keep_alive = | 165 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) { |
167 next_state_ == STATE_NONE && | |
168 stream_->CanFindEndOfResponse() && | |
169 (!headers || headers->IsKeepAlive()); | |
170 if (!try_to_keep_alive) { | |
171 stream_->Close(true /* not reusable */); | 166 stream_->Close(true /* not reusable */); |
| 167 } else if (stream_->IsResponseBodyComplete()) { |
| 168 // If the response body is complete, we can just reuse the socket. |
| 169 stream_->Close(false /* reusable */); |
172 } else { | 170 } else { |
173 if (stream_->IsResponseBodyComplete()) { | 171 // Otherwise, we try to drain the response body. |
174 // If the response body is complete, we can just reuse the socket. | 172 HttpStream* stream = stream_.release(); |
175 stream_->Close(false /* reusable */); | 173 stream->Drain(session_); |
176 } else if (stream_->IsSpdyHttpStream()) { | |
177 // Doesn't really matter for SpdyHttpStream. Just close it. | |
178 stream_->Close(true /* not reusable */); | |
179 } else { | |
180 // Otherwise, we try to drain the response body. | |
181 HttpStream* stream = stream_.release(); | |
182 stream->Drain(session_); | |
183 } | |
184 } | 174 } |
185 } | 175 } |
186 | 176 |
187 if (request_ && request_->upload_data_stream) | 177 if (request_ && request_->upload_data_stream) |
188 request_->upload_data_stream->Reset(); // Invalidate pending callbacks. | 178 request_->upload_data_stream->Reset(); // Invalidate pending callbacks. |
189 } | 179 } |
190 | 180 |
191 int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, | 181 int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
192 const CompletionCallback& callback, | 182 const CompletionCallback& callback, |
193 const BoundNetLog& net_log) { | 183 const BoundNetLog& net_log) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 return rv; | 276 return rv; |
287 } | 277 } |
288 | 278 |
289 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { | 279 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
290 DCHECK(HaveAuth(target)); | 280 DCHECK(HaveAuth(target)); |
291 DCHECK(!stream_request_.get()); | 281 DCHECK(!stream_request_.get()); |
292 | 282 |
293 bool keep_alive = false; | 283 bool keep_alive = false; |
294 // Even if the server says the connection is keep-alive, we have to be | 284 // Even if the server says the connection is keep-alive, we have to be |
295 // able to find the end of each response in order to reuse the connection. | 285 // able to find the end of each response in order to reuse the connection. |
296 if (GetResponseHeaders()->IsKeepAlive() && | 286 if (stream_->CanReuseConnection()) { |
297 stream_->CanFindEndOfResponse()) { | |
298 // If the response body hasn't been completely read, we need to drain | 287 // If the response body hasn't been completely read, we need to drain |
299 // it first. | 288 // it first. |
300 if (!stream_->IsResponseBodyComplete()) { | 289 if (!stream_->IsResponseBodyComplete()) { |
301 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; | 290 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
302 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. | 291 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. |
303 read_buf_len_ = kDrainBodyBufferSize; | 292 read_buf_len_ = kDrainBodyBufferSize; |
304 return; | 293 return; |
305 } | 294 } |
306 keep_alive = true; | 295 keep_alive = true; |
307 } | 296 } |
308 | 297 |
309 // We don't need to drain the response body, so we act as if we had drained | 298 // We don't need to drain the response body, so we act as if we had drained |
310 // the response body. | 299 // the response body. |
311 DidDrainBodyForAuthRestart(keep_alive); | 300 DidDrainBodyForAuthRestart(keep_alive); |
312 } | 301 } |
313 | 302 |
314 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { | 303 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
315 DCHECK(!stream_request_.get()); | 304 DCHECK(!stream_request_.get()); |
316 | 305 |
317 if (stream_.get()) { | 306 if (stream_.get()) { |
318 total_received_bytes_ += stream_->GetTotalReceivedBytes(); | 307 total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
319 HttpStream* new_stream = NULL; | 308 HttpStream* new_stream = NULL; |
320 if (keep_alive && stream_->IsConnectionReusable()) { | 309 if (keep_alive && stream_->CanReuseConnection()) { |
321 // We should call connection_->set_idle_time(), but this doesn't occur | 310 // We should call connection_->set_idle_time(), but this doesn't occur |
322 // often enough to be worth the trouble. | 311 // often enough to be worth the trouble. |
323 stream_->SetConnectionReused(); | 312 stream_->SetConnectionReused(); |
324 new_stream = stream_->RenewStreamForAuth(); | 313 new_stream = stream_->RenewStreamForAuth(); |
325 } | 314 } |
326 | 315 |
327 if (!new_stream) { | 316 if (!new_stream) { |
328 // Close the stream and mark it as not_reusable. Even in the | 317 // Close the stream and mark it as not_reusable. Even in the |
329 // keep_alive case, we've determined that the stream_ is not | 318 // keep_alive case, we've determined that the stream_ is not |
330 // reusable if new_stream is NULL. | 319 // reusable if new_stream is NULL. |
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1108 } | 1097 } |
1109 | 1098 |
1110 int HttpNetworkTransaction::DoReadBodyComplete(int result) { | 1099 int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
1111 // We are done with the Read call. | 1100 // We are done with the Read call. |
1112 bool done = false; | 1101 bool done = false; |
1113 if (result <= 0) { | 1102 if (result <= 0) { |
1114 DCHECK_NE(ERR_IO_PENDING, result); | 1103 DCHECK_NE(ERR_IO_PENDING, result); |
1115 done = true; | 1104 done = true; |
1116 } | 1105 } |
1117 | 1106 |
1118 bool keep_alive = false; | 1107 // Clean up connection if we are done. |
1119 if (stream_->IsResponseBodyComplete()) { | 1108 if (done) { |
1120 // Note: Just because IsResponseBodyComplete is true, we're not | 1109 // Note: Just because IsResponseBodyComplete is true, we're not |
1121 // necessarily "done". We're only "done" when it is the last | 1110 // necessarily "done". We're only "done" when it is the last |
1122 // read on this HttpNetworkTransaction, which will be signified | 1111 // read on this HttpNetworkTransaction, which will be signified |
1123 // by a zero-length read. | 1112 // by a zero-length read. |
1124 // TODO(mbelshe): The keepalive property is really a property of | 1113 // TODO(mbelshe): The keep-alive property is really a property of |
1125 // the stream. No need to compute it here just to pass back | 1114 // the stream. No need to compute it here just to pass back |
1126 // to the stream's Close function. | 1115 // to the stream's Close function. |
1127 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no | 1116 bool keep_alive = |
1128 // ResponseHeaders. | 1117 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection(); |
1129 if (stream_->CanFindEndOfResponse()) { | |
1130 HttpResponseHeaders* headers = GetResponseHeaders(); | |
1131 if (headers) | |
1132 keep_alive = headers->IsKeepAlive(); | |
1133 } | |
1134 } | |
1135 | 1118 |
1136 // Clean up connection if we are done. | |
1137 if (done) { | |
1138 stream_->Close(!keep_alive); | 1119 stream_->Close(!keep_alive); |
1139 // Note: we don't reset the stream here. We've closed it, but we still | 1120 // Note: we don't reset the stream here. We've closed it, but we still |
1140 // need it around so that callers can call methods such as | 1121 // need it around so that callers can call methods such as |
1141 // GetUploadProgress() and have them be meaningful. | 1122 // GetUploadProgress() and have them be meaningful. |
1142 // TODO(mbelshe): This means we closed the stream here, and we close it | 1123 // TODO(mbelshe): This means we closed the stream here, and we close it |
1143 // again in ~HttpNetworkTransaction. Clean that up. | 1124 // again in ~HttpNetworkTransaction. Clean that up. |
1144 | 1125 |
1145 // The next Read call will return 0 (EOF). | 1126 // The next Read call will return 0 (EOF). |
1146 } | 1127 } |
1147 | 1128 |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1660 DCHECK(stream_request_); | 1641 DCHECK(stream_request_); |
1661 | 1642 |
1662 // Since the transaction can restart with auth credentials, it may create a | 1643 // Since the transaction can restart with auth credentials, it may create a |
1663 // stream more than once. Accumulate all of the connection attempts across | 1644 // stream more than once. Accumulate all of the connection attempts across |
1664 // those streams by appending them to the vector: | 1645 // those streams by appending them to the vector: |
1665 for (const auto& attempt : stream_request_->connection_attempts()) | 1646 for (const auto& attempt : stream_request_->connection_attempts()) |
1666 connection_attempts_.push_back(attempt); | 1647 connection_attempts_.push_back(attempt); |
1667 } | 1648 } |
1668 | 1649 |
1669 } // namespace net | 1650 } // namespace net |
OLD | NEW |