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 return ConvertChunksToFrames(&frame_chunks, frames); |
tyoshino (SeeGerritForStatus)
2013/09/19 06:45:12
empty |frames| may be returned without attempting
Adam Rice
2013/09/20 08:09:27
Good catch, thank you.
I was missing a test for t
| |
69 } | 74 } |
70 | 75 |
71 // Run until socket stops giving us data or we get some chunks. | 76 // Run until socket stops giving us data or we get some frames. |
72 while (true) { | 77 while (true) { |
73 // base::Unretained(this) here is safe because net::Socket guarantees not to | 78 // base::Unretained(this) here is safe because net::Socket guarantees not to |
74 // call any callbacks after Disconnect(), which we call from the | 79 // call any callbacks after Disconnect(), which we call from the |
75 // destructor. The caller of ReadFrames() is required to keep |frame_chunks| | 80 // destructor. The caller of ReadFrames() is required to keep |frames| |
76 // valid. | 81 // valid. |
77 int result = connection_->socket() | 82 int result = connection_->socket()->Read( |
78 ->Read(read_buffer_.get(), | 83 read_buffer_.get(), |
79 read_buffer_->size(), | 84 read_buffer_->size(), |
80 base::Bind(&WebSocketBasicStream::OnReadComplete, | 85 base::Bind(&WebSocketBasicStream::OnReadComplete, |
81 base::Unretained(this), | 86 base::Unretained(this), |
82 base::Unretained(frame_chunks), | 87 base::Unretained(frames), |
83 callback)); | 88 callback)); |
84 if (result == ERR_IO_PENDING) | 89 if (result == ERR_IO_PENDING) |
85 return result; | 90 return result; |
86 result = HandleReadResult(result, frame_chunks); | 91 result = HandleReadResult(result, frames); |
87 if (result != ERR_IO_PENDING) | 92 if (result != ERR_IO_PENDING) |
88 return result; | 93 return result; |
94 DCHECK(frames->empty()); | |
89 } | 95 } |
90 } | 96 } |
91 | 97 |
92 int WebSocketBasicStream::WriteFrames( | 98 int WebSocketBasicStream::WriteFrames(ScopedVector<WebSocketFrame>* frames, |
93 ScopedVector<WebSocketFrameChunk>* frame_chunks, | 99 const CompletionCallback& callback) { |
94 const CompletionCallback& callback) { | |
95 // This function always concatenates all frames into a single buffer. | 100 // This function always concatenates all frames into a single buffer. |
96 // TODO(ricea): Investigate whether it would be better in some cases to | 101 // TODO(ricea): Investigate whether it would be better in some cases to |
97 // perform multiple writes with smaller buffers. | 102 // perform multiple writes with smaller buffers. |
98 // | 103 // |
99 // First calculate the size of the buffer we need to allocate. | 104 // First calculate the size of the buffer we need to allocate. |
100 typedef ScopedVector<WebSocketFrameChunk>::const_iterator Iterator; | 105 typedef ScopedVector<WebSocketFrame>::const_iterator Iterator; |
101 const int kMaximumTotalSize = std::numeric_limits<int>::max(); | 106 const int kMaximumTotalSize = std::numeric_limits<int>::max(); |
102 int total_size = 0; | 107 int total_size = 0; |
103 for (Iterator it = frame_chunks->begin(); it != frame_chunks->end(); ++it) { | 108 for (Iterator it = frames->begin(); it != frames->end(); ++it) { |
104 WebSocketFrameChunk* chunk = *it; | 109 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. | 110 // Force the masked bit on. |
110 chunk->header->masked = true; | 111 frame->header.masked = true; |
111 // We enforce flow control so the renderer should never be able to force us | 112 // We enforce flow control so the renderer should never be able to force us |
112 // to cache anywhere near 2GB of frames. | 113 // to cache anywhere near 2GB of frames. |
113 int chunk_size = | 114 int frame_size = frame->header.payload_length + |
114 chunk->data->size() + GetWebSocketFrameHeaderSize(*(chunk->header)); | 115 GetWebSocketFrameHeaderSize(frame->header); |
115 CHECK_GE(kMaximumTotalSize - total_size, chunk_size) | 116 CHECK_GE(kMaximumTotalSize - total_size, frame_size) |
116 << "Aborting to prevent overflow"; | 117 << "Aborting to prevent overflow"; |
117 total_size += chunk_size; | 118 total_size += frame_size; |
118 } | 119 } |
119 scoped_refptr<IOBufferWithSize> combined_buffer( | 120 scoped_refptr<IOBufferWithSize> combined_buffer( |
120 new IOBufferWithSize(total_size)); | 121 new IOBufferWithSize(total_size)); |
121 char* dest = combined_buffer->data(); | 122 char* dest = combined_buffer->data(); |
122 int remaining_size = total_size; | 123 int remaining_size = total_size; |
123 for (Iterator it = frame_chunks->begin(); it != frame_chunks->end(); ++it) { | 124 for (Iterator it = frames->begin(); it != frames->end(); ++it) { |
124 WebSocketFrameChunk* chunk = *it; | 125 WebSocketFrame* frame = *it; |
125 WebSocketMaskingKey mask = generate_websocket_masking_key_(); | 126 WebSocketMaskingKey mask = generate_websocket_masking_key_(); |
126 int result = WriteWebSocketFrameHeader( | 127 int result = |
127 *(chunk->header), &mask, dest, remaining_size); | 128 WriteWebSocketFrameHeader(frame->header, &mask, dest, remaining_size); |
128 DCHECK(result != ERR_INVALID_ARGUMENT) | 129 DCHECK_NE(ERR_INVALID_ARGUMENT, result) |
129 << "WriteWebSocketFrameHeader() says that " << remaining_size | 130 << "WriteWebSocketFrameHeader() says that " << remaining_size |
130 << " is not enough to write the header in. This should not happen."; | 131 << " is not enough to write the header in. This should not happen."; |
131 CHECK_GE(result, 0) << "Potentially security-critical check failed"; | 132 CHECK_GE(result, 0) << "Potentially security-critical check failed"; |
132 dest += result; | 133 dest += result; |
133 remaining_size -= result; | 134 remaining_size -= result; |
134 | 135 |
135 const char* const frame_data = chunk->data->data(); | 136 const char* const frame_data = frame->data->data(); |
136 const int frame_size = chunk->data->size(); | 137 const int frame_size = frame->header.payload_length; |
137 CHECK_GE(remaining_size, frame_size); | 138 CHECK_GE(remaining_size, frame_size); |
138 std::copy(frame_data, frame_data + frame_size, dest); | 139 std::copy(frame_data, frame_data + frame_size, dest); |
139 MaskWebSocketFramePayload(mask, 0, dest, frame_size); | 140 MaskWebSocketFramePayload(mask, 0, dest, frame_size); |
140 dest += frame_size; | 141 dest += frame_size; |
141 remaining_size -= frame_size; | 142 remaining_size -= frame_size; |
142 } | 143 } |
143 DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; " | 144 DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; " |
144 << remaining_size << " bytes left over."; | 145 << remaining_size << " bytes left over."; |
145 scoped_refptr<DrainableIOBuffer> drainable_buffer( | 146 scoped_refptr<DrainableIOBuffer> drainable_buffer( |
146 new DrainableIOBuffer(combined_buffer, total_size)); | 147 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; | 190 stream->generate_websocket_masking_key_ = key_generator_function; |
190 return stream.Pass(); | 191 return stream.Pass(); |
191 } | 192 } |
192 | 193 |
193 int WebSocketBasicStream::WriteEverything( | 194 int WebSocketBasicStream::WriteEverything( |
194 const scoped_refptr<DrainableIOBuffer>& buffer, | 195 const scoped_refptr<DrainableIOBuffer>& buffer, |
195 const CompletionCallback& callback) { | 196 const CompletionCallback& callback) { |
196 while (buffer->BytesRemaining() > 0) { | 197 while (buffer->BytesRemaining() > 0) { |
197 // The use of base::Unretained() here is safe because on destruction we | 198 // The use of base::Unretained() here is safe because on destruction we |
198 // disconnect the socket, preventing any further callbacks. | 199 // disconnect the socket, preventing any further callbacks. |
199 int result = connection_->socket() | 200 int result = connection_->socket()->Write( |
200 ->Write(buffer.get(), | 201 buffer.get(), |
201 buffer->BytesRemaining(), | 202 buffer->BytesRemaining(), |
202 base::Bind(&WebSocketBasicStream::OnWriteComplete, | 203 base::Bind(&WebSocketBasicStream::OnWriteComplete, |
203 base::Unretained(this), | 204 base::Unretained(this), |
204 buffer, | 205 buffer, |
205 callback)); | 206 callback)); |
206 if (result > 0) { | 207 if (result > 0) { |
207 buffer->DidConsume(result); | 208 buffer->DidConsume(result); |
208 } else { | 209 } else { |
209 return result; | 210 return result; |
210 } | 211 } |
211 } | 212 } |
212 return OK; | 213 return OK; |
213 } | 214 } |
214 | 215 |
215 void WebSocketBasicStream::OnWriteComplete( | 216 void WebSocketBasicStream::OnWriteComplete( |
216 const scoped_refptr<DrainableIOBuffer>& buffer, | 217 const scoped_refptr<DrainableIOBuffer>& buffer, |
217 const CompletionCallback& callback, | 218 const CompletionCallback& callback, |
218 int result) { | 219 int result) { |
219 if (result < 0) { | 220 if (result < 0) { |
220 DCHECK(result != ERR_IO_PENDING); | 221 DCHECK_NE(ERR_IO_PENDING, result); |
221 callback.Run(result); | 222 callback.Run(result); |
222 return; | 223 return; |
223 } | 224 } |
224 | 225 |
225 DCHECK(result != 0); | 226 DCHECK_NE(0, result); |
226 buffer->DidConsume(result); | 227 buffer->DidConsume(result); |
227 result = WriteEverything(buffer, callback); | 228 result = WriteEverything(buffer, callback); |
228 if (result != ERR_IO_PENDING) | 229 if (result != ERR_IO_PENDING) |
229 callback.Run(result); | 230 callback.Run(result); |
230 } | 231 } |
231 | 232 |
232 int WebSocketBasicStream::HandleReadResult( | 233 int WebSocketBasicStream::HandleReadResult( |
233 int result, | 234 int result, |
234 ScopedVector<WebSocketFrameChunk>* frame_chunks) { | 235 ScopedVector<WebSocketFrame>* frames) { |
235 DCHECK_NE(ERR_IO_PENDING, result); | 236 DCHECK_NE(ERR_IO_PENDING, result); |
236 DCHECK(frame_chunks->empty()); | 237 DCHECK(frames->empty()); |
237 if (result < 0) | 238 if (result < 0) |
238 return result; | 239 return result; |
239 if (result == 0) | 240 if (result == 0) |
240 return ERR_CONNECTION_CLOSED; | 241 return ERR_CONNECTION_CLOSED; |
241 if (!parser_.Decode(read_buffer_->data(), result, frame_chunks)) | 242 ScopedVector<WebSocketFrameChunk> frame_chunks; |
243 if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks)) | |
242 return WebSocketErrorToNetError(parser_.websocket_error()); | 244 return WebSocketErrorToNetError(parser_.websocket_error()); |
243 if (!frame_chunks->empty()) | 245 if (frame_chunks.empty()) |
244 return OK; | 246 return ERR_IO_PENDING; |
245 return ERR_IO_PENDING; | 247 return ConvertChunksToFrames(&frame_chunks, frames); |
246 } | 248 } |
247 | 249 |
248 void WebSocketBasicStream::OnReadComplete( | 250 int WebSocketBasicStream::ConvertChunksToFrames( |
249 ScopedVector<WebSocketFrameChunk>* frame_chunks, | 251 ScopedVector<WebSocketFrameChunk>* frame_chunks, |
250 const CompletionCallback& callback, | 252 ScopedVector<WebSocketFrame>* frames) { |
251 int result) { | 253 for (size_t i = 0; i < frame_chunks->size(); ++i) { |
252 result = HandleReadResult(result, frame_chunks); | 254 scoped_ptr<WebSocketFrame> frame; |
255 int result = ConvertChunkToFrame( | |
256 scoped_ptr<WebSocketFrameChunk>((*frame_chunks)[i]), &frame); | |
257 (*frame_chunks)[i] = NULL; | |
258 if (result != OK) | |
259 return result; | |
260 if (frame) | |
261 frames->push_back(frame.release()); | |
262 } | |
263 // All the elements of |frame_chunks| are now NULL, so there is no point in | |
264 // calling delete on them all. | |
265 frame_chunks->weak_clear(); | |
266 if (frames->empty()) | |
267 return ERR_IO_PENDING; | |
268 return OK; | |
269 } | |
270 | |
271 int WebSocketBasicStream::ConvertChunkToFrame( | |
272 scoped_ptr<WebSocketFrameChunk> chunk, | |
273 scoped_ptr<WebSocketFrame>* frame) { | |
274 DCHECK(frame->get() == NULL); | |
275 bool is_first_chunk = false; | |
276 if (chunk->header) { | |
277 DCHECK(current_frame_header_ == NULL) | |
278 << "Received the header for a new frame without notification that " | |
279 << "the previous frame was complete (bug in WebSocketFrameParser?)"; | |
280 is_first_chunk = true; | |
281 current_frame_header_.swap(chunk->header); | |
282 } | |
283 const int chunk_size = chunk->data ? chunk->data->size() : 0; | |
284 DCHECK(current_frame_header_) << "Unexpected header-less chunk received " | |
285 << "(final_chunk = " << chunk->final_chunk | |
286 << ", data size = " << chunk_size | |
287 << ") (bug in WebSocketFrameParser?)"; | |
288 scoped_refptr<IOBufferWithSize> data_buffer; | |
289 data_buffer.swap(chunk->data); | |
290 const bool is_final_chunk = chunk->final_chunk; | |
291 const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; | |
292 if (WebSocketFrameHeader::IsKnownControlOpCode(opcode)) { | |
293 bool protocol_error = false; | |
294 if (!current_frame_header_->final) { | |
295 DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode | |
296 << " received with FIN bit unset."; | |
297 protocol_error = true; | |
298 } | |
299 if (current_frame_header_->payload_length > kMaxControlFramePayload) { | |
300 DVLOG(1) << "WebSocket protocol error. Control frame, opcode=" << opcode | |
301 << ", payload_length=" << current_frame_header_->payload_length | |
302 << " exceeds maximum payload length for a control message."; | |
303 protocol_error = true; | |
304 } | |
305 if (protocol_error) { | |
306 current_frame_header_.reset(); | |
307 return ERR_WS_PROTOCOL_ERROR; | |
308 } | |
309 if (!is_final_chunk) { | |
310 DVLOG(2) << "Encountered a split control frame, opcode " << opcode; | |
311 if (incomplete_control_frame_body_) { | |
312 DVLOG(3) << "Appending to an existing split control frame."; | |
313 AddToIncompleteControlFrameBody(data_buffer); | |
314 } else { | |
315 DVLOG(3) << "Creating new storage for an incomplete control frame."; | |
316 incomplete_control_frame_body_ = new GrowableIOBuffer(); | |
317 // This method checks for oversize control frames above, so as long as | |
318 // the frame parser is working correctly, this won't overflow. If a bug | |
319 // does cause it to overflow, it will CHECK() in | |
320 // AddToIncompleteControlFrameBody() without writing outside the buffer. | |
321 incomplete_control_frame_body_->SetCapacity(kMaxControlFramePayload); | |
322 AddToIncompleteControlFrameBody(data_buffer); | |
323 } | |
324 return OK; | |
325 } | |
326 if (incomplete_control_frame_body_) { | |
327 DVLOG(2) << "Rejoining a split control frame, opcode " << opcode; | |
328 AddToIncompleteControlFrameBody(data_buffer); | |
329 const int body_size = incomplete_control_frame_body_->offset(); | |
330 DCHECK_EQ(body_size, | |
331 static_cast<int>(current_frame_header_->payload_length)); | |
332 scoped_refptr<IOBufferWithSize> body = new IOBufferWithSize(body_size); | |
333 memcpy(body->data(), | |
334 incomplete_control_frame_body_->StartOfBuffer(), | |
335 body_size); | |
336 incomplete_control_frame_body_ = NULL; // Frame now complete. | |
337 DCHECK(is_final_chunk); | |
338 *frame = CreateFrame(is_final_chunk, body); | |
339 return OK; | |
340 } | |
341 } | |
342 | |
343 // Apply basic sanity checks to the |payload_length| field from the frame | |
344 // header. A check for exact equality can only be used when the whole frame | |
345 // arrives in one chunk. | |
346 DCHECK_GE(current_frame_header_->payload_length, | |
347 base::checked_numeric_cast<uint64>(chunk_size)); | |
348 DCHECK(!is_first_chunk || !is_final_chunk || | |
349 current_frame_header_->payload_length == | |
350 base::checked_numeric_cast<uint64>(chunk_size)); | |
351 | |
352 // Convert the chunk to a complete frame. | |
353 *frame = CreateFrame(is_final_chunk, data_buffer); | |
354 return OK; | |
355 } | |
356 | |
357 scoped_ptr<WebSocketFrame> WebSocketBasicStream::CreateFrame( | |
358 bool is_final_chunk, | |
359 const scoped_refptr<IOBufferWithSize>& data) { | |
360 scoped_ptr<WebSocketFrame> result_frame; | |
361 const bool is_final_chunk_in_message = | |
362 is_final_chunk && current_frame_header_->final; | |
363 const int data_size = data ? data->size() : 0; | |
364 const WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; | |
365 // Empty frames convey no useful information unless they have the "final" bit | |
366 // set. | |
367 if (is_final_chunk_in_message || data_size > 0) { | |
368 result_frame.reset(new WebSocketFrame(opcode)); | |
369 result_frame->header.CopyFrom(*current_frame_header_); | |
370 result_frame->header.final = is_final_chunk_in_message; | |
371 result_frame->header.payload_length = data_size; | |
372 result_frame->data = data; | |
373 // Ensure that opcodes Text and Binary are only used for the first frame in | |
374 // the message. | |
375 if (WebSocketFrameHeader::IsKnownDataOpCode(opcode)) | |
376 current_frame_header_->opcode = WebSocketFrameHeader::kOpCodeContinuation; | |
377 } | |
378 // Make sure that a frame header is not applied to any chunks that do not | |
379 // belong to it. | |
380 if (is_final_chunk) | |
381 current_frame_header_.reset(); | |
382 return result_frame.Pass(); | |
383 } | |
384 | |
385 void WebSocketBasicStream::AddToIncompleteControlFrameBody( | |
386 const scoped_refptr<IOBufferWithSize>& data_buffer) { | |
387 if (!data_buffer) | |
388 return; | |
389 const int new_offset = | |
390 incomplete_control_frame_body_->offset() + data_buffer->size(); | |
391 CHECK_GE(incomplete_control_frame_body_->capacity(), new_offset) | |
392 << "Control frame body larger than frame header indicates; frame parser " | |
393 "bug?"; | |
394 memcpy(incomplete_control_frame_body_->data(), | |
395 data_buffer->data(), | |
396 data_buffer->size()); | |
397 incomplete_control_frame_body_->set_offset(new_offset); | |
398 } | |
399 | |
400 void WebSocketBasicStream::OnReadComplete(ScopedVector<WebSocketFrame>* frames, | |
401 const CompletionCallback& callback, | |
402 int result) { | |
403 result = HandleReadResult(result, frames); | |
253 if (result == ERR_IO_PENDING) | 404 if (result == ERR_IO_PENDING) |
254 result = ReadFrames(frame_chunks, callback); | 405 result = ReadFrames(frames, callback); |
255 if (result != ERR_IO_PENDING) | 406 if (result != ERR_IO_PENDING) |
256 callback.Run(result); | 407 callback.Run(result); |
257 } | 408 } |
258 | 409 |
259 } // namespace net | 410 } // namespace net |
OLD | NEW |