-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppwhttp.py
334 lines (242 loc) · 8.87 KB
/
ppwhttp.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""Pimoroni Pico Wireless HTTP
A super-simple HTTP server library for Pico Wireless.
"""
import time
import picowireless
from micropython import const
try:
from secrets import WIFI_SSID, WIFI_PASS
except ImportError:
WIFI_SSID = None
WIFI_PASS = None
TCP_CLOSED = const(0)
TCP_LISTEN = const(1)
TCP_MODE = const(0)
UDP_MODE = const(1)
TLS_MODE = const(2)
UDP_MULTICAST_MODE = const(3)
TLS_BEARSSL_MODE = const(4)
TCP_STATE_CLOSED = const(0)
TCP_STATE_LISTEN = const(1)
TCP_STATE_SYN_SENT = const(2)
TCP_STATE_SVN_RCVD = const(3)
TCP_STATE_ESTABLISHED = const(4)
TCP_STATE_FIN_WAIT_1 = const(5)
TCP_STATE_FIN_WAIT_2 = const(6)
TCP_STATE_CLOSE_WAIT = const(7)
TCP_STATE_CLOSING = const(8)
TCP_STATE_LAST_ACK = const(9)
TCP_STATE_TIME_WAIT = const(10)
CLOUDFLARE_DNS = (1, 1, 1, 1)
GOOGLE_DNS = (8, 8, 8, 8)
DEFAULT_HTTP_PORT = const(80)
routes = {}
sockets = []
hosts = {}
def set_led(r, g, b):
"""Set """
picowireless.set_led(r, g, b)
def get_socket(force_new=False):
global sockets
if force_new or len(sockets) == 0:
socket = picowireless.get_socket()
sockets.append(socket)
return socket
return sockets[0]
def get_ip_address():
return picowireless.get_ip_address()
def set_dns(dns):
picowireless.set_dns(dns)
def get_host_by_name(hostname, no_cache=False):
# Already an IP
if type(hostname) is tuple and len(hostname) == 4:
return hostname
# Get from cached hosts
if hostname in hosts and not no_cache:
return hosts[hostname]
ip = picowireless.get_host_by_name(hostname)
hosts[hostname] = ip
return ip
def start_wifi(wifi_ssid=WIFI_SSID, wifi_pass=WIFI_PASS):
if wifi_ssid is None or wifi_pass is None:
raise RuntimeError("WiFi SSID/PASS required. Set them in secrets.py and copy it to your Pico, or pass them as arguments.")
picowireless.init()
print("Connecting to {}...".format(wifi_ssid))
picowireless.wifi_set_passphrase(wifi_ssid, wifi_pass)
while True:
if picowireless.get_connection_status() == 3:
break
print("Connected!")
def start_server(http_port=DEFAULT_HTTP_PORT, timeout=5000):
my_ip = picowireless.get_ip_address()
print("Starting server...")
server_sock = picowireless.get_socket()
picowireless.server_start(http_port, server_sock, 0)
t_start = time.ticks_ms()
while time.ticks_ms() - t_start < timeout:
state = picowireless.get_server_state(server_sock)
if state == TCP_LISTEN:
print("Server listening on {1}.{2}.{3}.{4}:{0}".format(http_port, *my_ip))
return server_sock
return None
def connect_to_server(host_address, port, client_sock, timeout=5000, connection_mode=TCP_MODE):
if connection_mode in (TLS_MODE, TLS_BEARSSL_MODE):
print("Connecting to {1}:{0}...".format(port, host_address))
picowireless.client_start(host_address, (0, 0, 0, 0), port, client_sock, connection_mode)
else:
host_address = get_host_by_name(host_address)
print("Connecting to {1}.{2}.{3}.{4}:{0}...".format(port, *host_address))
picowireless.client_start(host_address, port, client_sock, connection_mode)
t_start = time.ticks_ms()
while time.ticks_ms() - t_start < timeout:
state = picowireless.get_client_state(client_sock)
if state == TCP_STATE_ESTABLISHED:
print("Connected!")
return True
if state == TCP_STATE_CLOSED:
print("Connection failed!")
return False
print(state)
time.sleep(0.5)
print("Connection timeout!")
return False
def http_request(host_address, port, request_host, request_path, handler, timeout=5000, client_sock=None, connection_mode=TCP_MODE):
if client_sock is None:
client_sock = get_socket()
if not connect_to_server(host_address, port, client_sock, connection_mode=connection_mode):
return False
http_request = """GET {} HTTP/1.1
Host: {}
Connection: close
""".format(request_path, request_host).replace("\n", "\r\n")
picowireless.send_data(client_sock, http_request)
t_start = time.ticks_ms()
while True:
if time.ticks_ms() - t_start > timeout:
picowireless.client_stop(client_sock)
print("HTTP request to {}:{} timed out...".format(host_address, port))
return False
avail_length = picowireless.avail_data(client_sock)
if avail_length > 0:
break
print("Got response: {} bytes".format(avail_length))
response = b""
while len(response) < avail_length:
data = picowireless.get_data_buf(client_sock)
response += data
head, body = response.split(b"\r\n\r\n", 1)
head = head.decode("utf-8")
dhead = {}
for line in head.split("\r\n")[1:]:
key, value = line.split(": ", 1)
dhead[key] = value
encoding = "iso-8869-1"
content_type = "application/octet-stream"
if "Content-Type" in dhead:
ctype = dhead["Content-Type"].split("; ")
content_type = ctype[0].lower()
for c in ctype:
if c.startswith("encoding="):
encoding = c[9:]
# Handle JSON content type, this is prefixed with a length
# which we'll parse and use to truncate the body
if content_type == "application/json":
if not body.startswith(b"{"):
length, body = body.split(b"\r\n", 1)
length = int(length, 16)
body = body[:length]
body = body.decode(encoding)
handler(dhead, body)
picowireless.client_stop(client_sock)
def find_route(route, url, method, data):
if len(url) > 0:
for key, value in route.items():
if key == url[0]:
return find_route(route[url[0]], url[1:], method, data)
elif key.startswith("<") and key.endswith(">"):
key = key[1:-1]
if ":" in key:
dtype, key = key.split(":")
else:
dtype = "str"
if dtype == "int":
try:
data[key] = int(url[0])
except ValueError:
continue
else:
data[key] = url[0]
return find_route(value, url[1:], method, data)
return None, None
if method in route:
return route[method], data
return None, None
def handle_http_request(server_sock, timeout=5000):
t_start = time.ticks_ms()
client_sock = picowireless.avail_server(server_sock)
if client_sock in [server_sock, 255, -1]:
return False
print("Client connected!")
avail_length = picowireless.avail_data(client_sock)
if avail_length == 0:
picowireless.client_stop(client_sock)
return False
request = b""
while len(request) < avail_length:
data = picowireless.get_data_buf(client_sock)
request += data
if time.ticks_ms() - t_start > timeout:
print("Client timed out getting data!")
picowireless.client_stop(client_sock)
return False
request = request.decode("utf-8")
if len(request) > 0:
head, body = request.split("\r\n\r\n", 1)
dhead = {}
for line in head.split("\r\n")[1:]:
key, value = line.split(": ", 1)
dhead[key] = value
method, url, _ = head.split("\r\n", 1)[0].split(" ")
print("Serving {} on {}...".format(method, url))
response = None
data = {}
if url.startswith("/"):
url = url[1:]
url = url.split("/")
handler, data = find_route(routes, url, method, data)
# Dispatch the request to the relevant route
if callable(handler):
if method == "POST":
for var in body.split("&"):
key, value = var.split("=")
data[key] = value
if data == {}:
response = handler(method, url)
else:
response = handler(method, url, data)
if response is not None:
response = "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nContent-Type: text/html\r\n\r\n".format(len(response)) + response
picowireless.send_data(client_sock, response)
picowireless.client_stop(client_sock)
print("Success! Sending 200 OK")
return True
else:
picowireless.send_data(client_sock, "HTTP/1.1 501 Not Implemented\r\nContent-Length: 19\r\n\r\n501 Not Implemented")
picowireless.client_stop(client_sock)
print("Unhandled Request! Sending 501 OK")
return False
def route(url, methods="GET"):
if type(methods) is str:
methods = [methods]
if url.startswith("/"):
url = url[1:]
url = url.split("/")
def decorate(handler):
route = routes
for part in url:
if part not in route:
route[part] = {}
route = route[part]
for method in methods:
route[method] = handler
return decorate