Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: net/server/web_socket.cc

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use base::StringPiece in some places not to copy data. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "net/server/web_socket.h" 5 #include "net/server/web_socket.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/rand_util.h" 10 #include "base/rand_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/md5.h" 12 #include "base/md5.h"
13 #include "base/sha1.h" 13 #include "base/sha1.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/sys_byteorder.h" 16 #include "base/sys_byteorder.h"
17 #include "net/server/http_connection.h" 17 #include "net/server/http_server.h"
18 #include "net/server/http_server_request_info.h" 18 #include "net/server/http_server_request_info.h"
19 #include "net/server/http_server_response_info.h" 19 #include "net/server/http_server_response_info.h"
20 20
21 namespace net { 21 namespace net {
22 22
23 namespace { 23 namespace {
24 24
25 static uint32 WebSocketKeyFingerprint(const std::string& str) { 25 static uint32 WebSocketKeyFingerprint(const std::string& str) {
26 std::string result; 26 std::string result;
27 const char* p_char = str.c_str(); 27 const char* p_char = str.c_str();
28 int length = str.length(); 28 int length = str.length();
29 int spaces = 0; 29 int spaces = 0;
30 for (int i = 0; i < length; ++i) { 30 for (int i = 0; i < length; ++i) {
31 if (p_char[i] >= '0' && p_char[i] <= '9') 31 if (p_char[i] >= '0' && p_char[i] <= '9')
32 result.append(&p_char[i], 1); 32 result.append(&p_char[i], 1);
33 else if (p_char[i] == ' ') 33 else if (p_char[i] == ' ')
34 spaces++; 34 spaces++;
35 } 35 }
36 if (spaces == 0) 36 if (spaces == 0)
37 return 0; 37 return 0;
38 int64 number = 0; 38 int64 number = 0;
39 if (!base::StringToInt64(result, &number)) 39 if (!base::StringToInt64(result, &number))
40 return 0; 40 return 0;
41 return base::HostToNet32(static_cast<uint32>(number / spaces)); 41 return base::HostToNet32(static_cast<uint32>(number / spaces));
42 } 42 }
43 43
44 class WebSocketHixie76 : public net::WebSocket { 44 class WebSocketHixie76 : public net::WebSocket {
45 public: 45 public:
46 static net::WebSocket* Create(HttpConnection* connection, 46 static net::WebSocket* Create(HttpServer* server,
47 HttpConnection* connection,
47 const HttpServerRequestInfo& request, 48 const HttpServerRequestInfo& request,
48 size_t* pos) { 49 size_t* pos) {
49 if (connection->recv_data().length() < *pos + kWebSocketHandshakeBodyLen) 50 if (connection->read_buf()->GetUnconsumedSize()
51 < static_cast<int>(*pos + kWebSocketHandshakeBodyLen))
50 return NULL; 52 return NULL;
51 return new WebSocketHixie76(connection, request, pos); 53 return new WebSocketHixie76(server, connection, request, pos);
52 } 54 }
53 55
54 virtual void Accept(const HttpServerRequestInfo& request) OVERRIDE { 56 virtual void Accept(const HttpServerRequestInfo& request) OVERRIDE {
55 std::string key1 = request.GetHeaderValue("sec-websocket-key1"); 57 std::string key1 = request.GetHeaderValue("sec-websocket-key1");
56 std::string key2 = request.GetHeaderValue("sec-websocket-key2"); 58 std::string key2 = request.GetHeaderValue("sec-websocket-key2");
57 59
58 uint32 fp1 = WebSocketKeyFingerprint(key1); 60 uint32 fp1 = WebSocketKeyFingerprint(key1);
59 uint32 fp2 = WebSocketKeyFingerprint(key2); 61 uint32 fp2 = WebSocketKeyFingerprint(key2);
60 62
61 char data[16]; 63 char data[16];
62 memcpy(data, &fp1, 4); 64 memcpy(data, &fp1, 4);
63 memcpy(data + 4, &fp2, 4); 65 memcpy(data + 4, &fp2, 4);
64 memcpy(data + 8, &key3_[0], 8); 66 memcpy(data + 8, &key3_[0], 8);
65 67
66 base::MD5Digest digest; 68 base::MD5Digest digest;
67 base::MD5Sum(data, 16, &digest); 69 base::MD5Sum(data, 16, &digest);
68 70
69 std::string origin = request.GetHeaderValue("origin"); 71 std::string origin = request.GetHeaderValue("origin");
70 std::string host = request.GetHeaderValue("host"); 72 std::string host = request.GetHeaderValue("host");
71 std::string location = "ws://" + host + request.path; 73 std::string location = "ws://" + host + request.path;
72 connection_->Send(base::StringPrintf( 74 server_->SendRaw(
73 "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" 75 connection_->id(),
74 "Upgrade: WebSocket\r\n" 76 base::StringPrintf("HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
75 "Connection: Upgrade\r\n" 77 "Upgrade: WebSocket\r\n"
76 "Sec-WebSocket-Origin: %s\r\n" 78 "Connection: Upgrade\r\n"
77 "Sec-WebSocket-Location: %s\r\n" 79 "Sec-WebSocket-Origin: %s\r\n"
78 "\r\n", 80 "Sec-WebSocket-Location: %s\r\n"
79 origin.c_str(), 81 "\r\n",
80 location.c_str())); 82 origin.c_str(),
81 connection_->Send(reinterpret_cast<char*>(digest.a), 16); 83 location.c_str()));
84 server_->SendRaw(connection_->id(),
85 std::string(reinterpret_cast<char*>(digest.a), 16));
82 } 86 }
83 87
84 virtual ParseResult Read(std::string* message) OVERRIDE { 88 virtual ParseResult Read(std::string* message) OVERRIDE {
85 DCHECK(message); 89 DCHECK(message);
86 const std::string& data = connection_->recv_data(); 90 HttpConnection::ReadIOBuffer* read_buf = connection_->read_buf();
87 if (data[0]) 91 if (read_buf->data()[0])
88 return FRAME_ERROR; 92 return FRAME_ERROR;
89 93
94 base::StringPiece data(read_buf->data(), read_buf->GetUnconsumedSize());
90 size_t pos = data.find('\377', 1); 95 size_t pos = data.find('\377', 1);
91 if (pos == std::string::npos) 96 if (pos == base::StringPiece::npos)
92 return FRAME_INCOMPLETE; 97 return FRAME_INCOMPLETE;
93 98
94 std::string buffer(data.begin() + 1, data.begin() + pos); 99 message->assign(read_buf->data() + 1, pos - 1);
95 message->swap(buffer); 100 read_buf->DidConsume(pos + 1);
96 connection_->Shift(pos + 1);
97 101
98 return FRAME_OK; 102 return FRAME_OK;
99 } 103 }
100 104
101 virtual void Send(const std::string& message) OVERRIDE { 105 virtual void Send(const std::string& message) OVERRIDE {
102 char message_start = 0; 106 char message_start = 0;
103 char message_end = -1; 107 char message_end = -1;
104 connection_->Send(&message_start, 1); 108 server_->SendRaw(connection_->id(), std::string(1, message_start));
105 connection_->Send(message); 109 server_->SendRaw(connection_->id(), message);
106 connection_->Send(&message_end, 1); 110 server_->SendRaw(connection_->id(), std::string(1, message_end));
107 } 111 }
108 112
109 private: 113 private:
110 static const int kWebSocketHandshakeBodyLen; 114 static const int kWebSocketHandshakeBodyLen;
111 115
112 WebSocketHixie76(HttpConnection* connection, 116 WebSocketHixie76(HttpServer* server,
117 HttpConnection* connection,
113 const HttpServerRequestInfo& request, 118 const HttpServerRequestInfo& request,
114 size_t* pos) : WebSocket(connection) { 119 size_t* pos)
120 : WebSocket(server, connection) {
115 std::string key1 = request.GetHeaderValue("sec-websocket-key1"); 121 std::string key1 = request.GetHeaderValue("sec-websocket-key1");
116 std::string key2 = request.GetHeaderValue("sec-websocket-key2"); 122 std::string key2 = request.GetHeaderValue("sec-websocket-key2");
117 123
118 if (key1.empty()) { 124 if (key1.empty()) {
119 connection->Send(HttpServerResponseInfo::CreateFor500( 125 server->SendResponse(
120 "Invalid request format. Sec-WebSocket-Key1 is empty or isn't " 126 connection->id(),
121 "specified.")); 127 HttpServerResponseInfo::CreateFor500(
128 "Invalid request format. Sec-WebSocket-Key1 is empty or isn't "
129 "specified."));
122 return; 130 return;
123 } 131 }
124 132
125 if (key2.empty()) { 133 if (key2.empty()) {
126 connection->Send(HttpServerResponseInfo::CreateFor500( 134 server->SendResponse(
127 "Invalid request format. Sec-WebSocket-Key2 is empty or isn't " 135 connection->id(),
128 "specified.")); 136 HttpServerResponseInfo::CreateFor500(
137 "Invalid request format. Sec-WebSocket-Key2 is empty or isn't "
138 "specified."));
129 return; 139 return;
130 } 140 }
131 141
132 key3_ = connection->recv_data().substr( 142 key3_.assign(connection->read_buf()->data() + *pos,
133 *pos, 143 kWebSocketHandshakeBodyLen);
134 *pos + kWebSocketHandshakeBodyLen);
135 *pos += kWebSocketHandshakeBodyLen; 144 *pos += kWebSocketHandshakeBodyLen;
136 } 145 }
137 146
138 std::string key3_; 147 std::string key3_;
139 148
140 DISALLOW_COPY_AND_ASSIGN(WebSocketHixie76); 149 DISALLOW_COPY_AND_ASSIGN(WebSocketHixie76);
141 }; 150 };
142 151
143 const int WebSocketHixie76::kWebSocketHandshakeBodyLen = 8; 152 const int WebSocketHixie76::kWebSocketHandshakeBodyLen = 8;
144 153
(...skipping 17 matching lines...) Expand all
162 const unsigned char kMaskBit = 0x80; 171 const unsigned char kMaskBit = 0x80;
163 const unsigned char kPayloadLengthMask = 0x7F; 172 const unsigned char kPayloadLengthMask = 0x7F;
164 173
165 const size_t kMaxSingleBytePayloadLength = 125; 174 const size_t kMaxSingleBytePayloadLength = 125;
166 const size_t kTwoBytePayloadLengthField = 126; 175 const size_t kTwoBytePayloadLengthField = 126;
167 const size_t kEightBytePayloadLengthField = 127; 176 const size_t kEightBytePayloadLengthField = 127;
168 const size_t kMaskingKeyWidthInBytes = 4; 177 const size_t kMaskingKeyWidthInBytes = 4;
169 178
170 class WebSocketHybi17 : public WebSocket { 179 class WebSocketHybi17 : public WebSocket {
171 public: 180 public:
172 static WebSocket* Create(HttpConnection* connection, 181 static WebSocket* Create(HttpServer* server,
182 HttpConnection* connection,
173 const HttpServerRequestInfo& request, 183 const HttpServerRequestInfo& request,
174 size_t* pos) { 184 size_t* pos) {
175 std::string version = request.GetHeaderValue("sec-websocket-version"); 185 std::string version = request.GetHeaderValue("sec-websocket-version");
176 if (version != "8" && version != "13") 186 if (version != "8" && version != "13")
177 return NULL; 187 return NULL;
178 188
179 std::string key = request.GetHeaderValue("sec-websocket-key"); 189 std::string key = request.GetHeaderValue("sec-websocket-key");
180 if (key.empty()) { 190 if (key.empty()) {
181 connection->Send(HttpServerResponseInfo::CreateFor500( 191 server->SendResponse(
182 "Invalid request format. Sec-WebSocket-Key is empty or isn't " 192 connection->id(),
183 "specified.")); 193 HttpServerResponseInfo::CreateFor500(
194 "Invalid request format. Sec-WebSocket-Key is empty or isn't "
195 "specified."));
184 return NULL; 196 return NULL;
185 } 197 }
186 return new WebSocketHybi17(connection, request, pos); 198 return new WebSocketHybi17(server, connection, request, pos);
187 } 199 }
188 200
189 virtual void Accept(const HttpServerRequestInfo& request) OVERRIDE { 201 virtual void Accept(const HttpServerRequestInfo& request) OVERRIDE {
190 static const char* const kWebSocketGuid = 202 static const char* const kWebSocketGuid =
191 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; 203 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
192 std::string key = request.GetHeaderValue("sec-websocket-key"); 204 std::string key = request.GetHeaderValue("sec-websocket-key");
193 std::string data = base::StringPrintf("%s%s", key.c_str(), kWebSocketGuid); 205 std::string data = base::StringPrintf("%s%s", key.c_str(), kWebSocketGuid);
194 std::string encoded_hash; 206 std::string encoded_hash;
195 base::Base64Encode(base::SHA1HashString(data), &encoded_hash); 207 base::Base64Encode(base::SHA1HashString(data), &encoded_hash);
196 208
197 std::string response = base::StringPrintf( 209 server_->SendRaw(
198 "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" 210 connection_->id(),
199 "Upgrade: WebSocket\r\n" 211 base::StringPrintf("HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
200 "Connection: Upgrade\r\n" 212 "Upgrade: WebSocket\r\n"
201 "Sec-WebSocket-Accept: %s\r\n" 213 "Connection: Upgrade\r\n"
202 "\r\n", 214 "Sec-WebSocket-Accept: %s\r\n"
203 encoded_hash.c_str()); 215 "\r\n",
204 connection_->Send(response); 216 encoded_hash.c_str()));
205 } 217 }
206 218
207 virtual ParseResult Read(std::string* message) OVERRIDE { 219 virtual ParseResult Read(std::string* message) OVERRIDE {
208 const std::string& frame = connection_->recv_data(); 220 HttpConnection::ReadIOBuffer* read_buf = connection_->read_buf();
221 base::StringPiece frame(read_buf->data(), read_buf->GetUnconsumedSize());
209 int bytes_consumed = 0; 222 int bytes_consumed = 0;
210
211 ParseResult result = 223 ParseResult result =
212 WebSocket::DecodeFrameHybi17(frame, true, &bytes_consumed, message); 224 WebSocket::DecodeFrameHybi17(frame, true, &bytes_consumed, message);
213 if (result == FRAME_OK) 225 if (result == FRAME_OK)
214 connection_->Shift(bytes_consumed); 226 read_buf->DidConsume(bytes_consumed);
215 if (result == FRAME_CLOSE) 227 if (result == FRAME_CLOSE)
216 closed_ = true; 228 closed_ = true;
217 return result; 229 return result;
218 } 230 }
219 231
220 virtual void Send(const std::string& message) OVERRIDE { 232 virtual void Send(const std::string& message) OVERRIDE {
221 if (closed_) 233 if (closed_)
222 return; 234 return;
223 std::string data = WebSocket::EncodeFrameHybi17(message, 0); 235 server_->SendRaw(connection_->id(),
224 connection_->Send(data); 236 WebSocket::EncodeFrameHybi17(message, 0));
225 } 237 }
226 238
227 private: 239 private:
228 WebSocketHybi17(HttpConnection* connection, 240 WebSocketHybi17(HttpServer* server,
241 HttpConnection* connection,
229 const HttpServerRequestInfo& request, 242 const HttpServerRequestInfo& request,
230 size_t* pos) 243 size_t* pos)
231 : WebSocket(connection), 244 : WebSocket(server, connection),
232 op_code_(0), 245 op_code_(0),
233 final_(false), 246 final_(false),
234 reserved1_(false), 247 reserved1_(false),
235 reserved2_(false), 248 reserved2_(false),
236 reserved3_(false), 249 reserved3_(false),
237 masked_(false), 250 masked_(false),
238 payload_(0), 251 payload_(0),
239 payload_length_(0), 252 payload_length_(0),
240 frame_end_(0), 253 frame_end_(0),
241 closed_(false) { 254 closed_(false) {
242 } 255 }
243 256
244 OpCode op_code_; 257 OpCode op_code_;
245 bool final_; 258 bool final_;
246 bool reserved1_; 259 bool reserved1_;
247 bool reserved2_; 260 bool reserved2_;
248 bool reserved3_; 261 bool reserved3_;
249 bool masked_; 262 bool masked_;
250 const char* payload_; 263 const char* payload_;
251 size_t payload_length_; 264 size_t payload_length_;
252 const char* frame_end_; 265 const char* frame_end_;
253 bool closed_; 266 bool closed_;
254 267
255 DISALLOW_COPY_AND_ASSIGN(WebSocketHybi17); 268 DISALLOW_COPY_AND_ASSIGN(WebSocketHybi17);
256 }; 269 };
257 270
258 } // anonymous namespace 271 } // anonymous namespace
259 272
260 WebSocket* WebSocket::CreateWebSocket(HttpConnection* connection, 273 WebSocket* WebSocket::CreateWebSocket(HttpServer* server,
274 HttpConnection* connection,
261 const HttpServerRequestInfo& request, 275 const HttpServerRequestInfo& request,
262 size_t* pos) { 276 size_t* pos) {
263 WebSocket* socket = WebSocketHybi17::Create(connection, request, pos); 277 WebSocket* socket = WebSocketHybi17::Create(server, connection, request, pos);
264 if (socket) 278 if (socket)
265 return socket; 279 return socket;
266 280
267 return WebSocketHixie76::Create(connection, request, pos); 281 return WebSocketHixie76::Create(server, connection, request, pos);
268 } 282 }
269 283
270 // static 284 // static
271 WebSocket::ParseResult WebSocket::DecodeFrameHybi17(const std::string& frame, 285 WebSocket::ParseResult WebSocket::DecodeFrameHybi17(
272 bool client_frame, 286 const base::StringPiece& frame,
273 int* bytes_consumed, 287 bool client_frame,
274 std::string* output) { 288 int* bytes_consumed,
289 std::string* output) {
275 size_t data_length = frame.length(); 290 size_t data_length = frame.length();
276 if (data_length < 2) 291 if (data_length < 2)
277 return FRAME_INCOMPLETE; 292 return FRAME_INCOMPLETE;
278 293
279 const char* buffer_begin = const_cast<char*>(frame.data()); 294 const char* buffer_begin = const_cast<char*>(frame.data());
280 const char* p = buffer_begin; 295 const char* p = buffer_begin;
281 const char* buffer_end = p + data_length; 296 const char* buffer_end = p + data_length;
282 297
283 unsigned char first_byte = *p++; 298 unsigned char first_byte = *p++;
284 unsigned char second_byte = *p++; 299 unsigned char second_byte = *p++;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 if (static_cast<size_t>(buffer_end - p) < total_length) 357 if (static_cast<size_t>(buffer_end - p) < total_length)
343 return FRAME_INCOMPLETE; 358 return FRAME_INCOMPLETE;
344 359
345 if (masked) { 360 if (masked) {
346 output->resize(payload_length); 361 output->resize(payload_length);
347 const char* masking_key = p; 362 const char* masking_key = p;
348 char* payload = const_cast<char*>(p + kMaskingKeyWidthInBytes); 363 char* payload = const_cast<char*>(p + kMaskingKeyWidthInBytes);
349 for (size_t i = 0; i < payload_length; ++i) // Unmask the payload. 364 for (size_t i = 0; i < payload_length; ++i) // Unmask the payload.
350 (*output)[i] = payload[i] ^ masking_key[i % kMaskingKeyWidthInBytes]; 365 (*output)[i] = payload[i] ^ masking_key[i % kMaskingKeyWidthInBytes];
351 } else { 366 } else {
352 std::string buffer(p, p + payload_length); 367 output->assign(p, p + payload_length);
353 output->swap(buffer);
354 } 368 }
355 369
356 size_t pos = p + actual_masking_key_length + payload_length - buffer_begin; 370 size_t pos = p + actual_masking_key_length + payload_length - buffer_begin;
357 *bytes_consumed = pos; 371 *bytes_consumed = pos;
358 return closed ? FRAME_CLOSE : FRAME_OK; 372 return closed ? FRAME_CLOSE : FRAME_OK;
359 } 373 }
360 374
361 // static 375 // static
362 std::string WebSocket::EncodeFrameHybi17(const std::string& message, 376 std::string WebSocket::EncodeFrameHybi17(const std::string& message,
363 int masking_key) { 377 int masking_key) {
(...skipping 29 matching lines...) Expand all
393 const char* mask_bytes = reinterpret_cast<char*>(&masking_key); 407 const char* mask_bytes = reinterpret_cast<char*>(&masking_key);
394 frame.insert(frame.end(), mask_bytes, mask_bytes + 4); 408 frame.insert(frame.end(), mask_bytes, mask_bytes + 4);
395 for (size_t i = 0; i < data_length; ++i) // Mask the payload. 409 for (size_t i = 0; i < data_length; ++i) // Mask the payload.
396 frame.push_back(data[i] ^ mask_bytes[i % kMaskingKeyWidthInBytes]); 410 frame.push_back(data[i] ^ mask_bytes[i % kMaskingKeyWidthInBytes]);
397 } else { 411 } else {
398 frame.insert(frame.end(), data, data + data_length); 412 frame.insert(frame.end(), data, data + data_length);
399 } 413 }
400 return std::string(&frame[0], frame.size()); 414 return std::string(&frame[0], frame.size());
401 } 415 }
402 416
403 WebSocket::WebSocket(HttpConnection* connection) : connection_(connection) { 417 WebSocket::WebSocket(HttpServer* server, HttpConnection* connection)
418 : server_(server),
419 connection_(connection) {
404 } 420 }
405 421
406 } // namespace net 422 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698