[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 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/websockets/websocket_channel.h" |
| 6 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 7 | #include <limits.h> // for INT_MAX |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 9 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 10 | #include <algorithm> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 11 | #include <utility> |
| 12 | #include <vector> |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 13 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 14 | #include "base/big_endian.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 15 | #include "base/bind.h" |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 16 | #include "base/containers/circular_deque.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 17 | #include "base/location.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 18 | #include "base/memory/raw_ptr.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 19 | #include "base/memory/weak_ptr.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 20 | #include "base/numerics/safe_conversions.h" |
Nanami Mikiya | 504e704 | 2022-02-10 09:57:50 | [diff] [blame] | 21 | #include "base/strings/string_piece.h" |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 22 | #include "base/strings/stringprintf.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 23 | #include "base/task/single_thread_task_runner.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 24 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 25 | #include "base/time/time.h" |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 26 | #include "net/base/auth.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 27 | #include "net/base/io_buffer.h" |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 28 | #include "net/base/ip_endpoint.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 29 | #include "net/http/http_request_headers.h" |
| 30 | #include "net/http/http_response_headers.h" |
[email protected] | 53deacb | 2013-11-22 14:02:43 | [diff] [blame] | 31 | #include "net/http/http_util.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 32 | #include "net/log/net_log_with_source.h" |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 33 | #include "net/traffic_annotation/network_traffic_annotation.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 34 | #include "net/websockets/websocket_errors.h" |
| 35 | #include "net/websockets/websocket_event_interface.h" |
| 36 | #include "net/websockets/websocket_frame.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 37 | #include "net/websockets/websocket_handshake_request_info.h" |
| 38 | #include "net/websockets/websocket_handshake_response_info.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 39 | #include "net/websockets/websocket_stream.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 40 | #include "url/origin.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 41 | |
| 42 | namespace net { |
| 43 | |
| 44 | namespace { |
| 45 | |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 46 | using base::StreamingUtf8Validator; |
| 47 | |
Adam Rice | e77e5b7d | 2020-08-31 23:31:09 | [diff] [blame] | 48 | constexpr size_t kWebSocketCloseCodeLength = 2; |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 49 | // Timeout for waiting for the server to acknowledge a closing handshake. |
Adam Rice | e77e5b7d | 2020-08-31 23:31:09 | [diff] [blame] | 50 | constexpr int kClosingHandshakeTimeoutSeconds = 60; |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 51 | // We wait for the server to close the underlying connection as recommended in |
| 52 | // https://tools.ietf.org/html/rfc6455#section-7.1.1 |
| 53 | // We don't use 2MSL since there're server implementations that don't follow |
| 54 | // the recommendation and wait for the client to close the underlying |
| 55 | // connection. It leads to unnecessarily long time before CloseEvent |
| 56 | // invocation. We want to avoid this rather than strictly following the spec |
| 57 | // recommendation. |
Adam Rice | e77e5b7d | 2020-08-31 23:31:09 | [diff] [blame] | 58 | constexpr int kUnderlyingConnectionCloseTimeoutSeconds = 2; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 59 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 60 | using ChannelState = WebSocketChannel::ChannelState; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 61 | |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 62 | // Maximum close reason length = max control frame payload - |
| 63 | // status code length |
| 64 | // = 125 - 2 |
Adam Rice | e77e5b7d | 2020-08-31 23:31:09 | [diff] [blame] | 65 | constexpr size_t kMaximumCloseReasonLength = 125 - kWebSocketCloseCodeLength; |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 66 | |
| 67 | // Check a close status code for strict compliance with RFC6455. This is only |
| 68 | // used for close codes received from a renderer that we are intending to send |
| 69 | // out over the network. See ParseClose() for the restrictions on incoming close |
| 70 | // codes. The |code| parameter is type int for convenience of implementation; |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 71 | // the real type is uint16_t. Code 1005 is treated specially; it cannot be set |
[email protected] | aa984e5f | 2014-02-26 07:33:09 | [diff] [blame] | 72 | // explicitly by Javascript but the renderer uses it to indicate we should send |
| 73 | // a Close frame with no payload. |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 74 | bool IsStrictlyValidCloseStatusCode(int code) { |
| 75 | static const int kInvalidRanges[] = { |
| 76 | // [BAD, OK) |
| 77 | 0, 1000, // 1000 is the first valid code |
[email protected] | aa984e5f | 2014-02-26 07:33:09 | [diff] [blame] | 78 | 1006, 1007, // 1006 MUST NOT be set. |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 79 | 1014, 3000, // 1014 unassigned; 1015 up to 2999 are reserved. |
| 80 | 5000, 65536, // Codes above 5000 are invalid. |
| 81 | }; |
| 82 | const int* const kInvalidRangesEnd = |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 83 | kInvalidRanges + std::size(kInvalidRanges); |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 84 | |
| 85 | DCHECK_GE(code, 0); |
| 86 | DCHECK_LT(code, 65536); |
| 87 | const int* upper = std::upper_bound(kInvalidRanges, kInvalidRangesEnd, code); |
| 88 | DCHECK_NE(kInvalidRangesEnd, upper); |
| 89 | DCHECK_GT(upper, kInvalidRanges); |
| 90 | DCHECK_GT(*upper, code); |
| 91 | DCHECK_LE(*(upper - 1), code); |
| 92 | return ((upper - kInvalidRanges) % 2) == 0; |
| 93 | } |
| 94 | |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 95 | // Sets |name| to the name of the frame type for the given |opcode|. Note that |
| 96 | // for all of Text, Binary and Continuation opcode, this method returns |
| 97 | // "Data frame". |
| 98 | void GetFrameTypeForOpcode(WebSocketFrameHeader::OpCode opcode, |
| 99 | std::string* name) { |
| 100 | switch (opcode) { |
| 101 | case WebSocketFrameHeader::kOpCodeText: // fall-thru |
| 102 | case WebSocketFrameHeader::kOpCodeBinary: // fall-thru |
| 103 | case WebSocketFrameHeader::kOpCodeContinuation: |
| 104 | *name = "Data frame"; |
| 105 | break; |
| 106 | |
| 107 | case WebSocketFrameHeader::kOpCodePing: |
| 108 | *name = "Ping"; |
| 109 | break; |
| 110 | |
| 111 | case WebSocketFrameHeader::kOpCodePong: |
| 112 | *name = "Pong"; |
| 113 | break; |
| 114 | |
| 115 | case WebSocketFrameHeader::kOpCodeClose: |
| 116 | *name = "Close"; |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | *name = "Unknown frame type"; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | return; |
| 125 | } |
| 126 | |
Nanami Mikiya | 504e704 | 2022-02-10 09:57:50 | [diff] [blame] | 127 | base::Value NetLogFailParam(uint16_t code, |
| 128 | base::StringPiece reason, |
| 129 | base::StringPiece message) { |
Travis Skare | adae7ff | 2022-07-27 16:23:33 | [diff] [blame] | 130 | base::Value::Dict dict; |
| 131 | dict.Set("code", code); |
| 132 | dict.Set("reason", reason); |
| 133 | dict.Set("internal_reason", message); |
| 134 | return base::Value(std::move(dict)); |
Nanami Mikiya | 504e704 | 2022-02-10 09:57:50 | [diff] [blame] | 135 | } |
| 136 | |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 137 | class DependentIOBuffer : public WrappedIOBuffer { |
| 138 | public: |
| 139 | DependentIOBuffer(scoped_refptr<IOBuffer> buffer, size_t offset) |
| 140 | : WrappedIOBuffer(buffer->data() + offset), buffer_(std::move(buffer)) {} |
| 141 | |
| 142 | private: |
Chris Watkins | 28c2fdd | 2017-11-30 06:06:52 | [diff] [blame] | 143 | ~DependentIOBuffer() override = default; |
Matt Menke | 29a538d | 2020-04-29 16:12:17 | [diff] [blame] | 144 | scoped_refptr<IOBuffer> buffer_; |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 145 | }; |
| 146 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 147 | } // namespace |
| 148 | |
| 149 | // A class to encapsulate a set of frames and information about the size of |
| 150 | // those frames. |
| 151 | class WebSocketChannel::SendBuffer { |
| 152 | public: |
Tsuyoshi Horo | a0b9c0f | 2022-06-09 01:41:51 | [diff] [blame] | 153 | SendBuffer() = default; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 154 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 155 | // Add a WebSocketFrame to the buffer and increase total_bytes_. |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 156 | void AddFrame(std::unique_ptr<WebSocketFrame> chunk, |
| 157 | scoped_refptr<IOBuffer> buffer); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 158 | |
| 159 | // Return a pointer to the frames_ for write purposes. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 160 | std::vector<std::unique_ptr<WebSocketFrame>>* frames() { return &frames_; } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 161 | |
| 162 | private: |
| 163 | // The frames_ that will be sent in the next call to WriteFrames(). |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 164 | std::vector<std::unique_ptr<WebSocketFrame>> frames_; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 165 | // References of each WebSocketFrame.data; |
| 166 | std::vector<scoped_refptr<IOBuffer>> buffers_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 167 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 168 | // The total size of the payload data in |frames_|. This will be used to |
| 169 | // measure the throughput of the link. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 170 | // TODO(ricea): Measure the throughput of the link. |
Tsuyoshi Horo | a0b9c0f | 2022-06-09 01:41:51 | [diff] [blame] | 171 | uint64_t total_bytes_ = 0; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 172 | }; |
| 173 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 174 | void WebSocketChannel::SendBuffer::AddFrame( |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 175 | std::unique_ptr<WebSocketFrame> frame, |
| 176 | scoped_refptr<IOBuffer> buffer) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 177 | total_bytes_ += frame->header.payload_length; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 178 | frames_.push_back(std::move(frame)); |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 179 | buffers_.push_back(std::move(buffer)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Implementation of WebSocketStream::ConnectDelegate that simply forwards the |
| 183 | // calls on to the WebSocketChannel that created it. |
| 184 | class WebSocketChannel::ConnectDelegate |
| 185 | : public WebSocketStream::ConnectDelegate { |
| 186 | public: |
| 187 | explicit ConnectDelegate(WebSocketChannel* creator) : creator_(creator) {} |
| 188 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 189 | ConnectDelegate(const ConnectDelegate&) = delete; |
| 190 | ConnectDelegate& operator=(const ConnectDelegate&) = delete; |
| 191 | |
Matt Menke | 29a538d | 2020-04-29 16:12:17 | [diff] [blame] | 192 | void OnCreateRequest(URLRequest* request) override { |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 193 | creator_->OnCreateURLRequest(request); |
| 194 | } |
| 195 | |
Yoichi Osato | 1ead61a | 2020-01-06 04:52:57 | [diff] [blame] | 196 | void OnSuccess( |
| 197 | std::unique_ptr<WebSocketStream> stream, |
| 198 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) override { |
| 199 | creator_->OnConnectSuccess(std::move(stream), std::move(response)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 200 | // |this| may have been deleted. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 201 | } |
| 202 | |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 203 | void OnFailure(const std::string& message, |
| 204 | int net_error, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 205 | absl::optional<int> response_code) override { |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 206 | creator_->OnConnectFailure(message, net_error, response_code); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 207 | // |this| has been deleted. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 208 | } |
| 209 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 210 | void OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 211 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) override { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 212 | creator_->OnStartOpeningHandshake(std::move(request)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 213 | } |
| 214 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 215 | void OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 216 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks> |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 217 | ssl_error_callbacks, |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 218 | int net_error, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 219 | const SSLInfo& ssl_info, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 220 | bool fatal) override { |
Emily Stark | d9df3d3 | 2019-04-29 17:54:57 | [diff] [blame] | 221 | creator_->OnSSLCertificateError(std::move(ssl_error_callbacks), net_error, |
| 222 | ssl_info, fatal); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 223 | } |
| 224 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 225 | int OnAuthRequired(const AuthChallengeInfo& auth_info, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 226 | scoped_refptr<HttpResponseHeaders> headers, |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 227 | const IPEndPoint& remote_endpoint, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 228 | base::OnceCallback<void(const AuthCredentials*)> callback, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 229 | absl::optional<AuthCredentials>* credentials) override { |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 230 | return creator_->OnAuthRequired(auth_info, std::move(headers), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 231 | remote_endpoint, std::move(callback), |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 232 | credentials); |
| 233 | } |
| 234 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 235 | private: |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 236 | // A pointer to the WebSocketChannel that created this object. There is no |
| 237 | // danger of this pointer being stale, because deleting the WebSocketChannel |
| 238 | // cancels the connect process, deleting this object and preventing its |
| 239 | // callbacks from being called. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 240 | const raw_ptr<WebSocketChannel> creator_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | WebSocketChannel::WebSocketChannel( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 244 | std::unique_ptr<WebSocketEventInterface> event_interface, |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 245 | URLRequestContext* url_request_context) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 246 | : event_interface_(std::move(event_interface)), |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 247 | url_request_context_(url_request_context), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 248 | closing_handshake_timeout_( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 249 | base::Seconds(kClosingHandshakeTimeoutSeconds)), |
| 250 | underlying_connection_close_timeout_( |
Tsuyoshi Horo | a0b9c0f | 2022-06-09 01:41:51 | [diff] [blame] | 251 | base::Seconds(kUnderlyingConnectionCloseTimeoutSeconds)) {} |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 252 | |
| 253 | WebSocketChannel::~WebSocketChannel() { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 254 | // The stream may hold a pointer to read_frames_, and so it needs to be |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 255 | // destroyed first. |
| 256 | stream_.reset(); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 257 | // The timer may have a callback pointing back to us, so stop it just in case |
| 258 | // someone decides to run the event loop from their destructor. |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 259 | close_timer_.Stop(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void WebSocketChannel::SendAddChannelRequest( |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 263 | const GURL& socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 264 | const std::vector<std::string>& requested_subprotocols, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 265 | const url::Origin& origin, |
Maks Orlovich | 8be0e25 | 2019-12-09 18:35:49 | [diff] [blame] | 266 | const SiteForCookies& site_for_cookies, |
Matt Menke | 29a538d | 2020-04-29 16:12:17 | [diff] [blame] | 267 | const IsolationInfo& isolation_info, |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 268 | const HttpRequestHeaders& additional_headers, |
| 269 | NetworkTrafficAnnotationTag traffic_annotation) { |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 270 | SendAddChannelRequestWithSuppliedCallback( |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 271 | socket_url, requested_subprotocols, origin, site_for_cookies, |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 272 | isolation_info, additional_headers, traffic_annotation, |
Anna Malova | 5a2d1318 | 2020-03-05 11:04:07 | [diff] [blame] | 273 | base::BindOnce(&WebSocketStream::CreateAndConnectStream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 276 | void WebSocketChannel::SetState(State new_state) { |
| 277 | DCHECK_NE(state_, new_state); |
| 278 | |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 279 | state_ = new_state; |
| 280 | } |
| 281 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 282 | bool WebSocketChannel::InClosingState() const { |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 283 | // The state RECV_CLOSED is not supported here, because it is only used in one |
| 284 | // code path and should not leak into the code in general. |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 285 | DCHECK_NE(RECV_CLOSED, state_) |
| 286 | << "InClosingState called with state_ == RECV_CLOSED"; |
| 287 | return state_ == SEND_CLOSED || state_ == CLOSE_WAIT || state_ == CLOSED; |
| 288 | } |
| 289 | |
ricea | e01cd614 | 2016-01-14 20:41:30 | [diff] [blame] | 290 | WebSocketChannel::ChannelState WebSocketChannel::SendFrame( |
| 291 | bool fin, |
| 292 | WebSocketFrameHeader::OpCode op_code, |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 293 | scoped_refptr<IOBuffer> buffer, |
| 294 | size_t buffer_size) { |
Adam Rice | 666d035 | 2018-05-28 06:08:38 | [diff] [blame] | 295 | DCHECK_LE(buffer_size, static_cast<size_t>(INT_MAX)); |
| 296 | DCHECK(stream_) << "Got SendFrame without a connection established; fin=" |
| 297 | << fin << " op_code=" << op_code |
| 298 | << " buffer_size=" << buffer_size; |
| 299 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 300 | if (InClosingState()) { |
[email protected] | b8393cc | 2014-04-14 06:22:25 | [diff] [blame] | 301 | DVLOG(1) << "SendFrame called in state " << state_ |
| 302 | << ". This may be a bug, or a harmless race."; |
ricea | e01cd614 | 2016-01-14 20:41:30 | [diff] [blame] | 303 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 304 | } |
Adam Rice | 666d035 | 2018-05-28 06:08:38 | [diff] [blame] | 305 | |
| 306 | DCHECK_EQ(state_, CONNECTED); |
Adam Rice | 666d035 | 2018-05-28 06:08:38 | [diff] [blame] | 307 | |
| 308 | DCHECK(WebSocketFrameHeader::IsKnownDataOpCode(op_code)) |
| 309 | << "Got SendFrame with bogus op_code " << op_code << " fin=" << fin |
| 310 | << " buffer_size=" << buffer_size; |
| 311 | |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 312 | if (op_code == WebSocketFrameHeader::kOpCodeText || |
| 313 | (op_code == WebSocketFrameHeader::kOpCodeContinuation && |
| 314 | sending_text_message_)) { |
| 315 | StreamingUtf8Validator::State state = |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 316 | outgoing_utf8_validator_.AddBytes(buffer->data(), buffer_size); |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 317 | if (state == StreamingUtf8Validator::INVALID || |
| 318 | (state == StreamingUtf8Validator::VALID_MIDPOINT && fin)) { |
| 319 | // TODO(ricea): Kill renderer. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 320 | FailChannel("Browser sent a text frame containing invalid UTF-8", |
| 321 | kWebSocketErrorGoingAway, ""); |
| 322 | return CHANNEL_DELETED; |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 323 | // |this| has been deleted. |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 324 | } |
| 325 | sending_text_message_ = !fin; |
| 326 | DCHECK(!fin || state == StreamingUtf8Validator::VALID_ENDPOINT); |
| 327 | } |
Adam Rice | 250bb01 | 2020-05-26 15:56:10 | [diff] [blame] | 328 | |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 329 | return SendFrameInternal(fin, op_code, std::move(buffer), buffer_size); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 330 | // |this| may have been deleted. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 331 | } |
| 332 | |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 333 | ChannelState WebSocketChannel::StartClosingHandshake( |
| 334 | uint16_t code, |
| 335 | const std::string& reason) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 336 | if (InClosingState()) { |
[email protected] | e778f32 | 2014-07-28 10:00:53 | [diff] [blame] | 337 | // When the associated renderer process is killed while the channel is in |
| 338 | // CLOSING state we reach here. |
[email protected] | b8393cc | 2014-04-14 06:22:25 | [diff] [blame] | 339 | DVLOG(1) << "StartClosingHandshake called in state " << state_ |
| 340 | << ". This may be a bug, or a harmless race."; |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 341 | return CHANNEL_ALIVE; |
| 342 | } |
| 343 | if (has_received_close_frame_) { |
| 344 | // We reach here if the client wants to start a closing handshake while |
| 345 | // the browser is waiting for the client to consume incoming data frames |
| 346 | // before responding to a closing handshake initiated by the server. |
| 347 | // As the client doesn't want the data frames any more, we can respond to |
| 348 | // the closing handshake initiated by the server. |
| 349 | return RespondToClosingHandshake(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 350 | } |
[email protected] | 6dfd8b3 | 2014-02-05 11:24:49 | [diff] [blame] | 351 | if (state_ == CONNECTING) { |
| 352 | // Abort the in-progress handshake and drop the connection immediately. |
| 353 | stream_request_.reset(); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 354 | SetState(CLOSED); |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 355 | DoDropChannel(false, kWebSocketErrorAbnormalClosure, ""); |
| 356 | return CHANNEL_DELETED; |
[email protected] | 6dfd8b3 | 2014-02-05 11:24:49 | [diff] [blame] | 357 | } |
Adam Rice | 666d035 | 2018-05-28 06:08:38 | [diff] [blame] | 358 | DCHECK_EQ(state_, CONNECTED); |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 359 | |
| 360 | DCHECK(!close_timer_.IsRunning()); |
| 361 | // This use of base::Unretained() is safe because we stop the timer in the |
| 362 | // destructor. |
| 363 | close_timer_.Start( |
Yannic Bonenberger | 782a2860 | 2019-09-03 10:36:52 | [diff] [blame] | 364 | FROM_HERE, closing_handshake_timeout_, |
| 365 | base::BindOnce(&WebSocketChannel::CloseTimeout, base::Unretained(this))); |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 366 | |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 367 | // Javascript actually only permits 1000 and 3000-4999, but the implementation |
| 368 | // itself may produce different codes. The length of |reason| is also checked |
| 369 | // by Javascript. |
| 370 | if (!IsStrictlyValidCloseStatusCode(code) || |
| 371 | reason.size() > kMaximumCloseReasonLength) { |
| 372 | // "InternalServerError" is actually used for errors from any endpoint, per |
| 373 | // errata 3227 to RFC6455. If the renderer is sending us an invalid code or |
| 374 | // reason it must be malfunctioning in some way, and based on that we |
| 375 | // interpret this as an internal error. |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 376 | if (SendClose(kWebSocketErrorInternalServerError, "") == CHANNEL_DELETED) |
| 377 | return CHANNEL_DELETED; |
| 378 | DCHECK_EQ(CONNECTED, state_); |
| 379 | SetState(SEND_CLOSED); |
| 380 | return CHANNEL_ALIVE; |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 381 | } |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 382 | if (SendClose(code, StreamingUtf8Validator::Validate(reason) |
| 383 | ? reason |
| 384 | : std::string()) == CHANNEL_DELETED) |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 385 | return CHANNEL_DELETED; |
[email protected] | 0f5f1bb | 2014-04-22 08:34:35 | [diff] [blame] | 386 | DCHECK_EQ(CONNECTED, state_); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 387 | SetState(SEND_CLOSED); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 388 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void WebSocketChannel::SendAddChannelRequestForTesting( |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 392 | const GURL& socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 393 | const std::vector<std::string>& requested_subprotocols, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 394 | const url::Origin& origin, |
Maks Orlovich | 8be0e25 | 2019-12-09 18:35:49 | [diff] [blame] | 395 | const SiteForCookies& site_for_cookies, |
Matt Menke | 29a538d | 2020-04-29 16:12:17 | [diff] [blame] | 396 | const IsolationInfo& isolation_info, |
Yutaka Hirano | 2f65eec | 2018-05-23 01:58:22 | [diff] [blame] | 397 | const HttpRequestHeaders& additional_headers, |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 398 | NetworkTrafficAnnotationTag traffic_annotation, |
Anna Malova | 5a2d1318 | 2020-03-05 11:04:07 | [diff] [blame] | 399 | WebSocketStreamRequestCreationCallback callback) { |
Ehimare Okoyomon | 4446d8c | 2019-10-23 12:47:32 | [diff] [blame] | 400 | SendAddChannelRequestWithSuppliedCallback( |
| 401 | socket_url, requested_subprotocols, origin, site_for_cookies, |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 402 | isolation_info, additional_headers, traffic_annotation, |
| 403 | std::move(callback)); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void WebSocketChannel::SetClosingHandshakeTimeoutForTesting( |
| 407 | base::TimeDelta delay) { |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 408 | closing_handshake_timeout_ = delay; |
| 409 | } |
| 410 | |
| 411 | void WebSocketChannel::SetUnderlyingConnectionCloseTimeoutForTesting( |
| 412 | base::TimeDelta delay) { |
| 413 | underlying_connection_close_timeout_ = delay; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 414 | } |
| 415 | |
tyoshino | ccfcfde | 2016-07-21 14:06:55 | [diff] [blame] | 416 | void WebSocketChannel::SendAddChannelRequestWithSuppliedCallback( |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 417 | const GURL& socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 418 | const std::vector<std::string>& requested_subprotocols, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 419 | const url::Origin& origin, |
Maks Orlovich | 8be0e25 | 2019-12-09 18:35:49 | [diff] [blame] | 420 | const SiteForCookies& site_for_cookies, |
Matt Menke | 29a538d | 2020-04-29 16:12:17 | [diff] [blame] | 421 | const IsolationInfo& isolation_info, |
Yutaka Hirano | 2f65eec | 2018-05-23 01:58:22 | [diff] [blame] | 422 | const HttpRequestHeaders& additional_headers, |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 423 | NetworkTrafficAnnotationTag traffic_annotation, |
Anna Malova | 5a2d1318 | 2020-03-05 11:04:07 | [diff] [blame] | 424 | WebSocketStreamRequestCreationCallback callback) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 425 | DCHECK_EQ(FRESHLY_CONSTRUCTED, state_); |
[email protected] | 53deacb | 2013-11-22 14:02:43 | [diff] [blame] | 426 | if (!socket_url.SchemeIsWSOrWSS()) { |
| 427 | // TODO(ricea): Kill the renderer (this error should have been caught by |
| 428 | // Javascript). |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 429 | event_interface_->OnFailChannel("Invalid scheme", ERR_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 430 | absl::nullopt); |
[email protected] | 53deacb | 2013-11-22 14:02:43 | [diff] [blame] | 431 | // |this| is deleted here. |
| 432 | return; |
| 433 | } |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 434 | socket_url_ = socket_url; |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 435 | auto connect_delegate = std::make_unique<ConnectDelegate>(this); |
Anna Malova | 5a2d1318 | 2020-03-05 11:04:07 | [diff] [blame] | 436 | stream_request_ = std::move(callback).Run( |
Ehimare Okoyomon | 4446d8c | 2019-10-23 12:47:32 | [diff] [blame] | 437 | socket_url_, requested_subprotocols, origin, site_for_cookies, |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 438 | isolation_info, additional_headers, url_request_context_.get(), |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 439 | NetLogWithSource(), traffic_annotation, std::move(connect_delegate)); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 440 | SetState(CONNECTING); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 441 | } |
| 442 | |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 443 | void WebSocketChannel::OnCreateURLRequest(URLRequest* request) { |
| 444 | event_interface_->OnCreateURLRequest(request); |
| 445 | } |
| 446 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 447 | void WebSocketChannel::OnConnectSuccess( |
Yoichi Osato | 1ead61a | 2020-01-06 04:52:57 | [diff] [blame] | 448 | std::unique_ptr<WebSocketStream> stream, |
| 449 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 450 | DCHECK(stream); |
| 451 | DCHECK_EQ(CONNECTING, state_); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 452 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 453 | stream_ = std::move(stream); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 454 | |
| 455 | SetState(CONNECTED); |
| 456 | |
Yoichi Osato | 1bcffbc | 2019-10-30 03:17:31 | [diff] [blame] | 457 | // |stream_request_| is not used once the connection has succeeded. |
| 458 | stream_request_.reset(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 459 | |
Yoichi Osato | 1ead61a | 2020-01-06 04:52:57 | [diff] [blame] | 460 | event_interface_->OnAddChannelResponse( |
Adam Rice | 250bb01 | 2020-05-26 15:56:10 | [diff] [blame] | 461 | std::move(response), stream_->GetSubProtocol(), stream_->GetExtensions()); |
Yoichi Osato | 1bcffbc | 2019-10-30 03:17:31 | [diff] [blame] | 462 | // |this| may have been deleted after OnAddChannelResponse. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 463 | } |
| 464 | |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 465 | void WebSocketChannel::OnConnectFailure(const std::string& message, |
| 466 | int net_error, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 467 | absl::optional<int> response_code) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 468 | DCHECK_EQ(CONNECTING, state_); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 469 | |
[email protected] | 8aba017 | 2014-07-03 12:09:53 | [diff] [blame] | 470 | // Copy the message before we delete its owner. |
| 471 | std::string message_copy = message; |
| 472 | |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 473 | SetState(CLOSED); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 474 | stream_request_.reset(); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 475 | |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 476 | event_interface_->OnFailChannel(message_copy, net_error, response_code); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 477 | // |this| has been deleted. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 480 | void WebSocketChannel::OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 481 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks> |
| 482 | ssl_error_callbacks, |
Emily Stark | d9df3d3 | 2019-04-29 17:54:57 | [diff] [blame] | 483 | int net_error, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 484 | const SSLInfo& ssl_info, |
| 485 | bool fatal) { |
Emily Stark | d9df3d3 | 2019-04-29 17:54:57 | [diff] [blame] | 486 | event_interface_->OnSSLCertificateError( |
| 487 | std::move(ssl_error_callbacks), socket_url_, net_error, ssl_info, fatal); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 488 | } |
| 489 | |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 490 | int WebSocketChannel::OnAuthRequired( |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 491 | const AuthChallengeInfo& auth_info, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 492 | scoped_refptr<HttpResponseHeaders> response_headers, |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 493 | const IPEndPoint& remote_endpoint, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 494 | base::OnceCallback<void(const AuthCredentials*)> callback, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 495 | absl::optional<AuthCredentials>* credentials) { |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 496 | return event_interface_->OnAuthRequired( |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 497 | auth_info, std::move(response_headers), remote_endpoint, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 498 | std::move(callback), credentials); |
| 499 | } |
| 500 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 501 | void WebSocketChannel::OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 502 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) { |
Yutaka Hirano | 6c960cc | 2018-05-24 05:34:38 | [diff] [blame] | 503 | event_interface_->OnStartOpeningHandshake(std::move(request)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 506 | ChannelState WebSocketChannel::WriteFrames() { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 507 | int result = OK; |
| 508 | do { |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 509 | // This use of base::Unretained is safe because this object owns the |
| 510 | // WebSocketStream and destroying it cancels all callbacks. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 511 | result = stream_->WriteFrames( |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 512 | data_being_sent_->frames(), |
Yannic Bonenberger | 782a2860 | 2019-09-03 10:36:52 | [diff] [blame] | 513 | base::BindOnce(base::IgnoreResult(&WebSocketChannel::OnWriteDone), |
| 514 | base::Unretained(this), false)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 515 | if (result != ERR_IO_PENDING) { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 516 | if (OnWriteDone(true, result) == CHANNEL_DELETED) |
| 517 | return CHANNEL_DELETED; |
[email protected] | 3d563769 | 2014-03-19 16:48:22 | [diff] [blame] | 518 | // OnWriteDone() returns CHANNEL_DELETED on error. Here |state_| is |
| 519 | // guaranteed to be the same as before OnWriteDone() call. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 520 | } |
| 521 | } while (result == OK && data_being_sent_); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 522 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 523 | } |
| 524 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 525 | ChannelState WebSocketChannel::OnWriteDone(bool synchronous, int result) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 526 | DCHECK_NE(FRESHLY_CONSTRUCTED, state_); |
| 527 | DCHECK_NE(CONNECTING, state_); |
| 528 | DCHECK_NE(ERR_IO_PENDING, result); |
| 529 | DCHECK(data_being_sent_); |
| 530 | switch (result) { |
| 531 | case OK: |
| 532 | if (data_to_send_next_) { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 533 | data_being_sent_ = std::move(data_to_send_next_); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 534 | if (!synchronous) |
| 535 | return WriteFrames(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 536 | } else { |
| 537 | data_being_sent_.reset(); |
Adam Rice | d009570 | 2020-05-26 06:18:25 | [diff] [blame] | 538 | event_interface_->OnSendDataFrameDone(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 539 | } |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 540 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 541 | |
| 542 | // If a recoverable error condition existed, it would go here. |
| 543 | |
| 544 | default: |
| 545 | DCHECK_LT(result, 0) |
| 546 | << "WriteFrames() should only return OK or ERR_ codes"; |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 547 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 548 | stream_->Close(); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 549 | SetState(CLOSED); |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 550 | DoDropChannel(false, kWebSocketErrorAbnormalClosure, ""); |
| 551 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 555 | ChannelState WebSocketChannel::ReadFrames() { |
Yutaka Hirano | 8fa3284 | 2019-08-29 03:13:06 | [diff] [blame] | 556 | DCHECK(stream_); |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 557 | DCHECK(state_ == CONNECTED || state_ == SEND_CLOSED || state_ == CLOSE_WAIT); |
| 558 | DCHECK(read_frames_.empty()); |
| 559 | if (is_reading_) { |
| 560 | return CHANNEL_ALIVE; |
| 561 | } |
| 562 | |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 563 | if (!InClosingState() && has_received_close_frame_) { |
| 564 | DCHECK(!event_interface_->HasPendingDataFrames()); |
| 565 | // We've been waiting for the client to consume the frames before |
| 566 | // responding to the closing handshake initiated by the server. |
Yutaka Hirano | d933173 | 2019-09-30 09:46:20 | [diff] [blame] | 567 | if (RespondToClosingHandshake() == CHANNEL_DELETED) { |
| 568 | return CHANNEL_DELETED; |
| 569 | } |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 570 | } |
| 571 | |
Yutaka Hirano | fcfbeb4 | 2019-09-25 14:43:39 | [diff] [blame] | 572 | // TODO(crbug.com/999235): Remove this CHECK. |
| 573 | CHECK(event_interface_); |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 574 | while (!event_interface_->HasPendingDataFrames()) { |
Yutaka Hirano | 8fa3284 | 2019-08-29 03:13:06 | [diff] [blame] | 575 | DCHECK(stream_); |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 576 | // This use of base::Unretained is safe because this object owns the |
| 577 | // WebSocketStream, and any pending reads will be cancelled when it is |
| 578 | // destroyed. |
Yutaka Hirano | dfa67e5 | 2019-07-29 03:13:26 | [diff] [blame] | 579 | const int result = stream_->ReadFrames( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 580 | &read_frames_, |
Yannic Bonenberger | 782a2860 | 2019-09-03 10:36:52 | [diff] [blame] | 581 | base::BindOnce(base::IgnoreResult(&WebSocketChannel::OnReadDone), |
| 582 | base::Unretained(this), false)); |
Yutaka Hirano | dfa67e5 | 2019-07-29 03:13:26 | [diff] [blame] | 583 | if (result == ERR_IO_PENDING) { |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 584 | is_reading_ = true; |
Yutaka Hirano | dfa67e5 | 2019-07-29 03:13:26 | [diff] [blame] | 585 | return CHANNEL_ALIVE; |
| 586 | } |
| 587 | if (OnReadDone(true, result) == CHANNEL_DELETED) { |
| 588 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 589 | } |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 590 | DCHECK_NE(CLOSED, state_); |
Yutaka Hirano | fcfbeb4 | 2019-09-25 14:43:39 | [diff] [blame] | 591 | // TODO(crbug.com/999235): Remove this CHECK. |
| 592 | CHECK(event_interface_); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 593 | } |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 594 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 595 | } |
| 596 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 597 | ChannelState WebSocketChannel::OnReadDone(bool synchronous, int result) { |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 598 | DVLOG(3) << "WebSocketChannel::OnReadDone synchronous?" << synchronous |
| 599 | << ", result=" << result |
| 600 | << ", read_frames_.size=" << read_frames_.size(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 601 | DCHECK_NE(FRESHLY_CONSTRUCTED, state_); |
| 602 | DCHECK_NE(CONNECTING, state_); |
| 603 | DCHECK_NE(ERR_IO_PENDING, result); |
| 604 | switch (result) { |
| 605 | case OK: |
| 606 | // ReadFrames() must use ERR_CONNECTION_CLOSED for a closed connection |
| 607 | // with no data read, not an empty response. |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 608 | DCHECK(!read_frames_.empty()) |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 609 | << "ReadFrames() returned OK, but nothing was read."; |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame] | 610 | for (auto& read_frame : read_frames_) { |
| 611 | if (HandleFrame(std::move(read_frame)) == CHANNEL_DELETED) |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 612 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 613 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 614 | read_frames_.clear(); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 615 | DCHECK_NE(CLOSED, state_); |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 616 | if (!synchronous) { |
| 617 | is_reading_ = false; |
| 618 | if (!event_interface_->HasPendingDataFrames()) { |
| 619 | return ReadFrames(); |
| 620 | } |
| 621 | } |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 622 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 623 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 624 | case ERR_WS_PROTOCOL_ERROR: |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 625 | // This could be kWebSocketErrorProtocolError (specifically, non-minimal |
| 626 | // encoding of payload length) or kWebSocketErrorMessageTooBig, or an |
| 627 | // extension-specific error. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 628 | FailChannel("Invalid frame header", kWebSocketErrorProtocolError, |
| 629 | "WebSocket Protocol Error"); |
| 630 | return CHANNEL_DELETED; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 631 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 632 | default: |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 633 | DCHECK_LT(result, 0) |
| 634 | << "ReadFrames() should only return OK or ERR_ codes"; |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 635 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 636 | stream_->Close(); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 637 | SetState(CLOSED); |
| 638 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 639 | uint16_t code = kWebSocketErrorAbnormalClosure; |
[email protected] | 5742ae6 | 2014-02-06 11:03:18 | [diff] [blame] | 640 | std::string reason = ""; |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 641 | bool was_clean = false; |
tyoshino | ceae3b24 | 2014-10-31 06:43:19 | [diff] [blame] | 642 | if (has_received_close_frame_) { |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 643 | code = received_close_code_; |
| 644 | reason = received_close_reason_; |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 645 | was_clean = (result == ERR_CONNECTION_CLOSED); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 646 | } |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 647 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 648 | DoDropChannel(was_clean, code, reason); |
| 649 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 653 | ChannelState WebSocketChannel::HandleFrame( |
| 654 | std::unique_ptr<WebSocketFrame> frame) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 655 | if (frame->header.masked) { |
| 656 | // RFC6455 Section 5.1 "A client MUST close a connection if it detects a |
| 657 | // masked frame." |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 658 | FailChannel( |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 659 | "A server must not mask any frames that it sends to the " |
| 660 | "client.", |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 661 | kWebSocketErrorProtocolError, "Masked frame from server"); |
| 662 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 663 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 664 | const WebSocketFrameHeader::OpCode opcode = frame->header.opcode; |
[email protected] | bae4842 | 2014-03-05 15:07:25 | [diff] [blame] | 665 | DCHECK(!WebSocketFrameHeader::IsKnownControlOpCode(opcode) || |
| 666 | frame->header.final); |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 667 | if (frame->header.reserved1 || frame->header.reserved2 || |
| 668 | frame->header.reserved3) { |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 669 | FailChannel( |
| 670 | base::StringPrintf("One or more reserved bits are on: reserved1 = %d, " |
[email protected] | c46c261 | 2014-02-28 17:08:02 | [diff] [blame] | 671 | "reserved2 = %d, reserved3 = %d", |
| 672 | static_cast<int>(frame->header.reserved1), |
| 673 | static_cast<int>(frame->header.reserved2), |
| 674 | static_cast<int>(frame->header.reserved3)), |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 675 | kWebSocketErrorProtocolError, "Invalid reserved bit"); |
| 676 | return CHANNEL_DELETED; |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 677 | } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 678 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 679 | // Respond to the frame appropriately to its type. |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 680 | return HandleFrameByState( |
| 681 | opcode, frame->header.final, |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 682 | base::make_span(frame->payload, frame->header.payload_length)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 685 | ChannelState WebSocketChannel::HandleFrameByState( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 686 | const WebSocketFrameHeader::OpCode opcode, |
| 687 | bool final, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 688 | base::span<const char> payload) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 689 | DCHECK_NE(RECV_CLOSED, state_) |
| 690 | << "HandleFrame() does not support being called re-entrantly from within " |
| 691 | "SendClose()"; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 692 | DCHECK_NE(CLOSED, state_); |
| 693 | if (state_ == CLOSE_WAIT) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 694 | std::string frame_name; |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 695 | GetFrameTypeForOpcode(opcode, &frame_name); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 696 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 697 | // FailChannel() won't send another Close frame. |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 698 | FailChannel(frame_name + " received after close", |
| 699 | kWebSocketErrorProtocolError, ""); |
| 700 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 701 | } |
| 702 | switch (opcode) { |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 703 | case WebSocketFrameHeader::kOpCodeText: // fall-thru |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 704 | case WebSocketFrameHeader::kOpCodeBinary: |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 705 | case WebSocketFrameHeader::kOpCodeContinuation: |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 706 | return HandleDataFrame(opcode, final, std::move(payload)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 707 | |
| 708 | case WebSocketFrameHeader::kOpCodePing: |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 709 | DVLOG(1) << "Got Ping of size " << payload.size(); |
| 710 | if (state_ == CONNECTED) { |
| 711 | auto buffer = base::MakeRefCounted<IOBuffer>(payload.size()); |
| 712 | memcpy(buffer->data(), payload.data(), payload.size()); |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 713 | return SendFrameInternal(true, WebSocketFrameHeader::kOpCodePong, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 714 | std::move(buffer), payload.size()); |
| 715 | } |
[email protected] | b8393cc | 2014-04-14 06:22:25 | [diff] [blame] | 716 | DVLOG(3) << "Ignored ping in state " << state_; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 717 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 718 | |
| 719 | case WebSocketFrameHeader::kOpCodePong: |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 720 | DVLOG(1) << "Got Pong of size " << payload.size(); |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 721 | // There is no need to do anything with pong messages. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 722 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 723 | |
| 724 | case WebSocketFrameHeader::kOpCodeClose: { |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 725 | uint16_t code = kWebSocketNormalClosure; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 726 | std::string reason; |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 727 | std::string message; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 728 | if (!ParseClose(payload, &code, &reason, &message)) { |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 729 | FailChannel(message, code, reason); |
| 730 | return CHANNEL_DELETED; |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 731 | } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 732 | // TODO(ricea): Find a way to safely log the message from the close |
| 733 | // message (escape control codes and so on). |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 734 | return HandleCloseFrame(code, reason); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | default: |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 738 | FailChannel(base::StringPrintf("Unrecognized frame opcode: %d", opcode), |
| 739 | kWebSocketErrorProtocolError, "Unknown opcode"); |
| 740 | return CHANNEL_DELETED; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 744 | ChannelState WebSocketChannel::HandleDataFrame( |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 745 | WebSocketFrameHeader::OpCode opcode, |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 746 | bool final, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 747 | base::span<const char> payload) { |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 748 | DVLOG(3) << "WebSocketChannel::HandleDataFrame opcode=" << opcode |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 749 | << ", final?" << final << ", data=" << (void*)payload.data() |
| 750 | << ", size=" << payload.size(); |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 751 | if (state_ != CONNECTED) { |
| 752 | DVLOG(3) << "Ignored data packet received in state " << state_; |
| 753 | return CHANNEL_ALIVE; |
| 754 | } |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 755 | if (has_received_close_frame_) { |
| 756 | DVLOG(3) << "Ignored data packet as we've received a close frame."; |
| 757 | return CHANNEL_ALIVE; |
| 758 | } |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 759 | DCHECK(opcode == WebSocketFrameHeader::kOpCodeContinuation || |
| 760 | opcode == WebSocketFrameHeader::kOpCodeText || |
| 761 | opcode == WebSocketFrameHeader::kOpCodeBinary); |
| 762 | const bool got_continuation = |
| 763 | (opcode == WebSocketFrameHeader::kOpCodeContinuation); |
| 764 | if (got_continuation != expecting_to_handle_continuation_) { |
| 765 | const std::string console_log = got_continuation |
| 766 | ? "Received unexpected continuation frame." |
| 767 | : "Received start of new message but previous message is unfinished."; |
| 768 | const std::string reason = got_continuation |
| 769 | ? "Unexpected continuation" |
| 770 | : "Previous data frame unfinished"; |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 771 | FailChannel(console_log, kWebSocketErrorProtocolError, reason); |
| 772 | return CHANNEL_DELETED; |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 773 | } |
| 774 | expecting_to_handle_continuation_ = !final; |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 775 | WebSocketFrameHeader::OpCode opcode_to_send = opcode; |
| 776 | if (!initial_frame_forwarded_ && |
| 777 | opcode == WebSocketFrameHeader::kOpCodeContinuation) { |
| 778 | opcode_to_send = receiving_text_message_ |
| 779 | ? WebSocketFrameHeader::kOpCodeText |
| 780 | : WebSocketFrameHeader::kOpCodeBinary; |
| 781 | } |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 782 | if (opcode == WebSocketFrameHeader::kOpCodeText || |
| 783 | (opcode == WebSocketFrameHeader::kOpCodeContinuation && |
| 784 | receiving_text_message_)) { |
| 785 | // This call is not redundant when size == 0 because it tells us what |
| 786 | // the current state is. |
| 787 | StreamingUtf8Validator::State state = incoming_utf8_validator_.AddBytes( |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 788 | payload.data(), static_cast<size_t>(payload.size())); |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 789 | if (state == StreamingUtf8Validator::INVALID || |
| 790 | (state == StreamingUtf8Validator::VALID_MIDPOINT && final)) { |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 791 | FailChannel("Could not decode a text frame as UTF-8.", |
| 792 | kWebSocketErrorProtocolError, "Invalid UTF-8 in text frame"); |
| 793 | return CHANNEL_DELETED; |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 794 | } |
| 795 | receiving_text_message_ = !final; |
| 796 | DCHECK(!final || state == StreamingUtf8Validator::VALID_ENDPOINT); |
| 797 | } |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 798 | if (payload.size() == 0U && !final) |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 799 | return CHANNEL_ALIVE; |
| 800 | |
| 801 | initial_frame_forwarded_ = !final; |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 802 | // Sends the received frame to the renderer process. |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 803 | event_interface_->OnDataFrame(final, opcode_to_send, payload); |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 804 | return CHANNEL_ALIVE; |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 805 | } |
| 806 | |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 807 | ChannelState WebSocketChannel::HandleCloseFrame(uint16_t code, |
| 808 | const std::string& reason) { |
| 809 | DVLOG(1) << "Got Close with code " << code; |
| 810 | switch (state_) { |
| 811 | case CONNECTED: |
| 812 | has_received_close_frame_ = true; |
| 813 | received_close_code_ = code; |
| 814 | received_close_reason_ = reason; |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 815 | if (event_interface_->HasPendingDataFrames()) { |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 816 | // We have some data to be sent to the renderer before sending this |
| 817 | // frame. |
| 818 | return CHANNEL_ALIVE; |
| 819 | } |
| 820 | return RespondToClosingHandshake(); |
| 821 | |
| 822 | case SEND_CLOSED: |
| 823 | SetState(CLOSE_WAIT); |
| 824 | DCHECK(close_timer_.IsRunning()); |
| 825 | close_timer_.Stop(); |
| 826 | // This use of base::Unretained() is safe because we stop the timer |
| 827 | // in the destructor. |
Yannic Bonenberger | 782a2860 | 2019-09-03 10:36:52 | [diff] [blame] | 828 | close_timer_.Start(FROM_HERE, underlying_connection_close_timeout_, |
| 829 | base::BindOnce(&WebSocketChannel::CloseTimeout, |
| 830 | base::Unretained(this))); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 831 | |
| 832 | // From RFC6455 section 7.1.5: "Each endpoint |
| 833 | // will see the status code sent by the other end as _The WebSocket |
| 834 | // Connection Close Code_." |
| 835 | has_received_close_frame_ = true; |
| 836 | received_close_code_ = code; |
| 837 | received_close_reason_ = reason; |
| 838 | break; |
| 839 | |
| 840 | default: |
| 841 | LOG(DFATAL) << "Got Close in unexpected state " << state_; |
| 842 | break; |
| 843 | } |
| 844 | return CHANNEL_ALIVE; |
| 845 | } |
| 846 | |
| 847 | ChannelState WebSocketChannel::RespondToClosingHandshake() { |
| 848 | DCHECK(has_received_close_frame_); |
| 849 | DCHECK_EQ(CONNECTED, state_); |
| 850 | SetState(RECV_CLOSED); |
| 851 | if (SendClose(received_close_code_, received_close_reason_) == |
| 852 | CHANNEL_DELETED) |
| 853 | return CHANNEL_DELETED; |
| 854 | DCHECK_EQ(RECV_CLOSED, state_); |
| 855 | |
| 856 | SetState(CLOSE_WAIT); |
| 857 | DCHECK(!close_timer_.IsRunning()); |
| 858 | // This use of base::Unretained() is safe because we stop the timer |
| 859 | // in the destructor. |
| 860 | close_timer_.Start( |
| 861 | FROM_HERE, underlying_connection_close_timeout_, |
Yannic Bonenberger | 782a2860 | 2019-09-03 10:36:52 | [diff] [blame] | 862 | base::BindOnce(&WebSocketChannel::CloseTimeout, base::Unretained(this))); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 863 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 864 | event_interface_->OnClosingHandshake(); |
| 865 | return CHANNEL_ALIVE; |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 866 | } |
| 867 | |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 868 | ChannelState WebSocketChannel::SendFrameInternal( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 869 | bool fin, |
| 870 | WebSocketFrameHeader::OpCode op_code, |
darin | 0da77e92 | 2016-10-04 17:31:23 | [diff] [blame] | 871 | scoped_refptr<IOBuffer> buffer, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 872 | uint64_t buffer_size) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 873 | DCHECK(state_ == CONNECTED || state_ == RECV_CLOSED); |
| 874 | DCHECK(stream_); |
[email protected] | 3d563769 | 2014-03-19 16:48:22 | [diff] [blame] | 875 | |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 876 | auto frame = std::make_unique<WebSocketFrame>(op_code); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 877 | WebSocketFrameHeader& header = frame->header; |
| 878 | header.final = fin; |
| 879 | header.masked = true; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 880 | header.payload_length = buffer_size; |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 881 | frame->payload = buffer->data(); |
[email protected] | 3d563769 | 2014-03-19 16:48:22 | [diff] [blame] | 882 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 883 | if (data_being_sent_) { |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 884 | // Either the link to the WebSocket server is saturated, or several messages |
| 885 | // are being sent in a batch. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 886 | if (!data_to_send_next_) |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 887 | data_to_send_next_ = std::make_unique<SendBuffer>(); |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 888 | data_to_send_next_->AddFrame(std::move(frame), std::move(buffer)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 889 | return CHANNEL_ALIVE; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 890 | } |
[email protected] | 3d563769 | 2014-03-19 16:48:22 | [diff] [blame] | 891 | |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 892 | data_being_sent_ = std::make_unique<SendBuffer>(); |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 893 | data_being_sent_->AddFrame(std::move(frame), std::move(buffer)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 894 | return WriteFrames(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 895 | } |
| 896 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 897 | void WebSocketChannel::FailChannel(const std::string& message, |
| 898 | uint16_t code, |
| 899 | const std::string& reason) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 900 | DCHECK_NE(FRESHLY_CONSTRUCTED, state_); |
| 901 | DCHECK_NE(CONNECTING, state_); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 902 | DCHECK_NE(CLOSED, state_); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 903 | |
Nanami Mikiya | 504e704 | 2022-02-10 09:57:50 | [diff] [blame] | 904 | stream_->GetNetLogWithSource().AddEvent( |
| 905 | net::NetLogEventType::WEBSOCKET_INVALID_FRAME, |
| 906 | [&] { return NetLogFailParam(code, reason, message); }); |
| 907 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 908 | if (state_ == CONNECTED) { |
[email protected] | 3d563769 | 2014-03-19 16:48:22 | [diff] [blame] | 909 | if (SendClose(code, reason) == CHANNEL_DELETED) |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 910 | return; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 911 | } |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 912 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 913 | // Careful study of RFC6455 section 7.1.7 and 7.1.1 indicates the browser |
| 914 | // should close the connection itself without waiting for the closing |
| 915 | // handshake. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 916 | stream_->Close(); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 917 | SetState(CLOSED); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 918 | event_interface_->OnFailChannel(message, ERR_FAILED, absl::nullopt); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 919 | } |
| 920 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 921 | ChannelState WebSocketChannel::SendClose(uint16_t code, |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 922 | const std::string& reason) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 923 | DCHECK(state_ == CONNECTED || state_ == RECV_CLOSED); |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 924 | DCHECK_LE(reason.size(), kMaximumCloseReasonLength); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 925 | scoped_refptr<IOBuffer> body; |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 926 | uint64_t size = 0; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 927 | if (code == kWebSocketErrorNoStatusReceived) { |
| 928 | // Special case: translate kWebSocketErrorNoStatusReceived into a Close |
| 929 | // frame with no payload. |
[email protected] | aa984e5f | 2014-02-26 07:33:09 | [diff] [blame] | 930 | DCHECK(reason.empty()); |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 931 | body = base::MakeRefCounted<IOBuffer>(0); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 932 | } else { |
| 933 | const size_t payload_length = kWebSocketCloseCodeLength + reason.length(); |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 934 | body = base::MakeRefCounted<IOBuffer>(payload_length); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 935 | size = payload_length; |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 936 | base::WriteBigEndian(body->data(), code); |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 937 | static_assert(sizeof(code) == kWebSocketCloseCodeLength, |
| 938 | "they should both be two"); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 939 | std::copy( |
| 940 | reason.begin(), reason.end(), body->data() + kWebSocketCloseCodeLength); |
| 941 | } |
Adam Rice | 250bb01 | 2020-05-26 15:56:10 | [diff] [blame] | 942 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 943 | return SendFrameInternal(true, WebSocketFrameHeader::kOpCodeClose, |
| 944 | std::move(body), size); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 945 | } |
| 946 | |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 947 | bool WebSocketChannel::ParseClose(base::span<const char> payload, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 948 | uint16_t* code, |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 949 | std::string* reason, |
| 950 | std::string* message) { |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 951 | const uint64_t size = static_cast<uint64_t>(payload.size()); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 952 | reason->clear(); |
| 953 | if (size < kWebSocketCloseCodeLength) { |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 954 | if (size == 0U) { |
| 955 | *code = kWebSocketErrorNoStatusReceived; |
| 956 | return true; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 957 | } |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 958 | |
| 959 | DVLOG(1) << "Close frame with payload size " << size << " received " |
| 960 | << "(the first byte is " << std::hex |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 961 | << static_cast<int>(payload.data()[0]) << ")"; |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 962 | *code = kWebSocketErrorProtocolError; |
| 963 | *message = |
| 964 | "Received a broken close frame containing an invalid size body."; |
| 965 | return false; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 966 | } |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 967 | |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 968 | const char* data = payload.data(); |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 969 | uint16_t unchecked_code = 0; |
Felix Weilbach | b7f34d81 | 2021-11-17 18:42:45 | [diff] [blame] | 970 | base::ReadBigEndian(reinterpret_cast<const uint8_t*>(data), &unchecked_code); |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 971 | static_assert(sizeof(unchecked_code) == kWebSocketCloseCodeLength, |
| 972 | "they should both be two bytes"); |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 973 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 974 | switch (unchecked_code) { |
| 975 | case kWebSocketErrorNoStatusReceived: |
| 976 | case kWebSocketErrorAbnormalClosure: |
| 977 | case kWebSocketErrorTlsHandshake: |
| 978 | *code = kWebSocketErrorProtocolError; |
| 979 | *message = |
| 980 | "Received a broken close frame containing a reserved status code."; |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 981 | return false; |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 982 | |
| 983 | default: |
| 984 | *code = unchecked_code; |
| 985 | break; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 986 | } |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 987 | |
| 988 | std::string text(data + kWebSocketCloseCodeLength, data + size); |
| 989 | if (StreamingUtf8Validator::Validate(text)) { |
| 990 | reason->swap(text); |
| 991 | return true; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 992 | } |
[email protected] | c443293 | 2014-03-19 05:55:13 | [diff] [blame] | 993 | |
| 994 | *code = kWebSocketErrorProtocolError; |
| 995 | *reason = "Invalid UTF-8 in Close frame"; |
| 996 | *message = "Received a broken close frame containing invalid UTF-8."; |
| 997 | return false; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 998 | } |
| 999 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 1000 | void WebSocketChannel::DoDropChannel(bool was_clean, |
| 1001 | uint16_t code, |
| 1002 | const std::string& reason) { |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 1003 | event_interface_->OnDropChannel(was_clean, code, reason); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1004 | } |
| 1005 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 1006 | void WebSocketChannel::CloseTimeout() { |
Nanami Mikiya | 8d6f8a13 | 2022-02-09 02:14:18 | [diff] [blame] | 1007 | stream_->GetNetLogWithSource().AddEvent( |
| 1008 | net::NetLogEventType::WEBSOCKET_CLOSE_TIMEOUT); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 1009 | stream_->Close(); |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame] | 1010 | SetState(CLOSED); |
pkasting | 47ceb87 | 2014-10-09 18:53:22 | [diff] [blame] | 1011 | DoDropChannel(false, kWebSocketErrorAbnormalClosure, ""); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1012 | // |this| has been deleted. |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 1013 | } |
| 1014 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1015 | } // namespace net |