mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-01 07:49:10 +00:00
More robust response handling.
This commit is contained in:
parent
86fe199988
commit
7a49cdfef3
@ -40,23 +40,23 @@ class Pathoc(tcp.TCPClient):
|
||||
r = rparse.parse_request({}, i)
|
||||
req = r.serve(self.wfile)
|
||||
if reqdump:
|
||||
print >> fp, ">>", req["method"], req["path"]
|
||||
print >> fp, "\n>>", req["method"], req["path"]
|
||||
for a in req["actions"]:
|
||||
print >> fp, "\t",
|
||||
for x in a:
|
||||
print x,
|
||||
print
|
||||
print >> fp, x,
|
||||
print >> fp
|
||||
self.wfile.flush()
|
||||
resp = self.request(i)
|
||||
resp = http.read_response(self.rfile, r.method, None)
|
||||
except rparse.ParseException, v:
|
||||
print >> fp, "Error parsing request spec: %s"%v.msg
|
||||
print >> fp, v.marked()
|
||||
return
|
||||
except http.HttpError, v:
|
||||
print >> fp, v.msg
|
||||
print >> fp, "<<", v.msg
|
||||
return
|
||||
except tcp.NetLibTimeout:
|
||||
print >> fp, "Timeout"
|
||||
print >> fp, "<<", "Timeout"
|
||||
else:
|
||||
if respdump:
|
||||
print_full(fp, *resp)
|
||||
|
@ -18,30 +18,15 @@ class PathodHandler(tcp.BaseHandler):
|
||||
def handle_sni(self, connection):
|
||||
self.sni = connection.get_servername()
|
||||
|
||||
def handle(self):
|
||||
if self.server.ssloptions:
|
||||
try:
|
||||
self.convert_to_ssl(
|
||||
self.server.ssloptions["certfile"],
|
||||
self.server.ssloptions["keyfile"],
|
||||
)
|
||||
except tcp.NetLibError, v:
|
||||
s = str(v)
|
||||
self.server.add_log(
|
||||
dict(
|
||||
type = "error",
|
||||
msg = s
|
||||
)
|
||||
)
|
||||
self.info(s)
|
||||
self.finish()
|
||||
|
||||
while not self.finished:
|
||||
def handle_request(self):
|
||||
"""
|
||||
Returns True if handling should continue.
|
||||
"""
|
||||
line = self.rfile.readline()
|
||||
if line == "\r\n" or line == "\n": # Possible leftover from previous message
|
||||
line = self.rfile.readline()
|
||||
if line == "":
|
||||
return None
|
||||
return
|
||||
|
||||
parts = http.parse_init_http(line)
|
||||
if not parts:
|
||||
@ -53,7 +38,7 @@ class PathodHandler(tcp.BaseHandler):
|
||||
msg = s
|
||||
)
|
||||
)
|
||||
return None
|
||||
return
|
||||
method, path, httpversion = parts
|
||||
|
||||
headers = http.read_headers(self.rfile)
|
||||
@ -87,7 +72,7 @@ class PathodHandler(tcp.BaseHandler):
|
||||
if crafted:
|
||||
response_log = crafted.serve(self.wfile)
|
||||
if response_log["disconnect"]:
|
||||
self.finish()
|
||||
return
|
||||
self.server.add_log(
|
||||
dict(
|
||||
type = "crafted",
|
||||
@ -107,6 +92,39 @@ class PathodHandler(tcp.BaseHandler):
|
||||
)
|
||||
app.serve(req, self.wfile)
|
||||
self.debug("%s %s"%(method, path))
|
||||
return True
|
||||
|
||||
def handle(self):
|
||||
if self.server.ssloptions:
|
||||
try:
|
||||
self.convert_to_ssl(
|
||||
self.server.ssloptions["certfile"],
|
||||
self.server.ssloptions["keyfile"],
|
||||
)
|
||||
except tcp.NetLibError, v:
|
||||
s = str(v)
|
||||
self.server.add_log(
|
||||
dict(
|
||||
type = "error",
|
||||
msg = s
|
||||
)
|
||||
)
|
||||
self.info(s)
|
||||
return
|
||||
|
||||
while not self.finished:
|
||||
try:
|
||||
if not self.handle_request():
|
||||
return
|
||||
except tcp.NetLibDisconnect:
|
||||
self.info("Disconnect")
|
||||
self.server.add_log(
|
||||
dict(
|
||||
type = "error",
|
||||
msg = "Disconnect"
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
class Pathod(tcp.TCPServer):
|
||||
|
@ -64,11 +64,13 @@ class _DaemonTests:
|
||||
scheme = "https" if self.SSL else "http"
|
||||
return requests.get("%s://localhost:%s/p/%s"%(scheme, self.d.port, spec), verify=False)
|
||||
|
||||
def pathoc(self, spec):
|
||||
def pathoc(self, spec, timeout=None):
|
||||
c = pathoc.Pathoc("localhost", self.d.port)
|
||||
c.connect()
|
||||
if self.SSL:
|
||||
c.convert_to_ssl()
|
||||
if timeout:
|
||||
c.settimeout(timeout)
|
||||
return c.request(spec)
|
||||
|
||||
def test_preline(self):
|
||||
@ -114,6 +116,7 @@ class _DaemonTests:
|
||||
assert "foo" in l["msg"]
|
||||
|
||||
|
||||
|
||||
class TestDaemon(_DaemonTests):
|
||||
SSL = False
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user