Simplify the interface into httpserver and UnixHttpServer -- the root and

interface parameters are always used, so there's no need for them to be named
parameters with defaults.

Remove unused httpserver.getRoot and getPort, and simplify the main request
loop.  This means that socket errors do not have to be squelched.

Coalesce the two identical bind methods, one in UnixHttpServer, one in
UnixListener.  Fix this bind method to set the permissions on the socket
explicitly.  This closes a security hole, and fixes the intermittent failure
of xm-test/06_list_nonroot.test.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
This commit is contained in:
emellor@leeni.uk.xensource.com committed 2006-03-10 11:44:03 +01:00
1 parent 95c4e70768
commit c785b4684e
3 files changed
+42 -56

No files matched your search

+15 -37
View File
@@ -13,7 +13,9 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#============================================================================
# Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
# Copyright (C) 2006 XenSource Ltd.
#============================================================================
import threading
import string
@@ -28,6 +30,7 @@ from xen.xend.Args import ArgError
from xen.xend.XendError import XendError
import http
import unix
from resource import Resource, ErrorPage
from SrvDir import SrvDir
@@ -267,31 +270,28 @@ class HttpServer:
closed = False
def __init__(self, interface='', port=8080, root=None):
if root is None:
root = SrvDir()
def __init__(self, root, interface, port=8080):
self.root = root
self.interface = interface
self.port = port
self.root = root
# ready indicates when we are ready to begin accept connections
# it should be set after a successful bind
self.ready = False
def getRoot(self):
return self.root
def getPort(self):
return self.port
def run(self):
self.bind()
self.listen()
self.ready = True
self.requestLoop()
while not self.closed:
(sock, addr) = self.accept()
self.processRequest(sock, addr)
def stop(self):
self.close()
def bind(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -303,10 +303,6 @@ class HttpServer:
def accept(self):
return self.socket.accept()
def requestLoop(self):
while not self.closed:
self.acceptRequest()
def close(self):
self.closed = True
try:
@@ -314,13 +310,6 @@ class HttpServer:
except:
pass
def acceptRequest(self):
try:
(sock, addr) = self.accept()
self.processRequest(sock, addr)
except socket.error:
return
def processRequest(self, sock, addr):
try:
rp = RequestProcessor(self, sock, addr)
@@ -340,23 +329,12 @@ class HttpServer:
def getResource(self, req):
return self.root.getRequestResource(req)
class UnixHttpServer(HttpServer):
def __init__(self, path=None, root=None):
HttpServer.__init__(self, interface='localhost', root=root)
def __init__(self, root, path):
HttpServer.__init__(self, root, 'localhost')
self.path = path
def bind(self):
pathdir = os.path.dirname(self.path)
if not os.path.exists(pathdir):
os.makedirs(pathdir)
else:
try:
os.unlink(self.path)
except SystemExit:
raise
except Exception, ex:
pass
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.path)
self.socket = unix.bind(self.path)
+22 -15
View File
@@ -13,17 +13,36 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#============================================================================
# Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
# Copyright (C) 2005 XenSource Ltd.
# Copyright (C) 2005-2006 XenSource Ltd.
#============================================================================
import socket
import os
import os.path
import socket
import stat
import connection
def bind(path):
"""Create a Unix socket, and bind it to the given path. The socket is
created such that only the current user may access it."""
parent = os.path.dirname(path)
if os.path.exists(parent):
os.chown(parent, os.geteuid(), os.getegid())
os.chmod(parent, stat.S_IRWXU)
if os.path.exists(path):
os.unlink(path)
else:
os.makedirs(parent, stat.S_IRWXU)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(self.path)
return sock
class UnixListener(connection.SocketListener):
def __init__(self, path, protocol_class):
self.path = path
@@ -31,19 +50,7 @@ class UnixListener(connection.SocketListener):
def createSocket(self):
pathdir = os.path.dirname(self.path)
if not os.path.exists(pathdir):
os.makedirs(pathdir)
else:
try:
os.unlink(self.path)
except SystemExit:
raise
except Exception, ex:
pass
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(self.path)
return sock
return bind(self.path)
def acceptConnection(self, sock, _):
+5 -4
View File
@@ -13,6 +13,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#============================================================================
# Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
# Copyright (C) 2006 XenSource Ltd.
#============================================================================
"""Example xend HTTP
@@ -106,11 +107,11 @@ def create():
root.putChild('xend', SrvRoot())
servers = XendServers()
if xroot.get_xend_http_server():
port = xroot.get_xend_port()
interface = xroot.get_xend_address()
servers.add(HttpServer(root=root, interface=interface, port=port))
servers.add(HttpServer(root,
xroot.get_xend_address(),
xroot.get_xend_port()))
if xroot.get_xend_unix_server():
path = xroot.get_xend_unix_path()
log.info('unix path=' + path)
servers.add(UnixHttpServer(path=path, root=root))
servers.add(UnixHttpServer(root, path))
return servers
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.