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),
47 session_
->AddObserver(this);
50 QuicHttpStream::~QuicHttpStream() {
51 #ifdef TEMP_INSTRUMENTATION_468529
53 stack_trace_
= base::debug::StackTrace();
55 // Probably not necessary, but just in case compiler tries to optimize out the
56 // writes to liveness_ and stack_trace_.
57 base::debug::Alias(&liveness_
);
58 base::debug::Alias(&stack_trace_
);
63 session_
->RemoveObserver(this);
66 int QuicHttpStream::InitializeStream(const HttpRequestInfo
* request_info
,
67 RequestPriority priority
,
68 const BoundNetLog
& stream_net_log
,
69 const CompletionCallback
& callback
) {
72 return was_handshake_confirmed_
? ERR_CONNECTION_CLOSED
:
73 ERR_QUIC_HANDSHAKE_FAILED
;
75 stream_net_log
.AddEvent(
76 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION
,
77 session_
->net_log().source().ToEventParametersCallback());
79 if (request_info
->url
.SchemeIsCryptographic()) {
82 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
83 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
86 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
;
89 stream_net_log_
= stream_net_log
;
90 request_info_
= request_info
;
91 request_time_
= base::Time::Now();
94 int rv
= stream_request_
.StartRequest(
95 session_
, &stream_
, base::Bind(&QuicHttpStream::OnStreamReady
,
96 weak_factory_
.GetWeakPtr()));
97 if (rv
== ERR_IO_PENDING
) {
99 } else if (rv
== OK
) {
100 stream_
->SetDelegate(this);
101 } else if (!was_handshake_confirmed_
) {
102 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
108 void QuicHttpStream::OnStreamReady(int rv
) {
109 DCHECK(rv
== OK
|| !stream_
);
111 stream_
->SetDelegate(this);
112 } else if (!was_handshake_confirmed_
) {
113 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
116 ResetAndReturn(&callback_
).Run(rv
);
119 int QuicHttpStream::SendRequest(const HttpRequestHeaders
& request_headers
,
120 HttpResponseInfo
* response
,
121 const CompletionCallback
& callback
) {
122 CHECK(!request_body_stream_
);
123 CHECK(!response_info_
);
124 CHECK(!callback
.is_null());
127 // TODO(rch): remove this once we figure out why channel ID is not being
128 // sent when it should be.
129 HostPortPair origin
= HostPortPair::FromURL(request_info_
->url
);
130 if (origin
.Equals(HostPortPair("accounts.google.com", 443)) &&
131 request_headers
.HasHeader(HttpRequestHeaders::kCookie
)) {
133 bool secure_session
=
134 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
135 DCHECK(secure_session
);
136 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
137 ssl_info
.channel_id_sent
);
140 return ERR_CONNECTION_CLOSED
;
143 QuicPriority priority
= ConvertRequestPriorityToQuicPriority(priority_
);
144 stream_
->set_priority(priority
);
145 // Store the serialized request headers.
146 CreateSpdyHeadersFromHttpRequest(*request_info_
, request_headers
,
148 /*direct=*/true, &request_headers_
);
150 // Store the request body.
151 request_body_stream_
= request_info_
->upload_data_stream
;
152 if (request_body_stream_
) {
153 // TODO(rch): Can we be more precise about when to allocate
154 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
155 // was being called even if we didn't yet allocate raw_request_body_buf_.
156 // && (request_body_stream_->size() ||
157 // request_body_stream_->is_chunked()))
158 // Use 10 packets as the body buffer size to give enough space to
159 // help ensure we don't often send out partial packets.
160 raw_request_body_buf_
=
161 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize
));
162 // The request body buffer is empty at first.
163 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), 0);
166 // Store the response info.
167 response_info_
= response
;
169 next_state_
= STATE_SEND_HEADERS
;
171 if (rv
== ERR_IO_PENDING
)
172 callback_
= callback
;
174 return rv
> 0 ? OK
: rv
;
177 UploadProgress
QuicHttpStream::GetUploadProgress() const {
178 if (!request_body_stream_
)
179 return UploadProgress();
181 return UploadProgress(request_body_stream_
->position(),
182 request_body_stream_
->size());
185 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
186 CHECK(!callback
.is_null());
188 if (stream_
== nullptr)
189 return response_status_
;
191 // Check if we already have the response headers. If so, return synchronously.
192 if (response_headers_received_
)
195 // Still waiting for the response, return IO_PENDING.
196 CHECK(callback_
.is_null());
197 callback_
= callback
;
198 return ERR_IO_PENDING
;
201 int QuicHttpStream::ReadResponseBody(
202 IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
) {
204 // If the stream is already closed, there is no body to read.
205 return response_status_
;
208 int rv
= ReadAvailableData(buf
, buf_len
);
209 if (rv
!= ERR_IO_PENDING
)
212 CHECK(callback_
.is_null());
213 CHECK(!user_buffer_
.get());
214 CHECK_EQ(0, user_buffer_len_
);
216 callback_
= callback
;
218 user_buffer_len_
= buf_len
;
219 return ERR_IO_PENDING
;
222 void QuicHttpStream::Close(bool not_reusable
) {
223 // Note: the not_reusable flag has no meaning for SPDY streams.
225 stream_
->SetDelegate(nullptr);
226 stream_
->Reset(QUIC_STREAM_CANCELLED
);
228 response_status_
= was_handshake_confirmed_
?
229 ERR_CONNECTION_CLOSED
: ERR_QUIC_HANDSHAKE_FAILED
;
233 HttpStream
* QuicHttpStream::RenewStreamForAuth() {
237 bool QuicHttpStream::IsResponseBodyComplete() const {
238 return next_state_
== STATE_OPEN
&& !stream_
;
241 bool QuicHttpStream::CanFindEndOfResponse() const {
245 bool QuicHttpStream::IsConnectionReused() const {
246 // TODO(rch): do something smarter here.
247 return stream_
&& stream_
->id() > 1;
250 void QuicHttpStream::SetConnectionReused() {
251 // QUIC doesn't need an indicator here.
254 bool QuicHttpStream::IsConnectionReusable() const {
255 // QUIC streams aren't considered reusable.
259 int64
QuicHttpStream::GetTotalReceivedBytes() const {
261 return stream_
->stream_bytes_read();
264 return closed_stream_received_bytes_
;
267 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
268 // TODO(mmenke): Figure out what to do here.
272 void QuicHttpStream::GetSSLInfo(SSLInfo
* ssl_info
) {
274 session_
->GetSSLInfo(ssl_info
);
277 void QuicHttpStream::GetSSLCertRequestInfo(
278 SSLCertRequestInfo
* cert_request_info
) {
283 bool QuicHttpStream::IsSpdyHttpStream() const {
287 void QuicHttpStream::Drain(HttpNetworkSession
* session
) {
292 void QuicHttpStream::SetPriority(RequestPriority priority
) {
293 priority_
= priority
;
296 void QuicHttpStream::OnHeadersAvailable(const SpdyHeaderBlock
& headers
) {
297 int rv
= ProcessResponseHeaders(headers
);
298 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
303 void QuicHttpStream::OnDataAvailable() {
304 if (callback_
.is_null()) {
305 // Data is available, but can't be delivered
309 CHECK(user_buffer_
.get());
310 CHECK_NE(0, user_buffer_len_
);
311 int rv
= ReadAvailableData(user_buffer_
.get(), user_buffer_len_
);
312 if (rv
== ERR_IO_PENDING
) {
313 // This was a spurrious notification. Wait for the next one.
317 CHECK(!callback_
.is_null());
318 user_buffer_
= nullptr;
319 user_buffer_len_
= 0;
323 void QuicHttpStream::OnClose(QuicErrorCode error
) {
324 if (error
!= QUIC_NO_ERROR
) {
325 response_status_
= was_handshake_confirmed_
?
326 ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
327 } else if (!response_headers_received_
) {
328 response_status_
= ERR_ABORTED
;
332 if (!callback_
.is_null())
333 DoCallback(response_status_
);
336 void QuicHttpStream::OnError(int error
) {
338 response_status_
= was_handshake_confirmed_
?
339 error
: ERR_QUIC_HANDSHAKE_FAILED
;
340 if (!callback_
.is_null())
341 DoCallback(response_status_
);
344 bool QuicHttpStream::HasSendHeadersComplete() {
345 return next_state_
> STATE_SEND_HEADERS_COMPLETE
;
348 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
349 was_handshake_confirmed_
= true;
352 void QuicHttpStream::OnSessionClosed(int error
) {
354 session_error_
= error
;
358 void QuicHttpStream::OnIOComplete(int rv
) {
361 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
366 void QuicHttpStream::DoCallback(int rv
) {
367 CHECK_NE(rv
, ERR_IO_PENDING
);
368 CHECK(!callback_
.is_null());
370 // The client callback can do anything, including destroying this class,
371 // so any pending callback must be issued after everything else is done.
372 base::ResetAndReturn(&callback_
).Run(rv
);
375 int QuicHttpStream::DoLoop(int rv
) {
379 State state
= next_state_
;
380 next_state_
= STATE_NONE
;
382 case STATE_SEND_HEADERS
:
384 rv
= DoSendHeaders();
386 case STATE_SEND_HEADERS_COMPLETE
:
387 rv
= DoSendHeadersComplete(rv
);
389 case STATE_READ_REQUEST_BODY
:
391 rv
= DoReadRequestBody();
393 case STATE_READ_REQUEST_BODY_COMPLETE
:
394 rv
= DoReadRequestBodyComplete(rv
);
396 case STATE_SEND_BODY
:
400 case STATE_SEND_BODY_COMPLETE
:
401 rv
= DoSendBodyComplete(rv
);
407 NOTREACHED() << "next_state_: " << next_state_
;
410 } while (next_state_
!= STATE_NONE
&& next_state_
!= STATE_OPEN
&&
411 rv
!= ERR_IO_PENDING
);
416 int QuicHttpStream::DoSendHeaders() {
418 return ERR_UNEXPECTED
;
420 // Log the actual request with the URL Request's net log.
421 stream_net_log_
.AddEvent(
422 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS
,
423 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
425 // Also log to the QuicSession's net log.
426 stream_
->net_log().AddEvent(
427 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS
,
428 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
431 bool has_upload_data
= request_body_stream_
!= nullptr;
433 next_state_
= STATE_SEND_HEADERS_COMPLETE
;
434 int rv
= stream_
->WriteHeaders(request_headers_
, !has_upload_data
, nullptr);
435 request_headers_
.clear();
439 int QuicHttpStream::DoSendHeadersComplete(int rv
) {
443 next_state_
= request_body_stream_
?
444 STATE_READ_REQUEST_BODY
: STATE_OPEN
;
449 int QuicHttpStream::DoReadRequestBody() {
450 next_state_
= STATE_READ_REQUEST_BODY_COMPLETE
;
451 return request_body_stream_
->Read(
452 raw_request_body_buf_
.get(),
453 raw_request_body_buf_
->size(),
454 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
457 int QuicHttpStream::DoReadRequestBodyComplete(int rv
) {
458 // |rv| is the result of read from the request body from the last call to
463 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), rv
);
464 if (rv
== 0) { // Reached the end.
465 DCHECK(request_body_stream_
->IsEOF());
468 next_state_
= STATE_SEND_BODY
;
472 int QuicHttpStream::DoSendBody() {
474 return ERR_UNEXPECTED
;
476 CHECK(request_body_stream_
);
477 CHECK(request_body_buf_
.get());
478 const bool eof
= request_body_stream_
->IsEOF();
479 int len
= request_body_buf_
->BytesRemaining();
480 if (len
> 0 || eof
) {
481 next_state_
= STATE_SEND_BODY_COMPLETE
;
482 base::StringPiece
data(request_body_buf_
->data(), len
);
483 return stream_
->WriteStreamData(
485 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
488 next_state_
= STATE_OPEN
;
492 int QuicHttpStream::DoSendBodyComplete(int rv
) {
496 request_body_buf_
->DidConsume(request_body_buf_
->BytesRemaining());
498 if (!request_body_stream_
->IsEOF()) {
499 next_state_
= STATE_READ_REQUEST_BODY
;
503 next_state_
= STATE_OPEN
;
507 int QuicHttpStream::ProcessResponseHeaders(const SpdyHeaderBlock
& headers
) {
508 // The URLRequest logs these headers, so only log to the QuicSession's
510 stream_
->net_log().AddEvent(
511 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS
,
512 base::Bind(&SpdyHeaderBlockNetLogCallback
, &headers
));
514 if (!SpdyHeadersToHttpResponse(headers
, GetSpdyVersion(), response_info_
)) {
515 DLOG(WARNING
) << "Invalid headers";
516 return ERR_QUIC_PROTOCOL_ERROR
;
518 // Put the peer's IP address and port into the response.
519 IPEndPoint address
= session_
->peer_address();
520 response_info_
->socket_address
= HostPortPair::FromIPEndPoint(address
);
521 response_info_
->connection_info
=
522 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
;
523 response_info_
->vary_data
524 .Init(*request_info_
, *response_info_
->headers
.get());
525 response_info_
->was_npn_negotiated
= true;
526 response_info_
->npn_negotiated_protocol
= "quic/1+spdy/3";
527 response_info_
->response_time
= base::Time::Now();
528 response_info_
->request_time
= request_time_
;
529 response_headers_received_
= true;
534 int QuicHttpStream::ReadAvailableData(IOBuffer
* buf
, int buf_len
) {
535 int rv
= stream_
->Read(buf
, buf_len
);
536 if (stream_
->IsDoneReading()) {
537 stream_
->SetDelegate(nullptr);
538 stream_
->OnFinRead();
544 SpdyMajorVersion
QuicHttpStream::GetSpdyVersion() {
545 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_
->version());
548 void QuicHttpStream::ResetStream() {
551 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
555 void QuicHttpStream::CrashIfInvalid() const {
556 #ifdef TEMP_INSTRUMENTATION_468529
557 Liveness liveness
= liveness_
;
559 if (liveness
== ALIVE
)
562 // Copy relevant variables onto the stack to guarantee they will be available
563 // in minidumps, and then crash.
564 base::debug::StackTrace stack_trace
= stack_trace_
;
566 base::debug::Alias(&liveness
);
567 base::debug::Alias(&stack_trace
);
569 CHECK_EQ(ALIVE
, liveness
);