aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-09-26 14:25:39 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-09-26 14:25:39 +1200
commit10a19fc4da5a6fbbfd4a36d0e61d3bc80a38e937 (patch)
treead4d4e2f4fa8769d61ea06fbfc3881870eeb737b
parent424d15c28ba46d2d42b96d2247166eb13b4c7b8e (diff)
downloadmitmproxy-10a19fc4da5a6fbbfd4a36d0e61d3bc80a38e937.tar.gz
mitmproxy-10a19fc4da5a6fbbfd4a36d0e61d3bc80a38e937.tar.bz2
mitmproxy-10a19fc4da5a6fbbfd4a36d0e61d3bc80a38e937.zip
Refactor print_requests -> print_request
- Change to handle one request at a time - Shift error handling around
-rw-r--r--libpathod/pathoc.py85
-rwxr-xr-xpathoc17
-rw-r--r--test/test_pathoc.py17
3 files changed, 58 insertions, 61 deletions
diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py
index 18c86c5d..40f698fd 100644
--- a/libpathod/pathoc.py
+++ b/libpathod/pathoc.py
@@ -38,7 +38,7 @@ class Pathoc(tcp.TCPClient):
print >> fp, "%s (unprintables escaped):"%header
print >> fp, netlib.utils.cleanBin(data)
- def print_requests(self, reqs, showreq, showresp, explain, hexdump, fp=sys.stdout):
+ def print_request(self, spec, showreq, showresp, explain, hexdump, fp=sys.stdout):
"""
Performs a series of requests, and prints results to the specified
file descriptor.
@@ -49,47 +49,42 @@ class Pathoc(tcp.TCPClient):
explain: Print request explanation
hexdump: When printing requests or responses, use hex dump output
"""
- for i in reqs:
- try:
- r = rparse.parse_request(self.settings, i)
- if showreq:
- self.wfile.start_log()
- req = r.serve(self.wfile, None, self.host)
- if explain:
- print >> fp, ">> ", req["method"], repr(req["path"])
- for a in req["actions"]:
- print >> fp, "\t",
- for x in a:
- print >> fp, x,
- print >> fp
- if showreq:
- self._show(fp, ">> Request", self.wfile.get_log(), hexdump)
- self.wfile.flush()
- if showresp:
- self.rfile.start_log()
- 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 rparse.FileAccessDenied, v:
- print >> fp, "File access error: %s"%v
- return
- except http.HttpError, v:
- print >> fp, "<< HTTP Error:", v.msg
- if showresp:
- self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
- return
- except tcp.NetLibTimeout:
- print >> fp, "<<", "Timeout"
- if showresp:
- self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
- return
- except tcp.NetLibDisconnect: # pragma: nocover
- print >> fp, "<<", "Disconnect"
- return
- else:
- if showresp:
- self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
- else:
- self._show_summary(fp, *resp)
+ try:
+ r = rparse.parse_request(self.settings, spec)
+ except rparse.ParseException, v:
+ print >> fp, "Error parsing request spec: %s"%v.msg
+ print >> fp, v.marked()
+ return
+ except rparse.FileAccessDenied, v:
+ print >> fp, "File access error: %s"%v
+ return
+
+ resp = None
+ if showreq:
+ self.wfile.start_log()
+ try:
+ req = r.serve(self.wfile, None, self.host)
+ if explain:
+ print >> fp, ">> ", req["method"], repr(req["path"])
+ for a in req["actions"]:
+ print >> fp, "\t",
+ for x in a:
+ print >> fp, x,
+ print >> fp
+ if showreq:
+ self._show(fp, ">> Request", self.wfile.get_log(), hexdump)
+ self.wfile.flush()
+ if showresp:
+ self.rfile.start_log()
+ resp = http.read_response(self.rfile, r.method, None)
+ except http.HttpError, v:
+ print >> fp, "<< HTTP Error:", v.msg
+ except tcp.NetLibTimeout:
+ print >> fp, "<<", "Timeout"
+ except tcp.NetLibDisconnect: # pragma: nocover
+ print >> fp, "<<", "Disconnect"
+ if showresp:
+ self._show(fp, "<< Response", self.rfile.get_log(), hexdump)
+ else:
+ if resp:
+ self._show_summary(fp, *resp)
diff --git a/pathoc b/pathoc
index 19f70d4e..89470994 100755
--- a/pathoc
+++ b/pathoc
@@ -74,14 +74,15 @@ if __name__ == "__main__":
continue
if args.timeout:
p.settimeout(args.timeout)
- p.print_requests(
- args.request,
- showreq=args.showreq,
- showresp=args.showresp,
- explain=args.explain,
- hexdump=args.hexdump,
- )
- sys.stdout.flush()
+ for spec in args.request:
+ p.print_request(
+ spec,
+ showreq=args.showreq,
+ showresp=args.showresp,
+ explain=args.explain,
+ hexdump=args.hexdump,
+ )
+ sys.stdout.flush()
except KeyboardInterrupt:
pass
diff --git a/test/test_pathoc.py b/test/test_pathoc.py
index ce1476a6..0a37c4b7 100644
--- a/test/test_pathoc.py
+++ b/test/test_pathoc.py
@@ -30,14 +30,15 @@ class TestDaemon:
if timeout:
c.settimeout(timeout)
s = cStringIO.StringIO()
- c.print_requests(
- requests,
- showreq = showreq,
- showresp = showresp,
- explain = explain,
- hexdump = hexdump,
- fp = s
- )
+ for i in requests:
+ c.print_request(
+ i,
+ showreq = showreq,
+ showresp = showresp,
+ explain = explain,
+ hexdump = hexdump,
+ fp = s
+ )
return s.getvalue()
def test_timeout(self):