diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-04-29 14:59:54 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-04-29 14:59:54 +1200 |
commit | fd946f0c04c2471ab9e579087af3f4cb645be41a (patch) | |
tree | 65c9b2f20d0d5a4ba9569435d6b5f0cad053f10e /libpathod | |
parent | 6d4500c67943d03cf0910256ef9dab46789f366f (diff) | |
download | mitmproxy-fd946f0c04c2471ab9e579087af3f4cb645be41a.tar.gz mitmproxy-fd946f0c04c2471ab9e579087af3f4cb645be41a.tar.bz2 mitmproxy-fd946f0c04c2471ab9e579087af3f4cb645be41a.zip |
Basic logging.
Diffstat (limited to 'libpathod')
-rw-r--r-- | libpathod/app.py | 27 | ||||
-rw-r--r-- | libpathod/rparse.py | 29 |
2 files changed, 48 insertions, 8 deletions
diff --git a/libpathod/app.py b/libpathod/app.py index ac118602..ef549334 100644 --- a/libpathod/app.py +++ b/libpathod/app.py @@ -1,4 +1,4 @@ -import urllib +import urllib, pprint import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver import rparse, utils @@ -48,7 +48,20 @@ class Pathod(object): ) def _execute(self, transforms, *args, **kwargs): - self.response.render(self.request) + d = self.response.serve(self.request) + d["request"] = dict( + path = self.request.path, + method = self.request.method, + headers = self.request.headers, + host = self.request.host, + protocol = self.request.protocol, + remote_address = self.request.connection.address, + full_url = self.request.full_url(), + query = self.request.query, + version = self.request.version, + uri = self.request.uri, + ) + self.application.add_log(d) class RequestPathod(Pathod): @@ -59,6 +72,7 @@ class RequestPathod(Pathod): class PathodApp(tornado.web.Application): + LOGBUF = 500 def __init__(self, **settings): self.templates = tornado.template.Loader(utils.data.path("templates")) self.appsettings = settings @@ -75,6 +89,8 @@ class PathodApp(tornado.web.Application): template_path = utils.data.path("templates"), debug=True ) + self.log = [] + self.logid = 0 def add_anchor(self, pattern, spec): """ @@ -116,6 +132,13 @@ class PathodApp(tornado.web.Application): del l[i] return + def add_log(self, d): + d["id"] = self.logid + self.log.insert(0, d) + if len(self.log) > self.LOGBUF: + self.log.pop() + self.logid += 1 + # begin nocover def run(application, port, ssl_options): diff --git a/libpathod/rparse.py b/libpathod/rparse.py index de69e9b9..1a6a5e4c 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -1,4 +1,4 @@ -import operator, string, random, sys, time, mmap, os +import operator, string, random, sys, time, mmap, os, time import contrib.pyparsing as pp import http, utils import tornado.ioloop @@ -430,7 +430,8 @@ class Response: skip = 0 fp.finish() - def render(self, fp): + def serve(self, fp): + started = time.time() if self.body and not self.get_header("Content-Length"): self.headers.append( ( @@ -460,7 +461,13 @@ class Response: vals.reverse() actions = self.ready_actions(self.length(), self.actions) actions.reverse() - return self.write_values(fp, vals, actions) + self.write_values(fp, vals, actions[:]) + duration = time.time() - started + return dict( + started = started, + duration = duration, + actions = actions, + ) def __str__(self): parts = [ @@ -470,12 +477,17 @@ class Response: class CraftedResponse(Response): - def __init__(self, settings, tokens): + def __init__(self, settings, spec, tokens): Response.__init__(self) - self.tokens = tokens + self.spec, self.tokens = spec, tokens for i in tokens: i.mod_response(settings, self) + def serve(self, fp): + d = Response.serve(self, fp) + d["spec"] = self.spec + return d + class InternalResponse(Response): def __init__(self, code, body): @@ -494,9 +506,14 @@ class InternalResponse(Response): ) ] + def serve(self, fp): + d = Response.serve(self, fp) + d["internal"] = True + return d + def parse(settings, s): try: - return CraftedResponse(settings, Response.expr().parseString(s, parseAll=True)) + return CraftedResponse(settings, s, Response.expr().parseString(s, parseAll=True)) except pp.ParseException, v: raise ParseException(v.msg, v.line, v.col) |