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