diff options
author | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-26 12:28:22 +0000 |
---|---|---|
committer | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-26 12:28:22 +0000 |
commit | c982b895a97c1637f3eaf85ac515418b7e6e768f (patch) | |
tree | a34918b1c5006a99d10627f962472f565d2fa264 /net/server | |
parent | ba082027aa8592e01c940390ab0906b8551cbb44 (diff) | |
download | chromium_src-c982b895a97c1637f3eaf85ac515418b7e6e768f.zip chromium_src-c982b895a97c1637f3eaf85ac515418b7e6e768f.tar.gz chromium_src-c982b895a97c1637f3eaf85ac515418b7e6e768f.tar.bz2 |
DevTools: implement basic handshake for remote debugging.
Review URL: http://codereview.chromium.org/2870062
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53624 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/server')
-rw-r--r-- | net/server/http_listen_socket.cc | 68 | ||||
-rw-r--r-- | net/server/http_listen_socket.h | 16 | ||||
-rw-r--r-- | net/server/http_server_request_info.h | 2 |
3 files changed, 59 insertions, 27 deletions
diff --git a/net/server/http_listen_socket.cc b/net/server/http_listen_socket.cc index 16c664e..cdc6c71 100644 --- a/net/server/http_listen_socket.cc +++ b/net/server/http_listen_socket.cc @@ -58,11 +58,11 @@ HttpListenSocket* HttpListenSocket::Listen( } std::string GetHeaderValue( - HttpServerRequestInfo* request, + const HttpServerRequestInfo& request, const std::string& header_name) { HttpServerRequestInfo::HeadersMap::iterator it = - request->headers.find(header_name); - if (it != request->headers.end()) + request.headers.find(header_name); + if (it != request.headers.end()) return it->second; return ""; } @@ -86,7 +86,7 @@ uint32 WebSocketKeyFingerprint(const std::string& str) { return htonl(static_cast<uint32>(number / spaces)); } -void HttpListenSocket::AcceptWebSocket(HttpServerRequestInfo* request) { +void HttpListenSocket::AcceptWebSocket(const HttpServerRequestInfo& request) { std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1"); std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2"); @@ -96,14 +96,14 @@ void HttpListenSocket::AcceptWebSocket(HttpServerRequestInfo* request) { char data[16]; memcpy(data, &fp1, 4); memcpy(data + 4, &fp2, 4); - memcpy(data + 8, &request->data[0], 8); + memcpy(data + 8, &request.data[0], 8); MD5Digest digest; MD5Sum(data, 16, &digest); std::string origin = GetHeaderValue(request, "Origin"); std::string host = GetHeaderValue(request, "Host"); - std::string location = "ws://" + host + request->path; + std::string location = "ws://" + host + request.path; is_web_socket_ = true; Send(StringPrintf("HTTP/1.1 101 WebSocket Protocol Handshake\r\n" "Upgrade: WebSocket\r\n" @@ -125,6 +125,33 @@ void HttpListenSocket::SendOverWebSocket(const std::string& data) { Send(&message_end, 1); } +void HttpListenSocket::Send200(const std::string& data, + const std::string& content_type) { + Send(StringPrintf("HTTP/1.1 200 OK\r\n" + "Content-Type:%s\r\n" + "Content-Length:%d\r\n" + "\r\n", + content_type.c_str(), + static_cast<int>(data.length()))); + Send(data); +} + +void HttpListenSocket::Send404() { + Send("HTTP/1.1 404 Not Found\r\n" + "Content-Length: 0\r\n" + "\r\n"); +} + +void HttpListenSocket::Send500(const std::string& message) { + Send(StringPrintf("HTTP/1.1 500 Internal Error\r\n" + "Content-Type:text/html\r\n" + "Content-Length:%d\r\n" + "\r\n" + "%s", + static_cast<int>(message.length()), + message.c_str())); +} + // // HTTP Request Parser // This HTTP request parser uses a simple state machine to quickly parse @@ -199,11 +226,10 @@ int charToInput(char ch) { return INPUT_DEFAULT; } -HttpServerRequestInfo* HttpListenSocket::ParseHeaders() { +bool HttpListenSocket::ParseHeaders(HttpServerRequestInfo* info) { int pos = 0; int data_len = recv_data_.length(); int state = is_web_socket_ ? ST_WS_READY : ST_METHOD; - scoped_ptr<HttpServerRequestInfo> info(new HttpServerRequestInfo()); std::string buffer; std::string header_name; std::string header_value; @@ -247,7 +273,7 @@ HttpServerRequestInfo* HttpListenSocket::ParseHeaders() { recv_data_ = recv_data_.substr(pos); info->data = buffer; buffer.clear(); - return info.release(); + return true; break; } state = next_state; @@ -266,17 +292,17 @@ HttpServerRequestInfo* HttpListenSocket::ParseHeaders() { recv_data_ = recv_data_.substr(pos); info->data = recv_data_; recv_data_.clear(); - return info.release(); + return true; case ST_WS_CLOSE: is_web_socket_ = false; - return NULL; + return false; case ST_ERR: - return NULL; + return false; } } } // No more characters, but we haven't finished parsing yet. - return NULL; + return false; } void HttpListenSocket::DidAccept(ListenSocket* server, @@ -289,26 +315,26 @@ void HttpListenSocket::DidRead(ListenSocket*, int len) { recv_data_.append(data, len); while (recv_data_.length()) { - scoped_ptr<HttpServerRequestInfo> request(ParseHeaders()); - if (!request.get()) + HttpServerRequestInfo request; + if (!ParseHeaders(&request)) break; if (is_web_socket_) { - delegate_->OnWebSocketMessage(this, request->data); + delegate_->OnWebSocketMessage(this, request.data); continue; } - std::string connection = GetHeaderValue(request.get(), "Connection"); + std::string connection = GetHeaderValue(request, "Connection"); if (connection == "Upgrade") { // Is this WebSocket and if yes, upgrade the connection. - std::string key1 = GetHeaderValue(request.get(), "Sec-WebSocket-Key1"); - std::string key2 = GetHeaderValue(request.get(), "Sec-WebSocket-Key2"); + std::string key1 = GetHeaderValue(request, "Sec-WebSocket-Key1"); + std::string key2 = GetHeaderValue(request, "Sec-WebSocket-Key2"); if (!key1.empty() && !key2.empty()) { - delegate_->OnWebSocketRequest(this, request.get()); + delegate_->OnWebSocketRequest(this, request); continue; } } - delegate_->OnHttpRequest(this, request.get()); + delegate_->OnHttpRequest(this, request); } } diff --git a/net/server/http_listen_socket.h b/net/server/http_listen_socket.h index 5517af0..536abb2 100644 --- a/net/server/http_listen_socket.h +++ b/net/server/http_listen_socket.h @@ -16,10 +16,10 @@ class HttpListenSocket : public ListenSocket, class Delegate { public: virtual void OnHttpRequest(HttpListenSocket* socket, - HttpServerRequestInfo* info) = 0; + const HttpServerRequestInfo& info) = 0; virtual void OnWebSocketRequest(HttpListenSocket* socket, - HttpServerRequestInfo* info) = 0; + const HttpServerRequestInfo& info) = 0; virtual void OnWebSocketMessage(HttpListenSocket* socket, const std::string& data) = 0; @@ -29,16 +29,22 @@ class HttpListenSocket : public ListenSocket, virtual ~Delegate() {} }; + virtual void Accept(); + static HttpListenSocket* Listen(const std::string& ip, int port, HttpListenSocket::Delegate* delegate); - void AcceptWebSocket(HttpServerRequestInfo* request); + void AcceptWebSocket(const HttpServerRequestInfo& request); void SendOverWebSocket(const std::string& data); + void Send200(const std::string& data, const std::string& mime_type); + void Send404(); + void Send500(const std::string& message); + + void Close() { ListenSocket::Close(); } void Listen() { ListenSocket::Listen(); } - virtual void Accept(); // ListenSocketDelegate virtual void DidAccept(ListenSocket* server, ListenSocket* connection); @@ -53,7 +59,7 @@ class HttpListenSocket : public ListenSocket, // Expects the raw data to be stored in recv_data_. If parsing is successful, // will remove the data parsed from recv_data_, leaving only the unused // recv data. - HttpServerRequestInfo* ParseHeaders(); + bool ParseHeaders(HttpServerRequestInfo* info); HttpListenSocket::Delegate* delegate_; bool is_web_socket_; diff --git a/net/server/http_server_request_info.h b/net/server/http_server_request_info.h index 84f767f..39689c7 100644 --- a/net/server/http_server_request_info.h +++ b/net/server/http_server_request_info.h @@ -29,7 +29,7 @@ class HttpServerRequestInfo { // A map of the names -> values for HTTP headers. typedef std::map<std::string, std::string> HeadersMap; - HeadersMap headers; + mutable HeadersMap headers; }; #endif // NET_SERVER_HTTP_SERVER_REQUEST_INFO_H_ |