| 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/spdy/spdy_http_stream.h" | 5 #include "net/spdy/spdy_http_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <list> | 8 #include <list> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 namespace net { | 30 namespace net { |
| 31 | 31 |
| 32 const size_t SpdyHttpStream::kRequestBodyBufferSize = 1 << 14; // 16KB | 32 const size_t SpdyHttpStream::kRequestBodyBufferSize = 1 << 14; // 16KB |
| 33 | 33 |
| 34 SpdyHttpStream::SpdyHttpStream(const base::WeakPtr<SpdySession>& spdy_session, | 34 SpdyHttpStream::SpdyHttpStream(const base::WeakPtr<SpdySession>& spdy_session, |
| 35 bool direct) | 35 bool direct) |
| 36 : MultiplexedHttpStream(MultiplexedSessionHandle(spdy_session)), | 36 : MultiplexedHttpStream(MultiplexedSessionHandle(spdy_session)), |
| 37 spdy_session_(spdy_session), | 37 spdy_session_(spdy_session), |
| 38 is_reused_(spdy_session_->IsReused()), | 38 is_reused_(spdy_session_->IsReused()), |
| 39 stream_(nullptr), |
| 39 stream_closed_(false), | 40 stream_closed_(false), |
| 40 closed_stream_status_(ERR_FAILED), | 41 closed_stream_status_(ERR_FAILED), |
| 41 closed_stream_id_(0), | 42 closed_stream_id_(0), |
| 42 closed_stream_received_bytes_(0), | 43 closed_stream_received_bytes_(0), |
| 43 closed_stream_sent_bytes_(0), | 44 closed_stream_sent_bytes_(0), |
| 44 request_info_(NULL), | 45 request_info_(NULL), |
| 45 response_info_(NULL), | 46 response_info_(NULL), |
| 46 response_headers_complete_(false), | 47 response_headers_complete_(false), |
| 47 user_buffer_len_(0), | 48 user_buffer_len_(0), |
| 48 request_body_buf_size_(0), | 49 request_body_buf_size_(0), |
| 49 buffered_read_callback_pending_(false), | 50 buffered_read_callback_pending_(false), |
| 50 more_read_data_pending_(false), | 51 more_read_data_pending_(false), |
| 51 direct_(direct), | 52 direct_(direct), |
| 52 was_alpn_negotiated_(false), | 53 was_alpn_negotiated_(false), |
| 53 weak_factory_(this) { | 54 weak_factory_(this) { |
| 54 DCHECK(spdy_session_.get()); | 55 DCHECK(spdy_session_.get()); |
| 55 } | 56 } |
| 56 | 57 |
| 57 SpdyHttpStream::~SpdyHttpStream() { | 58 SpdyHttpStream::~SpdyHttpStream() { |
| 58 if (stream_.get()) { | 59 if (stream_) { |
| 59 stream_->DetachDelegate(); | 60 stream_->DetachDelegate(); |
| 60 DCHECK(!stream_.get()); | 61 DCHECK(!stream_); |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 | 64 |
| 64 int SpdyHttpStream::InitializeStream(const HttpRequestInfo* request_info, | 65 int SpdyHttpStream::InitializeStream(const HttpRequestInfo* request_info, |
| 65 RequestPriority priority, | 66 RequestPriority priority, |
| 66 const NetLogWithSource& stream_net_log, | 67 const NetLogWithSource& stream_net_log, |
| 67 const CompletionCallback& callback) { | 68 const CompletionCallback& callback) { |
| 68 DCHECK(!stream_); | 69 DCHECK(!stream_); |
| 69 if (!spdy_session_) | 70 if (!spdy_session_) |
| 70 return ERR_CONNECTION_CLOSED; | 71 return ERR_CONNECTION_CLOSED; |
| 71 | 72 |
| 72 request_info_ = request_info; | 73 request_info_ = request_info; |
| 73 if (request_info_->method == "GET") { | 74 if (request_info_->method == "GET") { |
| 74 int error = spdy_session_->GetPushStream(request_info_->url, priority, | 75 int error = spdy_session_->GetPushStream(request_info_->url, priority, |
| 75 &stream_, stream_net_log); | 76 &stream_, stream_net_log); |
| 76 if (error != OK) | 77 if (error != OK) |
| 77 return error; | 78 return error; |
| 78 | 79 |
| 79 // |stream_| may be NULL even if OK was returned. | 80 // |stream_| may be NULL even if OK was returned. |
| 80 if (stream_.get()) { | 81 if (stream_) { |
| 81 DCHECK_EQ(stream_->type(), SPDY_PUSH_STREAM); | 82 DCHECK_EQ(stream_->type(), SPDY_PUSH_STREAM); |
| 82 InitializeStreamHelper(); | 83 InitializeStreamHelper(); |
| 83 return OK; | 84 return OK; |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 | 87 |
| 87 int rv = stream_request_.StartRequest( | 88 int rv = stream_request_.StartRequest( |
| 88 SPDY_REQUEST_RESPONSE_STREAM, spdy_session_, request_info_->url, | 89 SPDY_REQUEST_RESPONSE_STREAM, spdy_session_, request_info_->url, |
| 89 priority, stream_net_log, | 90 priority, stream_net_log, |
| 90 base::Bind(&SpdyHttpStream::OnStreamCreated, | 91 base::Bind(&SpdyHttpStream::OnStreamCreated, |
| 91 weak_factory_.GetWeakPtr(), callback)); | 92 weak_factory_.GetWeakPtr(), callback)); |
| 92 | 93 |
| 93 if (rv == OK) { | 94 if (rv == OK) { |
| 94 stream_ = stream_request_.ReleaseStream(); | 95 stream_ = stream_request_.ReleaseStream().get(); |
| 95 InitializeStreamHelper(); | 96 InitializeStreamHelper(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 return rv; | 99 return rv; |
| 99 } | 100 } |
| 100 | 101 |
| 101 int SpdyHttpStream::ReadResponseHeaders(const CompletionCallback& callback) { | 102 int SpdyHttpStream::ReadResponseHeaders(const CompletionCallback& callback) { |
| 102 CHECK(!callback.is_null()); | 103 CHECK(!callback.is_null()); |
| 103 if (stream_closed_) | 104 if (stream_closed_) |
| 104 return closed_stream_status_; | 105 return closed_stream_status_; |
| 105 | 106 |
| 106 CHECK(stream_.get()); | 107 CHECK(stream_); |
| 107 | 108 |
| 108 // Check if we already have the response headers. If so, return synchronously. | 109 // Check if we already have the response headers. If so, return synchronously. |
| 109 if (response_headers_complete_) { | 110 if (response_headers_complete_) { |
| 110 CHECK(!stream_->IsIdle()); | 111 CHECK(!stream_->IsIdle()); |
| 111 return OK; | 112 return OK; |
| 112 } | 113 } |
| 113 | 114 |
| 114 // Still waiting for the response, return IO_PENDING. | 115 // Still waiting for the response, return IO_PENDING. |
| 115 CHECK(response_callback_.is_null()); | 116 CHECK(response_callback_.is_null()); |
| 116 response_callback_ = callback; | 117 response_callback_ = callback; |
| 117 return ERR_IO_PENDING; | 118 return ERR_IO_PENDING; |
| 118 } | 119 } |
| 119 | 120 |
| 120 int SpdyHttpStream::ReadResponseBody( | 121 int SpdyHttpStream::ReadResponseBody( |
| 121 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 122 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
| 122 // Invalidate HttpRequestInfo pointer. This is to allow the stream to be | 123 // Invalidate HttpRequestInfo pointer. This is to allow the stream to be |
| 123 // shared across multiple transactions which might require this | 124 // shared across multiple transactions which might require this |
| 124 // stream to outlive the request_'s owner. | 125 // stream to outlive the request_'s owner. |
| 125 // Only allowed when Reading of response body starts. It is safe to reset it | 126 // Only allowed when Reading of response body starts. It is safe to reset it |
| 126 // at this point since request_->upload_data_stream is also not needed | 127 // at this point since request_->upload_data_stream is also not needed |
| 127 // anymore. | 128 // anymore. |
| 128 request_info_ = nullptr; | 129 request_info_ = nullptr; |
| 129 | 130 |
| 130 if (stream_.get()) | 131 if (stream_) |
| 131 CHECK(!stream_->IsIdle()); | 132 CHECK(!stream_->IsIdle()); |
| 132 | 133 |
| 133 CHECK(buf); | 134 CHECK(buf); |
| 134 CHECK(buf_len); | 135 CHECK(buf_len); |
| 135 CHECK(!callback.is_null()); | 136 CHECK(!callback.is_null()); |
| 136 | 137 |
| 137 // If we have data buffered, complete the IO immediately. | 138 // If we have data buffered, complete the IO immediately. |
| 138 if (!response_body_queue_.IsEmpty()) { | 139 if (!response_body_queue_.IsEmpty()) { |
| 139 return response_body_queue_.Dequeue(buf->data(), buf_len); | 140 return response_body_queue_.Dequeue(buf->data(), buf_len); |
| 140 } else if (stream_closed_) { | 141 } else if (stream_closed_) { |
| 141 return closed_stream_status_; | 142 return closed_stream_status_; |
| 142 } | 143 } |
| 143 | 144 |
| 144 CHECK(response_callback_.is_null()); | 145 CHECK(response_callback_.is_null()); |
| 145 CHECK(!user_buffer_.get()); | 146 CHECK(!user_buffer_.get()); |
| 146 CHECK_EQ(0, user_buffer_len_); | 147 CHECK_EQ(0, user_buffer_len_); |
| 147 | 148 |
| 148 response_callback_ = callback; | 149 response_callback_ = callback; |
| 149 user_buffer_ = buf; | 150 user_buffer_ = buf; |
| 150 user_buffer_len_ = buf_len; | 151 user_buffer_len_ = buf_len; |
| 151 return ERR_IO_PENDING; | 152 return ERR_IO_PENDING; |
| 152 } | 153 } |
| 153 | 154 |
| 154 void SpdyHttpStream::Close(bool not_reusable) { | 155 void SpdyHttpStream::Close(bool not_reusable) { |
| 155 // Note: the not_reusable flag has no meaning for SPDY streams. | 156 // Note: the not_reusable flag has no meaning for SPDY streams. |
| 156 | 157 |
| 157 Cancel(); | 158 Cancel(); |
| 158 DCHECK(!stream_.get()); | 159 DCHECK(!stream_); |
| 159 } | 160 } |
| 160 | 161 |
| 161 bool SpdyHttpStream::IsResponseBodyComplete() const { | 162 bool SpdyHttpStream::IsResponseBodyComplete() const { |
| 162 return stream_closed_; | 163 return stream_closed_; |
| 163 } | 164 } |
| 164 | 165 |
| 165 bool SpdyHttpStream::IsConnectionReused() const { | 166 bool SpdyHttpStream::IsConnectionReused() const { |
| 166 return is_reused_; | 167 return is_reused_; |
| 167 } | 168 } |
| 168 | 169 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 206 } |
| 206 | 207 |
| 207 int SpdyHttpStream::SendRequest(const HttpRequestHeaders& request_headers, | 208 int SpdyHttpStream::SendRequest(const HttpRequestHeaders& request_headers, |
| 208 HttpResponseInfo* response, | 209 HttpResponseInfo* response, |
| 209 const CompletionCallback& callback) { | 210 const CompletionCallback& callback) { |
| 210 if (stream_closed_) { | 211 if (stream_closed_) { |
| 211 return closed_stream_status_; | 212 return closed_stream_status_; |
| 212 } | 213 } |
| 213 | 214 |
| 214 base::Time request_time = base::Time::Now(); | 215 base::Time request_time = base::Time::Now(); |
| 215 CHECK(stream_.get()); | 216 CHECK(stream_); |
| 216 | 217 |
| 217 stream_->SetRequestTime(request_time); | 218 stream_->SetRequestTime(request_time); |
| 218 // This should only get called in the case of a request occurring | 219 // This should only get called in the case of a request occurring |
| 219 // during server push that has already begun but hasn't finished, | 220 // during server push that has already begun but hasn't finished, |
| 220 // so we set the response's request time to be the actual one | 221 // so we set the response's request time to be the actual one |
| 221 if (response_info_) | 222 if (response_info_) |
| 222 response_info_->request_time = request_time; | 223 response_info_->request_time = request_time; |
| 223 | 224 |
| 224 CHECK(!request_body_buf_.get()); | 225 CHECK(!request_body_buf_.get()); |
| 225 if (HasUploadData()) { | 226 if (HasUploadData()) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 if (result == ERR_IO_PENDING) { | 277 if (result == ERR_IO_PENDING) { |
| 277 CHECK(request_callback_.is_null()); | 278 CHECK(request_callback_.is_null()); |
| 278 request_callback_ = callback; | 279 request_callback_ = callback; |
| 279 } | 280 } |
| 280 return result; | 281 return result; |
| 281 } | 282 } |
| 282 | 283 |
| 283 void SpdyHttpStream::Cancel() { | 284 void SpdyHttpStream::Cancel() { |
| 284 request_callback_.Reset(); | 285 request_callback_.Reset(); |
| 285 response_callback_.Reset(); | 286 response_callback_.Reset(); |
| 286 if (stream_.get()) { | 287 if (stream_) { |
| 287 stream_->Cancel(); | 288 stream_->Cancel(); |
| 288 DCHECK(!stream_.get()); | 289 DCHECK(!stream_); |
| 289 } | 290 } |
| 290 } | 291 } |
| 291 | 292 |
| 292 void SpdyHttpStream::OnHeadersSent() { | 293 void SpdyHttpStream::OnHeadersSent() { |
| 293 if (HasUploadData()) { | 294 if (HasUploadData()) { |
| 294 ReadAndSendRequestBodyData(); | 295 ReadAndSendRequestBodyData(); |
| 295 } else { | 296 } else { |
| 296 MaybePostRequestCallback(OK); | 297 MaybePostRequestCallback(OK); |
| 297 } | 298 } |
| 298 } | 299 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 327 DoResponseCallback(OK); | 328 DoResponseCallback(OK); |
| 328 } | 329 } |
| 329 } | 330 } |
| 330 | 331 |
| 331 void SpdyHttpStream::OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) { | 332 void SpdyHttpStream::OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) { |
| 332 DCHECK(response_headers_complete_); | 333 DCHECK(response_headers_complete_); |
| 333 | 334 |
| 334 // Note that data may be received for a SpdyStream prior to the user calling | 335 // Note that data may be received for a SpdyStream prior to the user calling |
| 335 // ReadResponseBody(), therefore user_buffer_ may be NULL. This may often | 336 // ReadResponseBody(), therefore user_buffer_ may be NULL. This may often |
| 336 // happen for server initiated streams. | 337 // happen for server initiated streams. |
| 337 DCHECK(stream_.get()); | 338 DCHECK(stream_); |
| 338 DCHECK(!stream_->IsClosed() || stream_->type() == SPDY_PUSH_STREAM); | 339 DCHECK(!stream_->IsClosed() || stream_->type() == SPDY_PUSH_STREAM); |
| 339 if (buffer) { | 340 if (buffer) { |
| 340 response_body_queue_.Enqueue(std::move(buffer)); | 341 response_body_queue_.Enqueue(std::move(buffer)); |
| 341 | 342 |
| 342 if (user_buffer_.get()) { | 343 if (user_buffer_.get()) { |
| 343 // Handing small chunks of data to the caller creates measurable overhead. | 344 // Handing small chunks of data to the caller creates measurable overhead. |
| 344 // We buffer data in short time-spans and send a single read notification. | 345 // We buffer data in short time-spans and send a single read notification. |
| 345 ScheduleBufferedReadCallback(); | 346 ScheduleBufferedReadCallback(); |
| 346 } | 347 } |
| 347 } | 348 } |
| 348 } | 349 } |
| 349 | 350 |
| 350 void SpdyHttpStream::OnDataSent() { | 351 void SpdyHttpStream::OnDataSent() { |
| 351 request_body_buf_size_ = 0; | 352 request_body_buf_size_ = 0; |
| 352 ReadAndSendRequestBodyData(); | 353 ReadAndSendRequestBodyData(); |
| 353 } | 354 } |
| 354 | 355 |
| 355 // TODO(xunjieli): Maybe do something with the trailers. crbug.com/422958. | 356 // TODO(xunjieli): Maybe do something with the trailers. crbug.com/422958. |
| 356 void SpdyHttpStream::OnTrailers(const SpdyHeaderBlock& trailers) {} | 357 void SpdyHttpStream::OnTrailers(const SpdyHeaderBlock& trailers) {} |
| 357 | 358 |
| 358 void SpdyHttpStream::OnClose(int status) { | 359 void SpdyHttpStream::OnClose(int status) { |
| 359 // Cancel any pending reads from the upload data stream. | 360 // Cancel any pending reads from the upload data stream. |
| 360 if (request_info_ && request_info_->upload_data_stream) | 361 if (request_info_ && request_info_->upload_data_stream) |
| 361 request_info_->upload_data_stream->Reset(); | 362 request_info_->upload_data_stream->Reset(); |
| 362 | 363 |
| 363 if (stream_.get()) { | 364 if (stream_) { |
| 364 stream_closed_ = true; | 365 stream_closed_ = true; |
| 365 closed_stream_status_ = status; | 366 closed_stream_status_ = status; |
| 366 closed_stream_id_ = stream_->stream_id(); | 367 closed_stream_id_ = stream_->stream_id(); |
| 367 closed_stream_has_load_timing_info_ = | 368 closed_stream_has_load_timing_info_ = |
| 368 stream_->GetLoadTimingInfo(&closed_stream_load_timing_info_); | 369 stream_->GetLoadTimingInfo(&closed_stream_load_timing_info_); |
| 369 closed_stream_received_bytes_ = stream_->raw_received_bytes(); | 370 closed_stream_received_bytes_ = stream_->raw_received_bytes(); |
| 370 closed_stream_sent_bytes_ = stream_->raw_sent_bytes(); | 371 closed_stream_sent_bytes_ = stream_->raw_sent_bytes(); |
| 371 } | 372 } |
| 372 stream_.reset(); | 373 stream_ = nullptr; |
| 373 | 374 |
| 374 // Callbacks might destroy |this|. | 375 // Callbacks might destroy |this|. |
| 375 base::WeakPtr<SpdyHttpStream> self = weak_factory_.GetWeakPtr(); | 376 base::WeakPtr<SpdyHttpStream> self = weak_factory_.GetWeakPtr(); |
| 376 | 377 |
| 377 if (!request_callback_.is_null()) { | 378 if (!request_callback_.is_null()) { |
| 378 DoRequestCallback(status); | 379 DoRequestCallback(status); |
| 379 if (!self) | 380 if (!self) |
| 380 return; | 381 return; |
| 381 } | 382 } |
| 382 | 383 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 397 return | 398 return |
| 398 request_info_->upload_data_stream && | 399 request_info_->upload_data_stream && |
| 399 ((request_info_->upload_data_stream->size() > 0) || | 400 ((request_info_->upload_data_stream->size() > 0) || |
| 400 request_info_->upload_data_stream->is_chunked()); | 401 request_info_->upload_data_stream->is_chunked()); |
| 401 } | 402 } |
| 402 | 403 |
| 403 void SpdyHttpStream::OnStreamCreated( | 404 void SpdyHttpStream::OnStreamCreated( |
| 404 const CompletionCallback& callback, | 405 const CompletionCallback& callback, |
| 405 int rv) { | 406 int rv) { |
| 406 if (rv == OK) { | 407 if (rv == OK) { |
| 407 stream_ = stream_request_.ReleaseStream(); | 408 stream_ = stream_request_.ReleaseStream().get(); |
| 408 InitializeStreamHelper(); | 409 InitializeStreamHelper(); |
| 409 } | 410 } |
| 410 callback.Run(rv); | 411 callback.Run(rv); |
| 411 } | 412 } |
| 412 | 413 |
| 413 void SpdyHttpStream::ReadAndSendRequestBodyData() { | 414 void SpdyHttpStream::ReadAndSendRequestBodyData() { |
| 414 CHECK(HasUploadData()); | 415 CHECK(HasUploadData()); |
| 415 CHECK_EQ(request_body_buf_size_, 0); | 416 CHECK_EQ(request_body_buf_size_, 0); |
| 416 if (request_info_->upload_data_stream->IsEOF()) { | 417 if (request_info_->upload_data_stream->IsEOF()) { |
| 417 MaybePostRequestCallback(OK); | 418 MaybePostRequestCallback(OK); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 DCHECK_GT(user_buffer_len_, 0); | 497 DCHECK_GT(user_buffer_len_, 0); |
| 497 return response_body_queue_.GetTotalSize() < | 498 return response_body_queue_.GetTotalSize() < |
| 498 static_cast<size_t>(user_buffer_len_); | 499 static_cast<size_t>(user_buffer_len_); |
| 499 } | 500 } |
| 500 | 501 |
| 501 void SpdyHttpStream::DoBufferedReadCallback() { | 502 void SpdyHttpStream::DoBufferedReadCallback() { |
| 502 buffered_read_callback_pending_ = false; | 503 buffered_read_callback_pending_ = false; |
| 503 | 504 |
| 504 // If the transaction is cancelled or errored out, we don't need to complete | 505 // If the transaction is cancelled or errored out, we don't need to complete |
| 505 // the read. | 506 // the read. |
| 506 if (!stream_.get() && !stream_closed_) | 507 if (!stream_ && !stream_closed_) |
| 507 return; | 508 return; |
| 508 | 509 |
| 509 int stream_status = | 510 int stream_status = |
| 510 stream_closed_ ? closed_stream_status_ : stream_->response_status(); | 511 stream_closed_ ? closed_stream_status_ : stream_->response_status(); |
| 511 if (stream_status != OK) | 512 if (stream_status != OK) |
| 512 return; | 513 return; |
| 513 | 514 |
| 514 // When more_read_data_pending_ is true, it means that more data has | 515 // When more_read_data_pending_ is true, it means that more data has |
| 515 // arrived since we started waiting. Wait a little longer and continue | 516 // arrived since we started waiting. Wait a little longer and continue |
| 516 // to buffer. | 517 // to buffer. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 details->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP2; | 573 details->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP2; |
| 573 return; | 574 return; |
| 574 } | 575 } |
| 575 | 576 |
| 576 void SpdyHttpStream::SetPriority(RequestPriority priority) { | 577 void SpdyHttpStream::SetPriority(RequestPriority priority) { |
| 577 // TODO(akalin): Plumb this through to |stream_request_| and | 578 // TODO(akalin): Plumb this through to |stream_request_| and |
| 578 // |stream_|. | 579 // |stream_|. |
| 579 } | 580 } |
| 580 | 581 |
| 581 } // namespace net | 582 } // namespace net |
| OLD | NEW |