OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "chrome/test/chromedriver/net/web_socket.h" | |
6 | |
7 #include "base/memory/scoped_vector.h" | |
8 #include "base/string_split.h" | |
9 #include "base/stringprintf.h" | |
10 #include "net/base/io_buffer.h" | |
11 #include "net/url_request/url_request_context_getter.h" | |
12 #include "net/websockets/websocket_frame.h" | |
13 #include "net/websockets/websocket_frame_parser.h" | |
14 #include "net/websockets/websocket_job.h" | |
15 | |
16 WebSocket::WebSocket( | |
17 net::URLRequestContextGetter* context_getter, | |
18 const GURL& url, | |
19 WebSocketListener* listener) | |
20 : context_getter_(context_getter), | |
21 url_(url), | |
22 listener_(listener), | |
23 connected_(false) { | |
24 net::WebSocketJob::EnsureInit(); | |
25 web_socket_ = new net::WebSocketJob(this); | |
26 } | |
27 | |
28 WebSocket::~WebSocket() { | |
29 CHECK(thread_checker_.CalledOnValidThread()); | |
30 web_socket_->Close(); | |
Takashi Toyoshima
2012/11/27 07:57:37
This Close() may not perform closing handshake cor
kkania
2012/11/27 19:58:23
Ok. Right now we don't have any need to close the
Takashi Toyoshima
2012/11/28 09:11:30
Without closing handshake, we have no confidence t
| |
31 web_socket_->DetachDelegate(); | |
32 } | |
33 | |
34 void WebSocket::Connect(const base::Callback<void(bool)>& callback) { | |
35 CHECK(thread_checker_.CalledOnValidThread()); | |
36 CHECK_EQ(net::WebSocketJob::INITIALIZED, web_socket_->state()); | |
37 | |
38 connect_callback_ = callback; | |
39 | |
40 scoped_refptr<net::SocketStream> socket = new net::SocketStream( | |
41 url_, web_socket_); | |
42 socket->set_context(context_getter_->GetURLRequestContext()); | |
43 | |
44 web_socket_->InitSocketStream(socket); | |
45 web_socket_->Connect(); | |
46 } | |
47 | |
48 bool WebSocket::Write(const std::string& message) { | |
49 CHECK(thread_checker_.CalledOnValidThread()); | |
50 | |
51 net::WebSocketFrameHeader header; | |
52 header.final = true; | |
53 header.reserved1 = false; | |
54 header.reserved2 = false; | |
55 header.reserved3 = false; | |
56 header.opcode = net::WebSocketFrameHeader::kOpCodeText; | |
57 header.masked = true; | |
58 header.payload_length = message.length(); | |
59 int header_size = net::GetWebSocketFrameHeaderSize(header); | |
60 net::WebSocketMaskingKey masking_key = net::GenerateWebSocketMaskingKey(); | |
61 std::string header_str; | |
62 header_str.resize(header_size); | |
63 CHECK_EQ(header_size, net::WriteWebSocketFrameHeader( | |
64 header, &masking_key, &header_str[0], header_str.length())); | |
65 | |
66 std::string masked_message = message; | |
67 net::MaskWebSocketFramePayload( | |
68 masking_key, 0, &masked_message[0], masked_message.length()); | |
69 std::string data = header_str + masked_message; | |
70 return web_socket_->SendData(data.c_str(), data.length()); | |
71 } | |
72 | |
73 void WebSocket::OnConnected(net::SocketStream* socket, | |
74 int max_pending_send_allowed) { | |
75 std::string handshake = base::StringPrintf( | |
76 "GET %s HTTP/1.1\r\n" | |
77 "Host: %s\r\n" | |
78 "Upgrade: WebSocket\r\n" | |
Takashi Toyoshima
2012/11/27 07:57:37
The value is case-insensitive, but I recommend to
kkania
2012/11/27 19:58:23
Done.
| |
79 "Connection: Upgrade\r\n" | |
80 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" | |
Takashi Toyoshima
2012/11/27 07:57:37
This value must not be a constant string.
It's a b
kkania
2012/11/27 19:58:23
Done.
| |
81 "Sec-WebSocket-Version: 13\r\n" | |
Takashi Toyoshima
2012/11/27 07:57:37
If ChromeDriver works over the internet, I suggest
kkania
2012/11/27 19:58:23
Done.
| |
82 "\r\n", | |
83 url_.path().c_str(), | |
84 url_.host().c_str()); | |
85 if (!web_socket_->SendData(handshake.c_str(), handshake.length())) | |
86 OnConnectFinished(false); | |
87 } | |
88 | |
89 void WebSocket::OnSentData(net::SocketStream* socket, | |
90 int amount_sent) {} | |
91 | |
92 void WebSocket::OnReceivedData(net::SocketStream* socket, | |
93 const char* data, int len) { | |
94 net::WebSocketJob::State state = web_socket_->state(); | |
95 if (!connect_callback_.is_null()) { | |
96 std::vector<std::string> parts; | |
97 base::SplitStringUsingSubstr(std::string(data, len), "\r\n", &parts); | |
98 if (parts.empty() || | |
99 parts[0] != "HTTP/1.1 101 WebSocket Protocol Handshake") { | |
100 OnConnectFinished(false); | |
101 return; | |
102 } | |
Takashi Toyoshima
2012/11/27 07:57:37
You should not expect 'WebSocket Protocol Handshak
kkania
2012/11/27 19:58:23
Done.
| |
103 OnConnectFinished(state == net::WebSocketJob::OPEN); | |
104 } else if (connected_) { | |
105 incoming_msg_buf_ += std::string(data, len); | |
106 ScopedVector<net::WebSocketFrameChunk> frame_chunks; | |
107 net::WebSocketFrameParser parser; | |
108 CHECK(parser.Decode(incoming_msg_buf_.c_str(), | |
109 incoming_msg_buf_.length(), | |
110 &frame_chunks)); | |
111 int processed_data = 0; | |
112 std::string message; | |
113 int header = 0; | |
114 for (size_t i = 0; i < frame_chunks.size(); ++i) { | |
115 scoped_refptr<net::IOBufferWithSize> buffer = frame_chunks[i]->data; | |
116 if (buffer) | |
117 message += std::string(buffer->data(), buffer->size()); | |
118 if (frame_chunks[i]->final_chunk) { | |
119 processed_data += frame_chunks[header]->header->payload_length + | |
120 net::GetWebSocketFrameHeaderSize(*frame_chunks[header]->header); | |
121 listener_->OnMessageReceived(message); | |
122 message.clear(); | |
123 header = i + 1; | |
124 } | |
125 } | |
126 if (processed_data) | |
127 incoming_msg_buf_.erase(0, processed_data); | |
128 } | |
129 } | |
130 | |
131 void WebSocket::OnClose(net::SocketStream* socket) { | |
132 if (!connect_callback_.is_null()) | |
133 OnConnectFinished(false); | |
134 else | |
135 listener_->OnClose(); | |
136 } | |
137 | |
138 void WebSocket::OnConnectFinished(bool success) { | |
139 connected_ = success; | |
140 base::Callback<void(bool)> temp = connect_callback_; | |
141 connect_callback_.Reset(); | |
142 temp.Run(success); | |
143 } | |
OLD | NEW |