[email protected] | e92fa7e1 | 2012-02-16 23:31:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/spdy/spdy_proxy_client_socket.h" |
| 6 | |
| 7 | #include <algorithm> // min |
| 8 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 15 | #include "net/base/auth.h" |
| 16 | #include "net/base/io_buffer.h" |
| 17 | #include "net/base/net_util.h" |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 18 | #include "net/http/http_auth_cache.h" |
| 19 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | b104b50 | 2010-10-18 20:21:31 | [diff] [blame] | 20 | #include "net/http/http_response_headers.h" |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 21 | #include "net/http/proxy_connect_redirect_http_stream.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 22 | #include "net/spdy/spdy_http_utils.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 23 | #include "url/gurl.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 24 | |
| 25 | namespace net { |
| 26 | |
| 27 | SpdyProxyClientSocket::SpdyProxyClientSocket( |
[email protected] | d26ff35 | 2013-05-13 08:48:28 | [diff] [blame] | 28 | const base::WeakPtr<SpdyStream>& spdy_stream, |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 29 | const std::string& user_agent, |
| 30 | const HostPortPair& endpoint, |
| 31 | const GURL& url, |
| 32 | const HostPortPair& proxy_server, |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 33 | const BoundNetLog& source_net_log, |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 34 | HttpAuthCache* auth_cache, |
| 35 | HttpAuthHandlerFactory* auth_handler_factory) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 36 | : next_state_(STATE_DISCONNECTED), |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 37 | spdy_stream_(spdy_stream), |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 38 | endpoint_(endpoint), |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame^] | 39 | auth_(new HttpAuthController(HttpAuth::AUTH_PROXY, |
| 40 | GURL("https://" + proxy_server.ToString()), |
| 41 | auth_cache, |
| 42 | auth_handler_factory)), |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 43 | user_buffer_len_(0), |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 44 | write_buffer_len_(0), |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 45 | was_ever_used_(false), |
| 46 | redirect_has_load_timing_info_(false), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 47 | net_log_(BoundNetLog::Make(spdy_stream->net_log().net_log(), |
[email protected] | 0a30cf51 | 2014-05-27 20:55:18 | [diff] [blame] | 48 | NetLog::SOURCE_PROXY_CLIENT_SOCKET)), |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame^] | 49 | weak_factory_(this), |
| 50 | write_callback_weak_factory_(this) { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 51 | request_.method = "CONNECT"; |
| 52 | request_.url = url; |
| 53 | if (!user_agent.empty()) |
| 54 | request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 55 | user_agent); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 56 | |
| 57 | net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 58 | source_net_log.source().ToEventParametersCallback()); |
| 59 | net_log_.AddEvent( |
| 60 | NetLog::TYPE_SPDY_PROXY_CLIENT_SESSION, |
| 61 | spdy_stream->net_log().source().ToEventParametersCallback()); |
| 62 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 63 | spdy_stream_->SetDelegate(this); |
| 64 | was_ever_used_ = spdy_stream_->WasEverUsed(); |
| 65 | } |
| 66 | |
| 67 | SpdyProxyClientSocket::~SpdyProxyClientSocket() { |
| 68 | Disconnect(); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 69 | net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 72 | const HttpResponseInfo* SpdyProxyClientSocket::GetConnectResponseInfo() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 73 | return response_.headers.get() ? &response_ : NULL; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 76 | const scoped_refptr<HttpAuthController>& |
| 77 | SpdyProxyClientSocket::GetAuthController() const { |
| 78 | return auth_; |
| 79 | } |
| 80 | |
| 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; |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 86 | return OK; |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | bool SpdyProxyClientSocket::IsUsingSpdy() const { |
| 90 | return true; |
| 91 | } |
| 92 | |
[email protected] | 8e3c78cb | 2012-03-31 03:58:46 | [diff] [blame] | 93 | NextProto SpdyProxyClientSocket::GetProtocolNegotiated() const { |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 94 | // Save the negotiated protocol |
| 95 | SSLInfo ssl_info; |
| 96 | bool was_npn_negotiated; |
[email protected] | 8e3c78cb | 2012-03-31 03:58:46 | [diff] [blame] | 97 | NextProto protocol_negotiated; |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 98 | spdy_stream_->GetSSLInfo(&ssl_info, &was_npn_negotiated, |
| 99 | &protocol_negotiated); |
| 100 | return protocol_negotiated; |
| 101 | } |
| 102 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 103 | HttpStream* SpdyProxyClientSocket::CreateConnectResponseStream() { |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 104 | return new ProxyConnectRedirectHttpStream( |
| 105 | redirect_has_load_timing_info_ ? &redirect_load_timing_info_ : NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 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. |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 114 | // TODO(rch): handle 407 proxy auth requested correctly, perhaps |
| 115 | // by creating a new stream for the subsequent request. |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 116 | // TODO(rch): create a more appropriate error code to disambiguate |
| 117 | // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure. |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 118 | int SpdyProxyClientSocket::Connect(const CompletionCallback& callback) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 119 | DCHECK(read_callback_.is_null()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 120 | if (next_state_ == STATE_OPEN) |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 121 | return OK; |
| 122 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 123 | DCHECK_EQ(STATE_DISCONNECTED, next_state_); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 124 | next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 125 | |
| 126 | int rv = DoLoop(OK); |
| 127 | if (rv == ERR_IO_PENDING) |
| 128 | read_callback_ = callback; |
| 129 | return rv; |
| 130 | } |
| 131 | |
| 132 | void SpdyProxyClientSocket::Disconnect() { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 133 | read_buffer_queue_.Clear(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 134 | user_buffer_ = NULL; |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 135 | user_buffer_len_ = 0; |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 136 | read_callback_.Reset(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 137 | |
| 138 | write_buffer_len_ = 0; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 139 | write_callback_.Reset(); |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame^] | 140 | write_callback_weak_factory_.InvalidateWeakPtrs(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 141 | |
| 142 | next_state_ = STATE_DISCONNECTED; |
| 143 | |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 144 | if (spdy_stream_.get()) { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 145 | // This will cause OnClose to be invoked, which takes care of |
| 146 | // cleaning up all the internal state. |
| 147 | spdy_stream_->Cancel(); |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 148 | DCHECK(!spdy_stream_.get()); |
[email protected] | f6a7829 | 2013-03-09 14:36:34 | [diff] [blame] | 149 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | bool SpdyProxyClientSocket::IsConnected() const { |
[email protected] | 194c7a9 | 2011-12-03 04:54:18 | [diff] [blame] | 153 | return next_state_ == STATE_OPEN; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool SpdyProxyClientSocket::IsConnectedAndIdle() const { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 157 | return IsConnected() && read_buffer_queue_.IsEmpty() && |
[email protected] | 5ff0ed3 | 2014-02-12 17:48:51 | [diff] [blame] | 158 | spdy_stream_->IsOpen(); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 161 | const BoundNetLog& SpdyProxyClientSocket::NetLog() const { |
| 162 | return net_log_; |
| 163 | } |
| 164 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 165 | void SpdyProxyClientSocket::SetSubresourceSpeculation() { |
| 166 | // TODO(rch): what should this implementation be? |
| 167 | } |
| 168 | |
| 169 | void SpdyProxyClientSocket::SetOmniboxSpeculation() { |
| 170 | // TODO(rch): what should this implementation be? |
| 171 | } |
| 172 | |
| 173 | bool SpdyProxyClientSocket::WasEverUsed() const { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 174 | return was_ever_used_ || (spdy_stream_.get() && spdy_stream_->WasEverUsed()); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 177 | bool SpdyProxyClientSocket::UsingTCPFastOpen() const { |
| 178 | return false; |
| 179 | } |
| 180 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 181 | bool SpdyProxyClientSocket::WasNpnNegotiated() const { |
| 182 | return false; |
| 183 | } |
| 184 | |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 185 | NextProto SpdyProxyClientSocket::GetNegotiatedProtocol() const { |
| 186 | return kProtoUnknown; |
| 187 | } |
| 188 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 189 | bool SpdyProxyClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
| 190 | bool was_npn_negotiated; |
| 191 | NextProto protocol_negotiated; |
| 192 | return spdy_stream_->GetSSLInfo(ssl_info, &was_npn_negotiated, |
| 193 | &protocol_negotiated); |
| 194 | } |
| 195 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 196 | int SpdyProxyClientSocket::Read(IOBuffer* buf, int buf_len, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 197 | const CompletionCallback& callback) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 198 | DCHECK(read_callback_.is_null()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 199 | DCHECK(!user_buffer_.get()); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 200 | |
| 201 | if (next_state_ == STATE_DISCONNECTED) |
| 202 | return ERR_SOCKET_NOT_CONNECTED; |
| 203 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 204 | if (next_state_ == STATE_CLOSED && read_buffer_queue_.IsEmpty()) { |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | DCHECK(next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED); |
| 209 | DCHECK(buf); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 210 | size_t result = PopulateUserReadBuffer(buf->data(), buf_len); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 211 | if (result == 0) { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 212 | user_buffer_ = buf; |
| 213 | user_buffer_len_ = static_cast<size_t>(buf_len); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 214 | DCHECK(!callback.is_null()); |
| 215 | read_callback_ = callback; |
| 216 | return ERR_IO_PENDING; |
| 217 | } |
| 218 | user_buffer_ = NULL; |
| 219 | return result; |
| 220 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 221 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 222 | size_t SpdyProxyClientSocket::PopulateUserReadBuffer(char* data, size_t len) { |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 223 | return read_buffer_queue_.Dequeue(data, len); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | int SpdyProxyClientSocket::Write(IOBuffer* buf, int buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 227 | const CompletionCallback& callback) { |
| 228 | DCHECK(write_callback_.is_null()); |
[email protected] | 194c7a9 | 2011-12-03 04:54:18 | [diff] [blame] | 229 | if (next_state_ != STATE_OPEN) |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 230 | return ERR_SOCKET_NOT_CONNECTED; |
| 231 | |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 232 | DCHECK(spdy_stream_.get()); |
[email protected] | edbfa8c | 2013-05-29 00:22:33 | [diff] [blame] | 233 | spdy_stream_->SendData(buf, buf_len, MORE_DATA_TO_SEND); |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 234 | net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, |
| 235 | buf_len, buf->data()); |
| 236 | write_callback_ = callback; |
| 237 | write_buffer_len_ = buf_len; |
| 238 | return ERR_IO_PENDING; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 241 | int SpdyProxyClientSocket::SetReceiveBufferSize(int32 size) { |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 242 | // Since this StreamSocket sits on top of a shared SpdySession, it |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 243 | // is not safe for callers to change this underlying socket. |
| 244 | return ERR_NOT_IMPLEMENTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 247 | int SpdyProxyClientSocket::SetSendBufferSize(int32 size) { |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 248 | // Since this StreamSocket sits on top of a shared SpdySession, it |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 249 | // is not safe for callers to change this underlying socket. |
| 250 | return ERR_NOT_IMPLEMENTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 253 | int SpdyProxyClientSocket::GetPeerAddress(IPEndPoint* address) const { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 254 | if (!IsConnected()) |
[email protected] | 88e03fa | 2010-10-05 03:09:04 | [diff] [blame] | 255 | return ERR_SOCKET_NOT_CONNECTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 256 | return spdy_stream_->GetPeerAddress(address); |
| 257 | } |
| 258 | |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 259 | int SpdyProxyClientSocket::GetLocalAddress(IPEndPoint* address) const { |
| 260 | if (!IsConnected()) |
| 261 | return ERR_SOCKET_NOT_CONNECTED; |
| 262 | return spdy_stream_->GetLocalAddress(address); |
| 263 | } |
| 264 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 265 | void SpdyProxyClientSocket::LogBlockedTunnelResponse() const { |
| 266 | ProxyClientSocket::LogBlockedTunnelResponse( |
| 267 | response_.headers->response_code(), |
| 268 | request_.url, |
| 269 | /* is_https_proxy = */ true); |
| 270 | } |
| 271 | |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame^] | 272 | void SpdyProxyClientSocket::RunCallback(const CompletionCallback& callback, |
| 273 | int result) const { |
| 274 | callback.Run(result); |
| 275 | } |
| 276 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 277 | void SpdyProxyClientSocket::OnIOComplete(int result) { |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 278 | DCHECK_NE(STATE_DISCONNECTED, next_state_); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 279 | int rv = DoLoop(result); |
| 280 | if (rv != ERR_IO_PENDING) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 281 | CompletionCallback c = read_callback_; |
| 282 | read_callback_.Reset(); |
| 283 | c.Run(rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | int SpdyProxyClientSocket::DoLoop(int last_io_result) { |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 288 | DCHECK_NE(next_state_, STATE_DISCONNECTED); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 289 | int rv = last_io_result; |
| 290 | do { |
| 291 | State state = next_state_; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 292 | next_state_ = STATE_DISCONNECTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 293 | switch (state) { |
| 294 | case STATE_GENERATE_AUTH_TOKEN: |
| 295 | DCHECK_EQ(OK, rv); |
| 296 | rv = DoGenerateAuthToken(); |
| 297 | break; |
| 298 | case STATE_GENERATE_AUTH_TOKEN_COMPLETE: |
| 299 | rv = DoGenerateAuthTokenComplete(rv); |
| 300 | break; |
| 301 | case STATE_SEND_REQUEST: |
| 302 | DCHECK_EQ(OK, rv); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 303 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 304 | rv = DoSendRequest(); |
| 305 | break; |
| 306 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 307 | net_log_.EndEventWithNetErrorCode( |
| 308 | NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 309 | rv = DoSendRequestComplete(rv); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 310 | if (rv >= 0 || rv == ERR_IO_PENDING) { |
| 311 | // Emit extra event so can use the same events as |
| 312 | // HttpProxyClientSocket. |
| 313 | net_log_.BeginEvent( |
| 314 | NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS); |
| 315 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 316 | break; |
| 317 | case STATE_READ_REPLY_COMPLETE: |
| 318 | rv = DoReadReplyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 319 | net_log_.EndEventWithNetErrorCode( |
| 320 | NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 321 | break; |
| 322 | default: |
| 323 | NOTREACHED() << "bad state"; |
| 324 | rv = ERR_UNEXPECTED; |
| 325 | break; |
| 326 | } |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 327 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_DISCONNECTED && |
| 328 | next_state_ != STATE_OPEN); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 329 | return rv; |
| 330 | } |
| 331 | |
| 332 | int SpdyProxyClientSocket::DoGenerateAuthToken() { |
| 333 | next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 334 | return auth_->MaybeGenerateAuthToken( |
| 335 | &request_, |
[email protected] | f6a7829 | 2013-03-09 14:36:34 | [diff] [blame] | 336 | base::Bind(&SpdyProxyClientSocket::OnIOComplete, |
| 337 | weak_factory_.GetWeakPtr()), |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 338 | net_log_); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) { |
| 342 | DCHECK_NE(ERR_IO_PENDING, result); |
| 343 | if (result == OK) |
| 344 | next_state_ = STATE_SEND_REQUEST; |
| 345 | return result; |
| 346 | } |
| 347 | |
| 348 | int SpdyProxyClientSocket::DoSendRequest() { |
| 349 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 350 | |
| 351 | // Add Proxy-Authentication header if necessary. |
| 352 | HttpRequestHeaders authorization_headers; |
| 353 | if (auth_->HaveAuth()) { |
| 354 | auth_->AddAuthorizationHeader(&authorization_headers); |
| 355 | } |
| 356 | |
| 357 | std::string request_line; |
| 358 | HttpRequestHeaders request_headers; |
| 359 | BuildTunnelRequest(request_, authorization_headers, endpoint_, &request_line, |
| 360 | &request_headers); |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 361 | |
| 362 | net_log_.AddEvent( |
| 363 | NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 364 | base::Bind(&HttpRequestHeaders::NetLogCallback, |
| 365 | base::Unretained(&request_headers), |
| 366 | &request_line)); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 367 | |
| 368 | request_.extra_headers.MergeFrom(request_headers); |
[email protected] | 0a01d890fc | 2012-07-18 17:24:05 | [diff] [blame] | 369 | scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock()); |
[email protected] | 7213e7c | 2010-10-20 15:33:52 | [diff] [blame] | 370 | CreateSpdyHeadersFromHttpRequest(request_, request_headers, headers.get(), |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 371 | spdy_stream_->GetProtocolVersion(), true); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 372 | // Reset the URL to be the endpoint of the connection |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 373 | if (spdy_stream_->GetProtocolVersion() > 2) { |
| 374 | (*headers)[":path"] = endpoint_.ToString(); |
| 375 | headers->erase(":scheme"); |
| 376 | } else { |
| 377 | (*headers)["url"] = endpoint_.ToString(); |
| 378 | headers->erase("scheme"); |
| 379 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 380 | |
[email protected] | 528b345 | 2013-05-25 01:28:54 | [diff] [blame] | 381 | return spdy_stream_->SendRequestHeaders(headers.Pass(), MORE_DATA_TO_SEND); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | int SpdyProxyClientSocket::DoSendRequestComplete(int result) { |
| 385 | if (result < 0) |
| 386 | return result; |
| 387 | |
| 388 | // Wait for SYN_REPLY frame from the server |
| 389 | next_state_ = STATE_READ_REPLY_COMPLETE; |
| 390 | return ERR_IO_PENDING; |
| 391 | } |
| 392 | |
| 393 | int SpdyProxyClientSocket::DoReadReplyComplete(int result) { |
| 394 | // We enter this method directly from DoSendRequestComplete, since |
| 395 | // we are notified by a callback when the SYN_REPLY frame arrives |
| 396 | |
| 397 | if (result < 0) |
| 398 | return result; |
| 399 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 400 | // Require the "HTTP/1.x" status line for SSL CONNECT. |
| 401 | if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) |
| 402 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 403 | |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 404 | net_log_.AddEvent( |
| 405 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 406 | base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers)); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 407 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 408 | switch (response_.headers->response_code()) { |
| 409 | case 200: // OK |
| 410 | next_state_ = STATE_OPEN; |
| 411 | return OK; |
| 412 | |
| 413 | case 302: // Found / Moved Temporarily |
| 414 | // Try to return a sanitized response so we can follow auth redirects. |
| 415 | // If we can't, fail the tunnel connection. |
| 416 | if (SanitizeProxyRedirect(&response_, request_.url)) { |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 417 | redirect_has_load_timing_info_ = |
| 418 | spdy_stream_->GetLoadTimingInfo(&redirect_load_timing_info_); |
[email protected] | 975da41a | 2014-06-05 03:36:24 | [diff] [blame] | 419 | // Note that this triggers a RST_STREAM_CANCEL. |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 420 | spdy_stream_->DetachDelegate(); |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 421 | next_state_ = STATE_DISCONNECTED; |
| 422 | return ERR_HTTPS_PROXY_TUNNEL_RESPONSE; |
| 423 | } else { |
| 424 | LogBlockedTunnelResponse(); |
| 425 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 426 | } |
| 427 | |
| 428 | case 407: // Proxy Authentication Required |
| 429 | next_state_ = STATE_OPEN; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 430 | return HandleProxyAuthChallenge(auth_.get(), &response_, net_log_); |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 431 | |
| 432 | default: |
| 433 | // Ignore response to avoid letting the proxy impersonate the target |
| 434 | // server. (See http://crbug.com/137891.) |
| 435 | LogBlockedTunnelResponse(); |
| 436 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 437 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // SpdyStream::Delegate methods: |
| 441 | // Called when SYN frame has been sent. |
| 442 | // Returns true if no more data to be sent after SYN frame. |
[email protected] | edbfa8c | 2013-05-29 00:22:33 | [diff] [blame] | 443 | void SpdyProxyClientSocket::OnRequestHeadersSent() { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 444 | DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE); |
| 445 | |
[email protected] | d46715c | 2013-04-15 00:21:42 | [diff] [blame] | 446 | OnIOComplete(OK); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 447 | } |
| 448 | |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 449 | SpdyResponseHeadersStatus SpdyProxyClientSocket::OnResponseHeadersUpdated( |
| 450 | const SpdyHeaderBlock& response_headers) { |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 451 | // If we've already received the reply, existing headers are too late. |
| 452 | // TODO(mbelshe): figure out a way to make HEADERS frames useful after the |
| 453 | // initial response. |
| 454 | if (next_state_ != STATE_READ_REPLY_COMPLETE) |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 455 | return RESPONSE_HEADERS_ARE_COMPLETE; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 456 | |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 457 | // Save the response |
[email protected] | d42dedd0 | 2012-04-03 19:42:06 | [diff] [blame] | 458 | if (!SpdyHeadersToHttpResponse( |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 459 | response_headers, spdy_stream_->GetProtocolVersion(), &response_)) |
| 460 | return RESPONSE_HEADERS_ARE_INCOMPLETE; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 461 | |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 462 | OnIOComplete(OK); |
| 463 | return RESPONSE_HEADERS_ARE_COMPLETE; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 466 | // Called when data is received or on EOF (if |buffer| is NULL). |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 467 | void SpdyProxyClientSocket::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 468 | if (buffer) { |
| 469 | net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, |
| 470 | buffer->GetRemainingSize(), |
| 471 | buffer->GetRemainingData()); |
| 472 | read_buffer_queue_.Enqueue(buffer.Pass()); |
| 473 | } else { |
| 474 | net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, 0, NULL); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 477 | if (!read_callback_.is_null()) { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 478 | int rv = PopulateUserReadBuffer(user_buffer_->data(), user_buffer_len_); |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 479 | CompletionCallback c = read_callback_; |
| 480 | read_callback_.Reset(); |
| 481 | user_buffer_ = NULL; |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 482 | user_buffer_len_ = 0; |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 483 | c.Run(rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 487 | void SpdyProxyClientSocket::OnDataSent() { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 488 | DCHECK(!write_callback_.is_null()); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 489 | |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 490 | int rv = write_buffer_len_; |
| 491 | write_buffer_len_ = 0; |
[email protected] | 89d4d79 | 2014-04-06 16:27:15 | [diff] [blame] | 492 | |
| 493 | // Proxy write callbacks result in deep callback chains. Post to allow the |
| 494 | // stream's write callback chain to unwind (see crbug.com/355511). |
| 495 | base::MessageLoop::current()->PostTask( |
| 496 | FROM_HERE, |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame^] | 497 | base::Bind(&SpdyProxyClientSocket::RunCallback, |
| 498 | write_callback_weak_factory_.GetWeakPtr(), |
| 499 | ResetAndReturn(&write_callback_), |
| 500 | rv)); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void SpdyProxyClientSocket::OnClose(int status) { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 504 | was_ever_used_ = spdy_stream_->WasEverUsed(); |
[email protected] | d26ff35 | 2013-05-13 08:48:28 | [diff] [blame] | 505 | spdy_stream_.reset(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 506 | |
| 507 | bool connecting = next_state_ != STATE_DISCONNECTED && |
| 508 | next_state_ < STATE_OPEN; |
| 509 | if (next_state_ == STATE_OPEN) |
| 510 | next_state_ = STATE_CLOSED; |
| 511 | else |
| 512 | next_state_ = STATE_DISCONNECTED; |
| 513 | |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 514 | base::WeakPtr<SpdyProxyClientSocket> weak_ptr = weak_factory_.GetWeakPtr(); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 515 | CompletionCallback write_callback = write_callback_; |
| 516 | write_callback_.Reset(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 517 | write_buffer_len_ = 0; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 518 | |
| 519 | // If we're in the middle of connecting, we need to make sure |
| 520 | // we invoke the connect callback. |
| 521 | if (connecting) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 522 | DCHECK(!read_callback_.is_null()); |
| 523 | CompletionCallback read_callback = read_callback_; |
| 524 | read_callback_.Reset(); |
| 525 | read_callback.Run(status); |
| 526 | } else if (!read_callback_.is_null()) { |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 527 | // If we have a read_callback_, the we need to make sure we call it back. |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 528 | OnDataReceived(scoped_ptr<SpdyBuffer>()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 529 | } |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 530 | // This may have been deleted by read_callback_, so check first. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 531 | if (weak_ptr.get() && !write_callback.is_null()) |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 532 | write_callback.Run(ERR_CONNECTION_CLOSED); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 533 | } |
| 534 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 535 | } // namespace net |