aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-07-21 20:20:37 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-07-21 20:20:37 +1200
commit86fe199988801232f209b7e39a2910065bf5db5f (patch)
treea4ef1fd23686b1404f8a3f3352a3b647aac52451
parent059a2329035b916c8762b58a385556266abb4629 (diff)
downloadmitmproxy-86fe199988801232f209b7e39a2910065bf5db5f.tar.gz
mitmproxy-86fe199988801232f209b7e39a2910065bf5db5f.tar.bz2
mitmproxy-86fe199988801232f209b7e39a2910065bf5db5f.zip
pathoc: add a flag to dump request information.
-rw-r--r--libpathod/pathoc.py26
-rw-r--r--libpathod/rparse.py17
-rwxr-xr-xpathoc6
-rw-r--r--test/test_pathoc.py4
-rw-r--r--test/test_rparse.py4
5 files changed, 40 insertions, 17 deletions
diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py
index f79ed3af..7af5a288 100644
--- a/libpathod/pathoc.py
+++ b/libpathod/pathoc.py
@@ -6,11 +6,11 @@ class PathocError(Exception): pass
def print_short(fp, httpversion, code, msg, headers, content):
- print >> fp, "%s %s: %s bytes"%(code, msg, len(content))
+ print >> fp, "<< %s %s: %s bytes"%(code, msg, len(content))
def print_full(fp, httpversion, code, msg, headers, content):
- print >> fp, "HTTP%s/%s %s %s"%(httpversion[0], httpversion[1], code, msg)
+ print >> fp, "<< HTTP%s/%s %s %s"%(httpversion[0], httpversion[1], code, msg)
print >> fp, headers
print >> fp, content
@@ -26,18 +26,28 @@ class Pathoc(tcp.TCPClient):
May raise rparse.ParseException and netlib.http.HttpError.
"""
r = rparse.parse_request({}, spec)
- r.serve(self.wfile)
+ ret = r.serve(self.wfile)
self.wfile.flush()
return http.read_response(self.rfile, r.method, None)
- def print_requests(self, reqs, verbose, fp=sys.stdout):
+ def print_requests(self, reqs, respdump, reqdump, fp=sys.stdout):
"""
Performs a series of requests, and prints results to the specified
file pointer.
"""
for i in reqs:
try:
- ret = self.request(i)
+ r = rparse.parse_request({}, i)
+ req = r.serve(self.wfile)
+ if reqdump:
+ print >> fp, ">>", req["method"], req["path"]
+ for a in req["actions"]:
+ print >> fp, "\t",
+ for x in a:
+ print x,
+ print
+ self.wfile.flush()
+ resp = self.request(i)
except rparse.ParseException, v:
print >> fp, "Error parsing request spec: %s"%v.msg
print >> fp, v.marked()
@@ -48,7 +58,7 @@ class Pathoc(tcp.TCPClient):
except tcp.NetLibTimeout:
print >> fp, "Timeout"
else:
- if verbose:
- print_full(fp, *ret)
+ if respdump:
+ print_full(fp, *resp)
else:
- print_short(fp, *ret)
+ print_short(fp, *resp)
diff --git a/libpathod/rparse.py b/libpathod/rparse.py
index 9d0f7e79..6eb7d5a4 100644
--- a/libpathod/rparse.py
+++ b/libpathod/rparse.py
@@ -130,21 +130,28 @@ class LiteralGenerator:
def __getslice__(self, a, b):
return self.s.__getslice__(a, b)
+ def __repr__(self):
+ return '"%s"'%self.s
+
class RandomGenerator:
- def __init__(self, chars, length):
- self.chars = chars
+ def __init__(self, dtype, length):
+ self.dtype = dtype
self.length = length
def __len__(self):
return self.length
def __getitem__(self, x):
- return random.choice(self.chars)
+ return random.choice(DATATYPES[self.dtype])
def __getslice__(self, a, b):
b = min(b, self.length)
- return "".join(random.choice(self.chars) for x in range(a, b))
+ chars = DATATYPES[self.dtype]
+ return "".join(random.choice(chars) for x in range(a, b))
+
+ def __repr__(self):
+ return "%s random from %s"%(self.length, self.dtype)
class FileGenerator:
@@ -205,7 +212,7 @@ class ValueGenerate:
return self.usize * self.UNITS[self.unit]
def get_generator(self, settings):
- return RandomGenerator(DATATYPES[self.datatype], self.bytes())
+ return RandomGenerator(self.datatype, self.bytes())
@classmethod
def expr(klass):
diff --git a/pathoc b/pathoc
index 93da3798..fc76cfc8 100755
--- a/pathoc
+++ b/pathoc
@@ -18,6 +18,10 @@ if __name__ == "__main__":
help="Port. Defaults to 80, or 443 if SSL is active."
)
parser.add_argument(
+ "-d", dest="reqdump", action="store_true", default=False,
+ help="Print request record before each response."
+ )
+ parser.add_argument(
"-s", dest="ssl", action="store_true", default=False,
help="Connect with SSL."
)
@@ -56,4 +60,4 @@ if __name__ == "__main__":
p.convert_to_ssl(sni=args.sni)
if args.timeout:
p.settimeout(args.timeout)
- p.print_requests(args.request, args.verbose)
+ p.print_requests(args.request, args.verbose, args.reqdump)
diff --git a/test/test_pathoc.py b/test/test_pathoc.py
index a9c38870..310d75f6 100644
--- a/test/test_pathoc.py
+++ b/test/test_pathoc.py
@@ -28,11 +28,11 @@ class TestDaemon:
c = pathoc.Pathoc("127.0.0.1", self.d.port)
c.connect()
s = cStringIO.StringIO()
- c.print_requests(requests, verbose, s)
+ c.print_requests(requests, verbose, True, s)
return s.getvalue()
def test_print_requests(self):
- reqs = [ "get:/api/info", "get:/api/info" ]
+ reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
assert self.tval(reqs, False).count("200") == 2
assert self.tval(reqs, True).count("Date") == 2
diff --git a/test/test_rparse.py b/test/test_rparse.py
index 11c831c6..1527bddf 100644
--- a/test/test_rparse.py
+++ b/test/test_rparse.py
@@ -12,7 +12,8 @@ class TestMisc:
assert g[:] == "val"
def test_randomgenerator(self):
- g = rparse.RandomGenerator("one", 100)
+ g = rparse.RandomGenerator("bytes", 100)
+ assert repr(g)
assert len(g[:10]) == 10
assert len(g[1:10]) == 9
assert len(g[:1000]) == 100
@@ -21,6 +22,7 @@ class TestMisc:
def test_literalgenerator(self):
g = rparse.LiteralGenerator("one")
+ assert repr(g)
assert g == "one"
assert g[:] == "one"
assert g[1] == "n"