diff options
author | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-11 09:44:41 +0000 |
---|---|---|
committer | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-11 09:44:41 +0000 |
commit | e67d2179178b955a07df2c908d8c49a26fb0e71d (patch) | |
tree | c7f1113691da747da61a7e76b5574d25ba2afebf /net/server | |
parent | a942b8c1ca4b002376306cf399e000d0842c630b (diff) | |
download | chromium_src-e67d2179178b955a07df2c908d8c49a26fb0e71d.zip chromium_src-e67d2179178b955a07df2c908d8c49a26fb0e71d.tar.gz chromium_src-e67d2179178b955a07df2c908d8c49a26fb0e71d.tar.bz2 |
DevTools: add /json/activate/ command to the discovery protocol.
This change adds suppot for /activate command and refactors the json commands handling so that all commands supported jsonp properly. For the jsonp support, we always respond with 200 OK and specify the actual code in the jsonp call parameter.
For the above purposes, migrates net/server/http_server from Send404() scheme to Send(net::HTTP_NOT_FOUND).
BUG=157495
Review URL: https://chromiumcodereview.appspot.com/11499004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@172285 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/server')
-rw-r--r-- | net/server/http_connection.cc | 48 | ||||
-rw-r--r-- | net/server/http_connection.h | 7 | ||||
-rw-r--r-- | net/server/http_server.cc | 30 | ||||
-rw-r--r-- | net/server/http_server.h | 8 | ||||
-rw-r--r-- | net/server/web_socket.cc | 18 |
5 files changed, 53 insertions, 58 deletions
diff --git a/net/server/http_connection.cc b/net/server/http_connection.cc index d601f34..e82c9fe 100644 --- a/net/server/http_connection.cc +++ b/net/server/http_connection.cc @@ -26,42 +26,40 @@ void HttpConnection::Send(const char* bytes, int len) { socket_->Send(bytes, len); } -void HttpConnection::Send200(const std::string& data, - const std::string& content_type) { +void HttpConnection::Send(HttpStatusCode status_code, + const std::string& data, + const std::string& content_type) { if (!socket_) return; + + std::string status_message; + switch (status_code) { + case HTTP_OK: + status_message = "OK"; + break; + case HTTP_NOT_FOUND: + status_message = "Not Found"; + break; + case HTTP_INTERNAL_SERVER_ERROR: + status_message = "Internal Error"; + break; + default: + status_message = ""; + break; + } + socket_->Send(base::StringPrintf( - "HTTP/1.1 200 OK\r\n" + "HTTP/1.1 %d %s\r\n" "Content-Type:%s\r\n" "Content-Length:%d\r\n" "\r\n", + status_code, + status_message.c_str(), content_type.c_str(), static_cast<int>(data.length()))); socket_->Send(data); } -void HttpConnection::Send404() { - if (!socket_) - return; - socket_->Send( - "HTTP/1.1 404 Not Found\r\n" - "Content-Length: 0\r\n" - "\r\n"); -} - -void HttpConnection::Send500(const std::string& message) { - if (!socket_) - return; - socket_->Send(base::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())); -} - HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock) : server_(server), socket_(sock) { diff --git a/net/server/http_connection.h b/net/server/http_connection.h index c3c573a..488d528 100644 --- a/net/server/http_connection.h +++ b/net/server/http_connection.h @@ -10,6 +10,7 @@ #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include "net/http/http_status_code.h" namespace net { @@ -23,9 +24,9 @@ class HttpConnection { void Send(const std::string& data); void Send(const char* bytes, int len); - void Send200(const std::string& data, const std::string& content_type); - void Send404(); - void Send500(const std::string& message); + void Send(HttpStatusCode status_code, + const std::string& data, + const std::string& content_type); void Shift(int num_bytes); diff --git a/net/server/http_server.cc b/net/server/http_server.cc index 9582487..b92c211 100644 --- a/net/server/http_server.cc +++ b/net/server/http_server.cc @@ -45,42 +45,28 @@ void HttpServer::SendOverWebSocket(int connection_id, connection->web_socket_->Send(data); } -void HttpServer::Send(int connection_id, const std::string& data) { +void HttpServer::Send(int connection_id, + HttpStatusCode status_code, + const std::string& data, + const std::string& content_type) { HttpConnection* connection = FindConnection(connection_id); if (connection == NULL) return; - connection->Send(data); -} - -void HttpServer::Send(int connection_id, const char* bytes, int len) { - HttpConnection* connection = FindConnection(connection_id); - if (connection == NULL) - return; - - connection->Send(bytes, len); + connection->Send(status_code, data, content_type); } void HttpServer::Send200(int connection_id, const std::string& data, const std::string& content_type) { - HttpConnection* connection = FindConnection(connection_id); - if (connection == NULL) - return; - connection->Send200(data, content_type); + Send(connection_id, HTTP_OK, data, content_type); } void HttpServer::Send404(int connection_id) { - HttpConnection* connection = FindConnection(connection_id); - if (connection == NULL) - return; - connection->Send404(); + Send(connection_id, HTTP_NOT_FOUND, "", "text/html"); } void HttpServer::Send500(int connection_id, const std::string& message) { - HttpConnection* connection = FindConnection(connection_id); - if (connection == NULL) - return; - connection->Send500(message); + Send(connection_id, HTTP_INTERNAL_SERVER_ERROR, message, "text/html"); } void HttpServer::Close(int connection_id) { diff --git a/net/server/http_server.h b/net/server/http_server.h index bc6a290..54773ad 100644 --- a/net/server/http_server.h +++ b/net/server/http_server.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "net/base/stream_listen_socket.h" +#include "net/http/http_status_code.h" namespace net { @@ -45,13 +46,16 @@ class HttpServer : public StreamListenSocket::Delegate, void AcceptWebSocket(int connection_id, const HttpServerRequestInfo& request); void SendOverWebSocket(int connection_id, const std::string& data); - void Send(int connection_id, const std::string& data); - void Send(int connection_id, const char* bytes, int len); + void Send(int connection_id, + HttpStatusCode status_code, + const std::string& data, + const std::string& mime_type); void Send200(int connection_id, const std::string& data, const std::string& mime_type); void Send404(int connection_id); void Send500(int connection_id, const std::string& message); + void Close(int connection_id); // Copies the local address to |address|. Returns a network error code. diff --git a/net/server/web_socket.cc b/net/server/web_socket.cc index e2829da..398e4e0 100644 --- a/net/server/web_socket.cc +++ b/net/server/web_socket.cc @@ -115,14 +115,18 @@ class WebSocketHixie76 : public net::WebSocket { std::string key2 = request.GetHeaderValue("Sec-WebSocket-Key2"); if (key1.empty()) { - connection->Send500("Invalid request format. " - "Sec-WebSocket-Key1 is empty or isn't specified."); + connection->Send(net::HTTP_INTERNAL_SERVER_ERROR, + "Invalid request format. " + "Sec-WebSocket-Key1 is empty or isn't specified.", + "text/html"); return; } if (key2.empty()) { - connection->Send500("Invalid request format. " - "Sec-WebSocket-Key2 is empty or isn't specified."); + connection->Send(net::HTTP_INTERNAL_SERVER_ERROR, + "Invalid request format. " + "Sec-WebSocket-Key2 is empty or isn't specified.", + "text/html"); return; } @@ -175,8 +179,10 @@ class WebSocketHybi17 : public WebSocket { std::string key = request.GetHeaderValue("Sec-WebSocket-Key"); if (key.empty()) { - connection->Send500("Invalid request format. " - "Sec-WebSocket-Key is empty or isn't specified."); + connection->Send(net::HTTP_INTERNAL_SERVER_ERROR, + "Invalid request format. " + "Sec-WebSocket-Key is empty or isn't specified.", + "text/html"); return NULL; } return new WebSocketHybi17(connection, request, pos); |