forked from rust-lang/rust-playpen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
executable file
·107 lines (88 loc) · 3.87 KB
/
web.py
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
#!/usr/bin/env python3
import functools
import os
import sys
from bottle import get, request, response, route, run, static_file
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import GasLexer, LlvmLexer
import playpen
@get("/")
def serve_index():
response = static_file("web.html", root="static")
# XSS protection is a misfeature unleashed upon the world by Internet
# Explorer 8. It uses ill conceived heuristics to block or mangle HTTP
# requests in an attempt to prevent cross-site scripting exploits. It's yet
# another idea from the "enumerating badness" school of security theater.
#
# Rust and JavaScript are both languages using a C style syntax, and GET
# queries containing Rust snippets end up being classified as cross-site
# scripting attacks. Luckily, there's a header for turning off this bug.
response.set_header("X-XSS-Protection", "0")
return response
@get("/<path:path>")
def serve_static(path):
return static_file(path, root="static")
@functools.lru_cache(maxsize=256)
def execute(version, command, arguments, code):
print("running:", version, command, arguments, file=sys.stderr, flush=True)
return playpen.execute(version, command, arguments, code)
def enable_post_cors(wrappee):
def wrapper(*args, **kwargs):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "POST, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "Origin, Accept, Content-Type"
if request.method != "OPTIONS":
return wrappee(*args, **kwargs)
return wrapper
def extractor(key, default, valid):
def decorator(wrappee):
def wrapper(*args, **kwargs):
value = request.json.get(key, default)
if value not in valid:
return {"error": "invalid value for {}".format(key)}
return wrappee(value, *args, **kwargs)
return wrapper
return decorator
@route("/evaluate.json", method=["POST", "OPTIONS"])
@enable_post_cors
@extractor("version", "stable", ("stable", "beta", "nightly"))
@extractor("optimize", "2", ("0", "1", "2", "3"))
def evaluate(optimize, version):
out, _ = execute(version, "/usr/local/bin/evaluate.sh", (optimize,), request.json["code"])
if request.json.get("separate_output") is True:
split = out.split(b"\xff", 1)
ret = {"rustc": split[0].decode()}
if len(split) == 2: # compilation succeeded
ret["program"] = split[1].decode(errors="replace")
return ret
else:
return {"result": out.replace(b"\xff", b"", 1).decode(errors="replace")}
@route("/format.json", method=["POST", "OPTIONS"])
@enable_post_cors
@extractor("version", "stable", ("stable", "beta", "nightly"))
def format(version):
out, rc = execute(version, "/usr/local/bin/format.sh", (), request.json["code"])
split = out.split(b"\xff", 1)
if rc:
return {"error": split[0].decode()}
else:
return {"result": split[1][:-1].decode()}
@route("/compile.json", method=["POST", "OPTIONS"])
@enable_post_cors
@extractor("version", "stable", ("stable", "beta", "nightly"))
@extractor("optimize", "2", ("0", "1", "2", "3"))
@extractor("emit", "asm", ("asm", "llvm-ir"))
def compile(emit, optimize, version):
out, rc = execute(version, "/usr/local/bin/compile.sh", (optimize, emit), request.json["code"])
split = out.split(b"\xff", 1)
if rc:
return {"error": split[0].decode()}
else:
if request.json.get("highlight") is not True:
return {"result": split[1].decode()}
if emit == "asm":
return {"result": highlight(split[1].decode(), GasLexer(), HtmlFormatter(nowrap=True))}
return {"result": highlight(split[1].decode(), LlvmLexer(), HtmlFormatter(nowrap=True))}
os.chdir(sys.path[0])
run(host='0.0.0.0', port=80, server='cherrypy')