1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <limits>
#include "net/websockets/websocket.h"
#include "base/message_loop.h"
#include "net/base/host_resolver.h"
#include "net/websockets/websocket_handshake.h"
#include "net/websockets/websocket_handshake_draft75.h"
namespace net {
static const char kClosingFrame[2] = {'\xff', '\x00'};
static int64 kClosingHandshakeTimeout = 1000; // msec.
WebSocket::WebSocket(Request* request, WebSocketDelegate* delegate)
: ready_state_(INITIALIZED),
request_(request),
handshake_(NULL),
delegate_(delegate),
origin_loop_(MessageLoop::current()),
socket_stream_(NULL),
max_pending_send_allowed_(0),
current_read_buf_(NULL),
read_consumed_len_(0),
current_write_buf_(NULL),
server_closing_handshake_(false),
client_closing_handshake_(false),
closing_handshake_started_(false),
force_close_task_(NULL),
closing_handshake_timeout_(kClosingHandshakeTimeout) {
DCHECK(request_.get());
DCHECK(delegate_);
DCHECK(origin_loop_);
}
WebSocket::~WebSocket() {
DCHECK(ready_state_ == INITIALIZED || !delegate_);
DCHECK(!socket_stream_);
DCHECK(!delegate_);
}
void WebSocket::Connect() {
DCHECK(ready_state_ == INITIALIZED);
DCHECK(request_.get());
DCHECK(delegate_);
DCHECK(!socket_stream_);
DCHECK(MessageLoop::current() == origin_loop_);
socket_stream_ = new SocketStream(request_->url(), this);
socket_stream_->set_context(request_->context());
if (request_->host_resolver())
socket_stream_->SetHostResolver(request_->host_resolver());
if (request_->client_socket_factory())
socket_stream_->SetClientSocketFactory(request_->client_socket_factory());
AddRef(); // Release in DoClose().
ready_state_ = CONNECTING;
socket_stream_->Connect();
}
void WebSocket::Send(const std::string& msg) {
if (ready_state_ == CLOSING || ready_state_ == CLOSED) {
return;
}
if (client_closing_handshake_) {
// We must not send any data after we start the WebSocket closing handshake.
return;
}
DCHECK(ready_state_ == OPEN);
DCHECK(MessageLoop::current() == origin_loop_);
IOBufferWithSize* buf = new IOBufferWithSize(msg.size() + 2);
char* p = buf->data();
*p = '\0';
memcpy(p + 1, msg.data(), msg.size());
*(p + 1 + msg.size()) = '\xff';
pending_write_bufs_.push_back(buf);
SendPending();
}
void WebSocket::Close() {
DCHECK(MessageLoop::current() == origin_loop_);
// If connection has not yet started, do nothing.
if (ready_state_ == INITIALIZED) {
DCHECK(!socket_stream_);
ready_state_ = CLOSED;
return;
}
// If the readyState attribute is in the CLOSING or CLOSED state, do nothing
if (ready_state_ == CLOSING || ready_state_ == CLOSED)
return;
if (request_->version() == DRAFT75) {
DCHECK(socket_stream_);
socket_stream_->Close();
return;
}
// If the WebSocket connection is not yet established, fail the WebSocket
// connection and set the readyState attribute's value to CLOSING.
if (ready_state_ == CONNECTING) {
ready_state_ = CLOSING;
origin_loop_->PostTask(
FROM_HERE,
NewRunnableMethod(this, &WebSocket::FailConnection));
}
// If the WebSocket closing handshake has not yet been started, start
// the WebSocket closing handshake and set the readyState attribute's value
// to CLOSING.
if (!closing_handshake_started_) {
ready_state_ = CLOSING;
origin_loop_->PostTask(
FROM_HERE,
NewRunnableMethod(this, &WebSocket::StartClosingHandshake));
}
// Otherwise, set the readyState attribute's value to CLOSING.
ready_state_ = CLOSING;
}
void WebSocket::DetachDelegate() {
if (!delegate_)
return;
delegate_ = NULL;
if (ready_state_ == INITIALIZED) {
DCHECK(!socket_stream_);
ready_state_ = CLOSED;
return;
}
if (ready_state_ != CLOSED) {
DCHECK(socket_stream_);
socket_stream_->Close();
}
}
void WebSocket::OnConnected(SocketStream* socket_stream,
int max_pending_send_allowed) {
DCHECK(socket_stream == socket_stream_);
max_pending_send_allowed_ = max_pending_send_allowed;
// Use |max_pending_send_allowed| as hint for initial size of read buffer.
current_read_buf_ = new GrowableIOBuffer();
current_read_buf_->SetCapacity(max_pending_send_allowed_);
read_consumed_len_ = 0;
DCHECK(!current_write_buf_);
DCHECK(!handshake_.get());
switch (request_->version()) {
case DEFAULT_VERSION:
handshake_.reset(new WebSocketHandshake(
request_->url(), request_->origin(), request_->location(),
request_->protocol()));
break;
case DRAFT75:
handshake_.reset(new WebSocketHandshakeDraft75(
request_->url(), request_->origin(), request_->location(),
request_->protocol()));
break;
default:
NOTREACHED() << "Unexpected protocol version:" << request_->version();
}
const std::string msg = handshake_->CreateClientHandshakeMessage();
IOBufferWithSize* buf = new IOBufferWithSize(msg.size());
memcpy(buf->data(), msg.data(), msg.size());
pending_write_bufs_.push_back(buf);
origin_loop_->PostTask(FROM_HERE,
NewRunnableMethod(this, &WebSocket::SendPending));
}
void WebSocket::OnSentData(SocketStream* socket_stream, int amount_sent) {
DCHECK(socket_stream == socket_stream_);
DCHECK(current_write_buf_);
current_write_buf_->DidConsume(amount_sent);
DCHECK_GE(current_write_buf_->BytesRemaining(), 0);
if (current_write_buf_->BytesRemaining() == 0) {
current_write_buf_ = NULL;
pending_write_bufs_.pop_front();
}
origin_loop_->PostTask(FROM_HERE,
NewRunnableMethod(this, &WebSocket::SendPending));
}
void WebSocket::OnReceivedData(SocketStream* socket_stream,
const char* data, int len) {
DCHECK(socket_stream == socket_stream_);
AddToReadBuffer(data, len);
origin_loop_->PostTask(FROM_HERE,
NewRunnableMethod(this, &WebSocket::DoReceivedData));
}
void WebSocket::OnClose(SocketStream* socket_stream) {
origin_loop_->PostTask(FROM_HERE,
NewRunnableMethod(this, &WebSocket::DoClose));
}
void WebSocket::OnError(const SocketStream* socket_stream, int error) {
origin_loop_->PostTask(
FROM_HERE, NewRunnableMethod(this, &WebSocket::DoSocketError, error));
}
void WebSocket::SendPending() {
DCHECK(MessageLoop::current() == origin_loop_);
if (!socket_stream_) {
DCHECK_EQ(CLOSED, ready_state_);
return;
}
if (!current_write_buf_) {
if (pending_write_bufs_.empty()) {
if (client_closing_handshake_) {
// Already sent 0xFF and 0x00 bytes.
// *The WebSocket closing handshake has started.*
closing_handshake_started_ = true;
if (server_closing_handshake_) {
// 4.2 3-8-3 If the WebSocket connection is not already closed,
// then close the WebSocket connection.
// *The WebSocket closing handshake has finished*
socket_stream_->Close();
} else {
// 5. Wait a user-agent-determined length of time, or until the
// WebSocket connection is closed.
force_close_task_ =
NewRunnableMethod(this, &WebSocket::DoForceCloseConnection);
origin_loop_->PostDelayedTask(
FROM_HERE, force_close_task_, closing_handshake_timeout_);
}
}
return;
}
current_write_buf_ = new DrainableIOBuffer(
pending_write_bufs_.front(), pending_write_bufs_.front()->size());
}
DCHECK_GT(current_write_buf_->BytesRemaining(), 0);
bool sent = socket_stream_->SendData(
current_write_buf_->data(),
std::min(current_write_buf_->BytesRemaining(),
max_pending_send_allowed_));
DCHECK(sent);
}
void WebSocket::DoReceivedData() {
DCHECK(MessageLoop::current() == origin_loop_);
scoped_refptr<WebSocket> protect(this);
switch (ready_state_) {
case CONNECTING:
{
DCHECK(handshake_.get());
DCHECK(current_read_buf_);
const char* data =
current_read_buf_->StartOfBuffer() + read_consumed_len_;
size_t len = current_read_buf_->offset() - read_consumed_len_;
int eoh = handshake_->ReadServerHandshake(data, len);
if (eoh < 0) {
// Not enough data, Retry when more data is available.
return;
}
SkipReadBuffer(eoh);
}
if (handshake_->mode() != WebSocketHandshake::MODE_CONNECTED) {
// Handshake failed.
socket_stream_->Close();
return;
}
ready_state_ = OPEN;
if (delegate_)
delegate_->OnOpen(this);
if (current_read_buf_->offset() == read_consumed_len_) {
// No remaining data after handshake message.
break;
}
// FALL THROUGH
case OPEN:
case CLOSING: // need to process closing-frame from server.
ProcessFrameData();
break;
case CLOSED:
// Closed just after DoReceivedData is queued on |origin_loop_|.
break;
default:
NOTREACHED();
break;
}
}
void WebSocket::ProcessFrameData() {
DCHECK(current_read_buf_);
if (server_closing_handshake_) {
// Any data on the connection after the 0xFF frame is discarded.
return;
}
scoped_refptr<WebSocket> protect(this);
const char* start_frame =
current_read_buf_->StartOfBuffer() + read_consumed_len_;
const char* next_frame = start_frame;
const char* p = next_frame;
const char* end =
current_read_buf_->StartOfBuffer() + current_read_buf_->offset();
while (p < end) {
// Let /error/ be false.
bool error = false;
// Handle the /frame type/ byte as follows.
unsigned char frame_byte = static_cast<unsigned char>(*p++);
if ((frame_byte & 0x80) == 0x80) {
int length = 0;
while (p < end) {
if (length > std::numeric_limits<int>::max() / 128) {
// frame length overflow.
socket_stream_->Close();
return;
}
unsigned char c = static_cast<unsigned char>(*p);
length = length * 128 + (c & 0x7f);
++p;
if ((c & 0x80) != 0x80)
break;
}
// Checks if the frame body hasn't been completely received yet.
// It also checks the case the frame length bytes haven't been completely
// received yet, because p == end and length > 0 in such case.
if (p + length < end) {
p += length;
next_frame = p;
if (request_->version() != DRAFT75 &&
frame_byte == 0xFF && length == 0) {
// 4.2 Data framing 3. Handle the /frame type/ byte.
// 8. If the /frame type/ is 0xFF and the /length/ was 0, then
// run the following substeps:
// 1. If the WebSocket closing handshake has not yet started, then
// start the WebSocket closing handshake.
server_closing_handshake_ = true;
if (!closing_handshake_started_) {
origin_loop_->PostTask(
FROM_HERE,
NewRunnableMethod(this, &WebSocket::StartClosingHandshake));
} else {
// If the WebSocket closing handshake has been started and
// the WebSocket connection is not already closed, then close
// the WebSocket connection.
socket_stream_->Close();
}
return;
}
// 4.2 3-8 Otherwise, let /error/ be true.
error = true;
} else {
// Not enough data in buffer.
break;
}
} else {
const char* msg_start = p;
while (p < end && *p != '\xff')
++p;
if (p < end && *p == '\xff') {
if (frame_byte == 0x00) {
if (delegate_) {
delegate_->OnMessage(this, std::string(msg_start, p - msg_start));
}
} else {
// Otherwise, discard the data and let /error/ to be true.
error = true;
}
++p;
next_frame = p;
}
}
// If /error/ is true, then *a WebSocket error has been detected.*
if (error && delegate_)
delegate_->OnError(this);
}
SkipReadBuffer(next_frame - start_frame);
}
void WebSocket::AddToReadBuffer(const char* data, int len) {
DCHECK(current_read_buf_);
// Check if |current_read_buf_| has enough space to store |len| of |data|.
if (len >= current_read_buf_->RemainingCapacity()) {
current_read_buf_->SetCapacity(
current_read_buf_->offset() + len);
}
DCHECK(current_read_buf_->RemainingCapacity() >= len);
memcpy(current_read_buf_->data(), data, len);
current_read_buf_->set_offset(current_read_buf_->offset() + len);
}
void WebSocket::SkipReadBuffer(int len) {
if (len == 0)
return;
DCHECK_GT(len, 0);
read_consumed_len_ += len;
int remaining = current_read_buf_->offset() - read_consumed_len_;
DCHECK_GE(remaining, 0);
if (remaining < read_consumed_len_ &&
current_read_buf_->RemainingCapacity() < read_consumed_len_) {
// Pre compaction:
// 0 v-read_consumed_len_ v-offset v- capacity
// |..processed..| .. remaining .. | .. RemainingCapacity |
//
memmove(current_read_buf_->StartOfBuffer(),
current_read_buf_->StartOfBuffer() + read_consumed_len_,
remaining);
read_consumed_len_ = 0;
current_read_buf_->set_offset(remaining);
// Post compaction:
// 0read_consumed_len_ v- offset v- capacity
// |.. remaining .. | .. RemainingCapacity ... |
//
}
}
void WebSocket::StartClosingHandshake() {
// 4.2 *start the WebSocket closing handshake*.
if (closing_handshake_started_ || client_closing_handshake_) {
// 1. If the WebSocket closing handshake has started, then abort these
// steps.
return;
}
// 2.,3. Send a 0xFF and 0x00 byte to the server.
client_closing_handshake_ = true;
IOBufferWithSize* buf = new IOBufferWithSize(2);
memcpy(buf->data(), kClosingFrame, 2);
pending_write_bufs_.push_back(buf);
SendPending();
}
void WebSocket::DoForceCloseConnection() {
// 4.2 *start the WebSocket closing handshake*
// 6. If the WebSocket connection is not already closed, then close the
// WebSocket connection. (If this happens, then the closing handshake
// doesn't finish.)
DCHECK(MessageLoop::current() == origin_loop_);
force_close_task_ = NULL;
FailConnection();
}
void WebSocket::FailConnection() {
DCHECK(MessageLoop::current() == origin_loop_);
// 6.1 Client-initiated closure.
// *fail the WebSocket connection*.
// the user agent must close the WebSocket connection, and may report the
// problem to the user.
if (!socket_stream_)
return;
socket_stream_->Close();
}
void WebSocket::DoClose() {
DCHECK(MessageLoop::current() == origin_loop_);
if (force_close_task_) {
// WebSocket connection is closed while waiting a user-agent-determined
// length of time after *The WebSocket closing handshake has started*.
force_close_task_->Cancel();
force_close_task_ = NULL;
}
WebSocketDelegate* delegate = delegate_;
delegate_ = NULL;
ready_state_ = CLOSED;
if (!socket_stream_)
return;
socket_stream_ = NULL;
if (delegate)
delegate->OnClose(this,
server_closing_handshake_ && closing_handshake_started_);
Release();
}
void WebSocket::DoSocketError(int error) {
DCHECK(MessageLoop::current() == origin_loop_);
if (delegate_)
delegate_->OnSocketError(this, error);
}
} // namespace net
|