[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> |
| 11 | #include <string> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 12 | #include <utility> |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 15 | #include "base/bind.h" |
| 16 | #include "base/logging.h" |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 17 | #include "base/metrics/histogram_macros.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 18 | #include "base/numerics/safe_conversions.h" |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 19 | #include "net/base/io_buffer.h" |
| 20 | #include "net/base/net_errors.h" |
| 21 | #include "net/socket/client_socket_handle.h" |
Ramin Halavati | cdb199a6 | 2018-01-25 12:38:19 | [diff] [blame^] | 22 | #include "net/traffic_annotation/network_traffic_annotation.h" |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 23 | #include "net/websockets/websocket_errors.h" |
| 24 | #include "net/websockets/websocket_frame.h" |
| 25 | #include "net/websockets/websocket_frame_parser.h" |
| 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. |
| 74 | const int kReadBufferSize = 32 * 1024; |
| 75 | |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 76 | // Returns the total serialized size of |frames|. This function assumes that |
| 77 | // |frames| will be serialized with mask field. This function forces the |
| 78 | // masked bit of the frames on. |
| 79 | int CalculateSerializedSizeAndTurnOnMaskBit( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 80 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 81 | const uint64_t kMaximumTotalSize = std::numeric_limits<int>::max(); |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 82 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 83 | uint64_t total_size = 0; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 84 | for (const auto& frame : *frames) { |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 85 | // Force the masked bit on. |
| 86 | frame->header.masked = true; |
| 87 | // We enforce flow control so the renderer should never be able to force us |
| 88 | // to cache anywhere near 2GB of frames. |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 89 | uint64_t frame_size = frame->header.payload_length + |
| 90 | GetWebSocketFrameHeaderSize(frame->header); |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 91 | CHECK_LE(frame_size, kMaximumTotalSize - total_size) |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 92 | << "Aborting to prevent overflow"; |
| 93 | total_size += frame_size; |
| 94 | } |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 95 | return static_cast<int>(total_size); |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 98 | } // namespace |
| 99 | |
| 100 | WebSocketBasicStream::WebSocketBasicStream( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 101 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | 86602d3f | 2013-11-06 00:08:22 | [diff] [blame] | 102 | const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 103 | const std::string& sub_protocol, |
| 104 | const std::string& extensions) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 105 | : read_buffer_(new IOBufferWithSize(kReadBufferSize)), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 106 | connection_(std::move(connection)), |
[email protected] | 86602d3f | 2013-11-06 00:08:22 | [diff] [blame] | 107 | http_read_buffer_(http_read_buffer), |
| 108 | sub_protocol_(sub_protocol), |
| 109 | extensions_(extensions), |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 110 | generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) { |
[email protected] | 50133d7a | 2013-11-07 13:08:36 | [diff] [blame] | 111 | // http_read_buffer_ should not be set if it contains no data. |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 112 | if (http_read_buffer_.get() && http_read_buffer_->offset() == 0) |
[email protected] | 50133d7a | 2013-11-07 13:08:36 | [diff] [blame] | 113 | http_read_buffer_ = NULL; |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 114 | DCHECK(connection_->is_initialized()); |
| 115 | } |
| 116 | |
| 117 | WebSocketBasicStream::~WebSocketBasicStream() { Close(); } |
| 118 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 119 | int WebSocketBasicStream::ReadFrames( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 120 | std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 121 | const CompletionCallback& callback) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 122 | DCHECK(frames->empty()); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 123 | // If there is data left over after parsing the HTTP headers, attempt to parse |
| 124 | // it as WebSocket frames. |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 125 | if (http_read_buffer_.get()) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 126 | DCHECK_GE(http_read_buffer_->offset(), 0); |
| 127 | // We cannot simply copy the data into read_buffer_, as it might be too |
| 128 | // large. |
| 129 | scoped_refptr<GrowableIOBuffer> buffered_data; |
| 130 | buffered_data.swap(http_read_buffer_); |
melandory | 1346cde | 2016-06-11 00:42:12 | [diff] [blame] | 131 | DCHECK(!http_read_buffer_); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 132 | std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks; |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 133 | if (!parser_.Decode(buffered_data->StartOfBuffer(), |
| 134 | buffered_data->offset(), |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 135 | &frame_chunks)) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 136 | return WebSocketErrorToNetError(parser_.websocket_error()); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 137 | if (!frame_chunks.empty()) { |
| 138 | int result = ConvertChunksToFrames(&frame_chunks, frames); |
| 139 | if (result != ERR_IO_PENDING) |
| 140 | return result; |
| 141 | } |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 144 | // Run until socket stops giving us data or we get some frames. |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 145 | while (true) { |
| 146 | // base::Unretained(this) here is safe because net::Socket guarantees not to |
| 147 | // call any callbacks after Disconnect(), which we call from the |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 148 | // destructor. The caller of ReadFrames() is required to keep |frames| |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 149 | // valid. |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 150 | int result = connection_->socket()->Read( |
| 151 | read_buffer_.get(), |
| 152 | read_buffer_->size(), |
| 153 | base::Bind(&WebSocketBasicStream::OnReadComplete, |
| 154 | base::Unretained(this), |
| 155 | base::Unretained(frames), |
| 156 | callback)); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 157 | if (result == ERR_IO_PENDING) |
| 158 | return result; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 159 | result = HandleReadResult(result, frames); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 160 | if (result != ERR_IO_PENDING) |
| 161 | return result; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 162 | DCHECK(frames->empty()); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 166 | int WebSocketBasicStream::WriteFrames( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 167 | std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 168 | const CompletionCallback& callback) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 169 | // This function always concatenates all frames into a single buffer. |
| 170 | // TODO(ricea): Investigate whether it would be better in some cases to |
| 171 | // perform multiple writes with smaller buffers. |
| 172 | // |
| 173 | // First calculate the size of the buffer we need to allocate. |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 174 | int total_size = CalculateSerializedSizeAndTurnOnMaskBit(frames); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 175 | scoped_refptr<IOBufferWithSize> combined_buffer( |
| 176 | new IOBufferWithSize(total_size)); |
[email protected] | 9576544a | 2013-10-11 08:36:33 | [diff] [blame] | 177 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 178 | char* dest = combined_buffer->data(); |
| 179 | int remaining_size = total_size; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 180 | for (const auto& frame : *frames) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 181 | WebSocketMaskingKey mask = generate_websocket_masking_key_(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 182 | int result = |
| 183 | WriteWebSocketFrameHeader(frame->header, &mask, dest, remaining_size); |
| 184 | DCHECK_NE(ERR_INVALID_ARGUMENT, result) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 185 | << "WriteWebSocketFrameHeader() says that " << remaining_size |
| 186 | << " is not enough to write the header in. This should not happen."; |
| 187 | CHECK_GE(result, 0) << "Potentially security-critical check failed"; |
| 188 | dest += result; |
| 189 | remaining_size -= result; |
| 190 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 191 | CHECK_LE(frame->header.payload_length, |
| 192 | static_cast<uint64_t>(remaining_size)); |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 193 | const int frame_size = static_cast<int>(frame->header.payload_length); |
[email protected] | 403ee6e | 2014-01-27 10:10:44 | [diff] [blame] | 194 | if (frame_size > 0) { |
[email protected] | 403ee6e | 2014-01-27 10:10:44 | [diff] [blame] | 195 | const char* const frame_data = frame->data->data(); |
| 196 | std::copy(frame_data, frame_data + frame_size, dest); |
| 197 | MaskWebSocketFramePayload(mask, 0, dest, frame_size); |
| 198 | dest += frame_size; |
| 199 | remaining_size -= frame_size; |
| 200 | } |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 201 | } |
| 202 | DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; " |
| 203 | << remaining_size << " bytes left over."; |
| 204 | scoped_refptr<DrainableIOBuffer> drainable_buffer( |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 205 | new DrainableIOBuffer(combined_buffer.get(), total_size)); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 206 | return WriteEverything(drainable_buffer, callback); |
| 207 | } |
| 208 | |
| 209 | void WebSocketBasicStream::Close() { connection_->socket()->Disconnect(); } |
| 210 | |
| 211 | std::string WebSocketBasicStream::GetSubProtocol() const { |
| 212 | return sub_protocol_; |
| 213 | } |
| 214 | |
| 215 | std::string WebSocketBasicStream::GetExtensions() const { return extensions_; } |
| 216 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 217 | /*static*/ |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 218 | std::unique_ptr<WebSocketBasicStream> |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 219 | WebSocketBasicStream::CreateWebSocketBasicStreamForTesting( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 220 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 221 | const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 222 | const std::string& sub_protocol, |
| 223 | const std::string& extensions, |
| 224 | WebSocketMaskingKeyGeneratorFunction key_generator_function) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 225 | std::unique_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 226 | std::move(connection), http_read_buffer, sub_protocol, extensions)); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 227 | stream->generate_websocket_masking_key_ = key_generator_function; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 228 | return stream; |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int WebSocketBasicStream::WriteEverything( |
| 232 | const scoped_refptr<DrainableIOBuffer>& buffer, |
| 233 | const CompletionCallback& callback) { |
| 234 | while (buffer->BytesRemaining() > 0) { |
| 235 | // The use of base::Unretained() here is safe because on destruction we |
| 236 | // disconnect the socket, preventing any further callbacks. |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 237 | int result = connection_->socket()->Write( |
Ramin Halavati | cdb199a6 | 2018-01-25 12:38:19 | [diff] [blame^] | 238 | buffer.get(), buffer->BytesRemaining(), |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 239 | base::Bind(&WebSocketBasicStream::OnWriteComplete, |
Ramin Halavati | cdb199a6 | 2018-01-25 12:38:19 | [diff] [blame^] | 240 | base::Unretained(this), buffer, callback), |
| 241 | kTrafficAnnotation); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 242 | if (result > 0) { |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 243 | UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 244 | buffer->DidConsume(result); |
| 245 | } else { |
| 246 | return result; |
| 247 | } |
| 248 | } |
| 249 | return OK; |
| 250 | } |
| 251 | |
| 252 | void WebSocketBasicStream::OnWriteComplete( |
| 253 | const scoped_refptr<DrainableIOBuffer>& buffer, |
| 254 | const CompletionCallback& callback, |
| 255 | int result) { |
| 256 | if (result < 0) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 257 | DCHECK_NE(ERR_IO_PENDING, result); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 258 | callback.Run(result); |
| 259 | return; |
| 260 | } |
| 261 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 262 | DCHECK_NE(0, result); |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 263 | UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result); |
| 264 | |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 265 | buffer->DidConsume(result); |
| 266 | result = WriteEverything(buffer, callback); |
| 267 | if (result != ERR_IO_PENDING) |
| 268 | callback.Run(result); |
| 269 | } |
| 270 | |
| 271 | int WebSocketBasicStream::HandleReadResult( |
| 272 | int result, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 273 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 274 | DCHECK_NE(ERR_IO_PENDING, result); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 275 | DCHECK(frames->empty()); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 276 | if (result < 0) |
| 277 | return result; |
| 278 | if (result == 0) |
| 279 | return ERR_CONNECTION_CLOSED; |
rajendrant | 75fe34f | 2017-03-28 05:53:00 | [diff] [blame] | 280 | |
| 281 | UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Downstream", result); |
| 282 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 283 | std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 284 | if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks)) |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 285 | return WebSocketErrorToNetError(parser_.websocket_error()); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 286 | if (frame_chunks.empty()) |
| 287 | return ERR_IO_PENDING; |
| 288 | return ConvertChunksToFrames(&frame_chunks, frames); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 289 | } |
| 290 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 291 | int WebSocketBasicStream::ConvertChunksToFrames( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 292 | std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks, |
| 293 | std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 294 | for (size_t i = 0; i < frame_chunks->size(); ++i) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 295 | std::unique_ptr<WebSocketFrame> frame; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 296 | int result = ConvertChunkToFrame(std::move((*frame_chunks)[i]), &frame); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 297 | if (result != OK) |
| 298 | return result; |
| 299 | if (frame) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 300 | frames->push_back(std::move(frame)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 301 | } |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 302 | frame_chunks->clear(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 303 | if (frames->empty()) |
| 304 | return ERR_IO_PENDING; |
| 305 | return OK; |
| 306 | } |
| 307 | |
| 308 | int WebSocketBasicStream::ConvertChunkToFrame( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 309 | std::unique_ptr<WebSocketFrameChunk> chunk, |
| 310 | std::unique_ptr<WebSocketFrame>* frame) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 311 | DCHECK(frame->get() == NULL); |
| 312 | bool is_first_chunk = false; |
| 313 | if (chunk->header) { |
| 314 | DCHECK(current_frame_header_ == NULL) |
| 315 | << "Received the header for a new frame without notification that " |
| 316 | << "the previous frame was complete (bug in WebSocketFrameParser?)"; |
| 317 | is_first_chunk = true; |
| 318 | current_frame_header_.swap(chunk->header); |
| 319 | } |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 320 | const int chunk_size = chunk->data.get() ? chunk->data->size() : 0; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 321 | DCHECK(current_frame_header_) << "Unexpected header-less chunk received " |
| 322 | << "(final_chunk = " << chunk->final_chunk |
| 323 | << ", data size = " << chunk_size |
| 324 | << ") (bug in WebSocketFrameParser?)"; |
| 325 | scoped_refptr<IOBufferWithSize> data_buffer; |
| 326 | data_buffer.swap(chunk->data); |
| 327 | const bool is_final_chunk = chunk->final_chunk; |
| 328 | const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; |
| 329 | if (WebSocketFrameHeader::IsKnownControlOpCode(opcode)) { |
| 330 | bool protocol_error = false; |
| 331 | if (!current_frame_header_->final) { |
| 332 | DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode |
| 333 | << " received with FIN bit unset."; |
| 334 | protocol_error = true; |
| 335 | } |
| 336 | if (current_frame_header_->payload_length > kMaxControlFramePayload) { |
| 337 | DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode |
| 338 | << ", payload_length=" << current_frame_header_->payload_length |
| 339 | << " exceeds maximum payload length for a control message."; |
| 340 | protocol_error = true; |
| 341 | } |
| 342 | if (protocol_error) { |
| 343 | current_frame_header_.reset(); |
| 344 | return ERR_WS_PROTOCOL_ERROR; |
| 345 | } |
| 346 | if (!is_final_chunk) { |
| 347 | DVLOG(2) << "Encountered a split control frame, opcode " << opcode; |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 348 | if (incomplete_control_frame_body_.get()) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 349 | DVLOG(3) << "Appending to an existing split control frame."; |
| 350 | AddToIncompleteControlFrameBody(data_buffer); |
| 351 | } else { |
| 352 | DVLOG(3) << "Creating new storage for an incomplete control frame."; |
| 353 | incomplete_control_frame_body_ = new GrowableIOBuffer(); |
| 354 | // This method checks for oversize control frames above, so as long as |
| 355 | // the frame parser is working correctly, this won't overflow. If a bug |
| 356 | // does cause it to overflow, it will CHECK() in |
| 357 | // AddToIncompleteControlFrameBody() without writing outside the buffer. |
| 358 | incomplete_control_frame_body_->SetCapacity(kMaxControlFramePayload); |
| 359 | AddToIncompleteControlFrameBody(data_buffer); |
| 360 | } |
| 361 | return OK; |
| 362 | } |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 363 | if (incomplete_control_frame_body_.get()) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 364 | DVLOG(2) << "Rejoining a split control frame, opcode " << opcode; |
| 365 | AddToIncompleteControlFrameBody(data_buffer); |
| 366 | const int body_size = incomplete_control_frame_body_->offset(); |
| 367 | DCHECK_EQ(body_size, |
| 368 | static_cast<int>(current_frame_header_->payload_length)); |
| 369 | scoped_refptr<IOBufferWithSize> body = new IOBufferWithSize(body_size); |
| 370 | memcpy(body->data(), |
| 371 | incomplete_control_frame_body_->StartOfBuffer(), |
| 372 | body_size); |
| 373 | incomplete_control_frame_body_ = NULL; // Frame now complete. |
| 374 | DCHECK(is_final_chunk); |
| 375 | *frame = CreateFrame(is_final_chunk, body); |
| 376 | return OK; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // Apply basic sanity checks to the |payload_length| field from the frame |
| 381 | // header. A check for exact equality can only be used when the whole frame |
| 382 | // arrives in one chunk. |
| 383 | DCHECK_GE(current_frame_header_->payload_length, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 384 | base::checked_cast<uint64_t>(chunk_size)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 385 | DCHECK(!is_first_chunk || !is_final_chunk || |
| 386 | current_frame_header_->payload_length == |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 387 | base::checked_cast<uint64_t>(chunk_size)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 388 | |
| 389 | // Convert the chunk to a complete frame. |
| 390 | *frame = CreateFrame(is_final_chunk, data_buffer); |
| 391 | return OK; |
| 392 | } |
| 393 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 394 | std::unique_ptr<WebSocketFrame> WebSocketBasicStream::CreateFrame( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 395 | bool is_final_chunk, |
| 396 | const scoped_refptr<IOBufferWithSize>& data) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 397 | std::unique_ptr<WebSocketFrame> result_frame; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 398 | const bool is_final_chunk_in_message = |
| 399 | is_final_chunk && current_frame_header_->final; |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 400 | const int data_size = data.get() ? data->size() : 0; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 401 | const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; |
[email protected] | e678d4fe | 2013-10-11 11:27:55 | [diff] [blame] | 402 | // Empty frames convey no useful information unless they are the first frame |
| 403 | // (containing the type and flags) or have the "final" bit set. |
| 404 | if (is_final_chunk_in_message || data_size > 0 || |
| 405 | current_frame_header_->opcode != |
| 406 | WebSocketFrameHeader::kOpCodeContinuation) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 407 | result_frame.reset(new WebSocketFrame(opcode)); |
| 408 | result_frame->header.CopyFrom(*current_frame_header_); |
| 409 | result_frame->header.final = is_final_chunk_in_message; |
| 410 | result_frame->header.payload_length = data_size; |
| 411 | result_frame->data = data; |
| 412 | // 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] | 413 | // the message. Also clear the reserved bits. |
| 414 | // TODO(ricea): If a future extension requires the reserved bits to be |
| 415 | // retained on continuation frames, make this behaviour conditional on a |
| 416 | // flag set at construction time. |
| 417 | if (!is_final_chunk && WebSocketFrameHeader::IsKnownDataOpCode(opcode)) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 418 | current_frame_header_->opcode = WebSocketFrameHeader::kOpCodeContinuation; |
[email protected] | d52c0c4 | 2014-02-19 08:27:47 | [diff] [blame] | 419 | current_frame_header_->reserved1 = false; |
| 420 | current_frame_header_->reserved2 = false; |
| 421 | current_frame_header_->reserved3 = false; |
| 422 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 423 | } |
| 424 | // Make sure that a frame header is not applied to any chunks that do not |
| 425 | // belong to it. |
| 426 | if (is_final_chunk) |
| 427 | current_frame_header_.reset(); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 428 | return result_frame; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void WebSocketBasicStream::AddToIncompleteControlFrameBody( |
| 432 | const scoped_refptr<IOBufferWithSize>& data_buffer) { |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 433 | if (!data_buffer.get()) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 434 | return; |
| 435 | const int new_offset = |
| 436 | incomplete_control_frame_body_->offset() + data_buffer->size(); |
| 437 | CHECK_GE(incomplete_control_frame_body_->capacity(), new_offset) |
| 438 | << "Control frame body larger than frame header indicates; frame parser " |
| 439 | "bug?"; |
| 440 | memcpy(incomplete_control_frame_body_->data(), |
| 441 | data_buffer->data(), |
| 442 | data_buffer->size()); |
| 443 | incomplete_control_frame_body_->set_offset(new_offset); |
| 444 | } |
| 445 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 446 | void WebSocketBasicStream::OnReadComplete( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 447 | std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 448 | const CompletionCallback& callback, |
| 449 | int result) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 450 | result = HandleReadResult(result, frames); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 451 | if (result == ERR_IO_PENDING) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 452 | result = ReadFrames(frames, callback); |
[email protected] | adb225d | 2013-08-30 13:14:43 | [diff] [blame] | 453 | if (result != ERR_IO_PENDING) |
| 454 | callback.Run(result); |
| 455 | } |
| 456 | |
| 457 | } // namespace net |