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/spdy/spdy_proxy_client_socket.h"
7 #include <algorithm> // min
10 #include "base/bind_helpers.h"
11 #include "base/callback_helpers.h"
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15 #include "net/base/auth.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_util.h"
18 #include "net/http/http_auth_cache.h"
19 #include "net/http/http_auth_handler_factory.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/proxy_connect_redirect_http_stream.h"
22 #include "net/spdy/spdy_http_utils.h"
27 SpdyProxyClientSocket::SpdyProxyClientSocket(
28 const base::WeakPtr
<SpdyStream
>& spdy_stream
,
29 const std::string
& user_agent
,
30 const HostPortPair
& endpoint
,
32 const HostPortPair
& proxy_server
,
33 const BoundNetLog
& source_net_log
,
34 HttpAuthCache
* auth_cache
,
35 HttpAuthHandlerFactory
* auth_handler_factory
)
36 : next_state_(STATE_DISCONNECTED
),
37 spdy_stream_(spdy_stream
),
40 new HttpAuthController(HttpAuth::AUTH_PROXY
,
41 GURL("https://" + proxy_server
.ToString()),
43 auth_handler_factory
)),
46 was_ever_used_(false),
47 redirect_has_load_timing_info_(false),
48 net_log_(BoundNetLog::Make(spdy_stream
->net_log().net_log(),
49 NetLog::SOURCE_PROXY_CLIENT_SOCKET
)),
51 request_
.method
= "CONNECT";
53 if (!user_agent
.empty())
54 request_
.extra_headers
.SetHeader(HttpRequestHeaders::kUserAgent
,
57 net_log_
.BeginEvent(NetLog::TYPE_SOCKET_ALIVE
,
58 source_net_log
.source().ToEventParametersCallback());
60 NetLog::TYPE_SPDY_PROXY_CLIENT_SESSION
,
61 spdy_stream
->net_log().source().ToEventParametersCallback());
63 spdy_stream_
->SetDelegate(this);
64 was_ever_used_
= spdy_stream_
->WasEverUsed();
67 SpdyProxyClientSocket::~SpdyProxyClientSocket() {
69 net_log_
.EndEvent(NetLog::TYPE_SOCKET_ALIVE
);
72 const HttpResponseInfo
* SpdyProxyClientSocket::GetConnectResponseInfo() const {
73 return response_
.headers
.get() ? &response_
: NULL
;
76 const scoped_refptr
<HttpAuthController
>&
77 SpdyProxyClientSocket::GetAuthController() const {
81 int SpdyProxyClientSocket::RestartWithAuth(const CompletionCallback
& callback
) {
82 // A SPDY Stream can only handle a single request, so the underlying
83 // stream may not be reused and a new SpdyProxyClientSocket must be
84 // created (possibly on top of the same SPDY Session).
85 next_state_
= STATE_DISCONNECTED
;
89 bool SpdyProxyClientSocket::IsUsingSpdy() const {
93 NextProto
SpdyProxyClientSocket::GetProtocolNegotiated() const {
94 // Save the negotiated protocol
96 bool was_npn_negotiated
;
97 NextProto protocol_negotiated
;
98 spdy_stream_
->GetSSLInfo(&ssl_info
, &was_npn_negotiated
,
99 &protocol_negotiated
);
100 return protocol_negotiated
;
103 HttpStream
* SpdyProxyClientSocket::CreateConnectResponseStream() {
104 return new ProxyConnectRedirectHttpStream(
105 redirect_has_load_timing_info_
? &redirect_load_timing_info_
: NULL
);
108 // Sends a SYN_STREAM frame to the proxy with a CONNECT request
109 // for the specified endpoint. Waits for the server to send back
110 // a SYN_REPLY frame. OK will be returned if the status is 200.
111 // ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status.
112 // In any of these cases, Read() may be called to retrieve the HTTP
113 // response body. Any other return values should be considered fatal.
114 // TODO(rch): handle 407 proxy auth requested correctly, perhaps
115 // by creating a new stream for the subsequent request.
116 // TODO(rch): create a more appropriate error code to disambiguate
117 // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure.
118 int SpdyProxyClientSocket::Connect(const CompletionCallback
& callback
) {
119 DCHECK(read_callback_
.is_null());
120 if (next_state_
== STATE_OPEN
)
123 DCHECK_EQ(STATE_DISCONNECTED
, next_state_
);
124 next_state_
= STATE_GENERATE_AUTH_TOKEN
;
127 if (rv
== ERR_IO_PENDING
)
128 read_callback_
= callback
;
132 void SpdyProxyClientSocket::Disconnect() {
133 read_buffer_queue_
.Clear();
135 user_buffer_len_
= 0;
136 read_callback_
.Reset();
138 write_buffer_len_
= 0;
139 write_callback_
.Reset();
141 next_state_
= STATE_DISCONNECTED
;
143 if (spdy_stream_
.get()) {
144 // This will cause OnClose to be invoked, which takes care of
145 // cleaning up all the internal state.
146 spdy_stream_
->Cancel();
147 DCHECK(!spdy_stream_
.get());
151 bool SpdyProxyClientSocket::IsConnected() const {
152 return next_state_
== STATE_OPEN
;
155 bool SpdyProxyClientSocket::IsConnectedAndIdle() const {
156 return IsConnected() && read_buffer_queue_
.IsEmpty() &&
157 spdy_stream_
->IsOpen();
160 const BoundNetLog
& SpdyProxyClientSocket::NetLog() const {
164 void SpdyProxyClientSocket::SetSubresourceSpeculation() {
165 // TODO(rch): what should this implementation be?
168 void SpdyProxyClientSocket::SetOmniboxSpeculation() {
169 // TODO(rch): what should this implementation be?
172 bool SpdyProxyClientSocket::WasEverUsed() const {
173 return was_ever_used_
|| (spdy_stream_
.get() && spdy_stream_
->WasEverUsed());
176 bool SpdyProxyClientSocket::UsingTCPFastOpen() const {
180 bool SpdyProxyClientSocket::WasNpnNegotiated() const {
184 NextProto
SpdyProxyClientSocket::GetNegotiatedProtocol() const {
185 return kProtoUnknown
;
188 bool SpdyProxyClientSocket::GetSSLInfo(SSLInfo
* ssl_info
) {
189 bool was_npn_negotiated
;
190 NextProto protocol_negotiated
;
191 return spdy_stream_
->GetSSLInfo(ssl_info
, &was_npn_negotiated
,
192 &protocol_negotiated
);
195 int SpdyProxyClientSocket::Read(IOBuffer
* buf
, int buf_len
,
196 const CompletionCallback
& callback
) {
197 DCHECK(read_callback_
.is_null());
198 DCHECK(!user_buffer_
.get());
200 if (next_state_
== STATE_DISCONNECTED
)
201 return ERR_SOCKET_NOT_CONNECTED
;
203 if (next_state_
== STATE_CLOSED
&& read_buffer_queue_
.IsEmpty()) {
207 DCHECK(next_state_
== STATE_OPEN
|| next_state_
== STATE_CLOSED
);
209 size_t result
= PopulateUserReadBuffer(buf
->data(), buf_len
);
212 user_buffer_len_
= static_cast<size_t>(buf_len
);
213 DCHECK(!callback
.is_null());
214 read_callback_
= callback
;
215 return ERR_IO_PENDING
;
221 size_t SpdyProxyClientSocket::PopulateUserReadBuffer(char* data
, size_t len
) {
222 return read_buffer_queue_
.Dequeue(data
, len
);
225 int SpdyProxyClientSocket::Write(IOBuffer
* buf
, int buf_len
,
226 const CompletionCallback
& callback
) {
227 DCHECK(write_callback_
.is_null());
228 if (next_state_
!= STATE_OPEN
)
229 return ERR_SOCKET_NOT_CONNECTED
;
231 DCHECK(spdy_stream_
.get());
232 spdy_stream_
->SendData(buf
, buf_len
, MORE_DATA_TO_SEND
);
233 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
,
234 buf_len
, buf
->data());
235 write_callback_
= callback
;
236 write_buffer_len_
= buf_len
;
237 return ERR_IO_PENDING
;
240 int SpdyProxyClientSocket::SetReceiveBufferSize(int32 size
) {
241 // Since this StreamSocket sits on top of a shared SpdySession, it
242 // is not safe for callers to change this underlying socket.
243 return ERR_NOT_IMPLEMENTED
;
246 int SpdyProxyClientSocket::SetSendBufferSize(int32 size
) {
247 // Since this StreamSocket sits on top of a shared SpdySession, it
248 // is not safe for callers to change this underlying socket.
249 return ERR_NOT_IMPLEMENTED
;
252 int SpdyProxyClientSocket::GetPeerAddress(IPEndPoint
* address
) const {
254 return ERR_SOCKET_NOT_CONNECTED
;
255 return spdy_stream_
->GetPeerAddress(address
);
258 int SpdyProxyClientSocket::GetLocalAddress(IPEndPoint
* address
) const {
260 return ERR_SOCKET_NOT_CONNECTED
;
261 return spdy_stream_
->GetLocalAddress(address
);
264 void SpdyProxyClientSocket::LogBlockedTunnelResponse() const {
265 ProxyClientSocket::LogBlockedTunnelResponse(
266 response_
.headers
->response_code(),
268 /* is_https_proxy = */ true);
271 void SpdyProxyClientSocket::OnIOComplete(int result
) {
272 DCHECK_NE(STATE_DISCONNECTED
, next_state_
);
273 int rv
= DoLoop(result
);
274 if (rv
!= ERR_IO_PENDING
) {
275 CompletionCallback c
= read_callback_
;
276 read_callback_
.Reset();
281 int SpdyProxyClientSocket::DoLoop(int last_io_result
) {
282 DCHECK_NE(next_state_
, STATE_DISCONNECTED
);
283 int rv
= last_io_result
;
285 State state
= next_state_
;
286 next_state_
= STATE_DISCONNECTED
;
288 case STATE_GENERATE_AUTH_TOKEN
:
290 rv
= DoGenerateAuthToken();
292 case STATE_GENERATE_AUTH_TOKEN_COMPLETE
:
293 rv
= DoGenerateAuthTokenComplete(rv
);
295 case STATE_SEND_REQUEST
:
297 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST
);
298 rv
= DoSendRequest();
300 case STATE_SEND_REQUEST_COMPLETE
:
301 net_log_
.EndEventWithNetErrorCode(
302 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST
, rv
);
303 rv
= DoSendRequestComplete(rv
);
304 if (rv
>= 0 || rv
== ERR_IO_PENDING
) {
305 // Emit extra event so can use the same events as
306 // HttpProxyClientSocket.
308 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS
);
311 case STATE_READ_REPLY_COMPLETE
:
312 rv
= DoReadReplyComplete(rv
);
313 net_log_
.EndEventWithNetErrorCode(
314 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS
, rv
);
317 NOTREACHED() << "bad state";
321 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_DISCONNECTED
&&
322 next_state_
!= STATE_OPEN
);
326 int SpdyProxyClientSocket::DoGenerateAuthToken() {
327 next_state_
= STATE_GENERATE_AUTH_TOKEN_COMPLETE
;
328 return auth_
->MaybeGenerateAuthToken(
330 base::Bind(&SpdyProxyClientSocket::OnIOComplete
,
331 weak_factory_
.GetWeakPtr()),
335 int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result
) {
336 DCHECK_NE(ERR_IO_PENDING
, result
);
338 next_state_
= STATE_SEND_REQUEST
;
342 int SpdyProxyClientSocket::DoSendRequest() {
343 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
345 // Add Proxy-Authentication header if necessary.
346 HttpRequestHeaders authorization_headers
;
347 if (auth_
->HaveAuth()) {
348 auth_
->AddAuthorizationHeader(&authorization_headers
);
351 std::string request_line
;
352 HttpRequestHeaders request_headers
;
353 BuildTunnelRequest(request_
, authorization_headers
, endpoint_
, &request_line
,
357 NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS
,
358 base::Bind(&HttpRequestHeaders::NetLogCallback
,
359 base::Unretained(&request_headers
),
362 request_
.extra_headers
.MergeFrom(request_headers
);
363 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
364 CreateSpdyHeadersFromHttpRequest(request_
, request_headers
, headers
.get(),
365 spdy_stream_
->GetProtocolVersion(), true);
366 // Reset the URL to be the endpoint of the connection
367 if (spdy_stream_
->GetProtocolVersion() > 2) {
368 (*headers
)[":path"] = endpoint_
.ToString();
369 headers
->erase(":scheme");
371 (*headers
)["url"] = endpoint_
.ToString();
372 headers
->erase("scheme");
375 return spdy_stream_
->SendRequestHeaders(headers
.Pass(), MORE_DATA_TO_SEND
);
378 int SpdyProxyClientSocket::DoSendRequestComplete(int result
) {
382 // Wait for SYN_REPLY frame from the server
383 next_state_
= STATE_READ_REPLY_COMPLETE
;
384 return ERR_IO_PENDING
;
387 int SpdyProxyClientSocket::DoReadReplyComplete(int result
) {
388 // We enter this method directly from DoSendRequestComplete, since
389 // we are notified by a callback when the SYN_REPLY frame arrives
394 // Require the "HTTP/1.x" status line for SSL CONNECT.
395 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0))
396 return ERR_TUNNEL_CONNECTION_FAILED
;
399 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS
,
400 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
402 switch (response_
.headers
->response_code()) {
404 next_state_
= STATE_OPEN
;
407 case 302: // Found / Moved Temporarily
408 // Try to return a sanitized response so we can follow auth redirects.
409 // If we can't, fail the tunnel connection.
410 if (SanitizeProxyRedirect(&response_
, request_
.url
)) {
411 redirect_has_load_timing_info_
=
412 spdy_stream_
->GetLoadTimingInfo(&redirect_load_timing_info_
);
413 spdy_stream_
->DetachDelegate();
414 next_state_
= STATE_DISCONNECTED
;
415 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE
;
417 LogBlockedTunnelResponse();
418 return ERR_TUNNEL_CONNECTION_FAILED
;
421 case 407: // Proxy Authentication Required
422 next_state_
= STATE_OPEN
;
423 return HandleProxyAuthChallenge(auth_
.get(), &response_
, net_log_
);
426 // Ignore response to avoid letting the proxy impersonate the target
427 // server. (See http://crbug.com/137891.)
428 LogBlockedTunnelResponse();
429 return ERR_TUNNEL_CONNECTION_FAILED
;
433 // SpdyStream::Delegate methods:
434 // Called when SYN frame has been sent.
435 // Returns true if no more data to be sent after SYN frame.
436 void SpdyProxyClientSocket::OnRequestHeadersSent() {
437 DCHECK_EQ(next_state_
, STATE_SEND_REQUEST_COMPLETE
);
442 SpdyResponseHeadersStatus
SpdyProxyClientSocket::OnResponseHeadersUpdated(
443 const SpdyHeaderBlock
& response_headers
) {
444 // If we've already received the reply, existing headers are too late.
445 // TODO(mbelshe): figure out a way to make HEADERS frames useful after the
447 if (next_state_
!= STATE_READ_REPLY_COMPLETE
)
448 return RESPONSE_HEADERS_ARE_COMPLETE
;
451 if (!SpdyHeadersToHttpResponse(
452 response_headers
, spdy_stream_
->GetProtocolVersion(), &response_
))
453 return RESPONSE_HEADERS_ARE_INCOMPLETE
;
456 return RESPONSE_HEADERS_ARE_COMPLETE
;
459 // Called when data is received or on EOF (if |buffer| is NULL).
460 void SpdyProxyClientSocket::OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) {
462 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
,
463 buffer
->GetRemainingSize(),
464 buffer
->GetRemainingData());
465 read_buffer_queue_
.Enqueue(buffer
.Pass());
467 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, 0, NULL
);
470 if (!read_callback_
.is_null()) {
471 int rv
= PopulateUserReadBuffer(user_buffer_
->data(), user_buffer_len_
);
472 CompletionCallback c
= read_callback_
;
473 read_callback_
.Reset();
475 user_buffer_len_
= 0;
480 void SpdyProxyClientSocket::OnDataSent() {
481 DCHECK(!write_callback_
.is_null());
483 int rv
= write_buffer_len_
;
484 write_buffer_len_
= 0;
486 // Proxy write callbacks result in deep callback chains. Post to allow the
487 // stream's write callback chain to unwind (see crbug.com/355511).
488 base::MessageLoop::current()->PostTask(
490 base::Bind(ResetAndReturn(&write_callback_
), rv
));
493 void SpdyProxyClientSocket::OnClose(int status
) {
494 was_ever_used_
= spdy_stream_
->WasEverUsed();
495 spdy_stream_
.reset();
497 bool connecting
= next_state_
!= STATE_DISCONNECTED
&&
498 next_state_
< STATE_OPEN
;
499 if (next_state_
== STATE_OPEN
)
500 next_state_
= STATE_CLOSED
;
502 next_state_
= STATE_DISCONNECTED
;
504 base::WeakPtr
<SpdyProxyClientSocket
> weak_ptr
= weak_factory_
.GetWeakPtr();
505 CompletionCallback write_callback
= write_callback_
;
506 write_callback_
.Reset();
507 write_buffer_len_
= 0;
509 // If we're in the middle of connecting, we need to make sure
510 // we invoke the connect callback.
512 DCHECK(!read_callback_
.is_null());
513 CompletionCallback read_callback
= read_callback_
;
514 read_callback_
.Reset();
515 read_callback
.Run(status
);
516 } else if (!read_callback_
.is_null()) {
517 // If we have a read_callback_, the we need to make sure we call it back.
518 OnDataReceived(scoped_ptr
<SpdyBuffer
>());
520 // This may have been deleted by read_callback_, so check first.
521 if (weak_ptr
.get() && !write_callback
.is_null())
522 write_callback
.Run(ERR_CONNECTION_CLOSED
);