From a69d60208717db775ca68bbd1cd8a1af76082495 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 26 Sep 2012 10:38:47 +1200 Subject: [PATCH] Hex and verbatim output of requests. This works by sniffing traffic through the socket, so always gives us the exact traffic sent upstream. --- libpathod/pathoc.py | 15 +++++++++++++-- libpathod/rparse.py | 4 ++-- test/test_pathoc.py | 7 ++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 57bff46b9..b283c94e2 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -1,5 +1,6 @@ import sys, os from netlib import tcp, http +import netlib.utils import rparse, utils class PathocError(Exception): pass @@ -49,15 +50,25 @@ 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"]) + print >> fp, ">> ", req["method"], repr(req["path"]) for a in req["actions"]: print >> fp, "\t", 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: diff --git a/libpathod/rparse.py b/libpathod/rparse.py index 3eaf2569a..53878b976 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -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 diff --git a/test/test_pathoc.py b/test/test_pathoc.py index 8eb180afb..9485f84da 100644 --- a/test/test_pathoc.py +++ b/test/test_pathoc.py @@ -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"])