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

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

Issue 7482041: DevTools: no way to remote debug using ToT build. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cosmetic changes Created 9 years, 4 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 | Annotate | Revision Log
« net/server/http_server.h ('K') | « net/server/http_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/http_server.h" 5 #include "net/server/http_server.h"
6 6
7 #include "base/base64.h"
7 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "base/md5.h" 10 #include "base/md5.h"
11 #include "base/rand_util.h"
12 #include "base/sha1.h"
10 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
11 #include "base/string_util.h" 14 #include "base/string_util.h"
12 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
13 #include "build/build_config.h" 16 #include "build/build_config.h"
14 #include "net/server/http_server_request_info.h" 17 #include "net/server/http_server_request_info.h"
15 18
16 #if defined(OS_WIN) 19 #if defined(OS_WIN)
17 #include <winsock2.h> 20 #include <winsock2.h>
18 #else 21 #else
19 #include <arpa/inet.h> 22 #include <arpa/inet.h>
20 #endif 23 #endif
21 24
25 #include <limits>
26
22 namespace net { 27 namespace net {
23 28
24 int HttpServer::Connection::lastId_ = 0; 29 int HttpServer::Connection::lastId_ = 0;
25 30
26 HttpServer::HttpServer(const std::string& host, 31 HttpServer::HttpServer(const std::string& host,
27 int port, 32 int port,
28 HttpServer::Delegate* del) 33 HttpServer::Delegate* del)
29 : delegate_(del) { 34 : delegate_(del) {
30 server_ = ListenSocket::Listen(host, port, this); 35 server_ = ListenSocket::Listen(host, port, this);
31 } 36 }
(...skipping 28 matching lines...) Expand all
60 spaces++; 65 spaces++;
61 } 66 }
62 if (spaces == 0) 67 if (spaces == 0)
63 return 0; 68 return 0;
64 int64 number = 0; 69 int64 number = 0;
65 if (!base::StringToInt64(result, &number)) 70 if (!base::StringToInt64(result, &number))
66 return 0; 71 return 0;
67 return htonl(static_cast<uint32>(number / spaces)); 72 return htonl(static_cast<uint32>(number / spaces));
68 } 73 }
69 74
70 void HttpServer::AcceptWebSocket( 75 void HttpServer::AcceptWebSocketHixie76(
71 int connection_id, 76 HttpServer::Connection* connection,
72 const HttpServerRequestInfo& request) { 77 const HttpServerRequestInfo& request) {
73 Connection* connection = FindConnection(connection_id);
74 if (connection == NULL)
75 return;
76
77 std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1"); 78 std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1");
78 std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2"); 79 std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2");
79 80
80 uint32 fp1 = WebSocketKeyFingerprint(key1); 81 uint32 fp1 = WebSocketKeyFingerprint(key1);
81 uint32 fp2 = WebSocketKeyFingerprint(key2); 82 uint32 fp2 = WebSocketKeyFingerprint(key2);
82 83
83 char data[16]; 84 char data[16];
84 memcpy(data, &fp1, 4); 85 memcpy(data, &fp1, 4);
85 memcpy(data + 4, &fp2, 4); 86 memcpy(data + 4, &fp2, 4);
86 memcpy(data + 8, &request.data[0], 8); 87 memcpy(data + 8, &request.data[0], 8);
(...skipping 10 matching lines...) Expand all
97 "Upgrade: WebSocket\r\n" 98 "Upgrade: WebSocket\r\n"
98 "Connection: Upgrade\r\n" 99 "Connection: Upgrade\r\n"
99 "Sec-WebSocket-Origin: %s\r\n" 100 "Sec-WebSocket-Origin: %s\r\n"
100 "Sec-WebSocket-Location: %s\r\n" 101 "Sec-WebSocket-Location: %s\r\n"
101 "\r\n", 102 "\r\n",
102 origin.c_str(), 103 origin.c_str(),
103 location.c_str())); 104 location.c_str()));
104 connection->socket_->Send(reinterpret_cast<char*>(digest.a), 16); 105 connection->socket_->Send(reinterpret_cast<char*>(digest.a), 16);
105 } 106 }
106 107
107 void HttpServer::SendOverWebSocket(int connection_id, 108 void HttpServer::SendOverWebSocketHixie76(HttpServer::Connection* connection,
108 const std::string& data) { 109 const std::string& data) {
109 Connection* connection = FindConnection(connection_id);
110 if (connection == NULL)
111 return;
112
113 DCHECK(connection->is_web_socket_);
114 char message_start = 0; 110 char message_start = 0;
115 char message_end = -1; 111 char message_end = -1;
116 connection->socket_->Send(&message_start, 1); 112 connection->socket_->Send(&message_start, 1);
117 connection->socket_->Send(data); 113 connection->socket_->Send(data);
118 connection->socket_->Send(&message_end, 1); 114 connection->socket_->Send(&message_end, 1);
119 } 115 }
120 116
121 void HttpServer::Send(int connection_id, const std::string& data) { 117 void HttpServer::Send(int connection_id, const std::string& data) {
122 Connection* connection = FindConnection(connection_id); 118 Connection* connection = FindConnection(connection_id);
123 if (connection == NULL) 119 if (connection == NULL)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 Connection* connection = FindConnection(connection_id); 178 Connection* connection = FindConnection(connection_id);
183 if (connection == NULL) 179 if (connection == NULL)
184 return; 180 return;
185 181
186 connection->DetachSocket(); 182 connection->DetachSocket();
187 } 183 }
188 184
189 HttpServer::Connection::Connection(HttpServer* server, ListenSocket* sock) 185 HttpServer::Connection::Connection(HttpServer* server, ListenSocket* sock)
190 : server_(server), 186 : server_(server),
191 socket_(sock), 187 socket_(sock),
192 is_web_socket_(false) { 188 is_web_socket_(false),
189 use_hybi10_web_socket_(false) {
193 id_ = lastId_++; 190 id_ = lastId_++;
194 } 191 }
195 192
196 HttpServer::Connection::~Connection() { 193 HttpServer::Connection::~Connection() {
197 DetachSocket(); 194 DetachSocket();
198 server_->delegate_->OnClose(id_); 195 server_->delegate_->OnClose(id_);
199 } 196 }
200 197
201 void HttpServer::Connection::DetachSocket() { 198 void HttpServer::Connection::DetachSocket() {
202 socket_ = NULL; 199 socket_ = NULL;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 int len) { 367 int len) {
371 Connection* connection = FindConnection(socket); 368 Connection* connection = FindConnection(socket);
372 DCHECK(connection != NULL); 369 DCHECK(connection != NULL);
373 if (connection == NULL) 370 if (connection == NULL)
374 return; 371 return;
375 372
376 connection->recv_data_.append(data, len); 373 connection->recv_data_.append(data, len);
377 while (connection->recv_data_.length()) { 374 while (connection->recv_data_.length()) {
378 int pos = 0; 375 int pos = 0;
379 HttpServerRequestInfo request; 376 HttpServerRequestInfo request;
380 if (!ParseHeaders(connection, &request, &pos)) 377 if (!connection->use_hybi10_web_socket_ &&
378 !ParseHeaders(connection, &request, &pos))
381 break; 379 break;
382 380
383 if (connection->is_web_socket_) { 381 if (connection->is_web_socket_) {
384 delegate_->OnWebSocketMessage(connection->id_, request.data); 382 if (connection->use_hybi10_web_socket_) {
385 connection->Shift(pos); 383 std::string message;
384 if (!DidReadWebSocketHyBi10(
385 connection,
386 (char*)(connection->recv_data_.c_str() + pos),
387 connection->recv_data_.length() - pos,
388 &message))
389 break;
390 delegate_->OnWebSocketMessage(connection->id_, message);
391 } else {
392 delegate_->OnWebSocketMessage(connection->id_, request.data);
393 connection->Shift(pos);
394 }
395
386 continue; 396 continue;
387 } 397 }
388 398
389 std::string connection_header = GetHeaderValue(request, "Connection"); 399 std::string connection_header = GetHeaderValue(request, "Connection");
390 if (connection_header == "Upgrade") { 400 if (connection_header == "Upgrade") {
401 std::string version = GetHeaderValue(request, "Sec-WebSocket-Version");
402 if (version.length()) {
pfeldman 2011/07/28 09:32:47 compare to "8"?
403 if (WebSocketHandshakeHyBi10(connection, request))
404 connection->Shift(pos);
405 continue;
406 }
407
391 // Is this WebSocket and if yes, upgrade the connection. 408 // Is this WebSocket and if yes, upgrade the connection.
392 std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1"); 409 std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1");
393 std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2"); 410 std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2");
394 411
395 const int websocket_handshake_body_len = 8; 412 const int websocket_handshake_body_len = 8;
396 if (pos + websocket_handshake_body_len > 413 if (pos + websocket_handshake_body_len >
397 static_cast<int>(connection->recv_data_.length())) { 414 static_cast<int>(connection->recv_data_.length())) {
398 // We haven't received websocket handshake body yet. Wait. 415 // We haven't received websocket handshake body yet. Wait.
399 break; 416 break;
400 } 417 }
(...skipping 29 matching lines...) Expand all
430 return it->second; 447 return it->second;
431 } 448 }
432 449
433 HttpServer::Connection* HttpServer::FindConnection(ListenSocket* socket) { 450 HttpServer::Connection* HttpServer::FindConnection(ListenSocket* socket) {
434 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket); 451 SocketToConnectionMap::iterator it = socket_to_connection_.find(socket);
435 if (it == socket_to_connection_.end()) 452 if (it == socket_to_connection_.end())
436 return NULL; 453 return NULL;
437 return it->second; 454 return it->second;
438 } 455 }
439 456
457 bool HttpServer::WebSocketHandshakeHyBi10(
458 Connection* connection,
459 const HttpServerRequestInfo& request) {
460 if (GetHeaderValue(request, "Connection") == "Upgrade" &&
461 GetHeaderValue(request, "Upgrade") == "websocket") {
462 // Is this WebSocket and if yes, upgrade the connection.
463 std::string key = GetHeaderValue(request, "Sec-WebSocket-Key");
464
465 if (!key.empty()) {
466 connection->use_hybi10_web_socket_ = true;
467 delegate_->OnWebSocketRequest(connection->id_, request);
468 return true;
469 }
470 }
471 return false;
472 }
473
474 void HttpServer::AcceptWebSocket(
475 int connection_id,
476 const HttpServerRequestInfo& request) {
477 Connection* connection = FindConnection(connection_id);
478 if (connection == NULL)
479 return;
480 if (connection->use_hybi10_web_socket_)
481 AcceptWebSocketHyBi10(connection, request);
482 else
483 AcceptWebSocketHixie76(connection, request);
484 }
485
486 static std::string GenerateWebSocketHyBi10AcceptKey(
487 const std::string& sec_web_socket_key)
488 {
489 static const char* const web_socket_key_GUID =
490 "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
491 std::string data(sec_web_socket_key);
pfeldman 2011/07/28 09:32:47 Use StringPrintf ?
492 data += web_socket_key_GUID;
493 std::string encoded_hash;
494 base::Base64Encode(base::SHA1HashString(data), &encoded_hash);
495 return encoded_hash;
496 }
497
498 void HttpServer::AcceptWebSocketHyBi10(
499 HttpServer::Connection* connection,
500 const HttpServerRequestInfo& request) {
501 std::string key = GetHeaderValue(request, "Sec-WebSocket-Key");
502 connection->is_web_socket_ = true;
503
504 std::string response = base::StringPrintf(
505 "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
506 "Upgrade: WebSocket\r\n"
507 "Connection: Upgrade\r\n"
508 "Sec-WebSocket-Accept: %s\r\n"
509 "\r\n",
510 GenerateWebSocketHyBi10AcceptKey(key).c_str());
511 connection->socket_->Send(response);
512 }
513
514 struct WebSocketFrame {
pfeldman 2011/07/28 09:32:47 Declare this struct in anonymous namespace above.
515 // Hybi-10 opcodes.
516 typedef unsigned int OpCode;
517 static const OpCode kOpCodeContinuation;
518 static const OpCode kOpCodeText;
519 static const OpCode kOpCodeBinary;
520 static const OpCode kOpCodeClose;
521 static const OpCode kOpCodePing;
522 static const OpCode kOpCodePong;
523
524 static const unsigned char kFinalBit;
525 static const unsigned char kReserved1Bit;
526 static const unsigned char kReserved2Bit;
527 static const unsigned char kReserved3Bit;
528 static const unsigned char kOpCodeMask;
529 static const unsigned char kMaskBit;
530 static const unsigned char kPayloadLengthMask;
531
532 static const size_t kMaxSingleBytePayloadLength;
533 static const size_t kTwoBytePayloadLengthField;
534 static const size_t kEightBytePayloadLengthField;
535 static const size_t kMaskingKeyWidthInBytes;
536
537 enum ParseResult {
538 FrameOK,
539 FrameIncomplete,
540 FrameError
541 };
542
543 OpCode op_code_;
544 bool final_;
545 bool reserved1_;
546 bool reserved2_;
547 bool reserved3_;
548 bool masked_;
549 const char* payload_;
550 size_t payload_length_;
551 const char* frame_end_;
552
553 bool isNonControlOpCode() const {
554 return op_code_ == kOpCodeContinuation ||
555 op_code_ == kOpCodeText ||
556 op_code_ == kOpCodeBinary;
557 }
558 bool isControlOpCode() const {
559 return op_code_ == kOpCodeClose ||
560 op_code_ == kOpCodePing ||
561 op_code_ == kOpCodePong;
562 }
563 bool isReservedOpCode() const {
564 return !isNonControlOpCode() && !isControlOpCode();
565 }
566
567 ParseResult DecodeFrame(const char* data, size_t data_length);
568 std::vector<char> EncodeFrame(OpCode op_code,
569 const char* data,
570 size_t data_length);
571 };
572
573 // Constants for hybi-10 frame format.
574 const WebSocketFrame::OpCode WebSocketFrame::kOpCodeContinuation = 0x0;
575 const WebSocketFrame::OpCode WebSocketFrame::kOpCodeText = 0x1;
576 const WebSocketFrame::OpCode WebSocketFrame::kOpCodeBinary = 0x2;
577 const WebSocketFrame::OpCode WebSocketFrame::kOpCodeClose = 0x8;
578 const WebSocketFrame::OpCode WebSocketFrame::kOpCodePing = 0x9;
579 const WebSocketFrame::OpCode WebSocketFrame::kOpCodePong = 0xA;
580
581 const unsigned char WebSocketFrame::kFinalBit = 0x80;
582 const unsigned char WebSocketFrame::kReserved1Bit = 0x40;
583 const unsigned char WebSocketFrame::kReserved2Bit = 0x20;
584 const unsigned char WebSocketFrame::kReserved3Bit = 0x10;
585 const unsigned char WebSocketFrame::kOpCodeMask = 0xF;
586 const unsigned char WebSocketFrame::kMaskBit = 0x80;
587 const unsigned char WebSocketFrame::kPayloadLengthMask = 0x7F;
588
589 const size_t WebSocketFrame::kMaxSingleBytePayloadLength = 125;
590 const size_t WebSocketFrame::kTwoBytePayloadLengthField = 126;
591 const size_t WebSocketFrame::kEightBytePayloadLengthField = 127;
592 const size_t WebSocketFrame::kMaskingKeyWidthInBytes = 4;
593
594 void HttpServer::SendOverWebSocket(int connection_id,
595 const std::string& data) {
596 Connection* connection = FindConnection(connection_id);
597 if (connection == NULL)
598 return;
599 DCHECK(connection->is_web_socket_);
600 if (connection->use_hybi10_web_socket_)
601 SendOverWebSocketHyBi10(connection, data);
602 else
603 SendOverWebSocketHixie76(connection, data);
604 }
605
606 WebSocketFrame::ParseResult WebSocketFrame::DecodeFrame(
607 const char* data, size_t data_length) {
608 const char* p = data;
609 const char* buffer_end = data + data_length;
610
611 if (data_length < 2)
612 return FrameIncomplete;
613
614 unsigned char first_byte = *p++;
615 unsigned char second_byte = *p++;
616
617 final_ = first_byte & kFinalBit;
618 reserved1_ = first_byte & kReserved1Bit;
619 reserved2_ = first_byte & kReserved2Bit;
620 reserved3_ = first_byte & kReserved3Bit;
621 op_code_ = first_byte & kOpCodeMask;
622 masked_ = second_byte & kMaskBit;
623
624 uint64_t payload_length64 = second_byte & kPayloadLengthMask;
625 if (payload_length64 > kMaxSingleBytePayloadLength) {
626 int extended_payload_length_size;
627 if (payload_length64 == kTwoBytePayloadLengthField)
628 extended_payload_length_size = 2;
629 else {
630 CHECK(payload_length64 == kEightBytePayloadLengthField);
631 extended_payload_length_size = 8;
632 }
633 if (buffer_end - p < extended_payload_length_size)
634 return FrameIncomplete;
635 payload_length64 = 0;
636 for (int i = 0; i < extended_payload_length_size; ++i) {
637 payload_length64 <<= 8;
638 payload_length64 |= static_cast<unsigned char>(*p++);
639 }
640 }
641
642 static const uint64_t max_payload_ength = 0x7FFFFFFFFFFFFFFFull;
643 size_t masking_key_length = masked_ ? kMaskingKeyWidthInBytes : 0;
644 static size_t max_length = std::numeric_limits<size_t>::max();
645 if (payload_length64 > max_payload_ength ||
646 payload_length64 + masking_key_length > max_length) {
647 // WebSocket frame length too large.
648 return FrameError;
649 }
650 payload_length_ = static_cast<size_t>(payload_length64);
651
652 size_t total_length = masking_key_length + payload_length_;
653 if (static_cast<size_t>(buffer_end - p) < total_length)
654 return FrameIncomplete;
655
656 if (masked_) {
657 const char* masking_key = p;
658 char* payload = const_cast<char*>(p + kMaskingKeyWidthInBytes);
659 for (size_t i = 0; i < payload_length_; ++i) // Unmask the payload.
660 payload[i] ^= masking_key[i % kMaskingKeyWidthInBytes];
661 }
662
663 payload_ = p + masking_key_length;
664 frame_end_ = p + masking_key_length + payload_length_;
665 return FrameOK;
666 }
667
668 std::vector<char> WebSocketFrame::EncodeFrame(
669 OpCode op_code, const char* data, size_t data_length) {
670 std::vector<char> frame;
671
672 // Checks whether "op_code" fits in the range of opCodes.
673 CHECK(!(op_code & ~kOpCodeMask));
pfeldman 2011/07/28 09:32:47 DCHECK
674 frame.push_back(kFinalBit | op_code);
675 if (data_length <= kMaxSingleBytePayloadLength)
676 frame.push_back(kMaskBit | data_length);
677 else if (data_length <= 0xFFFF) {
678 frame.push_back(kMaskBit | kTwoBytePayloadLengthField);
679 frame.push_back((data_length & 0xFF00) >> 8);
680 frame.push_back(data_length & 0xFF);
681 } else {
682 frame.push_back(kMaskBit | kEightBytePayloadLengthField);
683 char extended_payload_length[8];
684 size_t remaining = data_length;
685 // Fill the length into extended_payload_length in the network byte order.
686 for (int i = 0; i < 8; ++i) {
687 extended_payload_length[7 - i] = remaining & 0xFF;
688 remaining >>= 8;
689 }
690 frame.insert(frame.end(),
691 extended_payload_length,
692 extended_payload_length + 8);
693 CHECK(!remaining);
694 }
695
696 // Mask the frame.
697 size_t masking_key_start = frame.size();
698 // Add placeholder for masking key. Will be overwritten.
699 frame.resize(frame.size() + kMaskingKeyWidthInBytes);
700 size_t payload_start = frame.size();
701 frame.insert(frame.end(), data, data + data_length);
702
703 base::RandBytes(frame.data() + masking_key_start,
704 kMaskingKeyWidthInBytes);
705 for (size_t i = 0; i < data_length; ++i)
pfeldman 2011/07/28 09:32:47 Use {} since more than one line.
706 frame[payload_start + i] ^=
707 frame[masking_key_start + i % kMaskingKeyWidthInBytes];
708
709 return frame;
710 }
711
712 void HttpServer::SendOverWebSocketHyBi10(HttpServer::Connection* connection,
713 const std::string& data) {
714 WebSocketFrame frame;
715 std::vector<char> buffer = frame.EncodeFrame(WebSocketFrame::kOpCodeText,
716 data.c_str(), data.length());
717 connection->socket_->Send(&buffer[0], buffer.size());
718 }
719
720 bool HttpServer::DidReadWebSocketHyBi10(HttpServer::Connection* connection,
721 char* data,
722 size_t length,
723 std::string* message) {
724 WebSocketFrame frame;
725 WebSocketFrame::ParseResult parse_result = frame.DecodeFrame(data, length);
726 if (parse_result == WebSocketFrame::FrameError)
727 return false;
728
729 if (parse_result == WebSocketFrame::FrameIncomplete)
730 return false;
731
732 CHECK(parse_result == WebSocketFrame::FrameOK);
733 *message = std::string(frame.payload_, frame.payload_length_);
734 connection->Shift(frame.frame_end_ - data);
735
736 return true;
737 }
738
440 } // namespace net 739 } // namespace net
OLDNEW
« net/server/http_server.h ('K') | « net/server/http_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698