2013-01-25 02:54:41 +00:00
|
|
|
import threading, Queue, cStringIO
|
2013-08-12 04:03:29 +00:00
|
|
|
import tcp, certutils
|
2013-01-25 02:54:41 +00:00
|
|
|
|
|
|
|
class ServerThread(threading.Thread):
|
|
|
|
def __init__(self, server):
|
|
|
|
self.server = server
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.server.serve_forever()
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.server.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
class ServerTestBase:
|
2013-01-25 03:03:59 +00:00
|
|
|
ssl = None
|
|
|
|
handler = None
|
2013-12-13 14:04:38 +00:00
|
|
|
addr = ("localhost", 0)
|
|
|
|
use_ipv6 = False
|
2013-12-15 05:43:54 +00:00
|
|
|
|
2013-01-25 02:54:41 +00:00
|
|
|
@classmethod
|
|
|
|
def setupAll(cls):
|
|
|
|
cls.q = Queue.Queue()
|
|
|
|
s = cls.makeserver()
|
|
|
|
cls.port = s.port
|
|
|
|
cls.server = ServerThread(s)
|
|
|
|
cls.server.start()
|
|
|
|
|
2013-01-25 03:03:59 +00:00
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
2013-12-13 14:04:38 +00:00
|
|
|
return TServer(cls.ssl, cls.q, cls.handler, cls.addr, cls.use_ipv6)
|
2013-01-25 03:03:59 +00:00
|
|
|
|
2013-01-25 02:54:41 +00:00
|
|
|
@classmethod
|
|
|
|
def teardownAll(cls):
|
|
|
|
cls.server.shutdown()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def last_handler(self):
|
|
|
|
return self.server.server.last_handler
|
|
|
|
|
|
|
|
|
|
|
|
class TServer(tcp.TCPServer):
|
2013-12-13 14:04:38 +00:00
|
|
|
def __init__(self, ssl, q, handler_klass, addr, use_ipv6):
|
2013-01-25 02:54:41 +00:00
|
|
|
"""
|
|
|
|
ssl: A {cert, key, v3_only} dict.
|
|
|
|
"""
|
2013-12-13 14:04:38 +00:00
|
|
|
tcp.TCPServer.__init__(self, addr, use_ipv6=use_ipv6)
|
2013-01-25 02:54:41 +00:00
|
|
|
self.ssl, self.q = ssl, q
|
|
|
|
self.handler_klass = handler_klass
|
|
|
|
self.last_handler = None
|
|
|
|
|
2014-01-09 00:57:37 +00:00
|
|
|
def handle_client_connection(self, request, client_address):
|
2013-01-25 02:54:41 +00:00
|
|
|
h = self.handler_klass(request, client_address, self)
|
|
|
|
self.last_handler = h
|
|
|
|
if self.ssl:
|
2013-08-12 04:03:29 +00:00
|
|
|
cert = certutils.SSLCert.from_pem(
|
2013-08-19 17:41:20 +00:00
|
|
|
file(self.ssl["cert"], "rb").read()
|
2013-08-12 04:03:29 +00:00
|
|
|
)
|
2013-01-25 02:54:41 +00:00
|
|
|
if self.ssl["v3_only"]:
|
|
|
|
method = tcp.SSLv3_METHOD
|
|
|
|
options = tcp.OP_NO_SSLv2|tcp.OP_NO_TLSv1
|
|
|
|
else:
|
|
|
|
method = tcp.SSLv23_METHOD
|
|
|
|
options = None
|
|
|
|
h.convert_to_ssl(
|
2013-08-12 04:03:29 +00:00
|
|
|
cert,
|
2013-01-25 02:54:41 +00:00
|
|
|
self.ssl["key"],
|
|
|
|
method = method,
|
|
|
|
options = options,
|
2013-05-12 20:48:21 +00:00
|
|
|
handle_sni = getattr(h, "handle_sni", None),
|
2013-08-21 10:42:30 +00:00
|
|
|
request_client_cert = self.ssl["request_client_cert"],
|
|
|
|
cipher_list = self.ssl.get("cipher_list", None)
|
2013-01-25 02:54:41 +00:00
|
|
|
)
|
|
|
|
h.handle()
|
|
|
|
h.finish()
|
|
|
|
|
|
|
|
def handle_error(self, request, client_address):
|
|
|
|
s = cStringIO.StringIO()
|
|
|
|
tcp.TCPServer.handle_error(self, request, client_address, s)
|
|
|
|
self.q.put(s.getvalue())
|