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
3 // found in the LICENSE file.
5 #include "net/quic/quic_http_stream.h"
7 #include "base/callback_helpers.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_util.h"
14 #include "net/quic/quic_client_session.h"
15 #include "net/quic/quic_http_utils.h"
16 #include "net/quic/quic_reliable_client_stream.h"
17 #include "net/quic/quic_utils.h"
18 #include "net/quic/spdy_utils.h"
19 #include "net/socket/next_proto.h"
20 #include "net/spdy/spdy_frame_builder.h"
21 #include "net/spdy/spdy_framer.h"
22 #include "net/spdy/spdy_http_utils.h"
23 #include "net/ssl/ssl_info.h"
27 QuicHttpStream::QuicHttpStream(const base::WeakPtr
<QuicClientSession
>& session
)
28 : next_state_(STATE_NONE
),
31 was_handshake_confirmed_(session
->IsCryptoHandshakeConfirmed()),
33 request_info_(nullptr),
34 request_body_stream_(nullptr),
35 priority_(MINIMUM_PRIORITY
),
36 response_info_(nullptr),
38 response_headers_received_(false),
39 closed_stream_received_bytes_(0),
43 session_
->AddObserver(this);
46 QuicHttpStream::~QuicHttpStream() {
49 session_
->RemoveObserver(this);
52 int QuicHttpStream::InitializeStream(const HttpRequestInfo
* request_info
,
53 RequestPriority priority
,
54 const BoundNetLog
& stream_net_log
,
55 const CompletionCallback
& callback
) {
58 return was_handshake_confirmed_
? ERR_CONNECTION_CLOSED
:
59 ERR_QUIC_HANDSHAKE_FAILED
;
61 stream_net_log
.AddEvent(
62 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION
,
63 session_
->net_log().source().ToEventParametersCallback());
65 if (request_info
->url
.SchemeIsCryptographic()) {
68 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
69 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
72 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
;
75 stream_net_log_
= stream_net_log
;
76 request_info_
= request_info
;
77 request_time_
= base::Time::Now();
80 int rv
= stream_request_
.StartRequest(
81 session_
, &stream_
, base::Bind(&QuicHttpStream::OnStreamReady
,
82 weak_factory_
.GetWeakPtr()));
83 if (rv
== ERR_IO_PENDING
) {
85 } else if (rv
== OK
) {
86 stream_
->SetDelegate(this);
87 } else if (!was_handshake_confirmed_
) {
88 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
94 void QuicHttpStream::OnStreamReady(int rv
) {
95 DCHECK(rv
== OK
|| !stream_
);
97 stream_
->SetDelegate(this);
98 } else if (!was_handshake_confirmed_
) {
99 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
102 ResetAndReturn(&callback_
).Run(rv
);
105 int QuicHttpStream::SendRequest(const HttpRequestHeaders
& request_headers
,
106 HttpResponseInfo
* response
,
107 const CompletionCallback
& callback
) {
108 CHECK(!request_body_stream_
);
109 CHECK(!response_info_
);
110 CHECK(!callback
.is_null());
113 // TODO(rch): remove this once we figure out why channel ID is not being
114 // sent when it should be.
115 HostPortPair origin
= HostPortPair::FromURL(request_info_
->url
);
116 if (origin
.Equals(HostPortPair("accounts.google.com", 443)) &&
117 request_headers
.HasHeader(HttpRequestHeaders::kCookie
)) {
119 bool secure_session
=
120 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
121 DCHECK(secure_session
);
122 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
123 ssl_info
.channel_id_sent
);
126 return ERR_CONNECTION_CLOSED
;
129 QuicPriority priority
= ConvertRequestPriorityToQuicPriority(priority_
);
130 stream_
->set_priority(priority
);
131 // Store the serialized request headers.
132 CreateSpdyHeadersFromHttpRequest(*request_info_
, request_headers
,
134 /*direct=*/true, &request_headers_
);
136 // Store the request body.
137 request_body_stream_
= request_info_
->upload_data_stream
;
138 if (request_body_stream_
) {
139 // TODO(rch): Can we be more precise about when to allocate
140 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
141 // was being called even if we didn't yet allocate raw_request_body_buf_.
142 // && (request_body_stream_->size() ||
143 // request_body_stream_->is_chunked()))
144 // Use 10 packets as the body buffer size to give enough space to
145 // help ensure we don't often send out partial packets.
146 raw_request_body_buf_
=
147 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize
));
148 // The request body buffer is empty at first.
149 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), 0);
152 // Store the response info.
153 response_info_
= response
;
155 next_state_
= STATE_SEND_HEADERS
;
157 if (rv
== ERR_IO_PENDING
)
158 callback_
= callback
;
160 return rv
> 0 ? OK
: rv
;
163 UploadProgress
QuicHttpStream::GetUploadProgress() const {
164 if (!request_body_stream_
)
165 return UploadProgress();
167 return UploadProgress(request_body_stream_
->position(),
168 request_body_stream_
->size());
171 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
172 CHECK(!callback
.is_null());
174 if (stream_
== nullptr)
175 return response_status_
;
177 // Check if we already have the response headers. If so, return synchronously.
178 if (response_headers_received_
)
181 // Still waiting for the response, return IO_PENDING.
182 CHECK(callback_
.is_null());
183 callback_
= callback
;
184 return ERR_IO_PENDING
;
187 int QuicHttpStream::ReadResponseBody(
188 IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
) {
191 CHECK(!callback
.is_null());
193 // If we have data buffered, complete the IO immediately.
194 if (!response_body_
.empty()) {
196 while (!response_body_
.empty() && buf_len
> 0) {
197 scoped_refptr
<IOBufferWithSize
> data
= response_body_
.front();
198 const int bytes_to_copy
= std::min(buf_len
, data
->size());
199 memcpy(&(buf
->data()[bytes_read
]), data
->data(), bytes_to_copy
);
200 buf_len
-= bytes_to_copy
;
201 if (bytes_to_copy
== data
->size()) {
202 response_body_
.pop_front();
204 const int bytes_remaining
= data
->size() - bytes_to_copy
;
205 IOBufferWithSize
* new_buffer
= new IOBufferWithSize(bytes_remaining
);
206 memcpy(new_buffer
->data(), &(data
->data()[bytes_to_copy
]),
208 response_body_
.pop_front();
209 response_body_
.push_front(make_scoped_refptr(new_buffer
));
211 bytes_read
+= bytes_to_copy
;
217 // If the stream is already closed, there is no body to read.
218 return response_status_
;
221 CHECK(callback_
.is_null());
222 CHECK(!user_buffer_
.get());
223 CHECK_EQ(0, user_buffer_len_
);
225 callback_
= callback
;
227 user_buffer_len_
= buf_len
;
228 return ERR_IO_PENDING
;
231 void QuicHttpStream::Close(bool not_reusable
) {
232 // Note: the not_reusable flag has no meaning for SPDY streams.
234 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
235 stream_
->SetDelegate(nullptr);
236 stream_
->Reset(QUIC_STREAM_CANCELLED
);
238 response_status_
= was_handshake_confirmed_
?
239 ERR_CONNECTION_CLOSED
: ERR_QUIC_HANDSHAKE_FAILED
;
243 HttpStream
* QuicHttpStream::RenewStreamForAuth() {
247 bool QuicHttpStream::IsResponseBodyComplete() const {
248 return next_state_
== STATE_OPEN
&& !stream_
;
251 bool QuicHttpStream::CanFindEndOfResponse() const {
255 bool QuicHttpStream::IsConnectionReused() const {
256 // TODO(rch): do something smarter here.
257 return stream_
&& stream_
->id() > 1;
260 void QuicHttpStream::SetConnectionReused() {
261 // QUIC doesn't need an indicator here.
264 bool QuicHttpStream::IsConnectionReusable() const {
265 // QUIC streams aren't considered reusable.
269 int64
QuicHttpStream::GetTotalReceivedBytes() const {
271 return stream_
->stream_bytes_read();
274 return closed_stream_received_bytes_
;
277 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
278 // TODO(mmenke): Figure out what to do here.
282 void QuicHttpStream::GetSSLInfo(SSLInfo
* ssl_info
) {
284 session_
->GetSSLInfo(ssl_info
);
287 void QuicHttpStream::GetSSLCertRequestInfo(
288 SSLCertRequestInfo
* cert_request_info
) {
293 bool QuicHttpStream::IsSpdyHttpStream() const {
297 void QuicHttpStream::Drain(HttpNetworkSession
* session
) {
302 void QuicHttpStream::SetPriority(RequestPriority priority
) {
303 priority_
= priority
;
306 void QuicHttpStream::OnHeadersAvailable(StringPiece headers
) {
307 int rv
= ParseResponseHeaders(headers
);
308 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
313 int QuicHttpStream::OnDataReceived(const char* data
, int length
) {
314 DCHECK_NE(0, length
);
316 if (callback_
.is_null()) {
317 BufferResponseBody(data
, length
);
321 if (length
<= user_buffer_len_
) {
322 memcpy(user_buffer_
->data(), data
, length
);
324 memcpy(user_buffer_
->data(), data
, user_buffer_len_
);
325 int delta
= length
- user_buffer_len_
;
326 BufferResponseBody(data
+ user_buffer_len_
, delta
);
327 length
= user_buffer_len_
;
330 user_buffer_
= nullptr;
331 user_buffer_len_
= 0;
336 void QuicHttpStream::OnClose(QuicErrorCode error
) {
337 if (error
!= QUIC_NO_ERROR
) {
338 response_status_
= was_handshake_confirmed_
?
339 ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
340 } else if (!response_headers_received_
) {
341 response_status_
= ERR_ABORTED
;
344 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
346 if (!callback_
.is_null())
347 DoCallback(response_status_
);
350 void QuicHttpStream::OnError(int error
) {
352 response_status_
= was_handshake_confirmed_
?
353 error
: ERR_QUIC_HANDSHAKE_FAILED
;
354 if (!callback_
.is_null())
355 DoCallback(response_status_
);
358 bool QuicHttpStream::HasSendHeadersComplete() {
359 return next_state_
> STATE_SEND_HEADERS_COMPLETE
;
362 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
363 was_handshake_confirmed_
= true;
366 void QuicHttpStream::OnSessionClosed(int error
) {
368 session_error_
= error
;
372 void QuicHttpStream::OnIOComplete(int rv
) {
375 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
380 void QuicHttpStream::DoCallback(int rv
) {
381 CHECK_NE(rv
, ERR_IO_PENDING
);
382 CHECK(!callback_
.is_null());
384 // The client callback can do anything, including destroying this class,
385 // so any pending callback must be issued after everything else is done.
386 base::ResetAndReturn(&callback_
).Run(rv
);
389 int QuicHttpStream::DoLoop(int rv
) {
391 State state
= next_state_
;
392 next_state_
= STATE_NONE
;
394 case STATE_SEND_HEADERS
:
396 rv
= DoSendHeaders();
398 case STATE_SEND_HEADERS_COMPLETE
:
399 rv
= DoSendHeadersComplete(rv
);
401 case STATE_READ_REQUEST_BODY
:
403 rv
= DoReadRequestBody();
405 case STATE_READ_REQUEST_BODY_COMPLETE
:
406 rv
= DoReadRequestBodyComplete(rv
);
408 case STATE_SEND_BODY
:
412 case STATE_SEND_BODY_COMPLETE
:
413 rv
= DoSendBodyComplete(rv
);
419 NOTREACHED() << "next_state_: " << next_state_
;
422 } while (next_state_
!= STATE_NONE
&& next_state_
!= STATE_OPEN
&&
423 rv
!= ERR_IO_PENDING
);
428 int QuicHttpStream::DoSendHeaders() {
430 return ERR_UNEXPECTED
;
432 // Log the actual request with the URL Request's net log.
433 stream_net_log_
.AddEvent(
434 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS
,
435 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
437 // Also log to the QuicSession's net log.
438 stream_
->net_log().AddEvent(
439 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS
,
440 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
443 bool has_upload_data
= request_body_stream_
!= nullptr;
445 next_state_
= STATE_SEND_HEADERS_COMPLETE
;
446 int rv
= stream_
->WriteHeaders(request_headers_
, !has_upload_data
, nullptr);
447 request_headers_
.clear();
451 int QuicHttpStream::DoSendHeadersComplete(int rv
) {
455 next_state_
= request_body_stream_
?
456 STATE_READ_REQUEST_BODY
: STATE_OPEN
;
461 int QuicHttpStream::DoReadRequestBody() {
462 next_state_
= STATE_READ_REQUEST_BODY_COMPLETE
;
463 return request_body_stream_
->Read(
464 raw_request_body_buf_
.get(),
465 raw_request_body_buf_
->size(),
466 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
469 int QuicHttpStream::DoReadRequestBodyComplete(int rv
) {
470 // |rv| is the result of read from the request body from the last call to
475 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), rv
);
476 if (rv
== 0) { // Reached the end.
477 DCHECK(request_body_stream_
->IsEOF());
480 next_state_
= STATE_SEND_BODY
;
484 int QuicHttpStream::DoSendBody() {
486 return ERR_UNEXPECTED
;
488 CHECK(request_body_stream_
);
489 CHECK(request_body_buf_
.get());
490 const bool eof
= request_body_stream_
->IsEOF();
491 int len
= request_body_buf_
->BytesRemaining();
492 if (len
> 0 || eof
) {
493 next_state_
= STATE_SEND_BODY_COMPLETE
;
494 base::StringPiece
data(request_body_buf_
->data(), len
);
495 return stream_
->WriteStreamData(
497 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
500 next_state_
= STATE_OPEN
;
504 int QuicHttpStream::DoSendBodyComplete(int rv
) {
508 request_body_buf_
->DidConsume(request_body_buf_
->BytesRemaining());
510 if (!request_body_stream_
->IsEOF()) {
511 next_state_
= STATE_READ_REQUEST_BODY
;
515 next_state_
= STATE_OPEN
;
519 int QuicHttpStream::ParseResponseHeaders(StringPiece headers_data
) {
520 SpdyFramer
framer(GetSpdyVersion());
521 SpdyHeaderBlock headers
;
522 size_t len
= framer
.ParseHeaderBlockInBuffer(headers_data
.data(),
523 headers_data
.length(), &headers
);
524 if (len
== 0 || len
!= headers_data
.length()) {
525 DLOG(WARNING
) << "Invalid headers";
526 return ERR_QUIC_PROTOCOL_ERROR
;
529 // The URLRequest logs these headers, so only log to the QuicSession's
531 stream_
->net_log().AddEvent(
532 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS
,
533 base::Bind(&SpdyHeaderBlockNetLogCallback
, &headers
));
535 if (!SpdyHeadersToHttpResponse(headers
, GetSpdyVersion(), response_info_
)) {
536 DLOG(WARNING
) << "Invalid headers";
537 return ERR_QUIC_PROTOCOL_ERROR
;
539 // Put the peer's IP address and port into the response.
540 IPEndPoint address
= session_
->peer_address();
541 response_info_
->socket_address
= HostPortPair::FromIPEndPoint(address
);
542 response_info_
->connection_info
=
543 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
;
544 response_info_
->vary_data
545 .Init(*request_info_
, *response_info_
->headers
.get());
546 response_info_
->was_npn_negotiated
= true;
547 response_info_
->npn_negotiated_protocol
= "quic/1+spdy/3";
548 response_info_
->response_time
= base::Time::Now();
549 response_info_
->request_time
= request_time_
;
550 response_headers_received_
= true;
555 void QuicHttpStream::BufferResponseBody(const char* data
, int length
) {
558 IOBufferWithSize
* io_buffer
= new IOBufferWithSize(length
);
559 memcpy(io_buffer
->data(), data
, length
);
560 response_body_
.push_back(make_scoped_refptr(io_buffer
));
563 SpdyMajorVersion
QuicHttpStream::GetSpdyVersion() {
564 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_
->version());