2012-06-21 04:54:49 +00:00
|
|
|
import logging, pprint, cStringIO
|
2012-08-11 05:06:51 +00:00
|
|
|
from flask import Flask, jsonify, render_template, request, abort, make_response
|
2012-07-23 04:39:25 +00:00
|
|
|
import version, rparse, utils
|
2012-06-21 04:25:27 +00:00
|
|
|
|
|
|
|
logging.basicConfig(level="DEBUG")
|
2012-06-19 04:57:57 +00:00
|
|
|
app = Flask(__name__)
|
2012-06-19 01:23:07 +00:00
|
|
|
|
2012-07-23 11:31:26 +00:00
|
|
|
def api():
|
|
|
|
@app.route('/api/info')
|
|
|
|
def api_info():
|
|
|
|
return jsonify(
|
|
|
|
version = version.IVERSION
|
|
|
|
)
|
2012-06-19 04:57:57 +00:00
|
|
|
|
|
|
|
|
2012-07-23 11:31:26 +00:00
|
|
|
@app.route('/api/log')
|
|
|
|
def api_log():
|
|
|
|
return jsonify(
|
|
|
|
log = app.config["pathod"].get_log()
|
|
|
|
)
|
2012-06-19 01:23:07 +00:00
|
|
|
|
|
|
|
|
2012-07-23 11:31:26 +00:00
|
|
|
@app.route('/api/clear_log')
|
|
|
|
def api_clear_log():
|
|
|
|
app.config["pathod"].clear_log()
|
|
|
|
return "OK"
|
2012-06-19 01:23:07 +00:00
|
|
|
|
|
|
|
|
2012-08-11 05:06:51 +00:00
|
|
|
def render(s, cacheable, **kwargs):
|
2012-07-24 10:27:04 +00:00
|
|
|
kwargs["noapi"] = app.config["pathod"].noapi
|
2012-07-24 11:38:41 +00:00
|
|
|
kwargs["nocraft"] = app.config["pathod"].nocraft
|
|
|
|
kwargs["craftanchor"] = app.config["pathod"].craftanchor
|
2012-08-11 05:06:51 +00:00
|
|
|
resp = make_response(render_template(s, **kwargs), 200)
|
|
|
|
if cacheable:
|
|
|
|
resp.headers["Cache-control"] = "public, max-age=4320"
|
|
|
|
return resp
|
2012-07-24 10:27:04 +00:00
|
|
|
|
|
|
|
|
2012-06-21 04:25:27 +00:00
|
|
|
@app.route('/')
|
|
|
|
@app.route('/index.html')
|
|
|
|
def index():
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("index.html", True, section="main")
|
2012-07-25 00:39:51 +00:00
|
|
|
|
2012-07-24 23:55:44 +00:00
|
|
|
|
2012-08-23 00:54:58 +00:00
|
|
|
@app.route('/download')
|
|
|
|
@app.route('/download.html')
|
|
|
|
def download():
|
|
|
|
return render("download.html", True, section="download")
|
|
|
|
|
|
|
|
|
2012-07-24 23:55:44 +00:00
|
|
|
@app.route('/about')
|
|
|
|
@app.route('/about.html')
|
|
|
|
def about():
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("about.html", True, section="about")
|
2012-06-21 04:54:49 +00:00
|
|
|
|
|
|
|
|
2012-06-28 23:53:59 +00:00
|
|
|
@app.route('/docs/pathod')
|
|
|
|
def docs_pathod():
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("docs_pathod.html", True, section="docs")
|
2012-06-28 23:53:59 +00:00
|
|
|
|
|
|
|
|
2012-07-22 10:24:16 +00:00
|
|
|
@app.route('/docs/language')
|
|
|
|
def docs_language():
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("docs_lang.html", True, section="docs")
|
2012-07-22 10:24:16 +00:00
|
|
|
|
|
|
|
|
2012-06-28 23:53:59 +00:00
|
|
|
@app.route('/docs/pathoc')
|
|
|
|
def docs_pathoc():
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("docs_pathoc.html", True, section="docs")
|
2012-06-28 23:53:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/docs/test')
|
|
|
|
def docs_test():
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("docs_test.html", True, section="docs")
|
2012-06-23 23:14:54 +00:00
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
|
|
|
|
@app.route('/log')
|
|
|
|
def log():
|
2012-07-24 10:27:04 +00:00
|
|
|
if app.config["pathod"].noapi:
|
|
|
|
abort(404)
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("log.html", False, section="log", log=app.config["pathod"].get_log())
|
2012-06-23 23:14:54 +00:00
|
|
|
|
2012-06-21 04:25:27 +00:00
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
@app.route('/log/<int:lid>')
|
|
|
|
def onelog(lid):
|
2012-07-23 04:18:47 +00:00
|
|
|
item = app.config["pathod"].log_by_id(int(lid))
|
|
|
|
if not item:
|
|
|
|
abort(404)
|
|
|
|
l = pprint.pformat(item)
|
2012-08-11 05:06:51 +00:00
|
|
|
return render("onelog.html", False, section="log", alog=l, lid=lid)
|
2012-06-21 04:25:27 +00:00
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
|
2012-07-24 12:16:24 +00:00
|
|
|
def _preview(is_request):
|
|
|
|
if is_request:
|
|
|
|
template = "request_preview.html"
|
|
|
|
else:
|
|
|
|
template = "response_preview.html"
|
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
spec = request.args["spec"]
|
2012-07-24 22:53:29 +00:00
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
args = dict(
|
|
|
|
spec = spec,
|
|
|
|
section = "main",
|
|
|
|
syntaxerror = None,
|
2012-07-25 00:49:22 +00:00
|
|
|
error = None,
|
|
|
|
pauses = None
|
2012-06-21 04:54:49 +00:00
|
|
|
)
|
2012-07-24 22:53:29 +00:00
|
|
|
if not spec.strip():
|
|
|
|
args["error"] = "Can't parse an empty spec."
|
2012-08-11 05:06:51 +00:00
|
|
|
return render(template, False, **args)
|
2012-07-24 22:53:29 +00:00
|
|
|
|
2012-06-21 04:54:49 +00:00
|
|
|
try:
|
2012-07-24 12:16:24 +00:00
|
|
|
if is_request:
|
|
|
|
r = rparse.parse_request(app.config["pathod"].request_settings, spec)
|
|
|
|
else:
|
|
|
|
r = rparse.parse_response(app.config["pathod"].request_settings, spec)
|
2012-06-21 04:54:49 +00:00
|
|
|
except rparse.ParseException, v:
|
|
|
|
args["syntaxerror"] = str(v)
|
|
|
|
args["marked"] = v.marked()
|
2012-08-11 05:06:51 +00:00
|
|
|
return render(template, False, **args)
|
2012-07-24 12:16:24 +00:00
|
|
|
except rparse.FileAccessDenied:
|
|
|
|
args["error"] = "File access is disabled."
|
2012-08-11 05:06:51 +00:00
|
|
|
return render(template, False, **args)
|
2012-07-23 03:38:06 +00:00
|
|
|
|
|
|
|
s = cStringIO.StringIO()
|
2012-07-25 00:49:22 +00:00
|
|
|
args["pauses"] = r.preview_safe()
|
2012-07-24 12:16:24 +00:00
|
|
|
|
|
|
|
if is_request:
|
2012-07-26 08:01:51 +00:00
|
|
|
r.serve(s, check=app.config["pathod"].check_policy, host="example.com")
|
2012-07-24 12:16:24 +00:00
|
|
|
else:
|
2012-07-26 08:01:51 +00:00
|
|
|
r.serve(s, check=app.config["pathod"].check_policy)
|
2012-07-24 12:16:24 +00:00
|
|
|
|
2012-07-23 04:39:25 +00:00
|
|
|
args["output"] = utils.escape_unprintables(s.getvalue())
|
2012-08-11 05:06:51 +00:00
|
|
|
return render(template, False, **args)
|
2012-07-24 11:13:04 +00:00
|
|
|
|
|
|
|
|
2012-07-24 12:16:24 +00:00
|
|
|
@app.route('/response_preview')
|
|
|
|
def response_preview():
|
|
|
|
return _preview(False)
|
2012-07-24 22:34:57 +00:00
|
|
|
|
2012-07-24 12:16:24 +00:00
|
|
|
|
2012-07-24 11:13:04 +00:00
|
|
|
@app.route('/request_preview')
|
|
|
|
def request_preview():
|
2012-07-24 12:16:24 +00:00
|
|
|
return _preview(True)
|
2012-07-24 11:13:04 +00:00
|
|
|
|