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),
44 session_
->AddObserver(this);
47 QuicHttpStream::~QuicHttpStream() {
50 session_
->RemoveObserver(this);
53 int QuicHttpStream::InitializeStream(const HttpRequestInfo
* request_info
,
54 RequestPriority priority
,
55 const BoundNetLog
& stream_net_log
,
56 const CompletionCallback
& callback
) {
59 return was_handshake_confirmed_
? ERR_CONNECTION_CLOSED
:
60 ERR_QUIC_HANDSHAKE_FAILED
;
62 stream_net_log
.AddEvent(
63 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION
,
64 session_
->net_log().source().ToEventParametersCallback());
66 if (request_info
->url
.SchemeIsCryptographic()) {
69 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
70 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
73 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
;
76 stream_net_log_
= stream_net_log
;
77 request_info_
= request_info
;
78 request_time_
= base::Time::Now();
81 int rv
= stream_request_
.StartRequest(
82 session_
, &stream_
, base::Bind(&QuicHttpStream::OnStreamReady
,
83 weak_factory_
.GetWeakPtr()));
84 if (rv
== ERR_IO_PENDING
) {
86 } else if (rv
== OK
) {
87 stream_
->SetDelegate(this);
88 } else if (!was_handshake_confirmed_
) {
89 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
95 void QuicHttpStream::OnStreamReady(int rv
) {
96 DCHECK(rv
== OK
|| !stream_
);
98 stream_
->SetDelegate(this);
99 } else if (!was_handshake_confirmed_
) {
100 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
103 ResetAndReturn(&callback_
).Run(rv
);
106 int QuicHttpStream::SendRequest(const HttpRequestHeaders
& request_headers
,
107 HttpResponseInfo
* response
,
108 const CompletionCallback
& callback
) {
109 CHECK(!request_body_stream_
);
110 CHECK(!response_info_
);
111 CHECK(!callback
.is_null());
114 // TODO(rch): remove this once we figure out why channel ID is not being
115 // sent when it should be.
116 HostPortPair origin
= HostPortPair::FromURL(request_info_
->url
);
117 if (origin
.Equals(HostPortPair("accounts.google.com", 443)) &&
118 request_headers
.HasHeader(HttpRequestHeaders::kCookie
)) {
120 bool secure_session
=
121 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
122 DCHECK(secure_session
);
123 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
124 ssl_info
.channel_id_sent
);
127 return ERR_CONNECTION_CLOSED
;
130 QuicPriority priority
= ConvertRequestPriorityToQuicPriority(priority_
);
131 stream_
->set_priority(priority
);
132 // Store the serialized request headers.
133 CreateSpdyHeadersFromHttpRequest(*request_info_
, request_headers
,
135 /*direct=*/true, &request_headers_
);
137 // Store the request body.
138 request_body_stream_
= request_info_
->upload_data_stream
;
139 if (request_body_stream_
) {
140 // TODO(rch): Can we be more precise about when to allocate
141 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
142 // was being called even if we didn't yet allocate raw_request_body_buf_.
143 // && (request_body_stream_->size() ||
144 // request_body_stream_->is_chunked()))
145 // Use 10 packets as the body buffer size to give enough space to
146 // help ensure we don't often send out partial packets.
147 raw_request_body_buf_
=
148 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize
));
149 // The request body buffer is empty at first.
150 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), 0);
153 // Store the response info.
154 response_info_
= response
;
156 next_state_
= STATE_SEND_HEADERS
;
158 if (rv
== ERR_IO_PENDING
)
159 callback_
= callback
;
161 return rv
> 0 ? OK
: rv
;
164 UploadProgress
QuicHttpStream::GetUploadProgress() const {
165 if (!request_body_stream_
)
166 return UploadProgress();
168 return UploadProgress(request_body_stream_
->position(),
169 request_body_stream_
->size());
172 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
173 CHECK(!callback
.is_null());
175 if (stream_
== nullptr)
176 return response_status_
;
178 // Check if we already have the response headers. If so, return synchronously.
179 if (response_headers_received_
)
182 // Still waiting for the response, return IO_PENDING.
183 CHECK(callback_
.is_null());
184 callback_
= callback
;
185 return ERR_IO_PENDING
;
188 int QuicHttpStream::ReadResponseBody(
189 IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
) {
192 CHECK(!callback
.is_null());
194 // If we have data buffered, complete the IO immediately.
195 if (!response_body_
.empty()) {
197 while (!response_body_
.empty() && buf_len
> 0) {
198 scoped_refptr
<IOBufferWithSize
> data
= response_body_
.front();
199 const int bytes_to_copy
= std::min(buf_len
, data
->size());
200 memcpy(&(buf
->data()[bytes_read
]), data
->data(), bytes_to_copy
);
201 buf_len
-= bytes_to_copy
;
202 if (bytes_to_copy
== data
->size()) {
203 response_body_
.pop_front();
205 const int bytes_remaining
= data
->size() - bytes_to_copy
;
206 IOBufferWithSize
* new_buffer
= new IOBufferWithSize(bytes_remaining
);
207 memcpy(new_buffer
->data(), &(data
->data()[bytes_to_copy
]),
209 response_body_
.pop_front();
210 response_body_
.push_front(make_scoped_refptr(new_buffer
));
212 bytes_read
+= bytes_to_copy
;
218 // If the stream is already closed, there is no body to read.
219 return response_status_
;
222 CHECK(callback_
.is_null());
223 CHECK(!user_buffer_
.get());
224 CHECK_EQ(0, user_buffer_len_
);
226 callback_
= callback
;
228 user_buffer_len_
= buf_len
;
229 return ERR_IO_PENDING
;
232 void QuicHttpStream::Close(bool not_reusable
) {
233 // Note: the not_reusable flag has no meaning for SPDY streams.
235 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
236 stream_
->SetDelegate(nullptr);
237 stream_
->Reset(QUIC_STREAM_CANCELLED
);
239 response_status_
= was_handshake_confirmed_
?
240 ERR_CONNECTION_CLOSED
: ERR_QUIC_HANDSHAKE_FAILED
;
244 HttpStream
* QuicHttpStream::RenewStreamForAuth() {
248 bool QuicHttpStream::IsResponseBodyComplete() const {
249 return next_state_
== STATE_OPEN
&& !stream_
;
252 bool QuicHttpStream::CanFindEndOfResponse() const {
256 bool QuicHttpStream::IsConnectionReused() const {
257 // TODO(rch): do something smarter here.
258 return stream_
&& stream_
->id() > 1;
261 void QuicHttpStream::SetConnectionReused() {
262 // QUIC doesn't need an indicator here.
265 bool QuicHttpStream::IsConnectionReusable() const {
266 // QUIC streams aren't considered reusable.
270 int64
QuicHttpStream::GetTotalReceivedBytes() const {
272 return stream_
->stream_bytes_read();
275 return closed_stream_received_bytes_
;
278 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
279 // TODO(mmenke): Figure out what to do here.
283 void QuicHttpStream::GetSSLInfo(SSLInfo
* ssl_info
) {
285 session_
->GetSSLInfo(ssl_info
);
288 void QuicHttpStream::GetSSLCertRequestInfo(
289 SSLCertRequestInfo
* cert_request_info
) {
294 bool QuicHttpStream::IsSpdyHttpStream() const {
298 void QuicHttpStream::Drain(HttpNetworkSession
* session
) {
303 void QuicHttpStream::SetPriority(RequestPriority priority
) {
304 priority_
= priority
;
307 void QuicHttpStream::OnHeadersAvailable(StringPiece headers
) {
308 int rv
= ParseResponseHeaders(headers
);
309 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
314 int QuicHttpStream::OnDataReceived(const char* data
, int length
) {
315 DCHECK_NE(0, length
);
317 if (callback_
.is_null()) {
318 BufferResponseBody(data
, length
);
322 if (length
<= user_buffer_len_
) {
323 memcpy(user_buffer_
->data(), data
, length
);
325 memcpy(user_buffer_
->data(), data
, user_buffer_len_
);
326 int delta
= length
- user_buffer_len_
;
327 BufferResponseBody(data
+ user_buffer_len_
, delta
);
328 length
= user_buffer_len_
;
331 user_buffer_
= nullptr;
332 user_buffer_len_
= 0;
337 void QuicHttpStream::OnClose(QuicErrorCode error
) {
338 if (error
!= QUIC_NO_ERROR
) {
339 response_status_
= was_handshake_confirmed_
?
340 ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
341 } else if (!response_headers_received_
) {
342 response_status_
= ERR_ABORTED
;
345 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
347 if (!callback_
.is_null())
348 DoCallback(response_status_
);
351 void QuicHttpStream::OnError(int error
) {
353 response_status_
= was_handshake_confirmed_
?
354 error
: ERR_QUIC_HANDSHAKE_FAILED
;
355 if (!callback_
.is_null())
356 DoCallback(response_status_
);
359 bool QuicHttpStream::HasSendHeadersComplete() {
360 return next_state_
> STATE_SEND_HEADERS_COMPLETE
;
363 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
364 was_handshake_confirmed_
= true;
367 void QuicHttpStream::OnSessionClosed(int error
) {
369 session_error_
= error
;
373 void QuicHttpStream::OnIOComplete(int rv
) {
376 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
381 void QuicHttpStream::DoCallback(int rv
) {
382 CHECK_NE(rv
, ERR_IO_PENDING
);
383 CHECK(!callback_
.is_null());
385 // The client callback can do anything, including destroying this class,
386 // so any pending callback must be issued after everything else is done.
387 base::ResetAndReturn(&callback_
).Run(rv
);
390 int QuicHttpStream::DoLoop(int rv
) {
392 State state
= next_state_
;
393 next_state_
= STATE_NONE
;
395 case STATE_SEND_HEADERS
:
397 rv
= DoSendHeaders();
399 case STATE_SEND_HEADERS_COMPLETE
:
400 rv
= DoSendHeadersComplete(rv
);
402 case STATE_READ_REQUEST_BODY
:
404 rv
= DoReadRequestBody();
406 case STATE_READ_REQUEST_BODY_COMPLETE
:
407 rv
= DoReadRequestBodyComplete(rv
);
409 case STATE_SEND_BODY
:
413 case STATE_SEND_BODY_COMPLETE
:
414 rv
= DoSendBodyComplete(rv
);
420 NOTREACHED() << "next_state_: " << next_state_
;
423 } while (next_state_
!= STATE_NONE
&& next_state_
!= STATE_OPEN
&&
424 rv
!= ERR_IO_PENDING
);
429 int QuicHttpStream::DoSendHeaders() {
431 return ERR_UNEXPECTED
;
433 // Log the actual request with the URL Request's net log.
434 stream_net_log_
.AddEvent(
435 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS
,
436 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
438 // Also log to the QuicSession's net log.
439 stream_
->net_log().AddEvent(
440 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS
,
441 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
444 bool has_upload_data
= request_body_stream_
!= nullptr;
446 next_state_
= STATE_SEND_HEADERS_COMPLETE
;
447 int rv
= stream_
->WriteHeaders(request_headers_
, !has_upload_data
, nullptr);
448 request_headers_
.clear();
452 int QuicHttpStream::DoSendHeadersComplete(int rv
) {
456 next_state_
= request_body_stream_
?
457 STATE_READ_REQUEST_BODY
: STATE_OPEN
;
462 int QuicHttpStream::DoReadRequestBody() {
463 next_state_
= STATE_READ_REQUEST_BODY_COMPLETE
;
464 return request_body_stream_
->Read(
465 raw_request_body_buf_
.get(),
466 raw_request_body_buf_
->size(),
467 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
470 int QuicHttpStream::DoReadRequestBodyComplete(int rv
) {
471 // |rv| is the result of read from the request body from the last call to
476 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), rv
);
477 if (rv
== 0) { // Reached the end.
478 DCHECK(request_body_stream_
->IsEOF());
481 next_state_
= STATE_SEND_BODY
;
485 int QuicHttpStream::DoSendBody() {
487 return ERR_UNEXPECTED
;
489 CHECK(request_body_stream_
);
490 CHECK(request_body_buf_
.get());
491 const bool eof
= request_body_stream_
->IsEOF();
492 int len
= request_body_buf_
->BytesRemaining();
493 if (len
> 0 || eof
) {
494 next_state_
= STATE_SEND_BODY_COMPLETE
;
495 base::StringPiece
data(request_body_buf_
->data(), len
);
496 return stream_
->WriteStreamData(
498 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
501 next_state_
= STATE_OPEN
;
505 int QuicHttpStream::DoSendBodyComplete(int rv
) {
509 request_body_buf_
->DidConsume(request_body_buf_
->BytesRemaining());
511 if (!request_body_stream_
->IsEOF()) {
512 next_state_
= STATE_READ_REQUEST_BODY
;
516 next_state_
= STATE_OPEN
;
520 int QuicHttpStream::ParseResponseHeaders(StringPiece headers_data
) {
521 SpdyFramer
framer(GetSpdyVersion());
522 SpdyHeaderBlock headers
;
523 size_t len
= framer
.ParseHeaderBlockInBuffer(headers_data
.data(),
524 headers_data
.length(), &headers
);
525 if (len
== 0 || len
!= headers_data
.length()) {
526 DLOG(WARNING
) << "Invalid headers";
527 return ERR_QUIC_PROTOCOL_ERROR
;
530 // The URLRequest logs these headers, so only log to the QuicSession's
532 stream_
->net_log().AddEvent(
533 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS
,
534 base::Bind(&SpdyHeaderBlockNetLogCallback
, &headers
));
536 if (!SpdyHeadersToHttpResponse(headers
, GetSpdyVersion(), response_info_
)) {
537 DLOG(WARNING
) << "Invalid headers";
538 return ERR_QUIC_PROTOCOL_ERROR
;
540 // Put the peer's IP address and port into the response.
541 IPEndPoint address
= session_
->peer_address();
542 response_info_
->socket_address
= HostPortPair::FromIPEndPoint(address
);
543 response_info_
->connection_info
=
544 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
;
545 response_info_
->vary_data
546 .Init(*request_info_
, *response_info_
->headers
.get());
547 response_info_
->was_npn_negotiated
= true;
548 response_info_
->npn_negotiated_protocol
= "quic/1+spdy/3";
549 response_info_
->response_time
= base::Time::Now();
550 response_info_
->request_time
= request_time_
;
551 response_headers_received_
= true;
556 void QuicHttpStream::BufferResponseBody(const char* data
, int length
) {
559 IOBufferWithSize
* io_buffer
= new IOBufferWithSize(length
);
560 memcpy(io_buffer
->data(), data
, length
);
561 response_body_
.push_back(make_scoped_refptr(io_buffer
));
564 SpdyMajorVersion
QuicHttpStream::GetSpdyVersion() {
565 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_
->version());