Fine-tune threading and fix an exception.

This commit is contained in:
Aldo Cortesi 2011-01-27 14:19:48 +13:00
parent efe11a0782
commit eb93cc22ce
2 changed files with 20 additions and 12 deletions

View File

@ -9,6 +9,7 @@ class ReplayConnection:
pass pass
# begin nocover
class ReplayThread(threading.Thread): class ReplayThread(threading.Thread):
def __init__(self, flow, masterq): def __init__(self, flow, masterq):
self.flow, self.masterq = flow, masterq self.flow, self.masterq = flow, masterq
@ -22,6 +23,7 @@ class ReplayThread(threading.Thread):
except proxy.ProxyError, v: except proxy.ProxyError, v:
err = proxy.Error(self.flow.connection, v.msg) err = proxy.Error(self.flow.connection, v.msg)
err.send(self.masterq) err.send(self.masterq)
# end nocover
class Flow: class Flow:

View File

@ -305,9 +305,11 @@ class ServerConnection:
def read_response(self): def read_response(self):
proto = self.rfile.readline() proto = self.rfile.readline()
if not proto:
raise ProxyError(200, "Blank server response.")
parts = proto.strip().split(" ", 2) parts = proto.strip().split(" ", 2)
if not len(parts) == 3: if not len(parts) == 3:
raise ProxyError(200, "Invalid server response.") raise ProxyError(200, "Invalid server response: %s."%proto)
proto, code, msg = parts proto, code, msg = parts
code = int(code) code = int(code)
headers = utils.Headers() headers = utils.Headers()
@ -415,21 +417,25 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
self.terminate(self.connection, self.wfile, self.rfile) self.terminate(self.connection, self.wfile, self.rfile)
def send_error(self, code, body): def send_error(self, code, body):
import BaseHTTPServer try:
response = BaseHTTPServer.BaseHTTPRequestHandler.responses[code][0] import BaseHTTPServer
self.wfile.write("HTTP/1.0 %s %s\r\n" % (code, response)) response = BaseHTTPServer.BaseHTTPRequestHandler.responses[code][0]
self.wfile.write("Server: %s\r\n"%NAME) self.wfile.write("HTTP/1.0 %s %s\r\n" % (code, response))
self.wfile.write("Content-type: text/html\r\n") self.wfile.write("Server: %s\r\n"%NAME)
self.wfile.write("\r\n") self.wfile.write("Content-type: text/html\r\n")
self.wfile.write('<html><head>\n<title>%d %s</title>\n</head>\n' self.wfile.write("\r\n")
'<body>\n%s\n</body>\n</html>' % (code, response, body)) self.wfile.write('<html><head>\n<title>%d %s</title>\n</head>\n'
self.wfile.flush() '<body>\n%s\n</body>\n</html>' % (code, response, body))
self.wfile.close() self.wfile.flush()
self.rfile.close() self.wfile.close()
self.rfile.close()
except IOError:
pass
ServerBase = SocketServer.ThreadingTCPServer ServerBase = SocketServer.ThreadingTCPServer
class ProxyServer(ServerBase): class ProxyServer(ServerBase):
request_queue_size = 20
allow_reuse_address = True allow_reuse_address = True
def __init__(self, port): def __init__(self, port):
self.port = port self.port = port