mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 08:11:00 +00:00
Finalize porting built-in web app to Flask.
This commit is contained in:
parent
f88e899274
commit
cd8fba1d70
110
libpathod/app.py
110
libpathod/app.py
@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging, pprint, cStringIO
|
||||||
from flask import Flask, jsonify, render_template
|
from flask import Flask, jsonify, render_template, request
|
||||||
import version
|
import version, rparse
|
||||||
|
|
||||||
logging.basicConfig(level="DEBUG")
|
logging.basicConfig(level="DEBUG")
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -29,75 +29,47 @@ def api_clear_log():
|
|||||||
@app.route('/')
|
@app.route('/')
|
||||||
@app.route('/index.html')
|
@app.route('/index.html')
|
||||||
def index():
|
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>')
|
||||||
class _Page(tornado.web.RequestHandler):
|
def onelog(lid):
|
||||||
def render(self, name, **kwargs):
|
l = pprint.pformat(app.config["pathod"].log_by_id(int(lid)))
|
||||||
tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
|
return render_template("onelog.html", section="log", alog=l, lid=lid)
|
||||||
|
|
||||||
|
|
||||||
class Index(_Page):
|
|
||||||
name = "index"
|
|
||||||
section = "main"
|
|
||||||
def get(self):
|
|
||||||
self.render(self.name, section=self.section, spec="")
|
|
||||||
|
|
||||||
|
SANITY = 1024*1024
|
||||||
class Preview(_Page):
|
@app.route('/preview')
|
||||||
name = "preview"
|
def preview():
|
||||||
section = "main"
|
spec = request.args["spec"]
|
||||||
SANITY = 1024*1024
|
args = dict(
|
||||||
def get(self):
|
spec = spec,
|
||||||
spec = self.get_argument("spec", None)
|
section = "main",
|
||||||
args = dict(
|
syntaxerror = None,
|
||||||
spec = spec,
|
error = None
|
||||||
section = self.section,
|
)
|
||||||
syntaxerror = None,
|
try:
|
||||||
error = None
|
r = rparse.parse(app.config["pathod"].request_settings, spec)
|
||||||
)
|
except rparse.ParseException, v:
|
||||||
try:
|
args["syntaxerror"] = str(v)
|
||||||
r = rparse.parse(self.application.settings, spec)
|
args["marked"] = v.marked()
|
||||||
except rparse.ParseException, v:
|
return render_template("preview.html", **args)
|
||||||
args["syntaxerror"] = str(v)
|
if r.length() > SANITY:
|
||||||
args["marked"] = v.marked()
|
error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
|
||||||
return self.render(self.name, **args)
|
args["error"] = error
|
||||||
if r.length() > self.SANITY:
|
else:
|
||||||
error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
|
s = cStringIO.StringIO()
|
||||||
args["error"] = error
|
r.serve(s)
|
||||||
else:
|
args["output"] = s.getvalue()
|
||||||
d = utils.DummyRequest()
|
return render_template("preview.html", **args)
|
||||||
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")
|
|
||||||
"""
|
|
||||||
|
@ -35,6 +35,17 @@ class PathodHandler(tcp.BaseHandler):
|
|||||||
ret = presp.serve(self.wfile)
|
ret = presp.serve(self.wfile)
|
||||||
if ret["disconnect"]:
|
if ret["disconnect"]:
|
||||||
self.close()
|
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)
|
self.server.add_log(ret)
|
||||||
else:
|
else:
|
||||||
cc = wsgi.ClientConn(self.client_address)
|
cc = wsgi.ClientConn(self.client_address)
|
||||||
@ -60,6 +71,10 @@ class Pathod(tcp.TCPServer):
|
|||||||
self.log = []
|
self.log = []
|
||||||
self.logid = 0
|
self.logid = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def request_settings(self):
|
||||||
|
return {}
|
||||||
|
|
||||||
def handle_connection(self, request, client_address):
|
def handle_connection(self, request, client_address):
|
||||||
PathodHandler(request, client_address, self)
|
PathodHandler(request, client_address, self)
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{% extends frame.html %}
|
{% extends "frame.html" %}
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<!-- Text below generated with "markdown2 README.mkd" -->
|
<!-- Text below generated with "markdown2 README.mkd" -->
|
||||||
|
|
||||||
@ -251,4 +251,4 @@ ascii
|
|||||||
bytes
|
bytes
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
{% end %}
|
{% endblock %}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{% extends frame.html %}
|
{% extends "frame.html" %}
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<form style="float: right" method="POST" action="/log/clear">
|
<form style="float: right" method="POST" action="/log/clear">
|
||||||
<button type="submit" class="btn">clear</button>
|
<button type="submit" class="btn">clear</button>
|
||||||
@ -10,17 +10,19 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>id</th>
|
<th>id</th>
|
||||||
<th>url</th>
|
<th>method</th>
|
||||||
|
<th>path</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for i in log %}
|
{% for i in log %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ i["id"] }}</td>
|
<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>
|
</tr>
|
||||||
{% end %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
{% end %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{% extends frame.html %}
|
{% extends "frame.html" %}
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<h2> Log entry {{ lid }} </h2>
|
<h2> Log entry {{ lid }} </h2>
|
||||||
<hr>
|
<hr>
|
||||||
@ -6,5 +6,5 @@
|
|||||||
{{ alog }}
|
{{ alog }}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
{% end %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{% extends frame.html %}
|
{% extends "frame.html" %}
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
|
||||||
<h1>Preview</h1>
|
<h1>Preview</h1>
|
||||||
@ -10,7 +10,6 @@
|
|||||||
<h2> Error </h2>
|
<h2> Error </h2>
|
||||||
<p style="color: #ff0000">{{ error }}</p>
|
<p style="color: #ff0000">{{ error }}</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
||||||
<h2>Spec:</h2>
|
<h2>Spec:</h2>
|
||||||
|
|
||||||
<pre>{{ spec }}</pre>
|
<pre>{{ spec }}</pre>
|
||||||
@ -19,6 +18,6 @@
|
|||||||
|
|
||||||
<pre>{{ output }}</pre>
|
<pre>{{ output }}</pre>
|
||||||
|
|
||||||
{% end %}
|
{% endif %}
|
||||||
{% include previewform.html %}
|
{% include "previewform.html" %}
|
||||||
{% end %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user