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_chromium_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(
28 const base::WeakPtr
<QuicChromiumClientSession
>& session
)
29 : next_state_(STATE_NONE
),
32 was_handshake_confirmed_(session
->IsCryptoHandshakeConfirmed()),
34 request_info_(nullptr),
35 request_body_stream_(nullptr),
36 priority_(MINIMUM_PRIORITY
),
37 response_info_(nullptr),
39 response_headers_received_(false),
40 closed_stream_received_bytes_(0),
41 closed_stream_sent_bytes_(0),
45 session_
->AddObserver(this);
48 QuicHttpStream::~QuicHttpStream() {
51 session_
->RemoveObserver(this);
54 int QuicHttpStream::InitializeStream(const HttpRequestInfo
* request_info
,
55 RequestPriority priority
,
56 const BoundNetLog
& stream_net_log
,
57 const CompletionCallback
& callback
) {
60 return was_handshake_confirmed_
? ERR_CONNECTION_CLOSED
:
61 ERR_QUIC_HANDSHAKE_FAILED
;
63 stream_net_log
.AddEvent(
64 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION
,
65 session_
->net_log().source().ToEventParametersCallback());
67 if (request_info
->url
.SchemeIsCryptographic()) {
70 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
71 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
74 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
;
77 stream_net_log_
= stream_net_log
;
78 request_info_
= request_info
;
79 request_time_
= base::Time::Now();
82 int rv
= stream_request_
.StartRequest(
83 session_
, &stream_
, base::Bind(&QuicHttpStream::OnStreamReady
,
84 weak_factory_
.GetWeakPtr()));
85 if (rv
== ERR_IO_PENDING
) {
87 } else if (rv
== OK
) {
88 stream_
->SetDelegate(this);
89 } else if (!was_handshake_confirmed_
) {
90 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
96 void QuicHttpStream::OnStreamReady(int rv
) {
97 DCHECK(rv
== OK
|| !stream_
);
99 stream_
->SetDelegate(this);
100 } else if (!was_handshake_confirmed_
) {
101 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
104 ResetAndReturn(&callback_
).Run(rv
);
107 int QuicHttpStream::SendRequest(const HttpRequestHeaders
& request_headers
,
108 HttpResponseInfo
* response
,
109 const CompletionCallback
& callback
) {
110 CHECK(!request_body_stream_
);
111 CHECK(!response_info_
);
112 CHECK(!callback
.is_null());
115 // TODO(rch): remove this once we figure out why channel ID is not being
116 // sent when it should be.
117 HostPortPair origin
= HostPortPair::FromURL(request_info_
->url
);
118 if (origin
.Equals(HostPortPair("accounts.google.com", 443)) &&
119 request_headers
.HasHeader(HttpRequestHeaders::kCookie
)) {
121 bool secure_session
=
122 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
123 DCHECK(secure_session
);
124 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
125 ssl_info
.channel_id_sent
);
128 return ERR_CONNECTION_CLOSED
;
131 QuicPriority priority
= ConvertRequestPriorityToQuicPriority(priority_
);
132 stream_
->set_priority(priority
);
133 // Store the serialized request headers.
134 CreateSpdyHeadersFromHttpRequest(*request_info_
, request_headers
,
136 /*direct=*/true, &request_headers_
);
138 // Store the request body.
139 request_body_stream_
= request_info_
->upload_data_stream
;
140 if (request_body_stream_
) {
141 // TODO(rch): Can we be more precise about when to allocate
142 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
143 // was being called even if we didn't yet allocate raw_request_body_buf_.
144 // && (request_body_stream_->size() ||
145 // request_body_stream_->is_chunked()))
146 // Use 10 packets as the body buffer size to give enough space to
147 // help ensure we don't often send out partial packets.
148 raw_request_body_buf_
=
149 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize
));
150 // The request body buffer is empty at first.
151 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), 0);
154 // Store the response info.
155 response_info_
= response
;
157 next_state_
= STATE_SEND_HEADERS
;
159 if (rv
== ERR_IO_PENDING
)
160 callback_
= callback
;
162 return rv
> 0 ? OK
: rv
;
165 UploadProgress
QuicHttpStream::GetUploadProgress() const {
166 if (!request_body_stream_
)
167 return UploadProgress();
169 return UploadProgress(request_body_stream_
->position(),
170 request_body_stream_
->size());
173 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
174 CHECK(!callback
.is_null());
176 if (stream_
== nullptr)
177 return response_status_
;
179 // Check if we already have the response headers. If so, return synchronously.
180 if (response_headers_received_
)
183 // Still waiting for the response, return IO_PENDING.
184 CHECK(callback_
.is_null());
185 callback_
= callback
;
186 return ERR_IO_PENDING
;
189 int QuicHttpStream::ReadResponseBody(
190 IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
) {
192 // If the stream is already closed, there is no body to read.
193 return response_status_
;
196 int rv
= ReadAvailableData(buf
, buf_len
);
197 if (rv
!= ERR_IO_PENDING
)
200 CHECK(callback_
.is_null());
201 CHECK(!user_buffer_
.get());
202 CHECK_EQ(0, user_buffer_len_
);
204 callback_
= callback
;
206 user_buffer_len_
= buf_len
;
207 return ERR_IO_PENDING
;
210 void QuicHttpStream::Close(bool not_reusable
) {
211 // Note: the not_reusable flag has no meaning for SPDY streams.
213 stream_
->SetDelegate(nullptr);
214 stream_
->Reset(QUIC_STREAM_CANCELLED
);
216 response_status_
= was_handshake_confirmed_
?
217 ERR_CONNECTION_CLOSED
: ERR_QUIC_HANDSHAKE_FAILED
;
221 HttpStream
* QuicHttpStream::RenewStreamForAuth() {
225 bool QuicHttpStream::IsResponseBodyComplete() const {
226 return next_state_
== STATE_OPEN
&& !stream_
;
229 bool QuicHttpStream::IsConnectionReused() const {
230 // TODO(rch): do something smarter here.
231 return stream_
&& stream_
->id() > 1;
234 void QuicHttpStream::SetConnectionReused() {
235 // QUIC doesn't need an indicator here.
238 bool QuicHttpStream::CanReuseConnection() const {
239 // QUIC streams aren't considered reusable.
243 int64
QuicHttpStream::GetTotalReceivedBytes() const {
244 // TODO(sclittle): Currently, this only includes response body bytes. Change
245 // this to include headers and QUIC overhead as well.
247 return stream_
->stream_bytes_read();
250 return closed_stream_received_bytes_
;
253 int64_t QuicHttpStream::GetTotalSentBytes() const {
254 // TODO(sclittle): Currently, this only includes request body bytes. Change
255 // this to include headers and QUIC overhead as well.
257 return stream_
->stream_bytes_written();
260 return closed_stream_sent_bytes_
;
263 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
264 // TODO(mmenke): Figure out what to do here.
268 void QuicHttpStream::GetSSLInfo(SSLInfo
* ssl_info
) {
270 session_
->GetSSLInfo(ssl_info
);
273 void QuicHttpStream::GetSSLCertRequestInfo(
274 SSLCertRequestInfo
* cert_request_info
) {
279 void QuicHttpStream::Drain(HttpNetworkSession
* session
) {
285 void QuicHttpStream::SetPriority(RequestPriority priority
) {
286 priority_
= priority
;
289 void QuicHttpStream::OnHeadersAvailable(const SpdyHeaderBlock
& headers
) {
290 int rv
= ProcessResponseHeaders(headers
);
291 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
296 void QuicHttpStream::OnDataAvailable() {
297 if (callback_
.is_null()) {
298 // Data is available, but can't be delivered
302 CHECK(user_buffer_
.get());
303 CHECK_NE(0, user_buffer_len_
);
304 int rv
= ReadAvailableData(user_buffer_
.get(), user_buffer_len_
);
305 if (rv
== ERR_IO_PENDING
) {
306 // This was a spurrious notification. Wait for the next one.
310 CHECK(!callback_
.is_null());
311 user_buffer_
= nullptr;
312 user_buffer_len_
= 0;
316 void QuicHttpStream::OnClose(QuicErrorCode error
) {
317 if (error
!= QUIC_NO_ERROR
) {
318 response_status_
= was_handshake_confirmed_
?
319 ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
320 } else if (!response_headers_received_
) {
321 response_status_
= ERR_ABORTED
;
325 if (!callback_
.is_null())
326 DoCallback(response_status_
);
329 void QuicHttpStream::OnError(int error
) {
331 response_status_
= was_handshake_confirmed_
?
332 error
: ERR_QUIC_HANDSHAKE_FAILED
;
333 if (!callback_
.is_null())
334 DoCallback(response_status_
);
337 bool QuicHttpStream::HasSendHeadersComplete() {
338 return next_state_
> STATE_SEND_HEADERS_COMPLETE
;
341 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
342 was_handshake_confirmed_
= true;
345 void QuicHttpStream::OnSessionClosed(int error
) {
347 session_error_
= error
;
351 void QuicHttpStream::OnIOComplete(int rv
) {
354 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
359 void QuicHttpStream::DoCallback(int rv
) {
360 CHECK_NE(rv
, ERR_IO_PENDING
);
361 CHECK(!callback_
.is_null());
363 // The client callback can do anything, including destroying this class,
364 // so any pending callback must be issued after everything else is done.
365 base::ResetAndReturn(&callback_
).Run(rv
);
368 int QuicHttpStream::DoLoop(int rv
) {
370 State state
= next_state_
;
371 next_state_
= STATE_NONE
;
373 case STATE_SEND_HEADERS
:
375 rv
= DoSendHeaders();
377 case STATE_SEND_HEADERS_COMPLETE
:
378 rv
= DoSendHeadersComplete(rv
);
380 case STATE_READ_REQUEST_BODY
:
382 rv
= DoReadRequestBody();
384 case STATE_READ_REQUEST_BODY_COMPLETE
:
385 rv
= DoReadRequestBodyComplete(rv
);
387 case STATE_SEND_BODY
:
391 case STATE_SEND_BODY_COMPLETE
:
392 rv
= DoSendBodyComplete(rv
);
398 NOTREACHED() << "next_state_: " << next_state_
;
401 } while (next_state_
!= STATE_NONE
&& next_state_
!= STATE_OPEN
&&
402 rv
!= ERR_IO_PENDING
);
407 int QuicHttpStream::DoSendHeaders() {
409 return ERR_UNEXPECTED
;
411 // Log the actual request with the URL Request's net log.
412 stream_net_log_
.AddEvent(
413 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS
,
414 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
416 // Also log to the QuicSession's net log.
417 stream_
->net_log().AddEvent(
418 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS
,
419 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
422 bool has_upload_data
= request_body_stream_
!= nullptr;
424 next_state_
= STATE_SEND_HEADERS_COMPLETE
;
425 int rv
= stream_
->WriteHeaders(request_headers_
, !has_upload_data
, nullptr);
426 request_headers_
.clear();
430 int QuicHttpStream::DoSendHeadersComplete(int rv
) {
434 next_state_
= request_body_stream_
?
435 STATE_READ_REQUEST_BODY
: STATE_OPEN
;
440 int QuicHttpStream::DoReadRequestBody() {
441 next_state_
= STATE_READ_REQUEST_BODY_COMPLETE
;
442 return request_body_stream_
->Read(
443 raw_request_body_buf_
.get(),
444 raw_request_body_buf_
->size(),
445 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
448 int QuicHttpStream::DoReadRequestBodyComplete(int rv
) {
449 // |rv| is the result of read from the request body from the last call to
454 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), rv
);
455 if (rv
== 0) { // Reached the end.
456 DCHECK(request_body_stream_
->IsEOF());
459 next_state_
= STATE_SEND_BODY
;
463 int QuicHttpStream::DoSendBody() {
465 return ERR_UNEXPECTED
;
467 CHECK(request_body_stream_
);
468 CHECK(request_body_buf_
.get());
469 const bool eof
= request_body_stream_
->IsEOF();
470 int len
= request_body_buf_
->BytesRemaining();
471 if (len
> 0 || eof
) {
472 next_state_
= STATE_SEND_BODY_COMPLETE
;
473 base::StringPiece
data(request_body_buf_
->data(), len
);
474 return stream_
->WriteStreamData(
476 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
479 next_state_
= STATE_OPEN
;
483 int QuicHttpStream::DoSendBodyComplete(int rv
) {
487 request_body_buf_
->DidConsume(request_body_buf_
->BytesRemaining());
489 if (!request_body_stream_
->IsEOF()) {
490 next_state_
= STATE_READ_REQUEST_BODY
;
494 next_state_
= STATE_OPEN
;
498 int QuicHttpStream::ProcessResponseHeaders(const SpdyHeaderBlock
& headers
) {
499 // The URLRequest logs these headers, so only log to the QuicSession's
501 stream_
->net_log().AddEvent(
502 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS
,
503 base::Bind(&SpdyHeaderBlockNetLogCallback
, &headers
));
505 if (!SpdyHeadersToHttpResponse(headers
, GetSpdyVersion(), response_info_
)) {
506 DLOG(WARNING
) << "Invalid headers";
507 return ERR_QUIC_PROTOCOL_ERROR
;
509 // Put the peer's IP address and port into the response.
510 IPEndPoint address
= session_
->peer_address();
511 response_info_
->socket_address
= HostPortPair::FromIPEndPoint(address
);
512 response_info_
->connection_info
=
513 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
;
514 response_info_
->vary_data
515 .Init(*request_info_
, *response_info_
->headers
.get());
516 response_info_
->was_npn_negotiated
= true;
517 response_info_
->npn_negotiated_protocol
= "quic/1+spdy/3";
518 response_info_
->response_time
= base::Time::Now();
519 response_info_
->request_time
= request_time_
;
520 response_headers_received_
= true;
525 int QuicHttpStream::ReadAvailableData(IOBuffer
* buf
, int buf_len
) {
526 int rv
= stream_
->Read(buf
, buf_len
);
527 if (stream_
->IsDoneReading()) {
528 stream_
->SetDelegate(nullptr);
529 stream_
->OnFinRead();
535 SpdyMajorVersion
QuicHttpStream::GetSpdyVersion() {
536 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_
->version());
539 void QuicHttpStream::ResetStream() {
542 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
543 closed_stream_sent_bytes_
= stream_
->stream_bytes_written();
546 // If |request_body_stream_| is non-NULL, Reset it, to abort any in progress
548 if (request_body_stream_
)
549 request_body_stream_
->Reset();