Hex and verbatim output of requests.

This works by sniffing traffic through the socket, so always gives us the exact
traffic sent upstream.
This commit is contained in:
Aldo Cortesi 2012-09-26 10:38:47 +12:00
parent 0a5d4fbbbb
commit a69d602087
3 changed files with 21 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import sys, os
from netlib import tcp, http
import netlib.utils
import rparse, utils
class PathocError(Exception): pass
@ -49,6 +50,8 @@ class Pathoc(tcp.TCPClient):
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"])
@ -57,7 +60,15 @@ class Pathoc(tcp.TCPClient):
for x in a:
print >> fp, x,
print >> fp
print req
if showreq:
data = self.wfile.get_log()
if hexdump:
print >> fp, ">> Request (hex dump):"
for line in netlib.utils.hexdump(data):
print >> fp, "\t%s %s %s"%line
else:
print >> fp, ">> Request (unprintables escaped):"
print >> fp, netlib.utils.cleanBin(data)
self.wfile.flush()
resp = http.read_response(self.rfile, r.method, None)
except rparse.ParseException, v:

View File

@ -653,7 +653,7 @@ class Response(Message):
ShortcutLocation,
Raw
)
logattrs = ["code", "version"]
logattrs = ["code", "version", "body"]
def __init__(self):
Message.__init__(self)
self.code = 200
@ -691,7 +691,7 @@ class Request(Message):
ShortcutContentType,
Raw
)
logattrs = ["method", "path"]
logattrs = ["method", "path", "body"]
def __init__(self):
Message.__init__(self)
self.method = None

View File

@ -49,11 +49,16 @@ class TestDaemon:
)
return s.getvalue()
def test_print_requests(self):
def test_showresp(self):
reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
assert self.tval(reqs).count("200") == 2
assert self.tval(reqs, showresp=True).count("Date") == 2
def test_showreq(self):
reqs = [ "get:/api/info:p0,0", "get:/api/info:p0,0" ]
assert self.tval(reqs, showreq=True).count("unprintables escaped") == 2
assert self.tval(reqs, showreq=True, hexdump=True).count("hex dump") == 2
def test_parse_err(self):
assert "Error parsing" in self.tval(["foo"])