| Index: net/server/http_server.cc
|
| diff --git a/net/server/http_server.cc b/net/server/http_server.cc
|
| index ad1fa6ee938a9aacc2e8e1e4df25663e15d5b8ae..c9d8ac755f34d08ab0646fe790f3b8add2c471bf 100644
|
| --- a/net/server/http_server.cc
|
| +++ b/net/server/http_server.cc
|
| @@ -24,12 +24,24 @@ HttpServer::HttpServer(const std::string& host,
|
| server_ = TCPListenSocket::CreateAndListen(host, port, this);
|
| }
|
|
|
| -HttpServer::~HttpServer() {
|
| - IdToConnectionMap copy = id_to_connection_;
|
| - for (IdToConnectionMap::iterator it = copy.begin(); it != copy.end(); ++it)
|
| - delete it->second;
|
| +void HttpServer::AcceptWebSocket(
|
| + int connection_id,
|
| + const HttpServerRequestInfo& request) {
|
| + HttpConnection* connection = FindConnection(connection_id);
|
| + if (connection == NULL)
|
| + return;
|
|
|
| - server_ = NULL;
|
| + DCHECK(connection->web_socket_.get());
|
| + connection->web_socket_->Accept(request);
|
| +}
|
| +
|
| +void HttpServer::SendOverWebSocket(int connection_id,
|
| + const std::string& data) {
|
| + HttpConnection* connection = FindConnection(connection_id);
|
| + if (connection == NULL)
|
| + return;
|
| + DCHECK(connection->web_socket_.get());
|
| + connection->web_socket_->Send(data);
|
| }
|
|
|
| void HttpServer::Send(int connection_id, const std::string& data) {
|
| @@ -81,6 +93,77 @@ void HttpServer::Close(int connection_id)
|
| DidClose(connection->socket_);
|
| }
|
|
|
| +void HttpServer::DidAccept(ListenSocket* server,
|
| + ListenSocket* socket) {
|
| + HttpConnection* connection = new HttpConnection(this, socket);
|
| + id_to_connection_[connection->id()] = connection;
|
| + socket_to_connection_[socket] = connection;
|
| +}
|
| +
|
| +void HttpServer::DidRead(ListenSocket* socket,
|
| + const char* data,
|
| + int len) {
|
| + HttpConnection* connection = FindConnection(socket);
|
| + DCHECK(connection != NULL);
|
| + if (connection == NULL)
|
| + return;
|
| +
|
| + connection->recv_data_.append(data, len);
|
| + while (connection->recv_data_.length()) {
|
| + if (connection->web_socket_.get()) {
|
| + std::string message;
|
| + WebSocket::ParseResult result = connection->web_socket_->Read(&message);
|
| + if (result == WebSocket::FRAME_INCOMPLETE)
|
| + break;
|
| +
|
| + if (result == WebSocket::FRAME_CLOSE ||
|
| + result == WebSocket::FRAME_ERROR) {
|
| + Close(connection->id());
|
| + break;
|
| + }
|
| + delegate_->OnWebSocketMessage(connection->id(), message);
|
| + continue;
|
| + }
|
| +
|
| + HttpServerRequestInfo request;
|
| + size_t pos = 0;
|
| + if (!ParseHeaders(connection, &request, &pos))
|
| + break;
|
| +
|
| + std::string connection_header = request.GetHeaderValue("Connection");
|
| + if (connection_header == "Upgrade") {
|
| + connection->web_socket_.reset(WebSocket::CreateWebSocket(connection,
|
| + request,
|
| + &pos));
|
| +
|
| + if (!connection->web_socket_.get()) // Not enought data was received.
|
| + break;
|
| + delegate_->OnWebSocketRequest(connection->id(), request);
|
| + connection->Shift(pos);
|
| + continue;
|
| + }
|
| + // Request body is not supported. It is always empty.
|
| + delegate_->OnHttpRequest(connection->id(), request);
|
| + connection->Shift(pos);
|
| + }
|
| +}
|
| +
|
| +void HttpServer::DidClose(ListenSocket* socket) {
|
| + HttpConnection* connection = FindConnection(socket);
|
| + DCHECK(connection != NULL);
|
| + id_to_connection_.erase(connection->id());
|
| + socket_to_connection_.erase(connection->socket_);
|
| + delete connection;
|
| +}
|
| +
|
| +HttpServer::~HttpServer() {
|
| + IdToConnectionMap copy = id_to_connection_;
|
| + for (IdToConnectionMap::iterator it = copy.begin(); it != copy.end(); ++it)
|
| + delete it->second;
|
| +
|
| + server_ = NULL;
|
| +}
|
| +
|
| //
|
| // HTTP Request Parser
|
| // This HTTP request parser uses a simple state machine to quickly parse
|
| @@ -212,69 +295,6 @@ bool HttpServer::ParseHeaders(HttpConnection* connection,
|
| return false;
|
| }
|
|
|
| -void HttpServer::DidAccept(ListenSocket* server,
|
| - ListenSocket* socket) {
|
| - HttpConnection* connection = new HttpConnection(this, socket);
|
| - id_to_connection_[connection->id()] = connection;
|
| - socket_to_connection_[socket] = connection;
|
| -}
|
| -
|
| -void HttpServer::DidRead(ListenSocket* socket,
|
| - const char* data,
|
| - int len) {
|
| - HttpConnection* connection = FindConnection(socket);
|
| - DCHECK(connection != NULL);
|
| - if (connection == NULL)
|
| - return;
|
| -
|
| - connection->recv_data_.append(data, len);
|
| - while (connection->recv_data_.length()) {
|
| - if (connection->web_socket_.get()) {
|
| - std::string message;
|
| - WebSocket::ParseResult result = connection->web_socket_->Read(&message);
|
| - if (result == WebSocket::FRAME_INCOMPLETE)
|
| - break;
|
| -
|
| - if (result == WebSocket::FRAME_CLOSE ||
|
| - result == WebSocket::FRAME_ERROR) {
|
| - Close(connection->id());
|
| - break;
|
| - }
|
| - delegate_->OnWebSocketMessage(connection->id(), message);
|
| - continue;
|
| - }
|
| -
|
| - HttpServerRequestInfo request;
|
| - size_t pos = 0;
|
| - if (!ParseHeaders(connection, &request, &pos))
|
| - break;
|
| -
|
| - std::string connection_header = request.GetHeaderValue("Connection");
|
| - if (connection_header == "Upgrade") {
|
| - connection->web_socket_.reset(WebSocket::CreateWebSocket(connection,
|
| - request,
|
| - &pos));
|
| -
|
| - if (!connection->web_socket_.get()) // Not enought data was received.
|
| - break;
|
| - delegate_->OnWebSocketRequest(connection->id(), request);
|
| - connection->Shift(pos);
|
| - continue;
|
| - }
|
| - // Request body is not supported. It is always empty.
|
| - delegate_->OnHttpRequest(connection->id(), request);
|
| - connection->Shift(pos);
|
| - }
|
| -}
|
| -
|
| -void HttpServer::DidClose(ListenSocket* socket) {
|
| - HttpConnection* connection = FindConnection(socket);
|
| - DCHECK(connection != NULL);
|
| - id_to_connection_.erase(connection->id());
|
| - socket_to_connection_.erase(connection->socket_);
|
| - delete connection;
|
| -}
|
| -
|
| HttpConnection* HttpServer::FindConnection(int connection_id) {
|
| IdToConnectionMap::iterator it = id_to_connection_.find(connection_id);
|
| if (it == id_to_connection_.end())
|
| @@ -289,24 +309,4 @@ HttpConnection* HttpServer::FindConnection(ListenSocket* socket) {
|
| return it->second;
|
| }
|
|
|
| -void HttpServer::AcceptWebSocket(
|
| - int connection_id,
|
| - const HttpServerRequestInfo& request) {
|
| - HttpConnection* connection = FindConnection(connection_id);
|
| - if (connection == NULL)
|
| - return;
|
| -
|
| - DCHECK(connection->web_socket_.get());
|
| - connection->web_socket_->Accept(request);
|
| -}
|
| -
|
| -void HttpServer::SendOverWebSocket(int connection_id,
|
| - const std::string& data) {
|
| - HttpConnection* connection = FindConnection(connection_id);
|
| - if (connection == NULL)
|
| - return;
|
| - DCHECK(connection->web_socket_.get());
|
| - connection->web_socket_->Send(data);
|
| -}
|
| -
|
| } // namespace net
|
|
|