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.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 static const size_t kHeaderBufInitialSize
= 4096;
29 QuicHttpStream::QuicHttpStream(const base::WeakPtr
<QuicClientSession
>& session
)
30 : next_state_(STATE_NONE
),
33 was_handshake_confirmed_(session
->IsCryptoHandshakeConfirmed()),
35 request_info_(nullptr),
36 request_body_stream_(nullptr),
37 priority_(MINIMUM_PRIORITY
),
38 response_info_(nullptr),
40 response_headers_received_(false),
41 read_buf_(new GrowableIOBuffer()),
42 closed_stream_received_bytes_(0),
46 session_
->AddObserver(this);
49 QuicHttpStream::~QuicHttpStream() {
52 session_
->RemoveObserver(this);
55 int QuicHttpStream::InitializeStream(const HttpRequestInfo
* request_info
,
56 RequestPriority priority
,
57 const BoundNetLog
& stream_net_log
,
58 const CompletionCallback
& callback
) {
61 return was_handshake_confirmed_
? ERR_CONNECTION_CLOSED
:
62 ERR_QUIC_HANDSHAKE_FAILED
;
64 stream_net_log
.AddEvent(
65 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION
,
66 session_
->net_log().source().ToEventParametersCallback());
68 if (request_info
->url
.SchemeIsCryptographic()) {
71 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
72 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
75 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
;
78 stream_net_log_
= stream_net_log
;
79 request_info_
= request_info
;
80 request_time_
= base::Time::Now();
83 int rv
= stream_request_
.StartRequest(
84 session_
, &stream_
, base::Bind(&QuicHttpStream::OnStreamReady
,
85 weak_factory_
.GetWeakPtr()));
86 if (rv
== ERR_IO_PENDING
) {
88 } else if (rv
== OK
) {
89 stream_
->SetDelegate(this);
90 } else if (!was_handshake_confirmed_
) {
91 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
97 void QuicHttpStream::OnStreamReady(int rv
) {
98 DCHECK(rv
== OK
|| !stream_
);
100 stream_
->SetDelegate(this);
101 } else if (!was_handshake_confirmed_
) {
102 rv
= ERR_QUIC_HANDSHAKE_FAILED
;
105 ResetAndReturn(&callback_
).Run(rv
);
108 int QuicHttpStream::SendRequest(const HttpRequestHeaders
& request_headers
,
109 HttpResponseInfo
* response
,
110 const CompletionCallback
& callback
) {
111 CHECK(!request_body_stream_
);
112 CHECK(!response_info_
);
113 CHECK(!callback
.is_null());
116 // TODO(rch): remove this once we figure out why channel ID is not being
117 // sent when it should be.
118 HostPortPair origin
= HostPortPair::FromURL(request_info_
->url
);
119 if (origin
.Equals(HostPortPair("accounts.google.com", 443)) &&
120 request_headers
.HasHeader(HttpRequestHeaders::kCookie
)) {
122 bool secure_session
=
123 session_
->GetSSLInfo(&ssl_info
) && ssl_info
.cert
.get();
124 DCHECK(secure_session
);
125 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
126 ssl_info
.channel_id_sent
);
129 return ERR_CONNECTION_CLOSED
;
132 QuicPriority priority
= ConvertRequestPriorityToQuicPriority(priority_
);
133 stream_
->set_priority(priority
);
134 // Store the serialized request headers.
135 CreateSpdyHeadersFromHttpRequest(*request_info_
, request_headers
,
137 /*direct=*/true, &request_headers_
);
139 // Store the request body.
140 request_body_stream_
= request_info_
->upload_data_stream
;
141 if (request_body_stream_
) {
142 // TODO(rch): Can we be more precise about when to allocate
143 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
144 // was being called even if we didn't yet allocate raw_request_body_buf_.
145 // && (request_body_stream_->size() ||
146 // request_body_stream_->is_chunked()))
147 // Use 10 packets as the body buffer size to give enough space to
148 // help ensure we don't often send out partial packets.
149 raw_request_body_buf_
=
150 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize
));
151 // The request body buffer is empty at first.
152 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), 0);
155 // Store the response info.
156 response_info_
= response
;
158 next_state_
= STATE_SEND_HEADERS
;
160 if (rv
== ERR_IO_PENDING
)
161 callback_
= callback
;
163 return rv
> 0 ? OK
: rv
;
166 UploadProgress
QuicHttpStream::GetUploadProgress() const {
167 if (!request_body_stream_
)
168 return UploadProgress();
170 return UploadProgress(request_body_stream_
->position(),
171 request_body_stream_
->size());
174 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
175 CHECK(!callback
.is_null());
177 if (stream_
== nullptr)
178 return response_status_
;
180 // Check if we already have the response headers. If so, return synchronously.
181 if (response_headers_received_
)
184 // Still waiting for the response, return IO_PENDING.
185 CHECK(callback_
.is_null());
186 callback_
= callback
;
187 return ERR_IO_PENDING
;
190 int QuicHttpStream::ReadResponseBody(
191 IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
) {
194 CHECK(!callback
.is_null());
196 // If we have data buffered, complete the IO immediately.
197 if (!response_body_
.empty()) {
199 while (!response_body_
.empty() && buf_len
> 0) {
200 scoped_refptr
<IOBufferWithSize
> data
= response_body_
.front();
201 const int bytes_to_copy
= std::min(buf_len
, data
->size());
202 memcpy(&(buf
->data()[bytes_read
]), data
->data(), bytes_to_copy
);
203 buf_len
-= bytes_to_copy
;
204 if (bytes_to_copy
== data
->size()) {
205 response_body_
.pop_front();
207 const int bytes_remaining
= data
->size() - bytes_to_copy
;
208 IOBufferWithSize
* new_buffer
= new IOBufferWithSize(bytes_remaining
);
209 memcpy(new_buffer
->data(), &(data
->data()[bytes_to_copy
]),
211 response_body_
.pop_front();
212 response_body_
.push_front(make_scoped_refptr(new_buffer
));
214 bytes_read
+= bytes_to_copy
;
220 // If the stream is already closed, there is no body to read.
221 return response_status_
;
224 CHECK(callback_
.is_null());
225 CHECK(!user_buffer_
.get());
226 CHECK_EQ(0, user_buffer_len_
);
228 callback_
= callback
;
230 user_buffer_len_
= buf_len
;
231 return ERR_IO_PENDING
;
234 void QuicHttpStream::Close(bool not_reusable
) {
235 // Note: the not_reusable flag has no meaning for SPDY streams.
237 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
238 stream_
->SetDelegate(nullptr);
239 stream_
->Reset(QUIC_STREAM_CANCELLED
);
241 response_status_
= was_handshake_confirmed_
?
242 ERR_CONNECTION_CLOSED
: ERR_QUIC_HANDSHAKE_FAILED
;
246 HttpStream
* QuicHttpStream::RenewStreamForAuth() {
250 bool QuicHttpStream::IsResponseBodyComplete() const {
251 return next_state_
== STATE_OPEN
&& !stream_
;
254 bool QuicHttpStream::CanFindEndOfResponse() const {
258 bool QuicHttpStream::IsConnectionReused() const {
259 // TODO(rch): do something smarter here.
260 return stream_
&& stream_
->id() > 1;
263 void QuicHttpStream::SetConnectionReused() {
264 // QUIC doesn't need an indicator here.
267 bool QuicHttpStream::IsConnectionReusable() const {
268 // QUIC streams aren't considered reusable.
272 int64
QuicHttpStream::GetTotalReceivedBytes() const {
274 return stream_
->stream_bytes_read();
277 return closed_stream_received_bytes_
;
280 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
281 // TODO(mmenke): Figure out what to do here.
285 void QuicHttpStream::GetSSLInfo(SSLInfo
* ssl_info
) {
287 session_
->GetSSLInfo(ssl_info
);
290 void QuicHttpStream::GetSSLCertRequestInfo(
291 SSLCertRequestInfo
* cert_request_info
) {
296 bool QuicHttpStream::IsSpdyHttpStream() const {
300 void QuicHttpStream::Drain(HttpNetworkSession
* session
) {
305 void QuicHttpStream::SetPriority(RequestPriority priority
) {
306 priority_
= priority
;
309 int QuicHttpStream::OnDataReceived(const char* data
, int length
) {
310 DCHECK_NE(0, length
);
311 // Are we still reading the response headers.
312 if (!response_headers_received_
) {
313 // Grow the read buffer if necessary.
314 if (read_buf_
->RemainingCapacity() < length
) {
315 size_t additional_capacity
= length
- read_buf_
->RemainingCapacity();
316 if (additional_capacity
< kHeaderBufInitialSize
)
317 additional_capacity
= kHeaderBufInitialSize
;
318 read_buf_
->SetCapacity(read_buf_
->capacity() + additional_capacity
);
320 memcpy(read_buf_
->data(), data
, length
);
321 read_buf_
->set_offset(read_buf_
->offset() + length
);
322 int rv
= ParseResponseHeaders();
323 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
329 if (callback_
.is_null()) {
330 BufferResponseBody(data
, length
);
334 if (length
<= user_buffer_len_
) {
335 memcpy(user_buffer_
->data(), data
, length
);
337 memcpy(user_buffer_
->data(), data
, user_buffer_len_
);
338 int delta
= length
- user_buffer_len_
;
339 BufferResponseBody(data
+ user_buffer_len_
, delta
);
340 length
= user_buffer_len_
;
343 user_buffer_
= nullptr;
344 user_buffer_len_
= 0;
349 void QuicHttpStream::OnClose(QuicErrorCode error
) {
350 if (error
!= QUIC_NO_ERROR
) {
351 response_status_
= was_handshake_confirmed_
?
352 ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
353 } else if (!response_headers_received_
) {
354 response_status_
= ERR_ABORTED
;
357 closed_stream_received_bytes_
= stream_
->stream_bytes_read();
359 if (!callback_
.is_null())
360 DoCallback(response_status_
);
363 void QuicHttpStream::OnError(int error
) {
365 response_status_
= was_handshake_confirmed_
?
366 error
: ERR_QUIC_HANDSHAKE_FAILED
;
367 if (!callback_
.is_null())
368 DoCallback(response_status_
);
371 bool QuicHttpStream::HasSendHeadersComplete() {
372 return next_state_
> STATE_SEND_HEADERS_COMPLETE
;
375 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
376 was_handshake_confirmed_
= true;
379 void QuicHttpStream::OnSessionClosed(int error
) {
381 session_error_
= error
;
385 void QuicHttpStream::OnIOComplete(int rv
) {
388 if (rv
!= ERR_IO_PENDING
&& !callback_
.is_null()) {
393 void QuicHttpStream::DoCallback(int rv
) {
394 CHECK_NE(rv
, ERR_IO_PENDING
);
395 CHECK(!callback_
.is_null());
397 // The client callback can do anything, including destroying this class,
398 // so any pending callback must be issued after everything else is done.
399 base::ResetAndReturn(&callback_
).Run(rv
);
402 int QuicHttpStream::DoLoop(int rv
) {
404 State state
= next_state_
;
405 next_state_
= STATE_NONE
;
407 case STATE_SEND_HEADERS
:
409 rv
= DoSendHeaders();
411 case STATE_SEND_HEADERS_COMPLETE
:
412 rv
= DoSendHeadersComplete(rv
);
414 case STATE_READ_REQUEST_BODY
:
416 rv
= DoReadRequestBody();
418 case STATE_READ_REQUEST_BODY_COMPLETE
:
419 rv
= DoReadRequestBodyComplete(rv
);
421 case STATE_SEND_BODY
:
425 case STATE_SEND_BODY_COMPLETE
:
426 rv
= DoSendBodyComplete(rv
);
432 NOTREACHED() << "next_state_: " << next_state_
;
435 } while (next_state_
!= STATE_NONE
&& next_state_
!= STATE_OPEN
&&
436 rv
!= ERR_IO_PENDING
);
441 int QuicHttpStream::DoSendHeaders() {
443 return ERR_UNEXPECTED
;
445 // Log the actual request with the URL Request's net log.
446 stream_net_log_
.AddEvent(
447 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS
,
448 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
450 // Also log to the QuicSession's net log.
451 stream_
->net_log().AddEvent(
452 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS
,
453 base::Bind(&QuicRequestNetLogCallback
, stream_
->id(), &request_headers_
,
456 bool has_upload_data
= request_body_stream_
!= nullptr;
458 next_state_
= STATE_SEND_HEADERS_COMPLETE
;
459 int rv
= stream_
->WriteHeaders(request_headers_
, !has_upload_data
, nullptr);
460 request_headers_
.clear();
464 int QuicHttpStream::DoSendHeadersComplete(int rv
) {
468 next_state_
= request_body_stream_
?
469 STATE_READ_REQUEST_BODY
: STATE_OPEN
;
474 int QuicHttpStream::DoReadRequestBody() {
475 next_state_
= STATE_READ_REQUEST_BODY_COMPLETE
;
476 return request_body_stream_
->Read(
477 raw_request_body_buf_
.get(),
478 raw_request_body_buf_
->size(),
479 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
482 int QuicHttpStream::DoReadRequestBodyComplete(int rv
) {
483 // |rv| is the result of read from the request body from the last call to
488 request_body_buf_
= new DrainableIOBuffer(raw_request_body_buf_
.get(), rv
);
489 if (rv
== 0) { // Reached the end.
490 DCHECK(request_body_stream_
->IsEOF());
493 next_state_
= STATE_SEND_BODY
;
497 int QuicHttpStream::DoSendBody() {
499 return ERR_UNEXPECTED
;
501 CHECK(request_body_stream_
);
502 CHECK(request_body_buf_
.get());
503 const bool eof
= request_body_stream_
->IsEOF();
504 int len
= request_body_buf_
->BytesRemaining();
505 if (len
> 0 || eof
) {
506 next_state_
= STATE_SEND_BODY_COMPLETE
;
507 base::StringPiece
data(request_body_buf_
->data(), len
);
508 return stream_
->WriteStreamData(
510 base::Bind(&QuicHttpStream::OnIOComplete
, weak_factory_
.GetWeakPtr()));
513 next_state_
= STATE_OPEN
;
517 int QuicHttpStream::DoSendBodyComplete(int rv
) {
521 request_body_buf_
->DidConsume(request_body_buf_
->BytesRemaining());
523 if (!request_body_stream_
->IsEOF()) {
524 next_state_
= STATE_READ_REQUEST_BODY
;
528 next_state_
= STATE_OPEN
;
532 int QuicHttpStream::ParseResponseHeaders() {
533 size_t read_buf_len
= static_cast<size_t>(read_buf_
->offset());
534 SpdyFramer
framer(GetSpdyVersion());
535 SpdyHeaderBlock headers
;
536 char* data
= read_buf_
->StartOfBuffer();
537 size_t len
= framer
.ParseHeaderBlockInBuffer(data
, read_buf_
->offset(),
541 return ERR_IO_PENDING
;
544 // Save the remaining received data.
545 size_t delta
= read_buf_len
- len
;
547 BufferResponseBody(data
+ len
, delta
);
550 // The URLRequest logs these headers, so only log to the QuicSession's
552 stream_
->net_log().AddEvent(
553 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS
,
554 base::Bind(&SpdyHeaderBlockNetLogCallback
, &headers
));
556 if (!SpdyHeadersToHttpResponse(headers
, GetSpdyVersion(), response_info_
)) {
557 DLOG(WARNING
) << "Invalid headers";
558 return ERR_QUIC_PROTOCOL_ERROR
;
560 // Put the peer's IP address and port into the response.
561 IPEndPoint address
= session_
->peer_address();
562 response_info_
->socket_address
= HostPortPair::FromIPEndPoint(address
);
563 response_info_
->connection_info
=
564 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
;
565 response_info_
->vary_data
566 .Init(*request_info_
, *response_info_
->headers
.get());
567 response_info_
->was_npn_negotiated
= true;
568 response_info_
->npn_negotiated_protocol
= "quic/1+spdy/3";
569 response_info_
->response_time
= base::Time::Now();
570 response_info_
->request_time
= request_time_
;
571 response_headers_received_
= true;
576 void QuicHttpStream::BufferResponseBody(const char* data
, int length
) {
579 IOBufferWithSize
* io_buffer
= new IOBufferWithSize(length
);
580 memcpy(io_buffer
->data(), data
, length
);
581 response_body_
.push_back(make_scoped_refptr(io_buffer
));
584 SpdyMajorVersion
QuicHttpStream::GetSpdyVersion() {
585 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_
->version());