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