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/websocket.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/rand_util.h" |
| 10 #include "base/sha1.h" |
| 11 #include "base/string_split.h" |
| 12 #include "base/stringprintf.h" |
| 13 #include "net/base/io_buffer.h" |
| 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/http/http_util.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "net/websockets/websocket_frame.h" |
| 18 #include "net/websockets/websocket_job.h" |
| 19 |
| 20 WebSocket::WebSocket( |
| 21 net::URLRequestContextGetter* context_getter, |
| 22 const GURL& url, |
| 23 WebSocketListener* listener) |
| 24 : context_getter_(context_getter), |
| 25 url_(url), |
| 26 listener_(listener), |
| 27 connected_(false) { |
| 28 net::WebSocketJob::EnsureInit(); |
| 29 web_socket_ = new net::WebSocketJob(this); |
| 30 } |
| 31 |
| 32 WebSocket::~WebSocket() { |
| 33 CHECK(thread_checker_.CalledOnValidThread()); |
| 34 web_socket_->Close(); |
| 35 web_socket_->DetachDelegate(); |
| 36 } |
| 37 |
| 38 void WebSocket::Connect(const net::CompletionCallback& callback) { |
| 39 CHECK(thread_checker_.CalledOnValidThread()); |
| 40 CHECK_EQ(net::WebSocketJob::INITIALIZED, web_socket_->state()); |
| 41 |
| 42 connect_callback_ = callback; |
| 43 |
| 44 scoped_refptr<net::SocketStream> socket = new net::SocketStream( |
| 45 url_, web_socket_); |
| 46 socket->set_context(context_getter_->GetURLRequestContext()); |
| 47 |
| 48 web_socket_->InitSocketStream(socket); |
| 49 web_socket_->Connect(); |
| 50 } |
| 51 |
| 52 bool WebSocket::Send(const std::string& message) { |
| 53 CHECK(thread_checker_.CalledOnValidThread()); |
| 54 |
| 55 net::WebSocketFrameHeader header; |
| 56 header.final = true; |
| 57 header.reserved1 = false; |
| 58 header.reserved2 = false; |
| 59 header.reserved3 = false; |
| 60 header.opcode = net::WebSocketFrameHeader::kOpCodeText; |
| 61 header.masked = true; |
| 62 header.payload_length = message.length(); |
| 63 int header_size = net::GetWebSocketFrameHeaderSize(header); |
| 64 net::WebSocketMaskingKey masking_key = net::GenerateWebSocketMaskingKey(); |
| 65 std::string header_str; |
| 66 header_str.resize(header_size); |
| 67 CHECK_EQ(header_size, net::WriteWebSocketFrameHeader( |
| 68 header, &masking_key, &header_str[0], header_str.length())); |
| 69 |
| 70 std::string masked_message = message; |
| 71 net::MaskWebSocketFramePayload( |
| 72 masking_key, 0, &masked_message[0], masked_message.length()); |
| 73 std::string data = header_str + masked_message; |
| 74 return web_socket_->SendData(data.c_str(), data.length()); |
| 75 } |
| 76 |
| 77 void WebSocket::OnConnected(net::SocketStream* socket, |
| 78 int max_pending_send_allowed) { |
| 79 CHECK(base::Base64Encode(base::RandBytesAsString(16), &sec_key_)); |
| 80 std::string handshake = base::StringPrintf( |
| 81 "GET %s HTTP/1.1\r\n" |
| 82 "Host: %s\r\n" |
| 83 "Upgrade: websocket\r\n" |
| 84 "Connection: Upgrade\r\n" |
| 85 "Sec-WebSocket-Key: %s\r\n" |
| 86 "Sec-WebSocket-Version: 13\r\n" |
| 87 "Pragma: no-cache\r\n" |
| 88 "Cache-Control: no-cache\r\n" |
| 89 "\r\n", |
| 90 url_.path().c_str(), |
| 91 url_.host().c_str(), |
| 92 sec_key_.c_str()); |
| 93 if (!web_socket_->SendData(handshake.c_str(), handshake.length())) |
| 94 OnConnectFinished(net::ERR_FAILED); |
| 95 } |
| 96 |
| 97 void WebSocket::OnSentData(net::SocketStream* socket, |
| 98 int amount_sent) {} |
| 99 |
| 100 void WebSocket::OnReceivedData(net::SocketStream* socket, |
| 101 const char* data, int len) { |
| 102 net::WebSocketJob::State state = web_socket_->state(); |
| 103 if (!connect_callback_.is_null()) { |
| 104 // WebSocketJob guarantees the first OnReceivedData call contains all |
| 105 // the response headers. |
| 106 const char kMagicKey[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 107 std::string websocket_accept; |
| 108 CHECK(base::Base64Encode(base::SHA1HashString(sec_key_ + kMagicKey), |
| 109 &websocket_accept)); |
| 110 scoped_refptr<net::HttpResponseHeaders> headers( |
| 111 new net::HttpResponseHeaders( |
| 112 net::HttpUtil::AssembleRawHeaders(data, len))); |
| 113 if (headers->response_code() != 101 || |
| 114 !headers->HasHeaderValue("Upgrade", "WebSocket") || |
| 115 !headers->HasHeaderValue("Connection", "Upgrade") || |
| 116 !headers->HasHeaderValue("Sec-WebSocket-Accept", websocket_accept)) { |
| 117 OnConnectFinished(net::ERR_FAILED); |
| 118 return; |
| 119 } |
| 120 OnConnectFinished( |
| 121 state == net::WebSocketJob::OPEN ? net::OK : net::ERR_FAILED); |
| 122 } else if (connected_) { |
| 123 ScopedVector<net::WebSocketFrameChunk> frame_chunks; |
| 124 CHECK(parser_.Decode(data, len, &frame_chunks)); |
| 125 for (size_t i = 0; i < frame_chunks.size(); ++i) { |
| 126 scoped_refptr<net::IOBufferWithSize> buffer = frame_chunks[i]->data; |
| 127 if (buffer) |
| 128 next_message_ += std::string(buffer->data(), buffer->size()); |
| 129 if (frame_chunks[i]->final_chunk) { |
| 130 listener_->OnMessageReceived(next_message_); |
| 131 next_message_.clear(); |
| 132 } |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 void WebSocket::OnClose(net::SocketStream* socket) { |
| 138 if (!connect_callback_.is_null()) |
| 139 OnConnectFinished(net::ERR_CONNECTION_CLOSED); |
| 140 else |
| 141 listener_->OnClose(); |
| 142 } |
| 143 |
| 144 void WebSocket::OnConnectFinished(net::Error error) { |
| 145 if (error == net::OK) |
| 146 connected_ = true; |
| 147 net::CompletionCallback temp = connect_callback_; |
| 148 connect_callback_.Reset(); |
| 149 temp.Run(error); |
| 150 } |
OLD | NEW |