OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/test/chromedriver/net/websocket.h" | 5 #include "chrome/test/chromedriver/net/websocket.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 Write(header_str + masked_message); | 114 Write(header_str + masked_message); |
115 return true; | 115 return true; |
116 } | 116 } |
117 | 117 |
118 void WebSocket::OnSocketConnect(int code) { | 118 void WebSocket::OnSocketConnect(int code) { |
119 if (code != net::OK) { | 119 if (code != net::OK) { |
120 Close(code); | 120 Close(code); |
121 return; | 121 return; |
122 } | 122 } |
123 | 123 |
124 CHECK(base::Base64Encode(base::RandBytesAsString(16), &sec_key_)); | 124 base::Base64Encode(base::RandBytesAsString(16), &sec_key_); |
125 std::string handshake = base::StringPrintf( | 125 std::string handshake = base::StringPrintf( |
126 "GET %s HTTP/1.1\r\n" | 126 "GET %s HTTP/1.1\r\n" |
127 "Host: %s\r\n" | 127 "Host: %s\r\n" |
128 "Upgrade: websocket\r\n" | 128 "Upgrade: websocket\r\n" |
129 "Connection: Upgrade\r\n" | 129 "Connection: Upgrade\r\n" |
130 "Sec-WebSocket-Key: %s\r\n" | 130 "Sec-WebSocket-Key: %s\r\n" |
131 "Sec-WebSocket-Version: 13\r\n" | 131 "Sec-WebSocket-Version: 13\r\n" |
132 "Pragma: no-cache\r\n" | 132 "Pragma: no-cache\r\n" |
133 "Cache-Control: no-cache\r\n" | 133 "Cache-Control: no-cache\r\n" |
134 "\r\n", | 134 "\r\n", |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 203 |
204 void WebSocket::OnReadDuringHandshake(const char* data, int len) { | 204 void WebSocket::OnReadDuringHandshake(const char* data, int len) { |
205 handshake_response_ += std::string(data, len); | 205 handshake_response_ += std::string(data, len); |
206 int headers_end = net::HttpUtil::LocateEndOfHeaders( | 206 int headers_end = net::HttpUtil::LocateEndOfHeaders( |
207 handshake_response_.data(), handshake_response_.size(), 0); | 207 handshake_response_.data(), handshake_response_.size(), 0); |
208 if (headers_end == -1) | 208 if (headers_end == -1) |
209 return; | 209 return; |
210 | 210 |
211 const char kMagicKey[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 211 const char kMagicKey[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
212 std::string websocket_accept; | 212 std::string websocket_accept; |
213 CHECK(base::Base64Encode(base::SHA1HashString(sec_key_ + kMagicKey), | 213 base::Base64Encode(base::SHA1HashString(sec_key_ + kMagicKey), |
214 &websocket_accept)); | 214 &websocket_accept); |
215 scoped_refptr<net::HttpResponseHeaders> headers( | 215 scoped_refptr<net::HttpResponseHeaders> headers( |
216 new net::HttpResponseHeaders( | 216 new net::HttpResponseHeaders( |
217 net::HttpUtil::AssembleRawHeaders( | 217 net::HttpUtil::AssembleRawHeaders( |
218 handshake_response_.data(), headers_end))); | 218 handshake_response_.data(), headers_end))); |
219 if (headers->response_code() != 101 || | 219 if (headers->response_code() != 101 || |
220 !headers->HasHeaderValue("Upgrade", "WebSocket") || | 220 !headers->HasHeaderValue("Upgrade", "WebSocket") || |
221 !headers->HasHeaderValue("Connection", "Upgrade") || | 221 !headers->HasHeaderValue("Connection", "Upgrade") || |
222 !headers->HasHeaderValue("Sec-WebSocket-Accept", websocket_accept)) { | 222 !headers->HasHeaderValue("Sec-WebSocket-Accept", websocket_accept)) { |
223 Close(net::ERR_FAILED); | 223 Close(net::ERR_FAILED); |
224 return; | 224 return; |
(...skipping 30 matching lines...) Expand all Loading... |
255 | 255 |
256 void WebSocket::Close(int code) { | 256 void WebSocket::Close(int code) { |
257 socket_->Disconnect(); | 257 socket_->Disconnect(); |
258 if (!connect_callback_.is_null()) | 258 if (!connect_callback_.is_null()) |
259 InvokeConnectCallback(code); | 259 InvokeConnectCallback(code); |
260 if (state_ == OPEN) | 260 if (state_ == OPEN) |
261 listener_->OnClose(); | 261 listener_->OnClose(); |
262 | 262 |
263 state_ = CLOSED; | 263 state_ = CLOSED; |
264 } | 264 } |
265 | |
266 | |
OLD | NEW |