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