/
web.py
93 lines (82 loc) · 3.9 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
from functools import wraps
import logging
import curio
import h11
from . import _request_local, log, Request, Router, respond, response_to_bytes
from .transport import CurioHTTPTransport
TIMEOUT = 10
class Web:
def __init__(self, name):
self.name = name
self.router = Router()
def run(self, host, port):
kernel = curio.Kernel()
logging.info(f"Listening on http://{host}:{port}")
kernel.run(curio.tcp_server(host, port, self.http_serve))
# TODO modify return annotations to include status code to make mypy happy
def route(self, path, **rules):
def decorator(handler):
logging.debug(f"building route {path} -> {handler.__name__}")
rules["path"] = path
self.router.add(rules, handler)
return handler
return decorator
# err_code doesn't really make sense as this is used
def error(self, status_code):
def decorator(handler):
logging.debug(f"building error handler for {status_code} -> {handler.__name__}")
@wraps(handler)
def wrapper(*args, **kwargs):
log.info("trying to send error response...")
if _request_local.transport.conn.our_state not in {h11.IDLE, h11.SEND_RESPONSE}:
log.info(f"...but I can't, because our state is {_request_local.transport.conn.our_state}")
return
try:
return handler(*args, **kwargs)
except Exception as exc:
log.info(f"error while sending error response: {exc}")
self.router.add_error(status_code, wrapper)
return wrapper
return decorator
async def http_serve(self, sock, addr):
transport = CurioHTTPTransport(sock)
_request_local.transport = transport
_request_local.end_of_message = False
while True:
assert transport.conn.states == {
h11.CLIENT: h11.IDLE, h11.SERVER: h11.IDLE}
try:
async with curio.timeout_after(TIMEOUT):
log.info("Server main loop waiting for request")
event = await transport.next_event()
log.info(f"Server main loop got event: {event}")
if type(event) is h11.Request:
request = Request(event)
handler = self.router.match(request)
status, response = await handler(request)
content_type, response = response_to_bytes(handler, response)
await respond(status, content_type, response)
except Exception as exc:
log.info(f"Error during response handler: {exc}")
handler = self.router.match_error(exc)
status, response = await handler(exc)
content_type, response = response_to_bytes(handler, response)
await respond(status, content_type, response)
if transport.conn.our_state is h11.MUST_CLOSE:
log.info("connection is not reusable, so shutting down")
await transport.shutdown_and_clean_up()
return
else:
try:
log.info("trying to re-use connection")
transport.conn.start_next_cycle()
except h11.ProtocolError:
states = transport.conn.states
log.info(f"unexpected state {states} -- bailing out")
exc = RuntimeError("unexpected state {}".format(states))
handler = self.router.match_error(exc)
status, response = await handler(exc)
content_type, response = response_to_bytes(handler, response)
await respond(status, content_type, response)
await transport.shutdown_and_clean_up()
return