-
Notifications
You must be signed in to change notification settings - Fork 8
/
BlocklyPropHTTPRequestHandler.py
137 lines (106 loc) · 3.78 KB
/
BlocklyPropHTTPRequestHandler.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
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
__author__ = 'Michel'
"""Simple HTTP Server.
This module builds on BaseHTTPServer by implementing the standard GET
and HEAD requests in a fairly straightforward manner.
"""
__version__ = "0.1"
__all__ = ["BlocklyPropHTTPRequestHandler"]
import os
import posixpath
import BaseHTTPServer
import urllib
import cgi
import shutil
import json
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from cgi import parse_header, parse_multipart
from urlparse import parse_qs
from PropellerLoad import PropellerLoad
from SpinCompiler import SpinCompiler
from PropCCompiler import PropCCompiler
class BlocklyPropHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Simple HTTP request handler with GET and HEAD commands.
The GET and HEAD requests are identical except that the HEAD
request omits the actual contents of the file.
"""
server_version = "BlocklyPropHTTP/" + __version__
def do_GET(self):
"""Serve a GET request."""
path = self.path
print("GET:", path)
f = StringIO()
if path == "/ports.json":
global propellerLoad
ports = propellerLoad.get_ports()
f.write(ports)
elif path == "/serverinfo.json":
serverinfo = {
"server":"BlocklyPropHTTP",
"version":__version__
}
f.write(json.dumps(serverinfo))
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-type", "text/json")
self.send_header("Content-Length", str(length))
self.end_headers()
if f:
self.copyfile(f, self.wfile)
f.close()
def do_POST(self):
"""Serve a POST request."""
postvars = self.parse_POST()
action = postvars.get("action")[0]
language = postvars.get("language")[0]
code = postvars.get("code")[0]
com_port = postvars.get("com-port")
if com_port is not None:
com_port = com_port[0]
global compiler
result = compiler[language].handle(action, code, com_port)
f = StringIO()
f.write(json.dumps(result))
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-type", "text/json")
self.send_header("Content-Length", str(length))
self.end_headers()
if f:
self.copyfile(f, self.wfile)
f.close()
def copyfile(self, source, outputfile):
"""Copy all data between two file objects.
The SOURCE argument is a file object open for reading
(or anything with a read() method) and the DESTINATION
argument is a file object open for writing (or
anything with a write() method).
The only reason for overriding this would be to change
the block size or perhaps to replace newlines by CRLF
-- note however that this the default server uses this
to copy binary data as well.
"""
shutil.copyfileobj(source, outputfile)
def parse_POST(self):
ctype, pdict = parse_header(self.headers['content-type'])
if ctype == 'multipart/form-data':
postvars = parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(
self.rfile.read(length),
keep_blank_values=1)
else:
postvars = {}
return postvars
propellerLoad = PropellerLoad()
compiler = {
"spin" : SpinCompiler(propellerLoad),
"prop-c": PropCCompiler(propellerLoad)
}