aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-06-21 16:54:49 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-06-21 16:54:49 +1200
commitcd8fba1d70fa8c7f4f39267d13b65c1bf2ab1b31 (patch)
tree940d89c39a2e620df5672325608d3c9d7ec5d7d8
parentf88e899274b097317db496245758181b43c1d72a (diff)
downloadmitmproxy-cd8fba1d70fa8c7f4f39267d13b65c1bf2ab1b31.tar.gz
mitmproxy-cd8fba1d70fa8c7f4f39267d13b65c1bf2ab1b31.tar.bz2
mitmproxy-cd8fba1d70fa8c7f4f39267d13b65c1bf2ab1b31.zip
Finalize porting built-in web app to Flask.
-rw-r--r--libpathod/app.py116
-rw-r--r--libpathod/pathod.py15
-rw-r--r--libpathod/templates/help.html4
-rw-r--r--libpathod/templates/log.html12
-rw-r--r--libpathod/templates/onelog.html4
-rw-r--r--libpathod/templates/preview.html9
6 files changed, 74 insertions, 86 deletions
diff --git a/libpathod/app.py b/libpathod/app.py
index 2a738b58..c0555422 100644
--- a/libpathod/app.py
+++ b/libpathod/app.py
@@ -1,6 +1,6 @@
-import logging
-from flask import Flask, jsonify, render_template
-import version
+import logging, pprint, cStringIO
+from flask import Flask, jsonify, render_template, request
+import version, rparse
logging.basicConfig(level="DEBUG")
app = Flask(__name__)
@@ -29,75 +29,47 @@ def api_clear_log():
@app.route('/')
@app.route('/index.html')
def index():
- return render_template("index.html", name="index", section="main")
+ return render_template("index.html", section="main")
+
+
+@app.route('/help')
+def help():
+ return render_template("help.html", section="help")
+
+
+@app.route('/log')
+def log():
+ return render_template("log.html", section="log", log=app.config["pathod"].get_log())
+@app.route('/log/<int:lid>')
+def onelog(lid):
+ l = pprint.pformat(app.config["pathod"].log_by_id(int(lid)))
+ return render_template("onelog.html", section="log", alog=l, lid=lid)
-"""
-class _Page(tornado.web.RequestHandler):
- def render(self, name, **kwargs):
- tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
-
-
-class Index(_Page):
- name = "index"
- section = "main"
- def get(self):
- self.render(self.name, section=self.section, spec="")
-
-
-class Preview(_Page):
- name = "preview"
- section = "main"
- SANITY = 1024*1024
- def get(self):
- spec = self.get_argument("spec", None)
- args = dict(
- spec = spec,
- section = self.section,
- syntaxerror = None,
- error = None
- )
- try:
- r = rparse.parse(self.application.settings, spec)
- except rparse.ParseException, v:
- args["syntaxerror"] = str(v)
- args["marked"] = v.marked()
- return self.render(self.name, **args)
- if r.length() > self.SANITY:
- error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
- args["error"] = error
- else:
- d = utils.DummyRequest()
- r.serve(d)
- args["output"] = d.getvalue()
- self.render(self.name, **args)
-
-
-class Help(_Page):
- name = "help"
- section = "help"
- def get(self):
- self.render(self.name, section=self.section)
-
-
-class Log(_Page):
- name = "log"
- section = "log"
- def get(self):
- self.render(self.name, section=self.section, log=self.application.log)
-
-
-class OneLog(_Page):
- name = "onelog"
- section = "log"
- def get(self, lid):
- l = pprint.pformat(self.application.log_by_id(int(lid)))
- self.render(self.name, section=self.section, alog=l, lid=lid)
-
-
-class ClearLog(_Page):
- def post(self):
- self.application.clear_logs()
- self.redirect("/log")
-"""
+
+
+SANITY = 1024*1024
+@app.route('/preview')
+def preview():
+ spec = request.args["spec"]
+ args = dict(
+ spec = spec,
+ section = "main",
+ syntaxerror = None,
+ error = None
+ )
+ try:
+ r = rparse.parse(app.config["pathod"].request_settings, spec)
+ except rparse.ParseException, v:
+ args["syntaxerror"] = str(v)
+ args["marked"] = v.marked()
+ return render_template("preview.html", **args)
+ if r.length() > SANITY:
+ error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
+ args["error"] = error
+ else:
+ s = cStringIO.StringIO()
+ r.serve(s)
+ args["output"] = s.getvalue()
+ return render_template("preview.html", **args)
diff --git a/libpathod/pathod.py b/libpathod/pathod.py
index edf6e03a..4f5fba22 100644
--- a/libpathod/pathod.py
+++ b/libpathod/pathod.py
@@ -35,6 +35,17 @@ class PathodHandler(tcp.BaseHandler):
ret = presp.serve(self.wfile)
if ret["disconnect"]:
self.close()
+
+ ret["request"] = dict(
+ path = path,
+ method = method,
+ headers = headers.lst,
+ #remote_address = self.request.connection.address,
+ #full_url = self.request.full_url(),
+ #query = self.request.query,
+ httpversion = httpversion,
+ #uri = self.request.uri,
+ )
self.server.add_log(ret)
else:
cc = wsgi.ClientConn(self.client_address)
@@ -60,6 +71,10 @@ class Pathod(tcp.TCPServer):
self.log = []
self.logid = 0
+ @property
+ def request_settings(self):
+ return {}
+
def handle_connection(self, request, client_address):
PathodHandler(request, client_address, self)
diff --git a/libpathod/templates/help.html b/libpathod/templates/help.html
index 3ab48c95..f5ac6796 100644
--- a/libpathod/templates/help.html
+++ b/libpathod/templates/help.html
@@ -1,4 +1,4 @@
-{% extends frame.html %}
+{% extends "frame.html" %}
{% block body %}
<!-- Text below generated with "markdown2 README.mkd" -->
@@ -251,4 +251,4 @@ ascii
bytes
</code></pre>
-{% end %}
+{% endblock %}
diff --git a/libpathod/templates/log.html b/libpathod/templates/log.html
index e8bf113a..22747e0e 100644
--- a/libpathod/templates/log.html
+++ b/libpathod/templates/log.html
@@ -1,4 +1,4 @@
-{% extends frame.html %}
+{% extends "frame.html" %}
{% block body %}
<form style="float: right" method="POST" action="/log/clear">
<button type="submit" class="btn">clear</button>
@@ -10,17 +10,19 @@
<thead>
<tr>
<th>id</th>
- <th>url</th>
+ <th>method</th>
+ <th>path</th>
</tr>
</thead>
<tbody>
{% for i in log %}
<tr>
<td>{{ i["id"] }}</td>
- <td><a href="/log/{{ i["id"] }}">{{ i["request"]["full_url"] }}</a></td>
+ <td>{{ i["request"]["method"] }}</td>
+ <td><a href="/log/{{ i["id"] }}">{{ i["request"]["path"] }}</a></td>
</tr>
- {% end %}
+ {% endfor %}
</tbody>
</table>
-{% end %}
+{% endblock %}
diff --git a/libpathod/templates/onelog.html b/libpathod/templates/onelog.html
index 378bac32..a94f443a 100644
--- a/libpathod/templates/onelog.html
+++ b/libpathod/templates/onelog.html
@@ -1,4 +1,4 @@
-{% extends frame.html %}
+{% extends "frame.html" %}
{% block body %}
<h2> Log entry {{ lid }} </h2>
<hr>
@@ -6,5 +6,5 @@
{{ alog }}
</pre>
-{% end %}
+{% endblock %}
diff --git a/libpathod/templates/preview.html b/libpathod/templates/preview.html
index 63960ebe..4b5182d7 100644
--- a/libpathod/templates/preview.html
+++ b/libpathod/templates/preview.html
@@ -1,4 +1,4 @@
-{% extends frame.html %}
+{% extends "frame.html" %}
{% block body %}
<h1>Preview</h1>
@@ -10,7 +10,6 @@
<h2> Error </h2>
<p style="color: #ff0000">{{ error }}</p>
{% else %}
-
<h2>Spec:</h2>
<pre>{{ spec }}</pre>
@@ -19,6 +18,6 @@
<pre>{{ output }}</pre>
-{% end %}
-{% include previewform.html %}
-{% end %}
+{% endif %}
+{% include "previewform.html" %}
+{% endblock %}