Refactor proxy.Server to fix a crash when replaying with -n

This commit is contained in:
Aldo Cortesi 2012-04-02 13:24:51 +12:00
parent f13e2213ea
commit c02fdb2463
6 changed files with 25 additions and 12 deletions

View File

@ -150,7 +150,7 @@ class StatusBar(common.WWrap):
r.append("[")
r.append(("heading_key", "u"))
r.append(":%s]"%self.master.stickyauth_txt)
if self.master.server and self.master.server.config.reverse_proxy:
if self.master.server.config.reverse_proxy:
r.append("[")
r.append(("heading_key", "P"))
r.append(":%s]"%utils.unparse_url(*self.master.server.config.reverse_proxy))
@ -187,7 +187,7 @@ class StatusBar(common.WWrap):
t = [
('heading', ("[%s]"%self.master.state.flow_count()).ljust(7)),
]
if self.master.server:
if self.master.server.bound:
boundaddr = "[%s:%s]"%(self.master.server.address or "*", self.master.server.port)
else:
boundaddr = ""
@ -510,9 +510,7 @@ class ConsoleMaster(flow.FlowMaster):
self.view_flowlist()
if self.server:
slave = controller.Slave(self.masterq, self.server)
slave.start()
self.server.start_slave(controller.Slave, self.masterq)
if self.options.rfile:
ret = self.load_flows(self.options.rfile)

View File

@ -83,9 +83,7 @@ class Master:
def run(self):
global should_exit
should_exit = False
if self.server:
slave = Slave(self.masterq, self.server)
slave.start()
self.server.start_slave(Slave, self.masterq)
while not should_exit:
self.tick(self.masterq)
self.shutdown()

View File

@ -478,6 +478,7 @@ ServerBase.daemon_threads = True # Terminate workers when main thread ter
class ProxyServer(ServerBase):
request_queue_size = 20
allow_reuse_address = True
bound = True
def __init__(self, config, port, address=''):
"""
Raises ProxyServerError if there's a startup problem.
@ -491,6 +492,10 @@ class ProxyServer(ServerBase):
self.certdir = tempfile.mkdtemp(prefix="mitmproxy")
config.certdir = self.certdir
def start_slave(self, klass, masterq):
slave = klass(masterq, self)
slave.start()
def set_mqueue(self, q):
self.masterq = q
@ -505,6 +510,18 @@ class ProxyServer(ServerBase):
pass
class DummyServer:
bound = False
def __init__(self, config):
self.config = config
def start_slave(self, klass, masterq):
pass
def shutdown(self):
pass
# Command-line utils
def certificate_option_group(parser):
group = optparse.OptionGroup(parser, "SSL")

View File

@ -40,7 +40,7 @@ if __name__ == '__main__':
proxyconfig = proxy.process_proxy_options(parser, options)
if options.no_server:
server = None
server = proxy.DummyServer(proxyconfig)
else:
try:
server = proxy.ProxyServer(proxyconfig, options.port, options.addr)

View File

@ -46,7 +46,7 @@ if __name__ == '__main__':
config = proxy.process_proxy_options(parser, options)
if options.no_server:
server = None
server = proxy.DummyServer(config)
else:
try:
server = proxy.ProxyServer(config, options.port, options.addr)

View File

@ -131,7 +131,7 @@ Larry
[["content-type", "application/json"]],
"[1, 2"
)
assert r[0] == "Raw"
assert "Raw" in r[0]
r = cv.get_content_view(
cv.VIEW_AUTO,
@ -153,7 +153,7 @@ Larry
encoding.encode('gzip', "[1, 2, 3]")
)
assert "decoded gzip" in r[0]
assert "forced" in r[0]
assert "XML" in r[0]
tests = [