| 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_channel.h" | 5 #include "net/websockets/websocket_channel.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" // for size_t | 9 #include "base/basictypes.h" // for size_t |
| 10 #include "base/big_endian.h" |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "net/base/big_endian.h" | |
| 19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 20 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
| 21 #include "net/http/http_request_headers.h" | 21 #include "net/http/http_request_headers.h" |
| 22 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
| 23 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
| 24 #include "net/websockets/websocket_errors.h" | 24 #include "net/websockets/websocket_errors.h" |
| 25 #include "net/websockets/websocket_event_interface.h" | 25 #include "net/websockets/websocket_event_interface.h" |
| 26 #include "net/websockets/websocket_frame.h" | 26 #include "net/websockets/websocket_frame.h" |
| 27 #include "net/websockets/websocket_handshake_request_info.h" | 27 #include "net/websockets/websocket_handshake_request_info.h" |
| 28 #include "net/websockets/websocket_handshake_response_info.h" | 28 #include "net/websockets/websocket_handshake_response_info.h" |
| (...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 scoped_refptr<IOBuffer> body; | 862 scoped_refptr<IOBuffer> body; |
| 863 size_t size = 0; | 863 size_t size = 0; |
| 864 if (code == kWebSocketErrorNoStatusReceived) { | 864 if (code == kWebSocketErrorNoStatusReceived) { |
| 865 // Special case: translate kWebSocketErrorNoStatusReceived into a Close | 865 // Special case: translate kWebSocketErrorNoStatusReceived into a Close |
| 866 // frame with no payload. | 866 // frame with no payload. |
| 867 body = new IOBuffer(0); | 867 body = new IOBuffer(0); |
| 868 } else { | 868 } else { |
| 869 const size_t payload_length = kWebSocketCloseCodeLength + reason.length(); | 869 const size_t payload_length = kWebSocketCloseCodeLength + reason.length(); |
| 870 body = new IOBuffer(payload_length); | 870 body = new IOBuffer(payload_length); |
| 871 size = payload_length; | 871 size = payload_length; |
| 872 WriteBigEndian(body->data(), code); | 872 base::WriteBigEndian(body->data(), code); |
| 873 COMPILE_ASSERT(sizeof(code) == kWebSocketCloseCodeLength, | 873 COMPILE_ASSERT(sizeof(code) == kWebSocketCloseCodeLength, |
| 874 they_should_both_be_two); | 874 they_should_both_be_two); |
| 875 std::copy( | 875 std::copy( |
| 876 reason.begin(), reason.end(), body->data() + kWebSocketCloseCodeLength); | 876 reason.begin(), reason.end(), body->data() + kWebSocketCloseCodeLength); |
| 877 } | 877 } |
| 878 // This use of base::Unretained() is safe because we stop the timer in the | 878 // This use of base::Unretained() is safe because we stop the timer in the |
| 879 // destructor. | 879 // destructor. |
| 880 timer_.Start( | 880 timer_.Start( |
| 881 FROM_HERE, | 881 FROM_HERE, |
| 882 timeout_, | 882 timeout_, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 905 << static_cast<int>(buffer->data()[0]) << ")"; | 905 << static_cast<int>(buffer->data()[0]) << ")"; |
| 906 parsed_ok = false; | 906 parsed_ok = false; |
| 907 *code = kWebSocketErrorProtocolError; | 907 *code = kWebSocketErrorProtocolError; |
| 908 *message = | 908 *message = |
| 909 "Received a broken close frame containing an invalid size body."; | 909 "Received a broken close frame containing an invalid size body."; |
| 910 } | 910 } |
| 911 return parsed_ok; | 911 return parsed_ok; |
| 912 } | 912 } |
| 913 const char* data = buffer->data(); | 913 const char* data = buffer->data(); |
| 914 uint16 unchecked_code = 0; | 914 uint16 unchecked_code = 0; |
| 915 ReadBigEndian(data, &unchecked_code); | 915 base::ReadBigEndian(data, &unchecked_code); |
| 916 COMPILE_ASSERT(sizeof(unchecked_code) == kWebSocketCloseCodeLength, | 916 COMPILE_ASSERT(sizeof(unchecked_code) == kWebSocketCloseCodeLength, |
| 917 they_should_both_be_two_bytes); | 917 they_should_both_be_two_bytes); |
| 918 switch (unchecked_code) { | 918 switch (unchecked_code) { |
| 919 case kWebSocketErrorNoStatusReceived: | 919 case kWebSocketErrorNoStatusReceived: |
| 920 case kWebSocketErrorAbnormalClosure: | 920 case kWebSocketErrorAbnormalClosure: |
| 921 case kWebSocketErrorTlsHandshake: | 921 case kWebSocketErrorTlsHandshake: |
| 922 *code = kWebSocketErrorProtocolError; | 922 *code = kWebSocketErrorProtocolError; |
| 923 *message = | 923 *message = |
| 924 "Received a broken close frame containing a reserved status code."; | 924 "Received a broken close frame containing a reserved status code."; |
| 925 parsed_ok = false; | 925 parsed_ok = false; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 954 | 954 |
| 955 void WebSocketChannel::CloseTimeout() { | 955 void WebSocketChannel::CloseTimeout() { |
| 956 stream_->Close(); | 956 stream_->Close(); |
| 957 DCHECK_NE(CLOSED, state_); | 957 DCHECK_NE(CLOSED, state_); |
| 958 state_ = CLOSED; | 958 state_ = CLOSED; |
| 959 AllowUnused(DoDropChannel(false, kWebSocketErrorAbnormalClosure, "")); | 959 AllowUnused(DoDropChannel(false, kWebSocketErrorAbnormalClosure, "")); |
| 960 // |this| has been deleted. | 960 // |this| has been deleted. |
| 961 } | 961 } |
| 962 | 962 |
| 963 } // namespace net | 963 } // namespace net |
| OLD | NEW |