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