[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 | |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 5 | #include "net/spdy/spdy_proxy_client_socket.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 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" |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 12 | #include "base/check_op.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 13 | #include "base/location.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 14 | #include "base/notreached.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 16 | #include "base/task/single_thread_task_runner.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" |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 21 | #include "net/base/proxy_delegate.h" |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 22 | #include "net/http/http_auth_cache.h" |
| 23 | #include "net/http/http_auth_handler_factory.h" |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 24 | #include "net/http/http_log_util.h" |
rch | c5c07de | 2015-04-08 07:28:18 | [diff] [blame] | 25 | #include "net/http/http_request_info.h" |
[email protected] | b104b50 | 2010-10-18 20:21:31 | [diff] [blame] | 26 | #include "net/http/http_response_headers.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 27 | #include "net/log/net_log_event_type.h" |
| 28 | #include "net/log/net_log_source_type.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 29 | #include "net/spdy/spdy_http_utils.h" |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 30 | #include "net/traffic_annotation/network_traffic_annotation.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 31 | #include "url/gurl.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 32 | |
| 33 | namespace net { |
| 34 | |
| 35 | SpdyProxyClientSocket::SpdyProxyClientSocket( |
[email protected] | d26ff35 | 2013-05-13 08:48:28 | [diff] [blame] | 36 | const base::WeakPtr<SpdyStream>& spdy_stream, |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 37 | const ProxyServer& proxy_server, |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 38 | const std::string& user_agent, |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 39 | const HostPortPair& endpoint, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 40 | const NetLogWithSource& source_net_log, |
Tsuyoshi Horo | 5c93598 | 2022-07-11 02:01:42 | [diff] [blame^] | 41 | scoped_refptr<HttpAuthController> auth_controller, |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 42 | ProxyDelegate* proxy_delegate) |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 43 | : spdy_stream_(spdy_stream), |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 44 | endpoint_(endpoint), |
Tsuyoshi Horo | 5c93598 | 2022-07-11 02:01:42 | [diff] [blame^] | 45 | auth_(std::move(auth_controller)), |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 46 | proxy_server_(proxy_server), |
| 47 | proxy_delegate_(proxy_delegate), |
rch | ecd3c55 | 2015-04-07 20:53:54 | [diff] [blame] | 48 | user_agent_(user_agent), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 49 | net_log_(NetLogWithSource::Make(spdy_stream->net_log().net_log(), |
| 50 | NetLogSourceType::PROXY_CLIENT_SOCKET)), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 51 | source_dependency_(source_net_log.source()) { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 52 | request_.method = "CONNECT"; |
rch | ecd3c55 | 2015-04-07 20:53:54 | [diff] [blame] | 53 | request_.url = GURL("https://" + endpoint.ToString()); |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 54 | net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE, |
| 55 | source_net_log.source()); |
| 56 | net_log_.AddEventReferencingSource( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 57 | NetLogEventType::HTTP2_PROXY_CLIENT_SESSION, |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 58 | spdy_stream->net_log().source()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 59 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 60 | spdy_stream_->SetDelegate(this); |
| 61 | was_ever_used_ = spdy_stream_->WasEverUsed(); |
| 62 | } |
| 63 | |
| 64 | SpdyProxyClientSocket::~SpdyProxyClientSocket() { |
| 65 | Disconnect(); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 66 | net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 69 | const HttpResponseInfo* SpdyProxyClientSocket::GetConnectResponseInfo() const { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 70 | return response_.headers.get() ? &response_ : nullptr; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 73 | const scoped_refptr<HttpAuthController>& |
| 74 | SpdyProxyClientSocket::GetAuthController() const { |
| 75 | return auth_; |
| 76 | } |
| 77 | |
Bence Béky | 9983223 | 2018-02-11 04:21:00 | [diff] [blame] | 78 | int SpdyProxyClientSocket::RestartWithAuth(CompletionOnceCallback callback) { |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 79 | // A SPDY Stream can only handle a single request, so the underlying |
| 80 | // stream may not be reused and a new SpdyProxyClientSocket must be |
| 81 | // created (possibly on top of the same SPDY Session). |
| 82 | next_state_ = STATE_DISCONNECTED; |
Matt Menke | ae2cbb8 | 2019-02-21 20:20:13 | [diff] [blame] | 83 | return ERR_UNABLE_TO_REUSE_CONNECTION_FOR_PROXY_AUTH; |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 84 | } |
| 85 | |
Matt Menke | edaf3b8 | 2019-03-14 21:39:44 | [diff] [blame] | 86 | // Ignore priority changes, just use priority of initial request. Since multiple |
| 87 | // requests are pooled on the SpdyProxyClientSocket, reprioritization doesn't |
| 88 | // really work. |
| 89 | // |
| 90 | // TODO(mmenke): Use a single priority value for all SpdyProxyClientSockets, |
| 91 | // regardless of what priority they're created with. |
| 92 | void SpdyProxyClientSocket::SetStreamPriority(RequestPriority priority) {} |
Lily Chen | f11e129 | 2018-11-29 16:42:09 | [diff] [blame] | 93 | |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 94 | // Sends a HEADERS frame to the proxy with a CONNECT request |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 95 | // for the specified endpoint. Waits for the server to send back |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 96 | // a HEADERS frame. OK will be returned if the status is 200. |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 97 | // ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status. |
| 98 | // In any of these cases, Read() may be called to retrieve the HTTP |
| 99 | // response body. Any other return values should be considered fatal. |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 100 | // TODO(rch): handle 407 proxy auth requested correctly, perhaps |
| 101 | // by creating a new stream for the subsequent request. |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 102 | // TODO(rch): create a more appropriate error code to disambiguate |
| 103 | // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 104 | int SpdyProxyClientSocket::Connect(CompletionOnceCallback callback) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 105 | DCHECK(read_callback_.is_null()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 106 | if (next_state_ == STATE_OPEN) |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 107 | return OK; |
| 108 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 109 | DCHECK_EQ(STATE_DISCONNECTED, next_state_); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 110 | next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 111 | |
| 112 | int rv = DoLoop(OK); |
| 113 | if (rv == ERR_IO_PENDING) |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 114 | read_callback_ = std::move(callback); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 115 | return rv; |
| 116 | } |
| 117 | |
| 118 | void SpdyProxyClientSocket::Disconnect() { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 119 | read_buffer_queue_.Clear(); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 120 | user_buffer_ = nullptr; |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 121 | user_buffer_len_ = 0; |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 122 | read_callback_.Reset(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 123 | |
| 124 | write_buffer_len_ = 0; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 125 | write_callback_.Reset(); |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame] | 126 | write_callback_weak_factory_.InvalidateWeakPtrs(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 127 | |
| 128 | next_state_ = STATE_DISCONNECTED; |
| 129 | |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 130 | if (spdy_stream_.get()) { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 131 | // This will cause OnClose to be invoked, which takes care of |
| 132 | // cleaning up all the internal state. |
Bence Béky | 6b9c135 | 2018-05-10 11:51:25 | [diff] [blame] | 133 | spdy_stream_->Cancel(ERR_ABORTED); |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 134 | DCHECK(!spdy_stream_.get()); |
[email protected] | f6a7829 | 2013-03-09 14:36:34 | [diff] [blame] | 135 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool SpdyProxyClientSocket::IsConnected() const { |
[email protected] | 194c7a9 | 2011-12-03 04:54:18 | [diff] [blame] | 139 | return next_state_ == STATE_OPEN; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool SpdyProxyClientSocket::IsConnectedAndIdle() const { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 143 | return IsConnected() && read_buffer_queue_.IsEmpty() && |
[email protected] | 5ff0ed3 | 2014-02-12 17:48:51 | [diff] [blame] | 144 | spdy_stream_->IsOpen(); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 145 | } |
| 146 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 147 | const NetLogWithSource& SpdyProxyClientSocket::NetLog() const { |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 148 | return net_log_; |
| 149 | } |
| 150 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 151 | bool SpdyProxyClientSocket::WasEverUsed() const { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 152 | return was_ever_used_ || (spdy_stream_.get() && spdy_stream_->WasEverUsed()); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 153 | } |
| 154 | |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 155 | bool SpdyProxyClientSocket::WasAlpnNegotiated() const { |
David Benjamin | 854a992 | 2022-01-22 01:25:10 | [diff] [blame] | 156 | // Do not delegate to `spdy_stream_`. While `spdy_stream_` negotiated ALPN |
| 157 | // with the proxy, this object represents the tunneled TCP connection to the |
| 158 | // origin. |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 162 | NextProto SpdyProxyClientSocket::GetNegotiatedProtocol() const { |
David Benjamin | 854a992 | 2022-01-22 01:25:10 | [diff] [blame] | 163 | // Do not delegate to `spdy_stream_`. While `spdy_stream_` negotiated ALPN |
| 164 | // with the proxy, this object represents the tunneled TCP connection to the |
| 165 | // origin. |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 166 | return kProtoUnknown; |
| 167 | } |
| 168 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 169 | bool SpdyProxyClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
David Benjamin | 854a992 | 2022-01-22 01:25:10 | [diff] [blame] | 170 | // Do not delegate to `spdy_stream_`. While `spdy_stream_` connected to the |
| 171 | // proxy with TLS, this object represents the tunneled TCP connection to the |
| 172 | // origin. |
| 173 | return false; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 174 | } |
| 175 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 176 | int64_t SpdyProxyClientSocket::GetTotalReceivedBytes() const { |
| 177 | NOTIMPLEMENTED(); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 181 | void SpdyProxyClientSocket::ApplySocketTag(const SocketTag& tag) { |
Matt Menke | 5b7974fa | 2019-03-09 01:18:12 | [diff] [blame] | 182 | // In the case of a connection to the proxy using HTTP/2 or HTTP/3 where the |
| 183 | // underlying socket may multiplex multiple streams, applying this request's |
| 184 | // socket tag to the multiplexed session would incorrectly apply the socket |
| 185 | // tag to all mutliplexed streams. Fortunately socket tagging is only |
| 186 | // supported on Android without the data reduction proxy, so only simple HTTP |
| 187 | // proxies are supported, so proxies won't be using HTTP/2 or HTTP/3. Enforce |
| 188 | // that a specific (non-default) tag isn't being applied. |
| 189 | CHECK(tag == SocketTag()); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 190 | } |
| 191 | |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 192 | int SpdyProxyClientSocket::Read(IOBuffer* buf, |
| 193 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 194 | CompletionOnceCallback callback) { |
Helen Li | d714188 | 2018-08-22 21:41:17 | [diff] [blame] | 195 | int rv = ReadIfReady(buf, buf_len, std::move(callback)); |
| 196 | if (rv == ERR_IO_PENDING) { |
| 197 | user_buffer_ = buf; |
| 198 | user_buffer_len_ = static_cast<size_t>(buf_len); |
| 199 | } |
| 200 | return rv; |
| 201 | } |
| 202 | |
| 203 | int SpdyProxyClientSocket::ReadIfReady(IOBuffer* buf, |
| 204 | int buf_len, |
| 205 | CompletionOnceCallback callback) { |
| 206 | DCHECK(!read_callback_); |
| 207 | DCHECK(!user_buffer_); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 208 | |
| 209 | if (next_state_ == STATE_DISCONNECTED) |
| 210 | return ERR_SOCKET_NOT_CONNECTED; |
| 211 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 212 | if (next_state_ == STATE_CLOSED && read_buffer_queue_.IsEmpty()) { |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | DCHECK(next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED); |
| 217 | DCHECK(buf); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 218 | size_t result = PopulateUserReadBuffer(buf->data(), buf_len); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 219 | if (result == 0) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 220 | read_callback_ = std::move(callback); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 221 | return ERR_IO_PENDING; |
| 222 | } |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 223 | return result; |
| 224 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 225 | |
Helen Li | d714188 | 2018-08-22 21:41:17 | [diff] [blame] | 226 | int SpdyProxyClientSocket::CancelReadIfReady() { |
| 227 | // Only a pending ReadIfReady() can be canceled. |
| 228 | DCHECK(!user_buffer_) << "Pending Read() cannot be canceled"; |
| 229 | read_callback_.Reset(); |
| 230 | return OK; |
| 231 | } |
| 232 | |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 233 | size_t SpdyProxyClientSocket::PopulateUserReadBuffer(char* data, size_t len) { |
[email protected] | 09a8d917 | 2013-04-17 19:23:49 | [diff] [blame] | 234 | return read_buffer_queue_.Dequeue(data, len); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 237 | int SpdyProxyClientSocket::Write( |
| 238 | IOBuffer* buf, |
| 239 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 240 | CompletionOnceCallback callback, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 241 | const NetworkTrafficAnnotationTag& traffic_annotation) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 242 | DCHECK(write_callback_.is_null()); |
[email protected] | 194c7a9 | 2011-12-03 04:54:18 | [diff] [blame] | 243 | if (next_state_ != STATE_OPEN) |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 244 | return ERR_SOCKET_NOT_CONNECTED; |
Kenichi Ishibashi | 43fb0f7 | 2022-05-06 00:08:32 | [diff] [blame] | 245 | if (end_stream_state_ == EndStreamState::kEndStreamSent) |
| 246 | return ERR_CONNECTION_CLOSED; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 247 | |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 248 | DCHECK(spdy_stream_.get()); |
[email protected] | edbfa8c | 2013-05-29 00:22:33 | [diff] [blame] | 249 | spdy_stream_->SendData(buf, buf_len, MORE_DATA_TO_SEND); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 250 | net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_SENT, buf_len, |
| 251 | buf->data()); |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 252 | write_callback_ = std::move(callback); |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 253 | write_buffer_len_ = buf_len; |
| 254 | return ERR_IO_PENDING; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 255 | } |
| 256 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 257 | int SpdyProxyClientSocket::SetReceiveBufferSize(int32_t size) { |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 258 | // Since this StreamSocket sits on top of a shared SpdySession, it |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 259 | // is not safe for callers to change this underlying socket. |
| 260 | return ERR_NOT_IMPLEMENTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 261 | } |
| 262 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 263 | int SpdyProxyClientSocket::SetSendBufferSize(int32_t size) { |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 264 | // Since this StreamSocket sits on top of a shared SpdySession, it |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 265 | // is not safe for callers to change this underlying socket. |
| 266 | return ERR_NOT_IMPLEMENTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 269 | int SpdyProxyClientSocket::GetPeerAddress(IPEndPoint* address) const { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 270 | if (!IsConnected()) |
[email protected] | 88e03fa | 2010-10-05 03:09:04 | [diff] [blame] | 271 | return ERR_SOCKET_NOT_CONNECTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 272 | return spdy_stream_->GetPeerAddress(address); |
| 273 | } |
| 274 | |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 275 | int SpdyProxyClientSocket::GetLocalAddress(IPEndPoint* address) const { |
| 276 | if (!IsConnected()) |
| 277 | return ERR_SOCKET_NOT_CONNECTED; |
| 278 | return spdy_stream_->GetLocalAddress(address); |
| 279 | } |
| 280 | |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 281 | void SpdyProxyClientSocket::RunWriteCallback(CompletionOnceCallback callback, |
| 282 | int result) const { |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 283 | std::move(callback).Run(result); |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 284 | |
| 285 | if (end_stream_state_ == EndStreamState::kEndStreamReceived) { |
| 286 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 287 | FROM_HERE, base::BindOnce(&SpdyProxyClientSocket::MaybeSendEndStream, |
| 288 | weak_factory_.GetWeakPtr())); |
| 289 | } |
[email protected] | 0a428c2 | 2014-06-14 02:26:37 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 292 | void SpdyProxyClientSocket::OnIOComplete(int result) { |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 293 | DCHECK_NE(STATE_DISCONNECTED, next_state_); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 294 | int rv = DoLoop(result); |
| 295 | if (rv != ERR_IO_PENDING) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 296 | std::move(read_callback_).Run(rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | int SpdyProxyClientSocket::DoLoop(int last_io_result) { |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 301 | DCHECK_NE(next_state_, STATE_DISCONNECTED); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 302 | int rv = last_io_result; |
| 303 | do { |
| 304 | State state = next_state_; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 305 | next_state_ = STATE_DISCONNECTED; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 306 | switch (state) { |
| 307 | case STATE_GENERATE_AUTH_TOKEN: |
| 308 | DCHECK_EQ(OK, rv); |
| 309 | rv = DoGenerateAuthToken(); |
| 310 | break; |
| 311 | case STATE_GENERATE_AUTH_TOKEN_COMPLETE: |
| 312 | rv = DoGenerateAuthTokenComplete(rv); |
| 313 | break; |
| 314 | case STATE_SEND_REQUEST: |
| 315 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 316 | net_log_.BeginEvent( |
| 317 | NetLogEventType::HTTP_TRANSACTION_TUNNEL_SEND_REQUEST); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 318 | rv = DoSendRequest(); |
| 319 | break; |
| 320 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 321 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 322 | NetLogEventType::HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 323 | rv = DoSendRequestComplete(rv); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 324 | if (rv >= 0 || rv == ERR_IO_PENDING) { |
| 325 | // Emit extra event so can use the same events as |
| 326 | // HttpProxyClientSocket. |
| 327 | net_log_.BeginEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 328 | NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 329 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 330 | break; |
| 331 | case STATE_READ_REPLY_COMPLETE: |
| 332 | rv = DoReadReplyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 333 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 334 | NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | NOTREACHED() << "bad state"; |
| 338 | rv = ERR_UNEXPECTED; |
| 339 | break; |
| 340 | } |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 341 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_DISCONNECTED && |
| 342 | next_state_ != STATE_OPEN); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 343 | return rv; |
| 344 | } |
| 345 | |
| 346 | int SpdyProxyClientSocket::DoGenerateAuthToken() { |
| 347 | next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 348 | return auth_->MaybeGenerateAuthToken( |
| 349 | &request_, |
Yannic Bonenberger | 00e0984 | 2019-08-31 20:46:19 | [diff] [blame] | 350 | base::BindOnce(&SpdyProxyClientSocket::OnIOComplete, |
| 351 | weak_factory_.GetWeakPtr()), |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 352 | net_log_); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) { |
| 356 | DCHECK_NE(ERR_IO_PENDING, result); |
| 357 | if (result == OK) |
| 358 | next_state_ = STATE_SEND_REQUEST; |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | int SpdyProxyClientSocket::DoSendRequest() { |
| 363 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 364 | |
| 365 | // Add Proxy-Authentication header if necessary. |
| 366 | HttpRequestHeaders authorization_headers; |
| 367 | if (auth_->HaveAuth()) { |
| 368 | auth_->AddAuthorizationHeader(&authorization_headers); |
| 369 | } |
| 370 | |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 371 | if (proxy_delegate_) { |
| 372 | HttpRequestHeaders proxy_delegate_headers; |
| 373 | proxy_delegate_->OnBeforeTunnelRequest(proxy_server_, |
| 374 | &proxy_delegate_headers); |
| 375 | request_.extra_headers.MergeFrom(proxy_delegate_headers); |
| 376 | } |
| 377 | |
Bence Béky | 4e83f49 | 2018-05-13 23:14:25 | [diff] [blame] | 378 | std::string request_line; |
rch | ecd3c55 | 2015-04-07 20:53:54 | [diff] [blame] | 379 | BuildTunnelRequest(endpoint_, authorization_headers, user_agent_, |
| 380 | &request_line, &request_.extra_headers); |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 381 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 382 | NetLogRequestHeaders(net_log_, |
| 383 | NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 384 | request_line, &request_.extra_headers); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 385 | |
Bence Béky | 4c325e5 | 2020-10-22 20:48:01 | [diff] [blame] | 386 | spdy::Http2HeaderBlock headers; |
Bence Béky | 1af94d6f | 2018-02-08 00:40:14 | [diff] [blame] | 387 | CreateSpdyHeadersFromHttpRequest(request_, request_.extra_headers, &headers); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 388 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 389 | return spdy_stream_->SendRequestHeaders(std::move(headers), |
| 390 | MORE_DATA_TO_SEND); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | int SpdyProxyClientSocket::DoSendRequestComplete(int result) { |
| 394 | if (result < 0) |
| 395 | return result; |
| 396 | |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 397 | // Wait for HEADERS frame from the server |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 398 | next_state_ = STATE_READ_REPLY_COMPLETE; |
| 399 | return ERR_IO_PENDING; |
| 400 | } |
| 401 | |
| 402 | int SpdyProxyClientSocket::DoReadReplyComplete(int result) { |
| 403 | // We enter this method directly from DoSendRequestComplete, since |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 404 | // we are notified by a callback when the HEADERS frame arrives. |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 405 | |
| 406 | if (result < 0) |
| 407 | return result; |
| 408 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 409 | // Require the "HTTP/1.x" status line for SSL CONNECT. |
bnc | be0f6af | 2015-10-15 17:49:56 | [diff] [blame] | 410 | if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 411 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 412 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 413 | NetLogResponseHeaders( |
| 414 | net_log_, NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 415 | response_.headers.get()); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 416 | |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 417 | if (proxy_delegate_) { |
| 418 | int rv = proxy_delegate_->OnTunnelHeadersReceived(proxy_server_, |
| 419 | *response_.headers); |
| 420 | if (rv != OK) { |
| 421 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 422 | return rv; |
| 423 | } |
| 424 | } |
| 425 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 426 | switch (response_.headers->response_code()) { |
| 427 | case 200: // OK |
| 428 | next_state_ = STATE_OPEN; |
| 429 | return OK; |
| 430 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 431 | case 407: // Proxy Authentication Required |
| 432 | next_state_ = STATE_OPEN; |
Jeremy Roman | 4803d233 | 2021-04-26 22:37:22 | [diff] [blame] | 433 | SanitizeProxyAuth(response_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 434 | return HandleProxyAuthChallenge(auth_.get(), &response_, net_log_); |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 435 | |
| 436 | default: |
| 437 | // Ignore response to avoid letting the proxy impersonate the target |
| 438 | // server. (See http://crbug.com/137891.) |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 439 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 440 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // SpdyStream::Delegate methods: |
| 444 | // Called when SYN frame has been sent. |
| 445 | // Returns true if no more data to be sent after SYN frame. |
bnc | 4c21431 | 2016-11-28 16:49:15 | [diff] [blame] | 446 | void SpdyProxyClientSocket::OnHeadersSent() { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 447 | DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE); |
| 448 | |
[email protected] | d46715c | 2013-04-15 00:21:42 | [diff] [blame] | 449 | OnIOComplete(OK); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 450 | } |
| 451 | |
Kenichi Ishibashi | 7415553 | 2021-03-13 01:38:06 | [diff] [blame] | 452 | void SpdyProxyClientSocket::OnEarlyHintsReceived( |
| 453 | const spdy::Http2HeaderBlock& headers) {} |
| 454 | |
bnc | 4c21431 | 2016-11-28 16:49:15 | [diff] [blame] | 455 | void SpdyProxyClientSocket::OnHeadersReceived( |
Bence Béky | 4c325e5 | 2020-10-22 20:48:01 | [diff] [blame] | 456 | const spdy::Http2HeaderBlock& response_headers, |
| 457 | const spdy::Http2HeaderBlock* pushed_request_headers) { |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 458 | // If we've already received the reply, existing headers are too late. |
| 459 | // TODO(mbelshe): figure out a way to make HEADERS frames useful after the |
| 460 | // initial response. |
| 461 | if (next_state_ != STATE_READ_REPLY_COMPLETE) |
bnc | 4c21431 | 2016-11-28 16:49:15 | [diff] [blame] | 462 | return; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 463 | |
[email protected] | d0835850 | 2010-12-03 22:04:03 | [diff] [blame] | 464 | // Save the response |
Kenichi Ishibashi | e3708df | 2022-02-26 00:55:40 | [diff] [blame] | 465 | const int rv = SpdyHeadersToHttpResponse(response_headers, &response_); |
| 466 | DCHECK_NE(rv, ERR_INCOMPLETE_HTTP2_HEADERS); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 467 | |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 468 | OnIOComplete(OK); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 469 | } |
| 470 | |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 471 | // Called when data is received or on EOF (if `buffer is nullptr). |
danakj | aee3e1ec | 2016-04-16 00:23:18 | [diff] [blame] | 472 | void SpdyProxyClientSocket::OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) { |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 473 | if (buffer) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 474 | net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 475 | buffer->GetRemainingSize(), |
| 476 | buffer->GetRemainingData()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 477 | read_buffer_queue_.Enqueue(std::move(buffer)); |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 478 | } else { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 479 | net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, 0, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 480 | nullptr); |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 481 | |
| 482 | if (end_stream_state_ == EndStreamState::kNone) { |
| 483 | // The peer sent END_STREAM. Schedule a DATA frame with END_STREAM. |
| 484 | end_stream_state_ = EndStreamState::kEndStreamReceived; |
| 485 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 486 | FROM_HERE, base::BindOnce(&SpdyProxyClientSocket::MaybeSendEndStream, |
| 487 | weak_factory_.GetWeakPtr())); |
| 488 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 489 | } |
| 490 | |
Helen Li | d714188 | 2018-08-22 21:41:17 | [diff] [blame] | 491 | if (read_callback_) { |
| 492 | if (user_buffer_) { |
| 493 | int rv = PopulateUserReadBuffer(user_buffer_->data(), user_buffer_len_); |
| 494 | user_buffer_ = nullptr; |
| 495 | user_buffer_len_ = 0; |
| 496 | std::move(read_callback_).Run(rv); |
| 497 | } else { |
| 498 | // If ReadIfReady() is used instead of Read(), tell the caller that data |
| 499 | // is available for reading. |
| 500 | std::move(read_callback_).Run(OK); |
| 501 | } |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 505 | void SpdyProxyClientSocket::OnDataSent() { |
| 506 | if (end_stream_state_ == EndStreamState::kEndStreamSent) { |
| 507 | CHECK(write_callback_.is_null()); |
| 508 | return; |
| 509 | } |
| 510 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 511 | DCHECK(!write_callback_.is_null()); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 512 | |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 513 | int rv = write_buffer_len_; |
| 514 | write_buffer_len_ = 0; |
[email protected] | 89d4d79 | 2014-04-06 16:27:15 | [diff] [blame] | 515 | |
| 516 | // Proxy write callbacks result in deep callback chains. Post to allow the |
| 517 | // stream's write callback chain to unwind (see crbug.com/355511). |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 518 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 519 | FROM_HERE, base::BindOnce(&SpdyProxyClientSocket::RunWriteCallback, |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 520 | write_callback_weak_factory_.GetWeakPtr(), |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 521 | std::move(write_callback_), rv)); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 522 | } |
| 523 | |
Bence Béky | 4c325e5 | 2020-10-22 20:48:01 | [diff] [blame] | 524 | void SpdyProxyClientSocket::OnTrailers(const spdy::Http2HeaderBlock& trailers) { |
xunjieli | 294da72 | 2015-08-11 19:15:02 | [diff] [blame] | 525 | // |spdy_stream_| is of type SPDY_BIDIRECTIONAL_STREAM, so trailers are |
| 526 | // combined with response headers and this method will not be calld. |
| 527 | NOTREACHED(); |
| 528 | } |
| 529 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 530 | void SpdyProxyClientSocket::OnClose(int status) { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 531 | was_ever_used_ = spdy_stream_->WasEverUsed(); |
[email protected] | d26ff35 | 2013-05-13 08:48:28 | [diff] [blame] | 532 | spdy_stream_.reset(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 533 | |
| 534 | bool connecting = next_state_ != STATE_DISCONNECTED && |
| 535 | next_state_ < STATE_OPEN; |
| 536 | if (next_state_ == STATE_OPEN) |
| 537 | next_state_ = STATE_CLOSED; |
| 538 | else |
| 539 | next_state_ = STATE_DISCONNECTED; |
| 540 | |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 541 | base::WeakPtr<SpdyProxyClientSocket> weak_ptr = weak_factory_.GetWeakPtr(); |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 542 | CompletionOnceCallback write_callback = std::move(write_callback_); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 543 | write_buffer_len_ = 0; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 544 | |
| 545 | // If we're in the middle of connecting, we need to make sure |
| 546 | // we invoke the connect callback. |
| 547 | if (connecting) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 548 | DCHECK(!read_callback_.is_null()); |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 549 | std::move(read_callback_).Run(status); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 550 | } else if (!read_callback_.is_null()) { |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 551 | // 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] | 552 | OnDataReceived(std::unique_ptr<SpdyBuffer>()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 553 | } |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 554 | // This may have been deleted by read_callback_, so check first. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 555 | if (weak_ptr.get() && !write_callback.is_null()) |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 556 | std::move(write_callback).Run(ERR_CONNECTION_CLOSED); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 557 | } |
| 558 | |
Bence Béky | 21755dd6 | 2019-11-19 13:47:35 | [diff] [blame] | 559 | bool SpdyProxyClientSocket::CanGreaseFrameType() const { |
| 560 | return false; |
| 561 | } |
| 562 | |
bnc | 3d95ca9 | 2017-03-29 13:20:34 | [diff] [blame] | 563 | NetLogSource SpdyProxyClientSocket::source_dependency() const { |
| 564 | return source_dependency_; |
| 565 | } |
| 566 | |
Kenichi Ishibashi | b0207d7 | 2022-02-09 22:06:36 | [diff] [blame] | 567 | void SpdyProxyClientSocket::MaybeSendEndStream() { |
| 568 | DCHECK_NE(end_stream_state_, EndStreamState::kNone); |
| 569 | if (end_stream_state_ == EndStreamState::kEndStreamSent) |
| 570 | return; |
| 571 | |
| 572 | if (!spdy_stream_) |
| 573 | return; |
| 574 | |
| 575 | // When there is a pending write, wait until the write completes. |
| 576 | if (write_callback_) |
| 577 | return; |
| 578 | |
| 579 | auto buffer = base::MakeRefCounted<IOBuffer>(/*buffer_size=*/0); |
| 580 | spdy_stream_->SendData(buffer.get(), /*length=*/0, NO_MORE_DATA_TO_SEND); |
| 581 | end_stream_state_ = EndStreamState::kEndStreamSent; |
| 582 | } |
| 583 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 584 | } // namespace net |