[email protected] | adb225d | 2013-08-30 13:14:43 | [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_basic_stream.h" |
| 6 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 7 | #include <stddef.h> |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 8 | #include <stdint.h> |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <limits> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 11 | #include <utility> |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 12 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 13 | #include "base/bind.h" |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 14 | #include "base/command_line.h" |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 15 | #include "base/logging.h" |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 16 | #include "base/metrics/histogram_macros.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 17 | #include "base/numerics/safe_conversions.h" |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
Yoichi Osato | 13f94a76 | 2019-09-09 09:47:35 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 20 | #include "net/base/io_buffer.h" |
| 21 | #include "net/base/net_errors.h" |
| 22 | #include "net/socket/client_socket_handle.h" |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 23 | #include "net/websockets/websocket_basic_stream_adapters.h" |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 24 | #include "net/websockets/websocket_errors.h" |
| 25 | #include "net/websockets/websocket_frame.h" |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 26 | |
| 27 | namespace net { |
| 28 | |
| 29 | namespace { |
| 30 | |
Ramin Halavati | cdb199a6 | 2018-01-25 12:38:19 | [diff] [blame] | 31 | // Please refer to the comment in class header if the usage changes. |
| 32 | constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation = |
| 33 | net::DefineNetworkTrafficAnnotation("websocket_basic_stream", R"( |
| 34 | semantics { |
| 35 | sender: "WebSocket Basic Stream" |
| 36 | description: |
| 37 | "Implementation of WebSocket API from web content (a page the user " |
| 38 | "visits)." |
| 39 | trigger: "Website calls the WebSocket API." |
| 40 | data: |
| 41 | "Any data provided by web content, masked and framed in accordance " |
| 42 | "with RFC6455." |
| 43 | destination: OTHER |
| 44 | destination_other: |
| 45 | "The address that the website has chosen to communicate to." |
| 46 | } |
| 47 | policy { |
| 48 | cookies_allowed: YES |
| 49 | cookies_store: "user" |
| 50 | setting: "These requests cannot be disabled." |
| 51 | policy_exception_justification: |
| 52 | "Not implemented. WebSocket is a core web platform API." |
| 53 | } |
| 54 | comments: |
| 55 | "The browser will never add cookies to a WebSocket message. But the " |
| 56 | "handshake that was performed when the WebSocket connection was " |
| 57 | "established may have contained cookies." |
| 58 | )"); |
| 59 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 60 | // This uses type uint64_t to match the definition of |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 61 | // WebSocketFrameHeader::payload_length in websocket_frame.h. |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 62 | const uint64_t kMaxControlFramePayload = 125; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 63 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 64 | // The number of bytes to attempt to read at a time. |
| 65 | // TODO(ricea): See if there is a better number or algorithm to fulfill our |
| 66 | // requirements: |
| 67 | // 1. We would like to use minimal memory on low-bandwidth or idle connections |
| 68 | // 2. We would like to read as close to line speed as possible on |
| 69 | // high-bandwidth connections |
| 70 | // 3. We can't afford to cause jank on the IO thread by copying large buffers |
| 71 | // around |
| 72 | // 4. We would like to hit any sweet-spots that might exist in terms of network |
| 73 | // packet sizes / encryption block sizes / IPC alignment issues, etc. |
Yoichi Osato | 13f94a76 | 2019-09-09 09:47:35 | [diff] [blame] | 74 | #if defined(OS_ANDROID) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 75 | const int kReadBufferSize = 32 * 1024; |
Yoichi Osato | 13f94a76 | 2019-09-09 09:47:35 | [diff] [blame] | 76 | #else |
Yoichi Osato | ba82cef | 2019-09-10 04:16:05 | [diff] [blame] | 77 | // |2^n - delta| is better than 2^n on Linux. See crrev.com/c/1792208. |
Yoichi Osato | 13f94a76 | 2019-09-09 09:47:35 | [diff] [blame] | 78 | const int kReadBufferSize = 131000; |
| 79 | #endif |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 80 | |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 81 | // Returns the total serialized size of |frames|. This function assumes that |
| 82 | // |frames| will be serialized with mask field. This function forces the |
| 83 | // masked bit of the frames on. |
| 84 | int CalculateSerializedSizeAndTurnOnMaskBit( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 85 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 86 | const uint64_t kMaximumTotalSize = std::numeric_limits<int>::max(); |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 87 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 88 | uint64_t total_size = 0; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 89 | for (const auto& frame : *frames) { |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 90 | // Force the masked bit on. |
| 91 | frame->header.masked = true; |
| 92 | // We enforce flow control so the renderer should never be able to force us |
| 93 | // to cache anywhere near 2GB of frames. |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 94 | uint64_t frame_size = frame->header.payload_length + |
| 95 | GetWebSocketFrameHeaderSize(frame->header); |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 96 | CHECK_LE(frame_size, kMaximumTotalSize - total_size) |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 97 | << "Aborting to prevent overflow"; |
| 98 | total_size += frame_size; |
| 99 | } |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 100 | return static_cast<int>(total_size); |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 103 | } // namespace |
| 104 | |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 105 | // Overrides default read buffer size for WebSocket. This flag will be used to |
| 106 | // investigate the performance issue of crbug.com/865001 and be deleted later |
| 107 | // on. |
| 108 | const char kWebSocketReadBufferSize[] = "websocket-read-buffer-size"; |
| 109 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 110 | WebSocketBasicStream::WebSocketBasicStream( |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 111 | std::unique_ptr<Adapter> connection, |
[email protected] | 86602d3f | 2013-11-06 00:08:22 | [diff] [blame] | 112 | const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 113 | const std::string& sub_protocol, |
| 114 | const std::string& extensions) |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 115 | : connection_(std::move(connection)), |
[email protected] | 86602d3f | 2013-11-06 00:08:22 | [diff] [blame] | 116 | http_read_buffer_(http_read_buffer), |
| 117 | sub_protocol_(sub_protocol), |
| 118 | extensions_(extensions), |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 119 | generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) { |
[email protected] | 50133d7a | 2013-11-07 13:08:36 | [diff] [blame] | 120 | // http_read_buffer_ should not be set if it contains no data. |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 121 | if (http_read_buffer_.get() && http_read_buffer_->offset() == 0) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 122 | http_read_buffer_ = nullptr; |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 123 | DCHECK(connection_->is_initialized()); |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 124 | base::CommandLine* const command_line = |
| 125 | base::CommandLine::ForCurrentProcess(); |
| 126 | DCHECK(command_line); |
| 127 | int websocket_buffer_size = kReadBufferSize; |
| 128 | if (command_line->HasSwitch(kWebSocketReadBufferSize)) { |
| 129 | std::string size_string = |
| 130 | command_line->GetSwitchValueASCII(kWebSocketReadBufferSize); |
| 131 | if (!base::StringToInt(size_string, &websocket_buffer_size) || |
| 132 | websocket_buffer_size <= 0) { |
| 133 | websocket_buffer_size = kReadBufferSize; |
| 134 | } |
| 135 | } |
| 136 | DVLOG(1) << "WebSocketReadBufferSize is " << websocket_buffer_size; |
| 137 | read_buffer_ = |
| 138 | (base::MakeRefCounted<IOBufferWithSize>(websocket_buffer_size)); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | WebSocketBasicStream::~WebSocketBasicStream() { Close(); } |
| 142 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 143 | int WebSocketBasicStream::ReadFrames( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 144 | std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 145 | CompletionOnceCallback callback) { |
| 146 | read_callback_ = std::move(callback); |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 147 | complete_control_frame_body_.clear(); |
| 148 | if (http_read_buffer_ && is_http_read_buffer_decoded_) { |
| 149 | http_read_buffer_.reset(); |
| 150 | } |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 151 | return ReadEverything(frames); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 152 | } |
| 153 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 154 | int WebSocketBasicStream::WriteFrames( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 155 | std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 156 | CompletionOnceCallback callback) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 157 | // This function always concatenates all frames into a single buffer. |
| 158 | // TODO(ricea): Investigate whether it would be better in some cases to |
| 159 | // perform multiple writes with smaller buffers. |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 160 | |
| 161 | write_callback_ = std::move(callback); |
| 162 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 163 | // First calculate the size of the buffer we need to allocate. |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 164 | int total_size = CalculateSerializedSizeAndTurnOnMaskBit(frames); |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 165 | auto combined_buffer = base::MakeRefCounted<IOBufferWithSize>(total_size); |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 166 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 167 | char* dest = combined_buffer->data(); |
| 168 | int remaining_size = total_size; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 169 | for (const auto& frame : *frames) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 170 | WebSocketMaskingKey mask = generate_websocket_masking_key_(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 171 | int result = |
| 172 | WriteWebSocketFrameHeader(frame->header, &mask, dest, remaining_size); |
| 173 | DCHECK_NE(ERR_INVALID_ARGUMENT, result) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 174 | << "WriteWebSocketFrameHeader() says that " << remaining_size |
| 175 | << " is not enough to write the header in. This should not happen."; |
| 176 | CHECK_GE(result, 0) << "Potentially security-critical check failed"; |
| 177 | dest += result; |
| 178 | remaining_size -= result; |
| 179 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 180 | CHECK_LE(frame->header.payload_length, |
| 181 | static_cast<uint64_t>(remaining_size)); |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 182 | const int frame_size = static_cast<int>(frame->header.payload_length); |
[email protected] | 403ee6e | 2014-01-27 10:10:44 | [diff] [blame] | 183 | if (frame_size > 0) { |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 184 | const char* const frame_data = frame->payload; |
[email protected] | 403ee6e | 2014-01-27 10:10:44 | [diff] [blame] | 185 | std::copy(frame_data, frame_data + frame_size, dest); |
| 186 | MaskWebSocketFramePayload(mask, 0, dest, frame_size); |
| 187 | dest += frame_size; |
| 188 | remaining_size -= frame_size; |
| 189 | } |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 190 | } |
| 191 | DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; " |
| 192 | << remaining_size << " bytes left over."; |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 193 | auto drainable_buffer = base::MakeRefCounted<DrainableIOBuffer>( |
| 194 | combined_buffer.get(), total_size); |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 195 | return WriteEverything(drainable_buffer); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 196 | } |
| 197 | |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 198 | void WebSocketBasicStream::Close() { |
| 199 | connection_->Disconnect(); |
| 200 | } |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 201 | |
| 202 | std::string WebSocketBasicStream::GetSubProtocol() const { |
| 203 | return sub_protocol_; |
| 204 | } |
| 205 | |
| 206 | std::string WebSocketBasicStream::GetExtensions() const { return extensions_; } |
| 207 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 208 | /*static*/ |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 209 | std::unique_ptr<WebSocketBasicStream> |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 210 | WebSocketBasicStream::CreateWebSocketBasicStreamForTesting( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 211 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 212 | const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 213 | const std::string& sub_protocol, |
| 214 | const std::string& extensions, |
| 215 | WebSocketMaskingKeyGeneratorFunction key_generator_function) { |
Bence Béky | 7294fc2 | 2018-02-08 14:26:17 | [diff] [blame] | 216 | auto stream = std::make_unique<WebSocketBasicStream>( |
| 217 | std::make_unique<WebSocketClientSocketHandleAdapter>( |
| 218 | std::move(connection)), |
| 219 | http_read_buffer, sub_protocol, extensions); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 220 | stream->generate_websocket_masking_key_ = key_generator_function; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 221 | return stream; |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 222 | } |
| 223 | |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 224 | int WebSocketBasicStream::ReadEverything( |
| 225 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
| 226 | DCHECK(frames->empty()); |
| 227 | |
| 228 | // If there is data left over after parsing the HTTP headers, attempt to parse |
| 229 | // it as WebSocket frames. |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 230 | if (http_read_buffer_.get() && !is_http_read_buffer_decoded_) { |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 231 | DCHECK_GE(http_read_buffer_->offset(), 0); |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 232 | is_http_read_buffer_decoded_ = true; |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 233 | std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 234 | if (!parser_.Decode(http_read_buffer_->StartOfBuffer(), |
| 235 | http_read_buffer_->offset(), &frame_chunks)) |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 236 | return WebSocketErrorToNetError(parser_.websocket_error()); |
| 237 | if (!frame_chunks.empty()) { |
| 238 | int result = ConvertChunksToFrames(&frame_chunks, frames); |
| 239 | if (result != ERR_IO_PENDING) |
| 240 | return result; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Run until socket stops giving us data or we get some frames. |
| 245 | while (true) { |
| 246 | // base::Unretained(this) here is safe because net::Socket guarantees not to |
| 247 | // call any callbacks after Disconnect(), which we call from the destructor. |
| 248 | // The caller of ReadEverything() is required to keep |frames| valid. |
| 249 | int result = connection_->Read( |
| 250 | read_buffer_.get(), read_buffer_->size(), |
| 251 | base::BindOnce(&WebSocketBasicStream::OnReadComplete, |
| 252 | base::Unretained(this), base::Unretained(frames))); |
| 253 | if (result == ERR_IO_PENDING) |
| 254 | return result; |
| 255 | result = HandleReadResult(result, frames); |
| 256 | if (result != ERR_IO_PENDING) |
| 257 | return result; |
| 258 | DCHECK(frames->empty()); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void WebSocketBasicStream::OnReadComplete( |
| 263 | std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
| 264 | int result) { |
| 265 | result = HandleReadResult(result, frames); |
| 266 | if (result == ERR_IO_PENDING) |
| 267 | result = ReadEverything(frames); |
| 268 | if (result != ERR_IO_PENDING) |
| 269 | std::move(read_callback_).Run(result); |
| 270 | } |
| 271 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 272 | int WebSocketBasicStream::WriteEverything( |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 273 | const scoped_refptr<DrainableIOBuffer>& buffer) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 274 | while (buffer->BytesRemaining() > 0) { |
| 275 | // The use of base::Unretained() here is safe because on destruction we |
| 276 | // disconnect the socket, preventing any further callbacks. |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 277 | int result = connection_->Write( |
| 278 | buffer.get(), buffer->BytesRemaining(), |
| 279 | base::BindOnce(&WebSocketBasicStream::OnWriteComplete, |
| 280 | base::Unretained(this), buffer), |
| 281 | kTrafficAnnotation); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 282 | if (result > 0) { |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 283 | UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 284 | buffer->DidConsume(result); |
| 285 | } else { |
| 286 | return result; |
| 287 | } |
| 288 | } |
| 289 | return OK; |
| 290 | } |
| 291 | |
| 292 | void WebSocketBasicStream::OnWriteComplete( |
| 293 | const scoped_refptr<DrainableIOBuffer>& buffer, |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 294 | int result) { |
| 295 | if (result < 0) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 296 | DCHECK_NE(ERR_IO_PENDING, result); |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 297 | std::move(write_callback_).Run(result); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 301 | DCHECK_NE(0, result); |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 302 | UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result); |
| 303 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 304 | buffer->DidConsume(result); |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 305 | result = WriteEverything(buffer); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 306 | if (result != ERR_IO_PENDING) |
Bence Béky | f4f56e2 | 2018-07-17 02:00:05 | [diff] [blame] | 307 | std::move(write_callback_).Run(result); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | int WebSocketBasicStream::HandleReadResult( |
| 311 | int result, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 312 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 313 | DCHECK_NE(ERR_IO_PENDING, result); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 314 | DCHECK(frames->empty()); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 315 | if (result < 0) |
| 316 | return result; |
| 317 | if (result == 0) |
| 318 | return ERR_CONNECTION_CLOSED; |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 319 | |
| 320 | UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Downstream", result); |
| 321 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 322 | std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 323 | if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks)) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 324 | return WebSocketErrorToNetError(parser_.websocket_error()); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 325 | if (frame_chunks.empty()) |
| 326 | return ERR_IO_PENDING; |
| 327 | return ConvertChunksToFrames(&frame_chunks, frames); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 330 | int WebSocketBasicStream::ConvertChunksToFrames( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 331 | std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks, |
| 332 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 333 | for (size_t i = 0; i < frame_chunks->size(); ++i) { |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 334 | auto& chunk = (*frame_chunks)[i]; |
| 335 | DCHECK(chunk == frame_chunks->back() || chunk->final_chunk) |
| 336 | << "Only last chunk can have |final_chunk| set to be false."; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 337 | std::unique_ptr<WebSocketFrame> frame; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 338 | int result = ConvertChunkToFrame(std::move(chunk), &frame); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 339 | if (result != OK) |
| 340 | return result; |
| 341 | if (frame) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 342 | frames->push_back(std::move(frame)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 343 | } |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 344 | frame_chunks->clear(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 345 | if (frames->empty()) |
| 346 | return ERR_IO_PENDING; |
| 347 | return OK; |
| 348 | } |
| 349 | |
| 350 | int WebSocketBasicStream::ConvertChunkToFrame( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 351 | std::unique_ptr<WebSocketFrameChunk> chunk, |
| 352 | std::unique_ptr<WebSocketFrame>* frame) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 353 | DCHECK(frame->get() == nullptr); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 354 | bool is_first_chunk = false; |
| 355 | if (chunk->header) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 356 | DCHECK(current_frame_header_ == nullptr) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 357 | << "Received the header for a new frame without notification that " |
| 358 | << "the previous frame was complete (bug in WebSocketFrameParser?)"; |
| 359 | is_first_chunk = true; |
| 360 | current_frame_header_.swap(chunk->header); |
| 361 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 362 | DCHECK(current_frame_header_) << "Unexpected header-less chunk received " |
| 363 | << "(final_chunk = " << chunk->final_chunk |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 364 | << ", payload size = " << chunk->payload.size() |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 365 | << ") (bug in WebSocketFrameParser?)"; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 366 | const bool is_final_chunk = chunk->final_chunk; |
| 367 | const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; |
| 368 | if (WebSocketFrameHeader::IsKnownControlOpCode(opcode)) { |
| 369 | bool protocol_error = false; |
| 370 | if (!current_frame_header_->final) { |
| 371 | DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode |
| 372 | << " received with FIN bit unset."; |
| 373 | protocol_error = true; |
| 374 | } |
| 375 | if (current_frame_header_->payload_length > kMaxControlFramePayload) { |
| 376 | DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode |
| 377 | << ", payload_length=" << current_frame_header_->payload_length |
| 378 | << " exceeds maximum payload length for a control message."; |
| 379 | protocol_error = true; |
| 380 | } |
| 381 | if (protocol_error) { |
| 382 | current_frame_header_.reset(); |
| 383 | return ERR_WS_PROTOCOL_ERROR; |
| 384 | } |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 385 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 386 | if (!is_final_chunk) { |
| 387 | DVLOG(2) << "Encountered a split control frame, opcode " << opcode; |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 388 | AddToIncompleteControlFrameBody(chunk->payload); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 389 | return OK; |
| 390 | } |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 391 | |
| 392 | if (!incomplete_control_frame_body_.empty()) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 393 | DVLOG(2) << "Rejoining a split control frame, opcode " << opcode; |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 394 | AddToIncompleteControlFrameBody(chunk->payload); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 395 | DCHECK(is_final_chunk); |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 396 | DCHECK(complete_control_frame_body_.empty()); |
| 397 | complete_control_frame_body_ = std::move(incomplete_control_frame_body_); |
| 398 | *frame = CreateFrame(is_final_chunk, complete_control_frame_body_); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 399 | return OK; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Apply basic sanity checks to the |payload_length| field from the frame |
| 404 | // header. A check for exact equality can only be used when the whole frame |
| 405 | // arrives in one chunk. |
| 406 | DCHECK_GE(current_frame_header_->payload_length, |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 407 | base::checked_cast<uint64_t>(chunk->payload.size())); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 408 | DCHECK(!is_first_chunk || !is_final_chunk || |
| 409 | current_frame_header_->payload_length == |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 410 | base::checked_cast<uint64_t>(chunk->payload.size())); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 411 | |
| 412 | // Convert the chunk to a complete frame. |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 413 | *frame = CreateFrame(is_final_chunk, chunk->payload); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 414 | return OK; |
| 415 | } |
| 416 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 417 | std::unique_ptr<WebSocketFrame> WebSocketBasicStream::CreateFrame( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 418 | bool is_final_chunk, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 419 | base::span<const char> data) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 420 | std::unique_ptr<WebSocketFrame> result_frame; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 421 | const bool is_final_chunk_in_message = |
| 422 | is_final_chunk && current_frame_header_->final; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 423 | const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; |
[email protected] | e678d4fe | 2013-10-11 11:27:55 | [diff] [blame] | 424 | // Empty frames convey no useful information unless they are the first frame |
| 425 | // (containing the type and flags) or have the "final" bit set. |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 426 | if (is_final_chunk_in_message || data.size() > 0 || |
[email protected] | e678d4fe | 2013-10-11 11:27:55 | [diff] [blame] | 427 | current_frame_header_->opcode != |
| 428 | WebSocketFrameHeader::kOpCodeContinuation) { |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 429 | result_frame = std::make_unique<WebSocketFrame>(opcode); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 430 | result_frame->header.CopyFrom(*current_frame_header_); |
| 431 | result_frame->header.final = is_final_chunk_in_message; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 432 | result_frame->header.payload_length = data.size(); |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 433 | result_frame->payload = data.data(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 434 | // Ensure that opcodes Text and Binary are only used for the first frame in |
[email protected] | d52c0c4 | 2014-02-19 08:27:47 | [diff] [blame] | 435 | // the message. Also clear the reserved bits. |
| 436 | // TODO(ricea): If a future extension requires the reserved bits to be |
| 437 | // retained on continuation frames, make this behaviour conditional on a |
| 438 | // flag set at construction time. |
| 439 | if (!is_final_chunk && WebSocketFrameHeader::IsKnownDataOpCode(opcode)) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 440 | current_frame_header_->opcode = WebSocketFrameHeader::kOpCodeContinuation; |
[email protected] | d52c0c4 | 2014-02-19 08:27:47 | [diff] [blame] | 441 | current_frame_header_->reserved1 = false; |
| 442 | current_frame_header_->reserved2 = false; |
| 443 | current_frame_header_->reserved3 = false; |
| 444 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 445 | } |
| 446 | // Make sure that a frame header is not applied to any chunks that do not |
| 447 | // belong to it. |
| 448 | if (is_final_chunk) |
| 449 | current_frame_header_.reset(); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 450 | return result_frame; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void WebSocketBasicStream::AddToIncompleteControlFrameBody( |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 454 | base::span<const char> data) { |
| 455 | if (data.empty()) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 456 | return; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 457 | } |
| 458 | incomplete_control_frame_body_.insert(incomplete_control_frame_body_.end(), |
| 459 | data.begin(), data.end()); |
| 460 | // This method checks for oversize control frames above, so as long as |
| 461 | // the frame parser is working correctly, this won't overflow. If a bug |
| 462 | // does cause it to overflow, it will CHECK() in |
| 463 | // AddToIncompleteControlFrameBody() without writing outside the buffer. |
| 464 | CHECK_LE(incomplete_control_frame_body_.size(), kMaxControlFramePayload) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 465 | << "Control frame body larger than frame header indicates; frame parser " |
| 466 | "bug?"; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 467 | } |
| 468 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 469 | } // namespace net |