1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
import logging
import pprint
import cStringIO
import copy
from flask import Flask, jsonify, render_template, request, abort, make_response
from . import version, language, utils
from netlib.http import user_agents
logging.basicConfig(level="DEBUG")
EXAMPLE_HOST = "example.com"
EXAMPLE_WEBSOCKET_KEY = "examplekey"
# pylint: disable=unused-variable
def make_app(noapi, debug):
app = Flask(__name__)
app.debug = debug
if not noapi:
@app.route('/api/info')
def api_info():
return jsonify(
version=version.IVERSION
)
@app.route('/api/log')
def api_log():
return jsonify(
log=app.config["pathod"].get_log()
)
@app.route('/api/clear_log')
def api_clear_log():
app.config["pathod"].clear_log()
return "OK"
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
@app.route('/')
@app.route('/index.html')
def index():
return render(
"index.html",
True,
section="main",
version=version.VERSION
)
@app.route('/download')
@app.route('/download.html')
def download():
return render(
"download.html", True, section="download", version=version.VERSION
)
@app.route('/about')
@app.route('/about.html')
def about():
return render("about.html", True, section="about")
@app.route('/docs/pathod')
def docs_pathod():
return render(
"docs_pathod.html", True, section="docs", subsection="pathod"
)
@app.route('/docs/language')
def docs_language():
return render(
"docs_lang.html", True,
section="docs", uastrings=user_agents.UASTRINGS,
subsection="lang"
)
@app.route('/docs/pathoc')
def docs_pathoc():
return render(
"docs_pathoc.html", True, section="docs", subsection="pathoc"
)
@app.route('/docs/lib_pathod')
def docs_lib_pathod():
return render(
"docs_lib_pathod.html", True, section="docs", subsection="pathod"
)
@app.route('/docs/test')
def docs_test():
return render(
"docs_test.html", True, section="docs", subsection="test"
)
@app.route('/log')
def log():
if app.config["pathod"].noapi:
abort(404)
return render(
"log.html",
False,
section="log",
log=app.config["pathod"].get_log()
)
@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)
def _preview(is_request):
if is_request:
template = "request_preview.html"
else:
template = "response_preview.html"
spec = request.args["spec"]
args = dict(
spec=spec,
section="main",
syntaxerror=None,
error=None,
)
if not spec.strip():
args["error"] = "Can't parse an empty spec."
return render(template, False, **args)
try:
if is_request:
r = language.parse_pathoc(spec).next()
else:
r = language.parse_pathod(spec).next()
except language.ParseException as v:
args["syntaxerror"] = str(v)
args["marked"] = v.marked()
return render(template, False, **args)
s = cStringIO.StringIO()
settings = copy.copy(app.config["pathod"].settings)
settings.request_host = EXAMPLE_HOST
settings.websocket_key = EXAMPLE_WEBSOCKET_KEY
safe = r.preview_safe()
err, safe = app.config["pathod"].check_policy(
safe,
settings
)
if err:
args["error"] = err
return render(template, False, **args)
if is_request:
settings.request_host = EXAMPLE_HOST
language.serve(safe, s, settings)
else:
settings.websocket_key = EXAMPLE_WEBSOCKET_KEY
language.serve(safe, s, settings)
args["output"] = utils.escape_unprintables(s.getvalue())
return render(template, False, **args)
@app.route('/response_preview')
def response_preview():
return _preview(False)
@app.route('/request_preview')
def request_preview():
return _preview(True)
return app
|