blob: fb8fb0a8b7aaac8865019d520b637674d01dd376 [file] [log] [blame]
[email protected]adb225d2013-08-30 13:14:431// 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
7#include <algorithm>
8#include <limits>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/bind.h"
14#include "base/logging.h"
[email protected]2f5d9f62013-09-26 12:14:2815#include "base/safe_numerics.h"
[email protected]adb225d2013-08-30 13:14:4316#include "net/base/io_buffer.h"
17#include "net/base/net_errors.h"
18#include "net/socket/client_socket_handle.h"
19#include "net/websockets/websocket_errors.h"
20#include "net/websockets/websocket_frame.h"
21#include "net/websockets/websocket_frame_parser.h"
22
23namespace net {
24
25namespace {
26
[email protected]2f5d9f62013-09-26 12:14:2827// This uses type uint64 to match the definition of
28// WebSocketFrameHeader::payload_length in websocket_frame.h.
29const uint64 kMaxControlFramePayload = 125;
30
[email protected]adb225d2013-08-30 13:14:4331// The number of bytes to attempt to read at a time.
32// TODO(ricea): See if there is a better number or algorithm to fulfill our
33// requirements:
34// 1. We would like to use minimal memory on low-bandwidth or idle connections
35// 2. We would like to read as close to line speed as possible on
36// high-bandwidth connections
37// 3. We can't afford to cause jank on the IO thread by copying large buffers
38// around
39// 4. We would like to hit any sweet-spots that might exist in terms of network
40// packet sizes / encryption block sizes / IPC alignment issues, etc.
41const int kReadBufferSize = 32 * 1024;
42
43} // namespace
44
45WebSocketBasicStream::WebSocketBasicStream(
46 scoped_ptr<ClientSocketHandle> connection)
47 : read_buffer_(new IOBufferWithSize(kReadBufferSize)),
48 connection_(connection.Pass()),
49 generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) {
50 DCHECK(connection_->is_initialized());
51}
52
53WebSocketBasicStream::~WebSocketBasicStream() { Close(); }
54
[email protected]2f5d9f62013-09-26 12:14:2855int WebSocketBasicStream::ReadFrames(ScopedVector<WebSocketFrame>* frames,
56 const CompletionCallback& callback) {
57 DCHECK(frames->empty());
[email protected]adb225d2013-08-30 13:14:4358 // If there is data left over after parsing the HTTP headers, attempt to parse
59 // it as WebSocket frames.
60 if (http_read_buffer_) {
61 DCHECK_GE(http_read_buffer_->offset(), 0);
62 // We cannot simply copy the data into read_buffer_, as it might be too
63 // large.
64 scoped_refptr<GrowableIOBuffer> buffered_data;
65 buffered_data.swap(http_read_buffer_);
66 DCHECK(http_read_buffer_.get() == NULL);
[email protected]2f5d9f62013-09-26 12:14:2867 ScopedVector<WebSocketFrameChunk> frame_chunks;
[email protected]adb225d2013-08-30 13:14:4368 if (!parser_.Decode(buffered_data->StartOfBuffer(),
69 buffered_data->offset(),
[email protected]2f5d9f62013-09-26 12:14:2870 &frame_chunks))
[email protected]adb225d2013-08-30 13:14:4371 return WebSocketErrorToNetError(parser_.websocket_error());
[email protected]2f5d9f62013-09-26 12:14:2872 if (!frame_chunks.empty()) {
73 int result = ConvertChunksToFrames(&frame_chunks, frames);
74 if (result != ERR_IO_PENDING)
75 return result;
76 }
[email protected]adb225d2013-08-30 13:14:4377 }
78
[email protected]2f5d9f62013-09-26 12:14:2879 // Run until socket stops giving us data or we get some frames.
[email protected]adb225d2013-08-30 13:14:4380 while (true) {
81 // base::Unretained(this) here is safe because net::Socket guarantees not to
82 // call any callbacks after Disconnect(), which we call from the
[email protected]2f5d9f62013-09-26 12:14:2883 // destructor. The caller of ReadFrames() is required to keep |frames|
[email protected]adb225d2013-08-30 13:14:4384 // valid.
[email protected]2f5d9f62013-09-26 12:14:2885 int result = connection_->socket()->Read(
86 read_buffer_.get(),
87 read_buffer_->size(),
88 base::Bind(&WebSocketBasicStream::OnReadComplete,
89 base::Unretained(this),
90 base::Unretained(frames),
91 callback));
[email protected]adb225d2013-08-30 13:14:4392 if (result == ERR_IO_PENDING)
93 return result;
[email protected]2f5d9f62013-09-26 12:14:2894 result = HandleReadResult(result, frames);
[email protected]adb225d2013-08-30 13:14:4395 if (result != ERR_IO_PENDING)
96 return result;
[email protected]2f5d9f62013-09-26 12:14:2897 DCHECK(frames->empty());
[email protected]adb225d2013-08-30 13:14:4398 }
99}
100
[email protected]2f5d9f62013-09-26 12:14:28101int WebSocketBasicStream::WriteFrames(ScopedVector<WebSocketFrame>* frames,
102 const CompletionCallback& callback) {
[email protected]adb225d2013-08-30 13:14:43103 // This function always concatenates all frames into a single buffer.
104 // TODO(ricea): Investigate whether it would be better in some cases to
105 // perform multiple writes with smaller buffers.
106 //
107 // First calculate the size of the buffer we need to allocate.
[email protected]2f5d9f62013-09-26 12:14:28108 typedef ScopedVector<WebSocketFrame>::const_iterator Iterator;
[email protected]adb225d2013-08-30 13:14:43109 const int kMaximumTotalSize = std::numeric_limits<int>::max();
110 int total_size = 0;
[email protected]2f5d9f62013-09-26 12:14:28111 for (Iterator it = frames->begin(); it != frames->end(); ++it) {
112 WebSocketFrame* frame = *it;
[email protected]adb225d2013-08-30 13:14:43113 // Force the masked bit on.
[email protected]2f5d9f62013-09-26 12:14:28114 frame->header.masked = true;
[email protected]adb225d2013-08-30 13:14:43115 // We enforce flow control so the renderer should never be able to force us
116 // to cache anywhere near 2GB of frames.
[email protected]2f5d9f62013-09-26 12:14:28117 int frame_size = frame->header.payload_length +
118 GetWebSocketFrameHeaderSize(frame->header);
119 CHECK_GE(kMaximumTotalSize - total_size, frame_size)
[email protected]adb225d2013-08-30 13:14:43120 << "Aborting to prevent overflow";
[email protected]2f5d9f62013-09-26 12:14:28121 total_size += frame_size;
[email protected]adb225d2013-08-30 13:14:43122 }
123 scoped_refptr<IOBufferWithSize> combined_buffer(
124 new IOBufferWithSize(total_size));
125 char* dest = combined_buffer->data();
126 int remaining_size = total_size;
[email protected]2f5d9f62013-09-26 12:14:28127 for (Iterator it = frames->begin(); it != frames->end(); ++it) {
128 WebSocketFrame* frame = *it;
[email protected]adb225d2013-08-30 13:14:43129 WebSocketMaskingKey mask = generate_websocket_masking_key_();
[email protected]2f5d9f62013-09-26 12:14:28130 int result =
131 WriteWebSocketFrameHeader(frame->header, &mask, dest, remaining_size);
132 DCHECK_NE(ERR_INVALID_ARGUMENT, result)
[email protected]adb225d2013-08-30 13:14:43133 << "WriteWebSocketFrameHeader() says that " << remaining_size
134 << " is not enough to write the header in. This should not happen.";
135 CHECK_GE(result, 0) << "Potentially security-critical check failed";
136 dest += result;
137 remaining_size -= result;
138
[email protected]2f5d9f62013-09-26 12:14:28139 const char* const frame_data = frame->data->data();
140 const int frame_size = frame->header.payload_length;
[email protected]adb225d2013-08-30 13:14:43141 CHECK_GE(remaining_size, frame_size);
142 std::copy(frame_data, frame_data + frame_size, dest);
143 MaskWebSocketFramePayload(mask, 0, dest, frame_size);
144 dest += frame_size;
145 remaining_size -= frame_size;
146 }
147 DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; "
148 << remaining_size << " bytes left over.";
149 scoped_refptr<DrainableIOBuffer> drainable_buffer(
150 new DrainableIOBuffer(combined_buffer, total_size));
151 return WriteEverything(drainable_buffer, callback);
152}
153
154void WebSocketBasicStream::Close() { connection_->socket()->Disconnect(); }
155
156std::string WebSocketBasicStream::GetSubProtocol() const {
157 return sub_protocol_;
158}
159
160std::string WebSocketBasicStream::GetExtensions() const { return extensions_; }
161
162int WebSocketBasicStream::SendHandshakeRequest(
163 const GURL& url,
164 const HttpRequestHeaders& headers,
165 HttpResponseInfo* response_info,
166 const CompletionCallback& callback) {
167 // TODO(ricea): Implement handshake-related functionality.
168 NOTREACHED();
169 return ERR_NOT_IMPLEMENTED;
170}
171
172int WebSocketBasicStream::ReadHandshakeResponse(
173 const CompletionCallback& callback) {
174 NOTREACHED();
175 return ERR_NOT_IMPLEMENTED;
176}
177
178/*static*/
179scoped_ptr<WebSocketBasicStream>
180WebSocketBasicStream::CreateWebSocketBasicStreamForTesting(
181 scoped_ptr<ClientSocketHandle> connection,
182 const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
183 const std::string& sub_protocol,
184 const std::string& extensions,
185 WebSocketMaskingKeyGeneratorFunction key_generator_function) {
186 scoped_ptr<WebSocketBasicStream> stream(
187 new WebSocketBasicStream(connection.Pass()));
188 if (http_read_buffer) {
189 stream->http_read_buffer_ = http_read_buffer;
190 }
191 stream->sub_protocol_ = sub_protocol;
192 stream->extensions_ = extensions;
193 stream->generate_websocket_masking_key_ = key_generator_function;
194 return stream.Pass();
195}
196
197int WebSocketBasicStream::WriteEverything(
198 const scoped_refptr<DrainableIOBuffer>& buffer,
199 const CompletionCallback& callback) {
200 while (buffer->BytesRemaining() > 0) {
201 // The use of base::Unretained() here is safe because on destruction we
202 // disconnect the socket, preventing any further callbacks.
[email protected]2f5d9f62013-09-26 12:14:28203 int result = connection_->socket()->Write(
204 buffer.get(),
205 buffer->BytesRemaining(),
206 base::Bind(&WebSocketBasicStream::OnWriteComplete,
207 base::Unretained(this),
208 buffer,
209 callback));
[email protected]adb225d2013-08-30 13:14:43210 if (result > 0) {
211 buffer->DidConsume(result);
212 } else {
213 return result;
214 }
215 }
216 return OK;
217}
218
219void WebSocketBasicStream::OnWriteComplete(
220 const scoped_refptr<DrainableIOBuffer>& buffer,
221 const CompletionCallback& callback,
222 int result) {
223 if (result < 0) {
[email protected]2f5d9f62013-09-26 12:14:28224 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]adb225d2013-08-30 13:14:43225 callback.Run(result);
226 return;
227 }
228
[email protected]2f5d9f62013-09-26 12:14:28229 DCHECK_NE(0, result);
[email protected]adb225d2013-08-30 13:14:43230 buffer->DidConsume(result);
231 result = WriteEverything(buffer, callback);
232 if (result != ERR_IO_PENDING)
233 callback.Run(result);
234}
235
236int WebSocketBasicStream::HandleReadResult(
237 int result,
[email protected]2f5d9f62013-09-26 12:14:28238 ScopedVector<WebSocketFrame>* frames) {
[email protected]adb225d2013-08-30 13:14:43239 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]2f5d9f62013-09-26 12:14:28240 DCHECK(frames->empty());
[email protected]adb225d2013-08-30 13:14:43241 if (result < 0)
242 return result;
243 if (result == 0)
244 return ERR_CONNECTION_CLOSED;
[email protected]2f5d9f62013-09-26 12:14:28245 ScopedVector<WebSocketFrameChunk> frame_chunks;
246 if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks))
[email protected]adb225d2013-08-30 13:14:43247 return WebSocketErrorToNetError(parser_.websocket_error());
[email protected]2f5d9f62013-09-26 12:14:28248 if (frame_chunks.empty())
249 return ERR_IO_PENDING;
250 return ConvertChunksToFrames(&frame_chunks, frames);
[email protected]adb225d2013-08-30 13:14:43251}
252
[email protected]2f5d9f62013-09-26 12:14:28253int WebSocketBasicStream::ConvertChunksToFrames(
[email protected]adb225d2013-08-30 13:14:43254 ScopedVector<WebSocketFrameChunk>* frame_chunks,
[email protected]2f5d9f62013-09-26 12:14:28255 ScopedVector<WebSocketFrame>* frames) {
256 for (size_t i = 0; i < frame_chunks->size(); ++i) {
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
274int 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
360scoped_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
388void 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
403void WebSocketBasicStream::OnReadComplete(ScopedVector<WebSocketFrame>* frames,
404 const CompletionCallback& callback,
405 int result) {
406 result = HandleReadResult(result, frames);
[email protected]adb225d2013-08-30 13:14:43407 if (result == ERR_IO_PENDING)
[email protected]2f5d9f62013-09-26 12:14:28408 result = ReadFrames(frames, callback);
[email protected]adb225d2013-08-30 13:14:43409 if (result != ERR_IO_PENDING)
410 callback.Run(result);
411}
412
413} // namespace net