2014-11-14 23:41:44 +00:00
|
|
|
import logging
|
|
|
|
import pprint
|
|
|
|
import cStringIO
|
2015-04-22 03:49:17 +00:00
|
|
|
import copy
|
2012-08-11 05:06:51 +00:00
|
|
|
from flask import Flask, jsonify, render_template, request, abort, make_response
|
2015-04-22 04:38:54 +00:00
|
|
|
from . import version, language, utils
|
2015-07-15 20:04:25 +00:00
|
|
|
from netlib.http import user_agents
|
2012-06-21 04:25:27 +00:00
|
|
|
|
|
|
|
logging.basicConfig(level="DEBUG")
|
2015-04-22 19:29:55 +00:00
|
|
|
EXAMPLE_HOST = "example.com"
|
|
|
|
EXAMPLE_WEBSOCKET_KEY = "examplekey"
|
2014-10-25 03:20:23 +00:00
|
|
|
|
2015-06-18 16:05:09 +00:00
|
|
|
# pylint: disable=unused-variable
|
2014-10-25 03:20:23 +00:00
|
|
|
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2015-04-22 03:58:25 +00:00
|
|
|
def make_app(noapi, debug):
|
2013-02-24 06:42:40 +00:00
|
|
|
app = Flask(__name__)
|
2015-04-22 03:58:25 +00:00
|
|
|
app.debug = debug
|
2012-06-19 04:57:57 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
if not noapi:
|
|
|
|
@app.route('/api/info')
|
|
|
|
def api_info():
|
|
|
|
return jsonify(
|
2015-06-18 16:12:11 +00:00
|
|
|
version=version.IVERSION
|
2013-02-24 06:42:40 +00:00
|
|
|
)
|
2012-06-19 04:57:57 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/api/log')
|
|
|
|
def api_log():
|
|
|
|
return jsonify(
|
2015-06-18 16:12:11 +00:00
|
|
|
log=app.config["pathod"].get_log()
|
2013-02-24 06:42:40 +00:00
|
|
|
)
|
2012-06-19 01:23:07 +00:00
|
|
|
|
2013-02-24 06:42:40 +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
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
def render(s, cacheable, **kwargs):
|
|
|
|
kwargs["noapi"] = app.config["pathod"].noapi
|
|
|
|
kwargs["nocraft"] = app.config["pathod"].nocraft
|
|
|
|
kwargs["craftanchor"] = app.config["pathod"].craftanchor
|
|
|
|
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
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/')
|
|
|
|
@app.route('/index.html')
|
|
|
|
def index():
|
2014-11-14 23:41:44 +00:00
|
|
|
return render(
|
|
|
|
"index.html",
|
|
|
|
True,
|
|
|
|
section="main",
|
|
|
|
version=version.VERSION
|
|
|
|
)
|
2012-07-24 23:55:44 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/download')
|
|
|
|
@app.route('/download.html')
|
|
|
|
def download():
|
2015-04-20 03:42:33 +00:00
|
|
|
return render(
|
|
|
|
"download.html", True, section="download", version=version.VERSION
|
|
|
|
)
|
2012-08-23 00:54:58 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/about')
|
|
|
|
@app.route('/about.html')
|
|
|
|
def about():
|
|
|
|
return render("about.html", True, section="about")
|
2012-06-21 04:54:49 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/docs/pathod')
|
|
|
|
def docs_pathod():
|
2015-04-20 03:42:33 +00:00
|
|
|
return render(
|
|
|
|
"docs_pathod.html", True, section="docs", subsection="pathod"
|
|
|
|
)
|
2012-06-28 23:53:59 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/docs/language')
|
|
|
|
def docs_language():
|
2013-05-01 21:11:16 +00:00
|
|
|
return render(
|
|
|
|
"docs_lang.html", True,
|
2015-07-15 20:04:25 +00:00
|
|
|
section="docs", uastrings=user_agents.UASTRINGS,
|
2013-05-01 21:11:16 +00:00
|
|
|
subsection="lang"
|
|
|
|
)
|
2012-07-22 10:24:16 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/docs/pathoc')
|
|
|
|
def docs_pathoc():
|
2015-04-20 03:42:33 +00:00
|
|
|
return render(
|
|
|
|
"docs_pathoc.html", True, section="docs", subsection="pathoc"
|
|
|
|
)
|
2012-06-28 23:53:59 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/docs/libpathod')
|
|
|
|
def docs_libpathod():
|
2015-04-20 03:42:33 +00:00
|
|
|
return render(
|
|
|
|
"docs_libpathod.html", True, section="docs", subsection="libpathod"
|
|
|
|
)
|
2012-10-03 23:17:35 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/docs/test')
|
|
|
|
def docs_test():
|
2015-04-20 03:42:33 +00:00
|
|
|
return render(
|
|
|
|
"docs_test.html", True, section="docs", subsection="test"
|
|
|
|
)
|
2012-06-21 04:54:49 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/log')
|
|
|
|
def log():
|
|
|
|
if app.config["pathod"].noapi:
|
|
|
|
abort(404)
|
2015-04-20 03:42:33 +00:00
|
|
|
return render(
|
|
|
|
"log.html",
|
|
|
|
False,
|
|
|
|
section="log",
|
|
|
|
log=app.config["pathod"].get_log()
|
|
|
|
)
|
2012-06-21 04:25:27 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/log/<int:lid>')
|
|
|
|
def onelog(lid):
|
|
|
|
item = app.config["pathod"].log_by_id(int(lid))
|
|
|
|
if not item:
|
|
|
|
abort(404)
|
|
|
|
l = pprint.pformat(item)
|
|
|
|
return render("onelog.html", False, section="log", alog=l, lid=lid)
|
2012-06-21 04:54:49 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
def _preview(is_request):
|
|
|
|
if is_request:
|
|
|
|
template = "request_preview.html"
|
|
|
|
else:
|
|
|
|
template = "response_preview.html"
|
2012-07-24 22:53:29 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
spec = request.args["spec"]
|
2012-07-24 22:53:29 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
args = dict(
|
2015-06-18 16:12:11 +00:00
|
|
|
spec=spec,
|
|
|
|
section="main",
|
|
|
|
syntaxerror=None,
|
|
|
|
error=None,
|
2013-02-24 06:42:40 +00:00
|
|
|
)
|
|
|
|
if not spec.strip():
|
|
|
|
args["error"] = "Can't parse an empty spec."
|
|
|
|
return render(template, False, **args)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if is_request:
|
2015-06-07 01:18:33 +00:00
|
|
|
r = language.parse_pathoc(spec).next()
|
2013-02-24 06:42:40 +00:00
|
|
|
else:
|
2015-06-07 04:11:32 +00:00
|
|
|
r = language.parse_pathod(spec).next()
|
2015-05-30 00:03:13 +00:00
|
|
|
except language.ParseException as v:
|
2013-02-24 06:42:40 +00:00
|
|
|
args["syntaxerror"] = str(v)
|
|
|
|
args["marked"] = v.marked()
|
|
|
|
return render(template, False, **args)
|
|
|
|
|
|
|
|
s = cStringIO.StringIO()
|
2015-04-22 19:29:55 +00:00
|
|
|
|
2015-06-18 16:05:09 +00:00
|
|
|
settings = copy.copy(app.config["pathod"].settings)
|
|
|
|
settings.request_host = EXAMPLE_HOST
|
|
|
|
settings.websocket_key = EXAMPLE_WEBSOCKET_KEY
|
2015-04-22 19:29:55 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
safe = r.preview_safe()
|
2015-04-22 04:38:54 +00:00
|
|
|
err, safe = app.config["pathod"].check_policy(
|
2015-04-20 03:42:33 +00:00
|
|
|
safe,
|
2015-06-18 16:05:09 +00:00
|
|
|
settings
|
2015-04-20 03:42:33 +00:00
|
|
|
)
|
2015-04-22 04:38:54 +00:00
|
|
|
if err:
|
|
|
|
args["error"] = err
|
2013-02-24 06:42:40 +00:00
|
|
|
return render(template, False, **args)
|
2012-07-24 12:16:24 +00:00
|
|
|
if is_request:
|
2015-06-18 16:05:09 +00:00
|
|
|
settings.request_host = EXAMPLE_HOST
|
|
|
|
language.serve(safe, s, settings)
|
2012-07-24 12:16:24 +00:00
|
|
|
else:
|
2015-06-18 16:05:09 +00:00
|
|
|
settings.websocket_key = EXAMPLE_WEBSOCKET_KEY
|
|
|
|
language.serve(safe, s, settings)
|
2012-10-27 20:06:55 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
args["output"] = utils.escape_unprintables(s.getvalue())
|
2012-10-27 04:40:22 +00:00
|
|
|
return render(template, False, **args)
|
2012-07-24 11:13:04 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/response_preview')
|
|
|
|
def response_preview():
|
|
|
|
return _preview(False)
|
2012-07-24 22:34:57 +00:00
|
|
|
|
2013-02-24 06:42:40 +00:00
|
|
|
@app.route('/request_preview')
|
|
|
|
def request_preview():
|
|
|
|
return _preview(True)
|
|
|
|
return app
|