-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflask_util.py
More file actions
62 lines (51 loc) · 1.75 KB
/
flask_util.py
File metadata and controls
62 lines (51 loc) · 1.75 KB
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
# this file has wrappers around flask's "request" object
# for invocations that are not _immediately_ within the route
#
# this is to be able to better track magic dataflow
class FLASK_UTIL:
@staticmethod
def CURRENT_URL():
return request.path
def CURRENT_NOTE():
return FLAT.try_from_url(FLASK_UTIL.CURRENT_URL())
@staticmethod
def REFERER_NOTE():
view, note = FLAT.try_from_url(request.environ['HTTP_REFERER'])
return view, note
@staticmethod
def TELEMETRY():
return f"""<script>
fetch("https://{request.headers['Host']}/receive_info",
{{ method: "POST",
headers: {{ 'Content-Type': 'application/json'}},
body: JSON.stringify({{width: window.screen.width, height: window.screen.height, dpr: window.devicePixelRatio }})
}}
)
</script>"""
@staticmethod
def URL_ROOT():
return request.url_root # like http://192.37.37.3:5000/ or https://10.50.50.2:5000
@staticmethod
def HOST():
return request.headers["Host"] # like 192.37.37.3:5000
@staticmethod
def ESCAPE(s):
return str(escape(s))
@app.route("/site-map")
def site_map():
from flask import url_for
raw_endpoints = list()
note_views = list()
unmatched = list()
for rule in app.url_map.iter_rules():
# Filter out rules we can't navigate to in a browser
# and rules that require parameters
note_arg = {'note'}
if "GET" in rule.methods:
if len(rule.arguments) == 0:
raw_endpoints.append(repr(rule))
elif len(rule.arguments) == 1 and list(rule.arguments)[0] == 'note':
note_views.append(repr(rule))
else:
unmatched.append((repr(rule), repr(rule.arguments)))
return {'raw': list(reversed(raw_endpoints)), "note": note_views, "unmatched": unmatched}