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 #ifdef TEMP_INSTRUMENTATION_468529
9 #include "base/debug/alias.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/http/http_util.h"
17 #include "net/quic/quic_chromium_client_session.h"
18 #include "net/quic/quic_http_utils.h"
19 #include "net/quic/quic_reliable_client_stream.h"
20 #include "net/quic/quic_utils.h"
21 #include "net/quic/spdy_utils.h"
22 #include "net/socket/next_proto.h"
23 #include "net/spdy/spdy_frame_builder.h"
24 #include "net/spdy/spdy_framer.h"
25 #include "net/spdy/spdy_http_utils.h"
26 #include "net/ssl/ssl_info.h"
30 QuicHttpStream::QuicHttpStream(
31 const base::WeakPtr
<QuicChromiumClientSession
>& session
)
32 : next_state_(STATE_NONE
),
35 was_handshake_confirmed_(session
->IsCryptoHandshakeConfirmed()),
37 request_info_(nullptr),
38 request_body_stream_(nullptr),
39 priority_(MINIMUM_PRIORITY
),
40 response_info_(nullptr),
42 response_headers_received_(false),
43 closed_stream_received_bytes_(0),
44 closed_stream_sent_bytes_(0),
48 session_
->AddObserver(this);
51 QuicHttpStream::~QuicHttpStream() {
52 #ifdef TEMP_INSTRUMENTATION_468529
54 stack_trace_
= base::debug::StackTrace();
56 // Probably not necessary, but just in case compiler tries to optimize out the
57 // writes to liveness_ and stack_trace_.
58 base::debug::Alias(&liveness_
);
59 base::debug::Alias(&stack_trace_
);
64 session_
->RemoveObserver(this);
67 int QuicHttpStream::InitializeStream(const HttpRequestInfo
* request_info
,
68 RequestPriority priority
,
69 const BoundNetLog
& stream_net_log
,
70 const CompletionCallback
& callback
) {
73 return was_handshake_confirmed_
? ERR_CONNECTION_CLOSED
:
74 ERR_QUIC_HANDSHAKE_FAILED
;
76 stream_net_log
.AddEvent(
77 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION
,
78 session_
->net_log().source().ToEventParametersCallback());
80 if (request_info
->url
.SchemeIsCryptographic()) {
83 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
84 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
87 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
;
90 stream_net_log_
= stream_net_log
;
91 request_info_
= request_info
;
92 request_time_
= base::Time::Now();
95 int rv
= stream_request_
.StartRequest(
96 session_
, &stream_
, base::Bind(&QuicHttpStream::OnStreamReady
,
97 weak_factory_
.GetWeakPtr()));
98 if (rv
== ERR_IO_PENDING
) {
100 } else if (rv
== OK
) {
101 stream_
->SetDelegate(this);
102 } else if (!was_handshake_confirmed_
) {
103 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
109 void QuicHttpStream::OnStreamReady(int rv
) {
110 DCHECK(rv
== OK
|| !stream_
);
112 stream_
->SetDelegate(this);
113 } else if (!was_handshake_confirmed_
) {
114 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
117 ResetAndReturn(&callback_
).Run(rv
);
120 int QuicHttpStream::SendRequest(const HttpRequestHeaders
& request_headers
,
121 HttpResponseInfo
* response
,
122 const CompletionCallback
& callback
) {
123 CHECK(!request_body_stream_
);
124 CHECK(!response_info_
);
125 CHECK(!callback
.is_null());
128 // TODO(rch): remove this once we figure out why channel ID is not being
129 // sent when it should be.
130 HostPortPair origin
= HostPortPair::FromURL(request_info_
->url
);
131 if (origin
.Equals(HostPortPair("accounts.google.com", 443)) &&
132 request_headers
.HasHeader(HttpRequestHeaders::kCookie
)) {
134 bool secure_session
=
135 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
136 DCHECK(secure_session
);
137 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
138 ssl_info
.channel_id_sent
);
141 return ERR_CONNECTION_CLOSED
;
144 QuicPriority priority
= ConvertRequestPriorityToQuicPriority(priority_
);
145 stream_
->set_priority(priority
);
146 // Store the serialized request headers.
147 CreateSpdyHeadersFromHttpRequest(*request_info_
, request_headers
,
149 /*direct=*/true, &request_headers_
);
151 // Store the request body.
152 request_body_stream_
= request_info_
->upload_data_stream
;
153 if (request_body_stream_
) {
154 // TODO(rch): Can we be more precise about when to allocate
155 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
156 // was being called even if we didn't yet allocate raw_request_body_buf_.
157 // && (request_body_stream_->size() ||
158 // request_body_stream_->is_chunked()))
159 // Use 10 packets as the body buffer size to give enough space to
160 // help ensure we don't often send out partial packets.
161 raw_request_body_buf_
=
162 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize
));
163 // The request body buffer is empty at first.
164 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), 0);
167 // Store the response info.
168 response_info_
= response
;
170 next_state_
= STATE_SEND_HEADERS
;
172 if (rv
== ERR_IO_PENDING
)
173 callback_
= callback
;
175 return rv
> 0 ? OK
: rv
;
178 UploadProgress
QuicHttpStream::GetUploadProgress() const {
179 if (!request_body_stream_
)
180 return UploadProgress();
182 return UploadProgress(request_body_stream_
->position(),
183 request_body_stream_
->size());
186 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
187 CHECK(!callback
.is_null());
189 if (stream_
== nullptr)
190 return response_status_
;
192 // Check if we already have the response headers. If so, return synchronously.
193 if (response_headers_received_
)
196 // Still waiting for the response, return IO_PENDING.
197 CHECK(callback_
.is_null());
198 callback_
= callback
;
199 return ERR_IO_PENDING
;
202 int QuicHttpStream::ReadResponseBody(
203 IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
) {
205 // If the stream is already closed, there is no body to read.
206 return response_status_
;
209 int rv
= ReadAvailableData(buf
, buf_len
);
210 if (rv
!= ERR_IO_PENDING
)
213 CHECK(callback_
.is_null());
214 CHECK(!user_buffer_
.get());
215 CHECK_EQ(0, user_buffer_len_
);
217 callback_
= callback
;
219 user_buffer_len_
= buf_len
;
220 return ERR_IO_PENDING
;
223 void QuicHttpStream::Close(bool not_reusable
) {
224 // Note: the not_reusable flag has no meaning for SPDY streams.
226 stream_
->SetDelegate(nullptr);
227 stream_
->Reset(QUIC_STREAM_CANCELLED
);
229 response_status_
= was_handshake_confirmed_
?
230 ERR_CONNECTION_CLOSED
: ERR_QUIC_HANDSHAKE_FAILED
;
234 HttpStream
* QuicHttpStream::RenewStreamForAuth() {
238 bool QuicHttpStream::IsResponseBodyComplete() const {
239 return next_state_
== STATE_OPEN
&& !stream_
;
242 bool QuicHttpStream::IsConnectionReused() const {
243 // TODO(rch): do something smarter here.
244 return stream_
&& stream_
->id() > 1;
247 void QuicHttpStream::SetConnectionReused() {
248 // QUIC doesn't need an indicator here.
251 bool QuicHttpStream::CanReuseConnection() const {
252 // QUIC streams aren't considered reusable.
256 int64
QuicHttpStream::GetTotalReceivedBytes() const {
257 // TODO(sclittle): Currently, this only includes response body bytes. Change
258 // this to include headers and QUIC overhead as well.
260 return stream_
->stream_bytes_read();
263 return closed_stream_received_bytes_
;
266 int64_t QuicHttpStream::GetTotalSentBytes() const {
267 // TODO(sclittle): Currently, this only includes request body bytes. Change
268 // this to include headers and QUIC overhead as well.
270 return stream_
->stream_bytes_written();
273 return closed_stream_sent_bytes_
;
276 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
277 // TODO(mmenke): Figure out what to do here.
281 void QuicHttpStream::GetSSLInfo(SSLInfo
* ssl_info
) {
283 session_
->GetSSLInfo(ssl_info
);
286 void QuicHttpStream::GetSSLCertRequestInfo(
287 SSLCertRequestInfo
* cert_request_info
) {
292 void QuicHttpStream::Drain(HttpNetworkSession
* session
) {
298 void QuicHttpStream::SetPriority(RequestPriority priority
) {
299 priority_
= priority
;
302 void QuicHttpStream::OnHeadersAvailable(const SpdyHeaderBlock
& headers
) {
303 int rv
= ProcessResponseHeaders(headers
);
304 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
309 void QuicHttpStream::OnDataAvailable() {
310 if (callback_
.is_null()) {
311 // Data is available, but can't be delivered
315 CHECK(user_buffer_
.get());
316 CHECK_NE(0, user_buffer_len_
);
317 int rv
= ReadAvailableData(user_buffer_
.get(), user_buffer_len_
);
318 if (rv
== ERR_IO_PENDING
) {
319 // This was a spurrious notification. Wait for the next one.
323 CHECK(!callback_
.is_null());
324 user_buffer_
= nullptr;
325 user_buffer_len_
= 0;
329 void QuicHttpStream::OnClose(QuicErrorCode error
) {
330 if (error
!= QUIC_NO_ERROR
) {
331 response_status_
= was_handshake_confirmed_
?
332 ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
333 } else if (!response_headers_received_
) {
334 response_status_
= ERR_ABORTED
;
338 if (!callback_
.is_null())
339 DoCallback(response_status_
);
342 void QuicHttpStream::OnError(int error
) {
344 response_status_
= was_handshake_confirmed_
?
345 error
: ERR_QUIC_HANDSHAKE_FAILED
;
346 if (!callback_
.is_null())
347 DoCallback(response_status_
);
350 bool QuicHttpStream::HasSendHeadersComplete() {
351 return next_state_
> STATE_SEND_HEADERS_COMPLETE
;
354 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
355 was_handshake_confirmed_
= true;
358 void QuicHttpStream::OnSessionClosed(int error
) {
360 session_error_
= error
;
364 void QuicHttpStream::OnIOComplete(int rv
) {
367 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
372 void QuicHttpStream::DoCallback(int rv
) {
373 CHECK_NE(rv
, ERR_IO_PENDING
);
374 CHECK(!callback_
.is_null());
376 // The client callback can do anything, including destroying this class,
377 // so any pending callback must be issued after everything else is done.
378 base::ResetAndReturn(&callback_
).Run(rv
);
381 int QuicHttpStream::DoLoop(int rv
) {
385 State state
= next_state_
;
386 next_state_
= STATE_NONE
;
388 case STATE_SEND_HEADERS
:
390 rv
= DoSendHeaders();
392 case STATE_SEND_HEADERS_COMPLETE
:
393 rv
= DoSendHeadersComplete(rv
);
395 case STATE_READ_REQUEST_BODY
:
397 rv
= DoReadRequestBody();
399 case STATE_READ_REQUEST_BODY_COMPLETE
:
400 rv
= DoReadRequestBodyComplete(rv
);
402 case STATE_SEND_BODY
:
406 case STATE_SEND_BODY_COMPLETE
:
407 rv
= DoSendBodyComplete(rv
);
413 NOTREACHED() << "next_state_: " << next_state_
;
416 } while (next_state_
!= STATE_NONE
&& next_state_
!= STATE_OPEN
&&
417 rv
!= ERR_IO_PENDING
);
422 int QuicHttpStream::DoSendHeaders() {
424 return ERR_UNEXPECTED
;
426 // Log the actual request with the URL Request's net log.
427 stream_net_log_
.AddEvent(
428 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS
,
429 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
431 // Also log to the QuicSession's net log.
432 stream_
->net_log().AddEvent(
433 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS
,
434 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
437 bool has_upload_data
= request_body_stream_
!= nullptr;
439 next_state_
= STATE_SEND_HEADERS_COMPLETE
;
440 int rv
= stream_
->WriteHeaders(request_headers_
, !has_upload_data
, nullptr);
441 request_headers_
.clear();
445 int QuicHttpStream::DoSendHeadersComplete(int rv
) {
449 next_state_
= request_body_stream_
?
450 STATE_READ_REQUEST_BODY
: STATE_OPEN
;
455 int QuicHttpStream::DoReadRequestBody() {
456 next_state_
= STATE_READ_REQUEST_BODY_COMPLETE
;
457 return request_body_stream_
->Read(
458 raw_request_body_buf_
.get(),
459 raw_request_body_buf_
->size(),
460 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
463 int QuicHttpStream::DoReadRequestBodyComplete(int rv
) {
464 // |rv| is the result of read from the request body from the last call to
469 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), rv
);
470 if (rv
== 0) { // Reached the end.
471 DCHECK(request_body_stream_
->IsEOF());
474 next_state_
= STATE_SEND_BODY
;
478 int QuicHttpStream::DoSendBody() {
480 return ERR_UNEXPECTED
;
482 CHECK(request_body_stream_
);
483 CHECK(request_body_buf_
.get());
484 const bool eof
= request_body_stream_
->IsEOF();
485 int len
= request_body_buf_
->BytesRemaining();
486 if (len
> 0 || eof
) {
487 next_state_
= STATE_SEND_BODY_COMPLETE
;
488 base::StringPiece
data(request_body_buf_
->data(), len
);
489 return stream_
->WriteStreamData(
491 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
494 next_state_
= STATE_OPEN
;
498 int QuicHttpStream::DoSendBodyComplete(int rv
) {
502 request_body_buf_
->DidConsume(request_body_buf_
->BytesRemaining());
504 if (!request_body_stream_
->IsEOF()) {
505 next_state_
= STATE_READ_REQUEST_BODY
;
509 next_state_
= STATE_OPEN
;
513 int QuicHttpStream::ProcessResponseHeaders(const SpdyHeaderBlock
& headers
) {
514 // The URLRequest logs these headers, so only log to the QuicSession's
516 stream_
->net_log().AddEvent(
517 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS
,
518 base::Bind(&SpdyHeaderBlockNetLogCallback
, &headers
));
520 if (!SpdyHeadersToHttpResponse(headers
, GetSpdyVersion(), response_info_
)) {
521 DLOG(WARNING
) << "Invalid headers";
522 return ERR_QUIC_PROTOCOL_ERROR
;
524 // Put the peer's IP address and port into the response.
525 IPEndPoint address
= session_
->peer_address();
526 response_info_
->socket_address
= HostPortPair::FromIPEndPoint(address
);
527 response_info_
->connection_info
=
528 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
;
529 response_info_
->vary_data
530 .Init(*request_info_
, *response_info_
->headers
.get());
531 response_info_
->was_npn_negotiated
= true;
532 response_info_
->npn_negotiated_protocol
= "quic/1+spdy/3";
533 response_info_
->response_time
= base::Time::Now();
534 response_info_
->request_time
= request_time_
;
535 response_headers_received_
= true;
540 int QuicHttpStream::ReadAvailableData(IOBuffer
* buf
, int buf_len
) {
541 int rv
= stream_
->Read(buf
, buf_len
);
542 if (stream_
->IsDoneReading()) {
543 stream_
->SetDelegate(nullptr);
544 stream_
->OnFinRead();
550 SpdyMajorVersion
QuicHttpStream::GetSpdyVersion() {
551 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_
->version());
554 void QuicHttpStream::ResetStream() {
557 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
558 closed_stream_sent_bytes_
= stream_
->stream_bytes_written();
561 // If |request_body_stream_| is non-NULL, Reset it, to abort any in progress
563 if (request_body_stream_
)
564 request_body_stream_
->Reset();
567 void QuicHttpStream::CrashIfInvalid() const {
568 #ifdef TEMP_INSTRUMENTATION_468529
569 Liveness liveness
= liveness_
;
571 if (liveness
== ALIVE
)
574 // Copy relevant variables onto the stack to guarantee they will be available
575 // in minidumps, and then crash.
576 base::debug::StackTrace stack_trace
= stack_trace_
;
578 base::debug::Alias(&liveness
);
579 base::debug::Alias(&stack_trace
);
581 CHECK_EQ(ALIVE
, liveness
);