Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_channel.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/basictypes.h" // for size_t | |
| 10 #include "base/bind.h" | |
| 11 #include "base/safe_numerics.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "net/base/big_endian.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/base/net_log.h" | |
| 16 #include "net/websockets/websocket_errors.h" | |
| 17 #include "net/websockets/websocket_event_interface.h" | |
| 18 #include "net/websockets/websocket_frame.h" | |
| 19 #include "net/websockets/websocket_mux.h" | |
| 20 #include "net/websockets/websocket_stream.h" | |
| 21 | |
| 22 namespace net { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const int kDefaultSendQuotaLowWaterMark = 1 << 16; | |
| 27 const int kDefaultSendQuotaHighWaterMark = 1 << 17; | |
| 28 const size_t kWebSocketCloseCodeLength = 2; | |
| 29 | |
| 30 // Concatenate the data from two IOBufferWithSize objects into a single one. | |
| 31 IOBufferWithSize* ConcatenateIOBuffers( | |
| 32 const scoped_refptr<IOBufferWithSize>& part1, | |
| 33 const scoped_refptr<IOBufferWithSize>& part2) { | |
| 34 int newsize = part1->size() + part2->size(); | |
| 35 IOBufferWithSize* newbuffer = new IOBufferWithSize(newsize); | |
| 36 std::copy(part1->data(), part1->data() + part1->size(), newbuffer->data()); | |
| 37 std::copy(part2->data(), | |
| 38 part2->data() + part2->size(), | |
| 39 newbuffer->data() + part1->size()); | |
| 40 return newbuffer; | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 struct WebSocketChannel::SendBuffer { | |
| 46 SendBuffer() : total_bytes(0) {} | |
| 47 ScopedVector<WebSocketFrameChunk> frames; | |
| 48 size_t total_bytes; | |
| 49 }; | |
| 50 | |
| 51 // Implementation of WebSocketStream::ConnectDelegate that simply forwards the | |
| 52 // calls on to the WebSocketChannel that created it. | |
| 53 class WebSocketChannel::ConnectDelegate | |
| 54 : public WebSocketStream::ConnectDelegate { | |
| 55 public: | |
| 56 explicit ConnectDelegate(WebSocketChannel* creator) : creator_(creator) {} | |
| 57 | |
| 58 virtual void OnSuccess(scoped_ptr<WebSocketStream> stream) OVERRIDE { | |
| 59 creator_->OnConnectSuccess(stream.Pass()); | |
| 60 } | |
| 61 | |
| 62 virtual void OnFailure(uint16 websocket_error) OVERRIDE { | |
| 63 creator_->OnConnectFailure(websocket_error); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 // A pointer to the WebSocketChannel that created us. We do not need to worry | |
| 68 // about this pointer being stale, because deleting WebSocketChannel cancels | |
| 69 // the connect process, deleting this object and preventing its callbacks from | |
| 70 // being called. | |
| 71 WebSocketChannel* const creator_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(ConnectDelegate); | |
| 74 }; | |
| 75 | |
| 76 WebSocketChannel::WebSocketChannel( | |
| 77 const GURL& socket_url, | |
| 78 scoped_ptr<WebSocketEventInterface> event_interface) | |
| 79 : socket_url_(socket_url), | |
| 80 event_interface_(event_interface.Pass()), | |
| 81 send_quota_low_water_mark_(kDefaultSendQuotaLowWaterMark), | |
| 82 send_quota_high_water_mark_(kDefaultSendQuotaHighWaterMark), | |
| 83 current_send_quota_(0), | |
| 84 closing_code_(0), | |
| 85 state_(FRESHLY_CONSTRUCTED) {} | |
| 86 | |
| 87 WebSocketChannel::~WebSocketChannel() { | |
| 88 // The stream may hold a pointer to read_frame_chunks_, and so it needs to be | |
| 89 // destroyed first. | |
| 90 stream_.reset(); | |
| 91 } | |
| 92 | |
| 93 void WebSocketChannel::SendAddChannelRequest( | |
| 94 const std::vector<std::string>& requested_subprotocols, | |
| 95 const GURL& origin, | |
| 96 URLRequestContext* url_request_context) { | |
| 97 // Delegate to the tested version. | |
| 98 SendAddChannelRequestWithFactory( | |
| 99 requested_subprotocols, | |
| 100 origin, | |
| 101 url_request_context, | |
| 102 base::Bind(&WebSocketStream::CreateAndConnectStream)); | |
| 103 } | |
| 104 | |
| 105 void WebSocketChannel::SendAddChannelRequestWithFactory( | |
| 106 const std::vector<std::string>& requested_subprotocols, | |
| 107 const GURL& origin, | |
| 108 URLRequestContext* url_request_context, | |
| 109 base::Callback<scoped_ptr<WebSocketStreamRequest>( | |
| 110 const GURL&, | |
| 111 const std::vector<std::string>&, | |
| 112 const GURL&, | |
| 113 URLRequestContext*, | |
| 114 const BoundNetLog&, | |
| 115 scoped_ptr<WebSocketStream::ConnectDelegate>)> factory) { | |
| 116 DCHECK_EQ(FRESHLY_CONSTRUCTED, state_); | |
| 117 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate( | |
| 118 new WebSocketChannel::ConnectDelegate(this)); | |
| 119 stream_request_ = factory.Run(socket_url_, | |
| 120 requested_subprotocols, | |
| 121 origin, | |
| 122 url_request_context, | |
| 123 BoundNetLog(), | |
| 124 connect_delegate.Pass()); | |
| 125 state_ = CONNECTING; | |
| 126 } | |
| 127 | |
| 128 void WebSocketChannel::OnConnectSuccess(scoped_ptr<WebSocketStream> stream) { | |
| 129 DCHECK(stream); | |
| 130 DCHECK_EQ(CONNECTING, state_); | |
| 131 stream_ = stream.Pass(); | |
| 132 state_ = CONNECTED; | |
| 133 event_interface_->OnAddChannelResponse(false, stream_->GetSubProtocol()); | |
| 134 | |
| 135 // TODO(ricea): Get flow control information from the WebSocketStream once we | |
| 136 // have a multiplexing WebSocketStream. | |
| 137 current_send_quota_ = send_quota_high_water_mark_; | |
| 138 event_interface_->OnFlowControl(send_quota_high_water_mark_); | |
| 139 | |
| 140 // We don't need this any more. | |
| 141 stream_request_.reset(); | |
| 142 ReadFrames(); | |
| 143 } | |
| 144 | |
| 145 void WebSocketChannel::OnConnectFailure(uint16 websocket_error) { | |
| 146 DCHECK_EQ(CONNECTING, state_); | |
| 147 state_ = CLOSED; | |
| 148 stream_request_.reset(); | |
| 149 event_interface_->OnAddChannelResponse(true, ""); | |
| 150 } | |
| 151 | |
| 152 void WebSocketChannel::SendFrame(bool fin, | |
| 153 WebSocketFrameHeader::OpCode op_code, | |
| 154 const std::vector<char>& data) { | |
| 155 if (data.size() > INT_MAX) { | |
| 156 NOTREACHED() << "Frame size sanity check failed"; | |
| 157 return; | |
| 158 } | |
| 159 if (stream_ == NULL) { | |
| 160 LOG(DFATAL) << "Got SendFrame without a connection established; " | |
| 161 << "misbehaving renderer? fin=" << fin << " op_code=" << op_code | |
| 162 << " data.size()=" << data.size(); | |
| 163 return; | |
| 164 } | |
| 165 if (state_ == SEND_CLOSED || state_ == CLOSED) { | |
| 166 VLOG(1) << "SendFrame called in state " << state_ | |
| 167 << ". This may be a bug, or a harmless race."; | |
| 168 return; | |
| 169 } | |
| 170 if (state_ != CONNECTED) { | |
| 171 NOTREACHED() << "SendFrame() called in state " << state_; | |
| 172 return; | |
| 173 } | |
| 174 if (data.size() > base::checked_numeric_cast<size_t>(current_send_quota_)) { | |
| 175 FailChannel(SEND_GOING_AWAY, | |
| 176 kWebSocketMuxErrorSendQuotaViolation, | |
| 177 "Send quota exceeded"); | |
| 178 return; | |
| 179 } | |
| 180 if (!WebSocketFrameHeader::IsKnownDataOpCode(op_code)) { | |
| 181 LOG(DFATAL) << "Got SendFrame with bogus op_code " << op_code | |
| 182 << "; misbehaving renderer? fin=" << fin | |
| 183 << " data.size()=" << data.size(); | |
| 184 return; | |
| 185 } | |
| 186 current_send_quota_ -= data.size(); | |
| 187 // TODO(ricea): If current_send_quota_ has dropped below | |
| 188 // send_quota_low_water_mark_, we may want to consider increasing the "low | |
| 189 // water mark" and "high water mark", but only if we think we are not | |
| 190 // saturating the link to the WebSocket server. | |
| 191 // TODO(ricea): For kOpCodeText, do UTF-8 validation? | |
| 192 scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(data.size())); | |
| 193 std::copy(data.begin(), data.end(), buffer->data()); | |
| 194 SendIOBufferWithSize(fin, op_code, buffer); | |
| 195 } | |
| 196 | |
| 197 void WebSocketChannel::SendIOBufferWithSize( | |
| 198 bool fin, | |
| 199 WebSocketFrameHeader::OpCode op_code, | |
| 200 const scoped_refptr<IOBufferWithSize>& buffer) { | |
| 201 DCHECK(state_ == CONNECTED || state_ == RECV_CLOSED); | |
| 202 DCHECK(stream_); | |
| 203 scoped_ptr<WebSocketFrameHeader> header(new WebSocketFrameHeader(op_code)); | |
| 204 header->final = fin; | |
| 205 header->masked = true; | |
| 206 header->payload_length = buffer->size(); | |
| 207 scoped_ptr<WebSocketFrameChunk> chunk(new WebSocketFrameChunk()); | |
| 208 chunk->header = header.Pass(); | |
| 209 chunk->final_chunk = true; | |
| 210 chunk->data = buffer; | |
| 211 if (data_being_sent_) { | |
| 212 // Either the link to the WebSocket server is saturated, or we are simply | |
| 213 // processing a batch of messages. | |
| 214 // TODO(ricea): We need to keep some statistics to work out which situation | |
| 215 // we are in and adjust quota appropriately. | |
| 216 if (!data_to_send_next_) { | |
| 217 data_to_send_next_.reset(new SendBuffer); | |
| 218 } | |
| 219 data_to_send_next_->frames.push_back(chunk.release()); | |
| 220 data_to_send_next_->total_bytes += buffer->size(); | |
| 221 } else { | |
| 222 data_being_sent_.reset(new SendBuffer); | |
| 223 data_being_sent_->frames.push_back(chunk.release()); | |
| 224 data_being_sent_->total_bytes += buffer->size(); | |
| 225 WriteFrames(); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 void WebSocketChannel::WriteFrames() { | |
| 230 int result = OK; | |
| 231 do { | |
| 232 // This use of base::Unretained is safe because we own the WebSocketStream | |
| 233 // and destroying it cancels all callbacks. | |
| 234 result = stream_->WriteFrames( | |
| 235 &(data_being_sent_->frames), | |
| 236 base::Bind( | |
| 237 &WebSocketChannel::OnWriteDone, base::Unretained(this), false)); | |
| 238 if (result != ERR_IO_PENDING) { | |
| 239 OnWriteDone(true, result); | |
| 240 } | |
| 241 } while (result == OK && data_being_sent_); | |
| 242 } | |
| 243 | |
| 244 void WebSocketChannel::OnWriteDone(bool synchronous, int result) { | |
| 245 DCHECK(state_ != FRESHLY_CONSTRUCTED && state_ != CONNECTING); | |
| 246 DCHECK_NE(ERR_IO_PENDING, result); | |
| 247 DCHECK(data_being_sent_); | |
| 248 switch (result) { | |
| 249 case OK: | |
| 250 if (data_to_send_next_) { | |
| 251 data_being_sent_ = data_to_send_next_.Pass(); | |
| 252 if (!synchronous) { | |
| 253 WriteFrames(); | |
| 254 } | |
| 255 } else { | |
| 256 data_being_sent_.reset(); | |
| 257 if (current_send_quota_ < send_quota_low_water_mark_) { | |
| 258 // TODO(ricea): Increase low_water_mark and high_water_mark if | |
| 259 // throughput is high, reduce them if throughput is low. Low water | |
| 260 // mark needs to be >= the bandwidth delay product *of the IPC | |
| 261 // channel*. Because factors like context-switch time, thread wake-up | |
| 262 // time, and bus speed come into play it is complex and probably needs | |
| 263 // to be determined empirically. | |
| 264 DCHECK_LE(send_quota_low_water_mark_, send_quota_high_water_mark_); | |
| 265 // TODO(ricea): Truncate quota by the quota specified by the remote | |
| 266 // server, if the protocol in use supports quota. | |
| 267 int fresh_quota = send_quota_high_water_mark_ - current_send_quota_; | |
| 268 current_send_quota_ += fresh_quota; | |
| 269 event_interface_->OnFlowControl(fresh_quota); | |
| 270 } | |
| 271 } | |
| 272 return; | |
| 273 | |
| 274 // If a recoverable error condition existed, it would go here. | |
| 275 | |
| 276 default: | |
| 277 DCHECK_LT(result, 0) | |
| 278 << "WriteFrames() should only return OK or ERR_ codes"; | |
| 279 stream_->Close(); | |
| 280 state_ = CLOSED; | |
| 281 event_interface_->OnDropChannel(kWebSocketErrorAbnormalClosure, | |
| 282 "Abnormal Closure"); | |
| 283 return; | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 void WebSocketChannel::ReadFrames() { | |
| 288 int result = OK; | |
| 289 do { | |
| 290 // This use of base::Unretained is safe because we own the WebSocketStream, | |
| 291 // and any pending reads will be cancelled when it is destroyed. | |
| 292 result = stream_->ReadFrames( | |
| 293 &read_frame_chunks_, | |
| 294 base::Bind( | |
| 295 &WebSocketChannel::OnReadDone, base::Unretained(this), false)); | |
| 296 if (result != ERR_IO_PENDING) { | |
| 297 OnReadDone(true, result); | |
| 298 } | |
| 299 } while (result == OK); | |
| 300 } | |
| 301 | |
| 302 void WebSocketChannel::OnReadDone(bool synchronous, int result) { | |
| 303 DCHECK(state_ != FRESHLY_CONSTRUCTED && state_ != CONNECTING); | |
| 304 DCHECK_NE(ERR_IO_PENDING, result); | |
| 305 switch (result) { | |
| 306 case OK: | |
| 307 // ReadFrames() must use ERR_CONNECTION_CLOSED for a closed connection | |
| 308 // with no data read, not an empty response. | |
| 309 DCHECK(!read_frame_chunks_.empty()) | |
| 310 << "ReadFrames() returned OK, but nothing was read."; | |
| 311 for (size_t i = 0; i < read_frame_chunks_.size(); ++i) { | |
| 312 scoped_ptr<WebSocketFrameChunk> chunk(read_frame_chunks_[i]); | |
| 313 read_frame_chunks_[i] = NULL; | |
| 314 ProcessFrameChunk(chunk.Pass()); | |
| 315 } | |
| 316 read_frame_chunks_.clear(); | |
| 317 // We need to always keep a call to ReadFrames pending. | |
| 318 if (!synchronous) { | |
| 319 ReadFrames(); | |
| 320 } | |
| 321 return; | |
| 322 | |
| 323 default: { | |
| 324 DCHECK_LT(result, 0) | |
| 325 << "ReadFrames() should only return OK or ERR_ codes"; | |
| 326 stream_->Close(); | |
| 327 state_ = CLOSED; | |
| 328 uint16 code = kWebSocketErrorAbnormalClosure; | |
| 329 std::string reason = "Abnormal Closure"; | |
| 330 if (closing_code_ != 0) { | |
| 331 code = closing_code_; | |
| 332 reason = closing_reason_; | |
| 333 } | |
| 334 event_interface_->OnDropChannel(code, reason); | |
| 335 return; | |
| 336 } | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 void WebSocketChannel::ProcessFrameChunk( | |
| 341 scoped_ptr<WebSocketFrameChunk> chunk) { | |
| 342 bool is_first_chunk = false; | |
| 343 if (chunk->header) { | |
| 344 DCHECK(current_frame_header_ == NULL) | |
| 345 << "Received the header for a new frame without notification that " | |
| 346 << "the previous frame was complete."; | |
| 347 is_first_chunk = true; | |
| 348 current_frame_header_.swap(chunk->header); | |
| 349 if (current_frame_header_->masked) { | |
| 350 // RFC6455 Section 5.1 "A client MUST close a connection if it detects a | |
| 351 // masked frame." | |
| 352 FailChannel(SEND_REAL_ERROR, | |
| 353 kWebSocketErrorProtocolError, | |
| 354 "Masked frame from server"); | |
| 355 return; | |
| 356 } | |
| 357 } | |
| 358 if (!current_frame_header_) { | |
| 359 // If we rejected the previous chunk as invalid, then we will have reset | |
| 360 // current_frame_header_ to avoid using it. More chunks of the invalid frame | |
| 361 // may still arrive, so this is not necessarily a bug on our side. However, | |
| 362 // if this happens when state_ is CONNECTED, it is definitely a bug. | |
| 363 DCHECK(state_ != CONNECTED) << "Unexpected header-less frame received " | |
| 364 << "(final_chunk = " << chunk->final_chunk | |
| 365 << ", data size = " << chunk->data->size() | |
| 366 << ")"; | |
| 367 return; | |
| 368 } | |
| 369 scoped_refptr<IOBufferWithSize> data_buffer; | |
| 370 data_buffer.swap(chunk->data); | |
| 371 const bool is_final_chunk = chunk->final_chunk; | |
| 372 chunk.reset(); | |
| 373 WebSocketFrameHeader::OpCode opcode = current_frame_header_->opcode; | |
| 374 if (WebSocketFrameHeader::IsKnownControlOpCode(opcode)) { | |
| 375 if (is_final_chunk) { | |
| 376 if (incomplete_control_frame_body_) { | |
| 377 VLOG(2) << "Rejoining a split control frame, opcode " << opcode; | |
| 378 data_buffer = | |
| 379 ConcatenateIOBuffers(incomplete_control_frame_body_, data_buffer); | |
| 380 incomplete_control_frame_body_ = NULL; | |
| 381 } | |
| 382 } else { | |
| 383 // TODO(ricea): Enforce a maximum size of 125 bytes on the control frames | |
| 384 // we accept. | |
| 385 VLOG(2) << "Encountered a split control frame, opcode " << opcode; | |
| 386 if (incomplete_control_frame_body_) { | |
| 387 // The really horrid case. We need to create a new IOBufferWithSize | |
| 388 // combining the new one and the old one. This should virtually never | |
| 389 // happen. | |
| 390 // TODO(ricea): This algorithm is O(N^2). Use a fixed 127-byte buffer | |
| 391 // instead. | |
| 392 VLOG(3) << "Hit the really horrid case"; | |
| 393 incomplete_control_frame_body_ = | |
| 394 ConcatenateIOBuffers(incomplete_control_frame_body_, data_buffer); | |
| 395 } else { | |
| 396 // The merely horrid case. Store the IOBufferWithSize to use when the | |
| 397 // rest of the control frame arrives. | |
| 398 incomplete_control_frame_body_.swap(data_buffer); | |
| 399 } | |
| 400 return; | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 HandleFrame(opcode, is_first_chunk, is_final_chunk, data_buffer); | |
| 405 | |
| 406 if (is_final_chunk) { | |
| 407 // Make sure we do not apply this frame header to any future chunks. | |
| 408 current_frame_header_.reset(); | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 void WebSocketChannel::HandleFrame( | |
| 413 const WebSocketFrameHeader::OpCode opcode, | |
| 414 const bool is_first_chunk, | |
| 415 const bool is_final_chunk, | |
| 416 const scoped_refptr<IOBufferWithSize>& data_buffer) { | |
| 417 if (state_ == CLOSED) { | |
|
tyoshino (SeeGerritForStatus)
2013/07/03 07:45:44
o, not state_ == RECV_CLOSED || state_ == CLOSED?
Adam Rice
2013/07/03 10:25:26
state_ cannot be RECV_CLOSED unless OnReadDone() i
tyoshino (SeeGerritForStatus)
2013/07/04 12:54:26
Got it. Thanks
| |
| 418 std::string frame_name; | |
| 419 switch (opcode) { | |
| 420 case WebSocketFrameHeader::kOpCodeText: // fall-thru | |
| 421 case WebSocketFrameHeader::kOpCodeBinary: // fall-thru | |
| 422 case WebSocketFrameHeader::kOpCodeContinuation: | |
| 423 frame_name = "Data frame"; | |
| 424 break; | |
| 425 | |
| 426 case WebSocketFrameHeader::kOpCodePing: | |
| 427 frame_name = "Ping"; | |
| 428 break; | |
| 429 | |
| 430 case WebSocketFrameHeader::kOpCodePong: | |
| 431 frame_name = "Pong"; | |
| 432 break; | |
| 433 | |
| 434 case WebSocketFrameHeader::kOpCodeClose: | |
| 435 frame_name = "Close"; | |
| 436 break; | |
| 437 | |
| 438 default: | |
| 439 frame_name = "Unknown frame type"; | |
| 440 break; | |
| 441 } | |
| 442 // SEND_REAL_ERROR makes no difference here, as we won't send another Close | |
| 443 // frame. | |
| 444 FailChannel(SEND_REAL_ERROR, | |
| 445 kWebSocketErrorProtocolError, | |
| 446 frame_name + " received after close"); | |
| 447 return; | |
| 448 } | |
| 449 switch (opcode) { | |
| 450 case WebSocketFrameHeader::kOpCodeText: // fall-thru | |
| 451 case WebSocketFrameHeader::kOpCodeBinary: // fall-thru | |
| 452 case WebSocketFrameHeader::kOpCodeContinuation: | |
| 453 if (state_ == CONNECTED) { | |
| 454 const bool final = is_final_chunk && current_frame_header_->final; | |
| 455 // TODO(ricea): Can this copy be eliminated? | |
| 456 const char* const data_begin = data_buffer->data(); | |
| 457 const char* const data_end = data_begin + data_buffer->size(); | |
| 458 const std::vector<char> data(data_begin, data_end); | |
| 459 // TODO(ricea): Handle the (improbable) case when ReadFrames returns far | |
| 460 // more data at once than we want to send in a single IPC (in which case | |
| 461 // we need to buffer the data and return to the event loop with a | |
| 462 // callback to send the rest in 32K chunks). | |
| 463 | |
| 464 // Send the received frame to the renderer process. | |
| 465 event_interface_->OnDataFrame( | |
| 466 final, | |
| 467 is_first_chunk ? opcode : WebSocketFrameHeader::kOpCodeContinuation, | |
| 468 data); | |
| 469 } else { | |
| 470 VLOG(3) << "Ignored data packet received in state " << state_; | |
| 471 } | |
| 472 return; | |
| 473 | |
| 474 case WebSocketFrameHeader::kOpCodePing: | |
| 475 VLOG(1) << "Got Ping of size " << data_buffer->size(); | |
| 476 if (state_ == CONNECTED) { | |
| 477 SendIOBufferWithSize( | |
| 478 true, WebSocketFrameHeader::kOpCodePong, data_buffer); | |
| 479 } else { | |
| 480 VLOG(3) << "Ignored ping in state " << state_; | |
| 481 } | |
| 482 return; | |
| 483 | |
| 484 case WebSocketFrameHeader::kOpCodePong: | |
| 485 VLOG(1) << "Got Pong of size " << data_buffer->size(); | |
| 486 // We do not need to do anything with pong messages. | |
| 487 return; | |
| 488 | |
| 489 case WebSocketFrameHeader::kOpCodeClose: { | |
| 490 uint16 code = kWebSocketNormalClosure; | |
| 491 std::string reason; | |
| 492 ParseClose(data_buffer, &code, &reason); | |
| 493 // TODO(ricea): Find a way to safely log the message from the close | |
| 494 // message (escape control codes and so on). | |
| 495 VLOG(1) << "Got Close with code " << code; | |
| 496 switch (state_) { | |
| 497 case CONNECTED: | |
| 498 state_ = RECV_CLOSED; | |
| 499 SendClose(code, reason); // Sets state_ to CLOSED | |
| 500 event_interface_->OnClosingHandshake(); | |
| 501 closing_code_ = code; | |
| 502 closing_reason_ = reason; | |
| 503 break; | |
| 504 | |
| 505 case SEND_CLOSED: | |
| 506 state_ = CLOSED; | |
| 507 // From RFC6455 section 7.1.5: "Each endpoint | |
| 508 // will see the status code sent by the other end as _The WebSocket | |
| 509 // Connection Close Code_." | |
| 510 closing_code_ = code; | |
| 511 closing_reason_ = reason; | |
| 512 break; | |
| 513 | |
| 514 default: | |
| 515 LOG(DFATAL) << "Got Close in unexpected state " << state_; | |
| 516 break; | |
| 517 } | |
| 518 return; | |
| 519 } | |
| 520 | |
| 521 default: | |
| 522 FailChannel( | |
| 523 SEND_REAL_ERROR, kWebSocketErrorProtocolError, "Unknown opcode"); | |
| 524 return; | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 void WebSocketChannel::SendFlowControl(int64 quota) { | |
| 529 DCHECK_EQ(CONNECTED, state_); | |
| 530 // TODO(ricea): Add interface to WebSocketStream and implement. | |
| 531 // stream_->SendFlowControl(quota); | |
| 532 } | |
| 533 | |
| 534 void WebSocketChannel::StartClosingHandshake(uint16 code, | |
| 535 const std::string& reason) { | |
| 536 if (state_ == SEND_CLOSED || state_ == CLOSED) { | |
| 537 VLOG(1) << "StartClosingHandshake called in state " << state_ | |
| 538 << ". This may be a bug, or a harmless race."; | |
| 539 return; | |
| 540 } | |
| 541 if (state_ != CONNECTED) { | |
| 542 NOTREACHED() << "StartClosingHandshake() called in state " << state_; | |
| 543 return; | |
| 544 } | |
| 545 // TODO(ricea): Validate |code|? Check that |reason| is valid UTF-8? | |
| 546 // TODO(ricea): There should be a timeout for the closing handshake. | |
| 547 SendClose(code, reason); // Sets state_ to SEND_CLOSED | |
| 548 } | |
| 549 | |
| 550 void WebSocketChannel::FailChannel(ExposeError expose, | |
| 551 uint16 code, | |
| 552 const std::string& reason) { | |
| 553 DCHECK(state_ != FRESHLY_CONSTRUCTED && state_ != CONNECTING); | |
| 554 // TODO(ricea): Logging. | |
| 555 State old_state = state_; | |
| 556 if (state_ == CONNECTED) { | |
| 557 uint16 send_code = kWebSocketErrorGoingAway; | |
| 558 std::string send_reason = "Internal Error"; | |
| 559 if (expose == SEND_REAL_ERROR) { | |
| 560 send_code = code; | |
| 561 send_reason = reason; | |
| 562 } | |
| 563 SendClose(send_code, send_reason); // Sets state_ to SEND_CLOSED | |
| 564 } | |
| 565 // Careful study of RFC6455 section 7.1.7 and 7.1.1 indicates we should close | |
| 566 // the connection ourselves without waiting for the closing handshake. | |
| 567 stream_->Close(); | |
| 568 state_ = CLOSED; | |
| 569 | |
| 570 // We may be in the middle of processing several chunks. We should not re-use | |
| 571 // the frame header. | |
| 572 current_frame_header_.reset(); | |
| 573 if (old_state != CLOSED) { | |
| 574 event_interface_->OnDropChannel(code, reason); | |
| 575 } | |
| 576 } | |
| 577 | |
| 578 void WebSocketChannel::SendClose(uint16 code, const std::string& reason) { | |
| 579 DCHECK(state_ == CONNECTED || state_ == RECV_CLOSED); | |
| 580 uint64 payload_length = kWebSocketCloseCodeLength + reason.length(); | |
| 581 scoped_refptr<IOBufferWithSize> body = new IOBufferWithSize(payload_length); | |
| 582 WriteBigEndian(body->data(), code); | |
| 583 COMPILE_ASSERT(sizeof(code) == kWebSocketCloseCodeLength, | |
| 584 they_should_both_be_two); | |
| 585 std::copy( | |
| 586 reason.begin(), reason.end(), body->data() + kWebSocketCloseCodeLength); | |
| 587 SendIOBufferWithSize(true, WebSocketFrameHeader::kOpCodeClose, body); | |
| 588 state_ = state_ == CONNECTED ? SEND_CLOSED : CLOSED; | |
| 589 } | |
| 590 | |
| 591 void WebSocketChannel::ParseClose(const scoped_refptr<IOBufferWithSize>& buffer, | |
| 592 uint16* code, | |
| 593 std::string* reason) { | |
| 594 const char* data = buffer->data(); | |
| 595 size_t size = base::checked_numeric_cast<size_t>(buffer->size()); | |
| 596 reason->clear(); | |
| 597 if (size < kWebSocketCloseCodeLength) { | |
| 598 *code = kWebSocketErrorNoStatusReceived; | |
| 599 if (size != 0) { | |
| 600 VLOG(1) << "Close frame with payload size " << size << " received " | |
| 601 << "(the first byte is " << std::hex << static_cast<int>(data[0]) | |
| 602 << ")"; | |
| 603 return; | |
| 604 } | |
| 605 return; | |
| 606 } | |
| 607 uint16 unchecked_code = 0; | |
| 608 ReadBigEndian(data, &unchecked_code); | |
| 609 COMPILE_ASSERT(sizeof(unchecked_code) == kWebSocketCloseCodeLength, | |
| 610 they_should_both_be_two_bytes); | |
| 611 if (unchecked_code >= static_cast<uint16>(kWebSocketNormalClosure) && | |
| 612 unchecked_code <= | |
| 613 static_cast<uint16>(kWebSocketErrorPrivateReservedMax)) { | |
| 614 *code = unchecked_code; | |
| 615 } else { | |
| 616 VLOG(1) << "Close frame contained code outside of the valid range: " | |
| 617 << unchecked_code; | |
| 618 *code = kWebSocketErrorAbnormalClosure; | |
| 619 } | |
| 620 std::string text(data + kWebSocketCloseCodeLength, data + size); | |
| 621 // TODO(ricea): Is this check strict enough? In particular, check the | |
| 622 // "Security Considerations" from RFC3629. | |
| 623 if (IsStringUTF8(text)) { | |
| 624 using std::swap; | |
| 625 swap(*reason, text); | |
| 626 } | |
| 627 } | |
| 628 | |
| 629 } // namespace net | |
| OLD | NEW |