summaryrefslogtreecommitdiffstats
path: root/net/tools
diff options
context:
space:
mode:
authorpliard@chromium.org <pliard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-16 09:22:30 +0000
committerpliard@chromium.org <pliard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-16 09:22:30 +0000
commit36633f403b4ba6c5f592bc65d387869aa5403fc1 (patch)
tree6a2f7fef89c837ed5d390aa878439761e948085b /net/tools
parentead613da9d4731a38b583b2fc141ccc10bfb9e77 (diff)
downloadchromium_src-36633f403b4ba6c5f592bc65d387869aa5403fc1.zip
chromium_src-36633f403b4ba6c5f592bc65d387869aa5403fc1.tar.gz
chromium_src-36633f403b4ba6c5f592bc65d387869aa5403fc1.tar.bz2
Refactor TCPListenSocket.
This is part of Chrome for Android upstreaming. This CL adds a common base class, StreamListenSocket, providing a default implementation inherited by TCPListenSocket and the upcoming UnixDomainSocket. That lets us share the common code used by both TCPListenSocket and UnixDomainSocket. This also removes the recently introduced ListenSocket class which is unnecessary now we have StreamListenSocket. TEST=net_unittests Review URL: https://chromiumcodereview.appspot.com/10161005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@137387 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/tools')
-rw-r--r--net/tools/fetch/http_listen_socket.cc59
-rw-r--r--net/tools/fetch/http_listen_socket.h30
-rw-r--r--net/tools/fetch/http_server.cc3
-rw-r--r--net/tools/fetch/http_session.cc4
4 files changed, 47 insertions, 49 deletions
diff --git a/net/tools/fetch/http_listen_socket.cc b/net/tools/fetch/http_listen_socket.cc
index 60c6f94..dbb34e6 100644
--- a/net/tools/fetch/http_listen_socket.cc
+++ b/net/tools/fetch/http_listen_socket.cc
@@ -13,43 +13,38 @@
#include "net/tools/fetch/http_server_request_info.h"
#include "net/tools/fetch/http_server_response_info.h"
-// must run in the IO thread
HttpListenSocket::HttpListenSocket(SOCKET s,
HttpListenSocket::Delegate* delegate)
: ALLOW_THIS_IN_INITIALIZER_LIST(net::TCPListenSocket(s, this)),
delegate_(delegate) {
}
-// must run in the IO thread
HttpListenSocket::~HttpListenSocket() {
}
-void HttpListenSocket::Listen() {
- net::TCPListenSocket::Listen();
-}
-
void HttpListenSocket::Accept() {
- SOCKET conn = net::TCPListenSocket::Accept(socket_);
+ SOCKET conn = net::TCPListenSocket::AcceptSocket();
DCHECK_NE(conn, net::TCPListenSocket::kInvalidSocket);
if (conn == net::TCPListenSocket::kInvalidSocket) {
// TODO
} else {
scoped_refptr<HttpListenSocket> sock(
new HttpListenSocket(conn, delegate_));
- // it's up to the delegate to AddRef if it wants to keep it around
+ // It's up to the delegate to AddRef if it wants to keep it around.
DidAccept(this, sock);
}
}
-HttpListenSocket* HttpListenSocket::Listen(
+// static
+scoped_refptr<HttpListenSocket> HttpListenSocket::CreateAndListen(
const std::string& ip,
int port,
HttpListenSocket::Delegate* delegate) {
SOCKET s = net::TCPListenSocket::CreateAndBind(ip, port);
if (s == net::TCPListenSocket::kInvalidSocket) {
- // TODO (ibrar): error handling
+ // TODO (ibrar): error handling.
} else {
- HttpListenSocket *serv = new HttpListenSocket(s, delegate);
+ scoped_refptr<HttpListenSocket> serv = new HttpListenSocket(s, delegate);
serv->Listen();
return serv;
}
@@ -78,19 +73,19 @@ enum header_parse_inputs {
// Parser states.
enum header_parse_states {
- ST_METHOD, // Receiving the method
- ST_URL, // Receiving the URL
- ST_PROTO, // Receiving the protocol
- ST_HEADER, // Starting a Request Header
- ST_NAME, // Receiving a request header name
- ST_SEPARATOR, // Receiving the separator between header name and value
- ST_VALUE, // Receiving a request header value
- ST_DONE, // Parsing is complete and successful
+ ST_METHOD, // Receiving the method.
+ ST_URL, // Receiving the URL.
+ ST_PROTO, // Receiving the protocol.
+ ST_HEADER, // Starting a Request Header.
+ ST_NAME, // Receiving a request header name.
+ ST_SEPARATOR, // Receiving the separator between header name and value.
+ ST_VALUE, // Receiving a request header value.
+ ST_DONE, // Parsing is complete and successful.
ST_ERR, // Parsing encountered invalid syntax.
MAX_STATES
};
-// State transition table
+// State transition table.
int parser_state[MAX_STATES][MAX_INPUTS] = {
/* METHOD */ { ST_URL, ST_ERR, ST_ERR, ST_ERR, ST_METHOD },
/* URL */ { ST_PROTO, ST_ERR, ST_ERR, ST_URL, ST_URL },
@@ -154,7 +149,7 @@ HttpServerRequestInfo* HttpListenSocket::ParseHeaders() {
break;
case ST_VALUE:
header_value = buffer;
- // TODO(mbelshe): Deal better with duplicate headers
+ // TODO(mbelshe): Deal better with duplicate headers.
DCHECK(info->headers.find(header_name) == info->headers.end());
info->headers[header_name] = header_value;
buffer.clear();
@@ -162,7 +157,7 @@ HttpServerRequestInfo* HttpListenSocket::ParseHeaders() {
}
state = next_state;
} else {
- // Do any actions based on current state
+ // Do any actions based on current state.
switch (state) {
case ST_METHOD:
case ST_URL:
@@ -185,12 +180,12 @@ HttpServerRequestInfo* HttpListenSocket::ParseHeaders() {
return NULL;
}
-void HttpListenSocket::DidAccept(net::ListenSocket* server,
- net::ListenSocket* connection) {
+void HttpListenSocket::DidAccept(net::StreamListenSocket* server,
+ net::StreamListenSocket* connection) {
connection->AddRef();
}
-void HttpListenSocket::DidRead(net::ListenSocket* connection,
+void HttpListenSocket::DidRead(net::StreamListenSocket* connection,
const char* data,
int len) {
recv_data_.append(data, len);
@@ -203,12 +198,12 @@ void HttpListenSocket::DidRead(net::ListenSocket* connection,
}
}
-void HttpListenSocket::DidClose(net::ListenSocket* sock) {
+void HttpListenSocket::DidClose(net::StreamListenSocket* sock) {
sock->Release();
}
// Convert the numeric status code to a string.
-// e.g. 200 -> "200 OK"
+// e.g. 200 -> "200 OK".
std::string ServerStatus(int code) {
switch(code) {
case 200:
@@ -223,12 +218,12 @@ void HttpListenSocket::Respond(HttpServerResponseInfo* info,
std::string& data) {
std::string response;
- // status line
+ // Status line.
response = info->protocol + " ";
response += ServerStatus(info->status);
response += "\r\n";
- // standard headers
+ // Standard headers.
if (info->content_type.length())
response += "Content-type: " + info->content_type + "\r\n";
@@ -239,12 +234,12 @@ void HttpListenSocket::Respond(HttpServerResponseInfo* info,
if (info->connection_close)
response += "Connection: close\r\n";
- // TODO(mbelshe): support additional headers
+ // TODO(mbelshe): support additional headers.
- // End of headers
+ // End of headers.
response += "\r\n";
- // Add data
+ // Add data.
response += data;
// Write it all out.
diff --git a/net/tools/fetch/http_listen_socket.h b/net/tools/fetch/http_listen_socket.h
index 8a09a9e..4c3b27b 100644
--- a/net/tools/fetch/http_listen_socket.h
+++ b/net/tools/fetch/http_listen_socket.h
@@ -7,6 +7,7 @@
#pragma once
#include "base/message_loop.h"
+#include "net/base/stream_listen_socket.h"
#include "net/base/tcp_listen_socket.h"
class HttpServerRequestInfo;
@@ -14,7 +15,7 @@ class HttpServerResponseInfo;
// Implements a simple HTTP listen socket on top of the raw socket interface.
class HttpListenSocket : public net::TCPListenSocket,
- public net::ListenSocket::ListenSocketDelegate {
+ public net::StreamListenSocket::Delegate {
public:
class Delegate {
public:
@@ -25,27 +26,30 @@ class HttpListenSocket : public net::TCPListenSocket,
virtual ~Delegate() {}
};
- static HttpListenSocket* Listen(const std::string& ip, int port,
- HttpListenSocket::Delegate* delegate);
-
- virtual void Listen() OVERRIDE;
- virtual void Accept() OVERRIDE;
+ static scoped_refptr<HttpListenSocket> CreateAndListen(
+ const std::string& ip, int port, HttpListenSocket::Delegate* delegate);
// Send a server response.
// TODO(mbelshe): make this capable of non-ascii data.
void Respond(HttpServerResponseInfo* info, std::string& data);
- // ListenSocketDelegate
- virtual void DidAccept(net::ListenSocket* server,
- net::ListenSocket* connection) OVERRIDE;
- virtual void DidRead(net::ListenSocket* connection,
+ // StreamListenSocket::Delegate.
+ virtual void DidAccept(net::StreamListenSocket* server,
+ net::StreamListenSocket* connection) OVERRIDE;
+ virtual void DidRead(net::StreamListenSocket* connection,
const char* data, int len) OVERRIDE;
- virtual void DidClose(net::ListenSocket* sock) OVERRIDE;
+ virtual void DidClose(net::StreamListenSocket* sock) OVERRIDE;
+
+ protected:
+ // Overrides TCPListenSocket::Accept().
+ virtual void Accept() OVERRIDE;
private:
- friend class base::RefCountedThreadSafe<net::ListenSocket>;
+ friend class base::RefCountedThreadSafe<net::StreamListenSocket>;
static const int kReadBufSize = 16 * 1024;
+
+ // Must run in the IO thread.
HttpListenSocket(SOCKET s, HttpListenSocket::Delegate* del);
virtual ~HttpListenSocket();
@@ -54,7 +58,7 @@ class HttpListenSocket : public net::TCPListenSocket,
// recv data.
HttpServerRequestInfo* ParseHeaders();
- HttpListenSocket::Delegate* delegate_;
+ HttpListenSocket::Delegate* const delegate_;
std::string recv_data_;
DISALLOW_COPY_AND_ASSIGN(HttpListenSocket);
diff --git a/net/tools/fetch/http_server.cc b/net/tools/fetch/http_server.cc
index 3fa416c..352f236 100644
--- a/net/tools/fetch/http_server.cc
+++ b/net/tools/fetch/http_server.cc
@@ -1,9 +1,8 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/tools/fetch/http_server.h"
-#include "net/tools/fetch/http_listen_socket.h"
HttpServer::HttpServer(std::string ip, int port)
: ALLOW_THIS_IN_INITIALIZER_LIST(session_(new HttpSession(ip, port))) {
diff --git a/net/tools/fetch/http_session.cc b/net/tools/fetch/http_session.cc
index c3b1d2c..51e4db0 100644
--- a/net/tools/fetch/http_session.cc
+++ b/net/tools/fetch/http_session.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,7 +7,7 @@
HttpSession::HttpSession(const std::string& ip, int port)
: ALLOW_THIS_IN_INITIALIZER_LIST(
- socket_(HttpListenSocket::Listen(ip, port, this))) {
+ socket_(HttpListenSocket::CreateAndListen(ip, port, this))) {
}
HttpSession::~HttpSession() {