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