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