Chromium Code Reviews| Index: net/server/http_server.cc |
| diff --git a/net/server/http_server.cc b/net/server/http_server.cc |
| index 061731a9d2b1b5ae7260d68ed1df49783645eab6..bf74b99b93637cc1fd5ec60c8be8b984e86049d4 100644 |
| --- a/net/server/http_server.cc |
| +++ b/net/server/http_server.cc |
| @@ -4,9 +4,12 @@ |
| #include "net/server/http_server.h" |
| +#include "base/base64.h" |
| #include "base/compiler_specific.h" |
| #include "base/logging.h" |
| #include "base/md5.h" |
| +#include "base/rand_util.h" |
| +#include "base/sha1.h" |
| #include "base/string_number_conversions.h" |
| #include "base/string_util.h" |
| #include "base/stringprintf.h" |
| @@ -19,6 +22,8 @@ |
| #include <arpa/inet.h> |
| #endif |
| +#include <limits> |
| + |
| namespace net { |
| int HttpServer::Connection::lastId_ = 0; |
| @@ -67,13 +72,9 @@ uint32 WebSocketKeyFingerprint(const std::string& str) { |
| return htonl(static_cast<uint32>(number / spaces)); |
| } |
| -void HttpServer::AcceptWebSocket( |
| - int connection_id, |
| +void HttpServer::AcceptWebSocketHixie76( |
| + HttpServer::Connection* connection, |
| const HttpServerRequestInfo& request) { |
| - Connection* connection = FindConnection(connection_id); |
| - if (connection == NULL) |
| - return; |
| - |
| std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1"); |
| std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2"); |
| @@ -104,13 +105,8 @@ void HttpServer::AcceptWebSocket( |
| connection->socket_->Send(reinterpret_cast<char*>(digest.a), 16); |
| } |
| -void HttpServer::SendOverWebSocket(int connection_id, |
| - const std::string& data) { |
| - Connection* connection = FindConnection(connection_id); |
| - if (connection == NULL) |
| - return; |
| - |
| - DCHECK(connection->is_web_socket_); |
| +void HttpServer::SendOverWebSocketHixie76(HttpServer::Connection* connection, |
| + const std::string& data) { |
| char message_start = 0; |
| char message_end = -1; |
| connection->socket_->Send(&message_start, 1); |
| @@ -189,7 +185,8 @@ void HttpServer::Close(int connection_id) |
| HttpServer::Connection::Connection(HttpServer* server, ListenSocket* sock) |
| : server_(server), |
| socket_(sock), |
| - is_web_socket_(false) { |
| + is_web_socket_(false), |
| + use_hybi10_web_socket_(false) { |
| id_ = lastId_++; |
| } |
| @@ -377,17 +374,37 @@ void HttpServer::DidRead(ListenSocket* socket, |
| while (connection->recv_data_.length()) { |
| int pos = 0; |
| HttpServerRequestInfo request; |
| - if (!ParseHeaders(connection, &request, &pos)) |
| + if (!connection->use_hybi10_web_socket_ && |
| + !ParseHeaders(connection, &request, &pos)) |
| break; |
| if (connection->is_web_socket_) { |
| - delegate_->OnWebSocketMessage(connection->id_, request.data); |
| - connection->Shift(pos); |
| + if (connection->use_hybi10_web_socket_) { |
| + std::string message; |
| + if (!DidReadWebSocketHyBi10( |
| + connection, |
| + (char*)(connection->recv_data_.c_str() + pos), |
| + connection->recv_data_.length() - pos, |
| + &message)) |
| + break; |
| + delegate_->OnWebSocketMessage(connection->id_, message); |
| + } else { |
| + delegate_->OnWebSocketMessage(connection->id_, request.data); |
| + connection->Shift(pos); |
| + } |
| + |
| continue; |
| } |
| std::string connection_header = GetHeaderValue(request, "Connection"); |
| if (connection_header == "Upgrade") { |
| + std::string version = GetHeaderValue(request, "Sec-WebSocket-Version"); |
| + if (version.length()) { |
|
pfeldman
2011/07/28 09:32:47
compare to "8"?
|
| + if (WebSocketHandshakeHyBi10(connection, request)) |
| + connection->Shift(pos); |
| + continue; |
| + } |
| + |
| // Is this WebSocket and if yes, upgrade the connection. |
| std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1"); |
| std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2"); |
| @@ -437,4 +454,286 @@ HttpServer::Connection* HttpServer::FindConnection(ListenSocket* socket) { |
| return it->second; |
| } |
| +bool HttpServer::WebSocketHandshakeHyBi10( |
| + Connection* connection, |
| + const HttpServerRequestInfo& request) { |
| + if (GetHeaderValue(request, "Connection") == "Upgrade" && |
| + GetHeaderValue(request, "Upgrade") == "websocket") { |
| + // Is this WebSocket and if yes, upgrade the connection. |
| + std::string key = GetHeaderValue(request, "Sec-WebSocket-Key"); |
| + |
| + if (!key.empty()) { |
| + connection->use_hybi10_web_socket_ = true; |
| + delegate_->OnWebSocketRequest(connection->id_, request); |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +void HttpServer::AcceptWebSocket( |
| + int connection_id, |
| + const HttpServerRequestInfo& request) { |
| + Connection* connection = FindConnection(connection_id); |
| + if (connection == NULL) |
| + return; |
| + if (connection->use_hybi10_web_socket_) |
| + AcceptWebSocketHyBi10(connection, request); |
| + else |
| + AcceptWebSocketHixie76(connection, request); |
| +} |
| + |
| +static std::string GenerateWebSocketHyBi10AcceptKey( |
| + const std::string& sec_web_socket_key) |
| +{ |
| + static const char* const web_socket_key_GUID = |
| + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| + std::string data(sec_web_socket_key); |
|
pfeldman
2011/07/28 09:32:47
Use StringPrintf ?
|
| + data += web_socket_key_GUID; |
| + std::string encoded_hash; |
| + base::Base64Encode(base::SHA1HashString(data), &encoded_hash); |
| + return encoded_hash; |
| +} |
| + |
| +void HttpServer::AcceptWebSocketHyBi10( |
| + HttpServer::Connection* connection, |
| + const HttpServerRequestInfo& request) { |
| + std::string key = GetHeaderValue(request, "Sec-WebSocket-Key"); |
| + connection->is_web_socket_ = true; |
| + |
| + std::string response = base::StringPrintf( |
| + "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" |
| + "Upgrade: WebSocket\r\n" |
| + "Connection: Upgrade\r\n" |
| + "Sec-WebSocket-Accept: %s\r\n" |
| + "\r\n", |
| + GenerateWebSocketHyBi10AcceptKey(key).c_str()); |
| + connection->socket_->Send(response); |
| +} |
| + |
| +struct WebSocketFrame { |
|
pfeldman
2011/07/28 09:32:47
Declare this struct in anonymous namespace above.
|
| + // Hybi-10 opcodes. |
| + typedef unsigned int OpCode; |
| + static const OpCode kOpCodeContinuation; |
| + static const OpCode kOpCodeText; |
| + static const OpCode kOpCodeBinary; |
| + static const OpCode kOpCodeClose; |
| + static const OpCode kOpCodePing; |
| + static const OpCode kOpCodePong; |
| + |
| + static const unsigned char kFinalBit; |
| + static const unsigned char kReserved1Bit; |
| + static const unsigned char kReserved2Bit; |
| + static const unsigned char kReserved3Bit; |
| + static const unsigned char kOpCodeMask; |
| + static const unsigned char kMaskBit; |
| + static const unsigned char kPayloadLengthMask; |
| + |
| + static const size_t kMaxSingleBytePayloadLength; |
| + static const size_t kTwoBytePayloadLengthField; |
| + static const size_t kEightBytePayloadLengthField; |
| + static const size_t kMaskingKeyWidthInBytes; |
| + |
| + enum ParseResult { |
| + FrameOK, |
| + FrameIncomplete, |
| + FrameError |
| + }; |
| + |
| + OpCode op_code_; |
| + bool final_; |
| + bool reserved1_; |
| + bool reserved2_; |
| + bool reserved3_; |
| + bool masked_; |
| + const char* payload_; |
| + size_t payload_length_; |
| + const char* frame_end_; |
| + |
| + bool isNonControlOpCode() const { |
| + return op_code_ == kOpCodeContinuation || |
| + op_code_ == kOpCodeText || |
| + op_code_ == kOpCodeBinary; |
| + } |
| + bool isControlOpCode() const { |
| + return op_code_ == kOpCodeClose || |
| + op_code_ == kOpCodePing || |
| + op_code_ == kOpCodePong; |
| + } |
| + bool isReservedOpCode() const { |
| + return !isNonControlOpCode() && !isControlOpCode(); |
| + } |
| + |
| + ParseResult DecodeFrame(const char* data, size_t data_length); |
| + std::vector<char> EncodeFrame(OpCode op_code, |
| + const char* data, |
| + size_t data_length); |
| +}; |
| + |
| +// Constants for hybi-10 frame format. |
| +const WebSocketFrame::OpCode WebSocketFrame::kOpCodeContinuation = 0x0; |
| +const WebSocketFrame::OpCode WebSocketFrame::kOpCodeText = 0x1; |
| +const WebSocketFrame::OpCode WebSocketFrame::kOpCodeBinary = 0x2; |
| +const WebSocketFrame::OpCode WebSocketFrame::kOpCodeClose = 0x8; |
| +const WebSocketFrame::OpCode WebSocketFrame::kOpCodePing = 0x9; |
| +const WebSocketFrame::OpCode WebSocketFrame::kOpCodePong = 0xA; |
| + |
| +const unsigned char WebSocketFrame::kFinalBit = 0x80; |
| +const unsigned char WebSocketFrame::kReserved1Bit = 0x40; |
| +const unsigned char WebSocketFrame::kReserved2Bit = 0x20; |
| +const unsigned char WebSocketFrame::kReserved3Bit = 0x10; |
| +const unsigned char WebSocketFrame::kOpCodeMask = 0xF; |
| +const unsigned char WebSocketFrame::kMaskBit = 0x80; |
| +const unsigned char WebSocketFrame::kPayloadLengthMask = 0x7F; |
| + |
| +const size_t WebSocketFrame::kMaxSingleBytePayloadLength = 125; |
| +const size_t WebSocketFrame::kTwoBytePayloadLengthField = 126; |
| +const size_t WebSocketFrame::kEightBytePayloadLengthField = 127; |
| +const size_t WebSocketFrame::kMaskingKeyWidthInBytes = 4; |
| + |
| +void HttpServer::SendOverWebSocket(int connection_id, |
| + const std::string& data) { |
| + Connection* connection = FindConnection(connection_id); |
| + if (connection == NULL) |
| + return; |
| + DCHECK(connection->is_web_socket_); |
| + if (connection->use_hybi10_web_socket_) |
| + SendOverWebSocketHyBi10(connection, data); |
| + else |
| + SendOverWebSocketHixie76(connection, data); |
| +} |
| + |
| +WebSocketFrame::ParseResult WebSocketFrame::DecodeFrame( |
| + const char* data, size_t data_length) { |
| + const char* p = data; |
| + const char* buffer_end = data + data_length; |
| + |
| + if (data_length < 2) |
| + return FrameIncomplete; |
| + |
| + unsigned char first_byte = *p++; |
| + unsigned char second_byte = *p++; |
| + |
| + final_ = first_byte & kFinalBit; |
| + reserved1_ = first_byte & kReserved1Bit; |
| + reserved2_ = first_byte & kReserved2Bit; |
| + reserved3_ = first_byte & kReserved3Bit; |
| + op_code_ = first_byte & kOpCodeMask; |
| + masked_ = second_byte & kMaskBit; |
| + |
| + uint64_t payload_length64 = second_byte & kPayloadLengthMask; |
| + if (payload_length64 > kMaxSingleBytePayloadLength) { |
| + int extended_payload_length_size; |
| + if (payload_length64 == kTwoBytePayloadLengthField) |
| + extended_payload_length_size = 2; |
| + else { |
| + CHECK(payload_length64 == kEightBytePayloadLengthField); |
| + extended_payload_length_size = 8; |
| + } |
| + if (buffer_end - p < extended_payload_length_size) |
| + return FrameIncomplete; |
| + payload_length64 = 0; |
| + for (int i = 0; i < extended_payload_length_size; ++i) { |
| + payload_length64 <<= 8; |
| + payload_length64 |= static_cast<unsigned char>(*p++); |
| + } |
| + } |
| + |
| + static const uint64_t max_payload_ength = 0x7FFFFFFFFFFFFFFFull; |
| + size_t masking_key_length = masked_ ? kMaskingKeyWidthInBytes : 0; |
| + static size_t max_length = std::numeric_limits<size_t>::max(); |
| + if (payload_length64 > max_payload_ength || |
| + payload_length64 + masking_key_length > max_length) { |
| + // WebSocket frame length too large. |
| + return FrameError; |
| + } |
| + payload_length_ = static_cast<size_t>(payload_length64); |
| + |
| + size_t total_length = masking_key_length + payload_length_; |
| + if (static_cast<size_t>(buffer_end - p) < total_length) |
| + return FrameIncomplete; |
| + |
| + if (masked_) { |
| + const char* masking_key = p; |
| + char* payload = const_cast<char*>(p + kMaskingKeyWidthInBytes); |
| + for (size_t i = 0; i < payload_length_; ++i) // Unmask the payload. |
| + payload[i] ^= masking_key[i % kMaskingKeyWidthInBytes]; |
| + } |
| + |
| + payload_ = p + masking_key_length; |
| + frame_end_ = p + masking_key_length + payload_length_; |
| + return FrameOK; |
| +} |
| + |
| +std::vector<char> WebSocketFrame::EncodeFrame( |
| + OpCode op_code, const char* data, size_t data_length) { |
| + std::vector<char> frame; |
| + |
| + // Checks whether "op_code" fits in the range of opCodes. |
| + CHECK(!(op_code & ~kOpCodeMask)); |
|
pfeldman
2011/07/28 09:32:47
DCHECK
|
| + frame.push_back(kFinalBit | op_code); |
| + if (data_length <= kMaxSingleBytePayloadLength) |
| + frame.push_back(kMaskBit | data_length); |
| + else if (data_length <= 0xFFFF) { |
| + frame.push_back(kMaskBit | kTwoBytePayloadLengthField); |
| + frame.push_back((data_length & 0xFF00) >> 8); |
| + frame.push_back(data_length & 0xFF); |
| + } else { |
| + frame.push_back(kMaskBit | kEightBytePayloadLengthField); |
| + char extended_payload_length[8]; |
| + size_t remaining = data_length; |
| + // Fill the length into extended_payload_length in the network byte order. |
| + for (int i = 0; i < 8; ++i) { |
| + extended_payload_length[7 - i] = remaining & 0xFF; |
| + remaining >>= 8; |
| + } |
| + frame.insert(frame.end(), |
| + extended_payload_length, |
| + extended_payload_length + 8); |
| + CHECK(!remaining); |
| + } |
| + |
| + // Mask the frame. |
| + size_t masking_key_start = frame.size(); |
| + // Add placeholder for masking key. Will be overwritten. |
| + frame.resize(frame.size() + kMaskingKeyWidthInBytes); |
| + size_t payload_start = frame.size(); |
| + frame.insert(frame.end(), data, data + data_length); |
| + |
| + base::RandBytes(frame.data() + masking_key_start, |
| + kMaskingKeyWidthInBytes); |
| + for (size_t i = 0; i < data_length; ++i) |
|
pfeldman
2011/07/28 09:32:47
Use {} since more than one line.
|
| + frame[payload_start + i] ^= |
| + frame[masking_key_start + i % kMaskingKeyWidthInBytes]; |
| + |
| + return frame; |
| +} |
| + |
| +void HttpServer::SendOverWebSocketHyBi10(HttpServer::Connection* connection, |
| + const std::string& data) { |
| + WebSocketFrame frame; |
| + std::vector<char> buffer = frame.EncodeFrame(WebSocketFrame::kOpCodeText, |
| + data.c_str(), data.length()); |
| + connection->socket_->Send(&buffer[0], buffer.size()); |
| +} |
| + |
| +bool HttpServer::DidReadWebSocketHyBi10(HttpServer::Connection* connection, |
| + char* data, |
| + size_t length, |
| + std::string* message) { |
| + WebSocketFrame frame; |
| + WebSocketFrame::ParseResult parse_result = frame.DecodeFrame(data, length); |
| + if (parse_result == WebSocketFrame::FrameError) |
| + return false; |
| + |
| + if (parse_result == WebSocketFrame::FrameIncomplete) |
| + return false; |
| + |
| + CHECK(parse_result == WebSocketFrame::FrameOK); |
| + *message = std::string(frame.payload_, frame.payload_length_); |
| + connection->Shift(frame.frame_end_ - data); |
| + |
| + return true; |
| +} |
| + |
| } // namespace net |