-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp3_server.py
More file actions
284 lines (250 loc) · 11.1 KB
/
Copy pathhttp3_server.py
File metadata and controls
284 lines (250 loc) · 11.1 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
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
# http3_server_min.py
import argparse, asyncio, importlib, logging, time
from email.utils import formatdate
from collections import deque
from typing import Callable, Dict, Optional, Deque
from aioquic.asyncio import QuicConnectionProtocol, serve
from aioquic.h3.connection import H3_ALPN, H3Connection
from aioquic.h3.events import DataReceived, HeadersReceived, H3Event, DatagramReceived, WebTransportStreamDataReceived
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.events import ProtocolNegotiated, QuicEvent, DatagramFrameReceived
AsgiApplication = Callable
SERVER_NAME = "aioquic-min"
class HttpRequestHandler:
def __init__(self, *, connection: H3Connection, scope: Dict, stream_id: int, transmit: Callable[[], None]) -> None:
self.connection = connection
self.scope = scope
self.stream_id = stream_id
self.transmit = transmit
self._queue: asyncio.Queue[Dict] = asyncio.Queue()
def http_event_received(self, event: H3Event) -> None:
if isinstance(event, DataReceived):
self._queue.put_nowait({
"type": "http.request",
"body": event.data,
"more_body": not event.stream_ended,
})
elif isinstance(event, HeadersReceived) and event.stream_ended:
self._queue.put_nowait({"type": "http.request", "body": b"", "more_body": False})
async def run_asgi(self, app: AsgiApplication) -> None:
await app(self.scope, self.receive, self.send, self.connection)
async def receive(self) -> Dict:
return await self._queue.get()
async def send(self, message: Dict) -> None:
if message["type"] == "http.response.start":
headers = [
(b":status", str(message["status"]).encode()),
(b"server", SERVER_NAME.encode()),
(b"date", formatdate(time.time(), usegmt=True).encode()),
] + [(k, v) for k, v in message["headers"]]
self.connection.send_headers(stream_id=self.stream_id, headers=headers)
elif message["type"] == "http.response.body":
self.connection.send_data(
stream_id=self.stream_id,
data=message.get("body", b""),
end_stream=not message.get("more_body", False),
)
self.transmit()
class WebTransportHandler:
def __init__(
self,
*,
connection: H3Connection,
scope: dict,
stream_id: int,
transmit: Callable[[], None],
) -> None:
self.accepted = False
self.closed = False
self.connection = connection
self.http_event_queue: Deque[H3Event] = deque()
self.queue: asyncio.Queue[dict] = asyncio.Queue()
self.scope = scope
self.stream_id = stream_id
self.transmit = transmit
def http_event_received(self, event: H3Event) -> None:
if not self.closed:
if self.accepted:
if isinstance(event, DatagramReceived):
self.queue.put_nowait(
{
"data": event.data,
"type": "webtransport.datagram.receive",
}
)
elif isinstance(event, WebTransportStreamDataReceived):
self.queue.put_nowait(
{
"data": event.data,
"stream": event.stream_id,
"type": "webtransport.stream.receive",
}
)
else:
# delay event processing until we get `webtransport.accept`
# from the ASGI application
self.http_event_queue.append(event)
async def run_asgi(self, app: AsgiApplication) -> None:
self.queue.put_nowait({"type": "webtransport.connect"})
try:
await app(self.scope, self.receive, self.send, self.connection)
finally:
if not self.closed:
await self.send({"type": "webtransport.close"})
async def receive(self) -> dict:
return await self.queue.get()
async def send(self, message: dict) -> None:
data = b""
end_stream = False
if message["type"] == "webtransport.accept":
self.accepted = True
headers = [
(b":status", b"200"),
(b"server", SERVER_NAME.encode()),
(b"date", formatdate(time.time(), usegmt=True).encode()),
(b"sec-webtransport-http3-draft", b"draft02"),
]
self.connection.send_headers(stream_id=self.stream_id, headers=headers)
# consume backlog
while self.http_event_queue:
self.http_event_received(self.http_event_queue.popleft())
elif message["type"] == "webtransport.close":
if not self.accepted:
self.connection.send_headers(
stream_id=self.stream_id, headers=[(b":status", b"403")]
)
end_stream = True
elif message["type"] == "webtransport.datagram.send":
if len(self.connection._quic._datagrams_pending) < 100:
self.connection.send_datagram(
stream_id=self.stream_id, data=message["data"]
)
else:
logging.warning("Dropping datagram because of congestion. Current pending datagrams: %d", len(self.connection._quic._datagrams_pending))
elif message["type"] == "webtransport.stream.send":
self.connection._quic.send_stream_data(
stream_id=message["stream"], data=message["data"]
)
if data or end_stream:
self.connection.send_data(
stream_id=self.stream_id, data=data, end_stream=end_stream
)
if end_stream:
self.closed = True
self.transmit()
class Http3Protocol(QuicConnectionProtocol):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._http: Optional[H3Connection] = None
self._handlers: Dict = {}
def http_event_received(self, event: H3Event) -> None:
if isinstance(event, HeadersReceived) and event.stream_id not in self._handlers:
# Build ASGI scope
headers = []
method = "GET"
raw_path = b"/"
protocol = None
for k, v in event.headers:
if k == b":method":
method = v.decode()
elif k == b":path":
raw_path = v
elif k == b":protocol":
protocol = v.decode()
elif k and not k.startswith(b":"):
headers.append((k, v))
if b"?" in raw_path:
path_bytes, query = raw_path.split(b"?", 1)
else:
path_bytes, query = raw_path, b""
# Peer address
client_addr = self._http._quic._network_paths[0].addr # type: ignore
client = (client_addr[0], client_addr[1])
handler = None
if method == "CONNECT" and protocol == "webtransport":
assert isinstance(self._http, H3Connection), (
"WebTransport is only supported over HTTP/3"
)
scope = {
"client": client,
"headers": headers,
"http_version": "3",
"method": method,
"path": path_bytes.decode(),
"query_string": query,
"raw_path": raw_path,
"root_path": "",
"scheme": "https",
"type": "webtransport",
}
handler = WebTransportHandler(
connection=self._http,
scope=scope,
stream_id=event.stream_id,
transmit=self.transmit,
)
else:
scope = {
"type": "http",
"asgi": {"version": "3.0"},
"http_version": "3",
"method": method,
"scheme": "https",
"path": path_bytes.decode(),
"raw_path": raw_path,
"query_string": query,
"headers": headers,
"client": client,
"server": None,
}
handler = HttpRequestHandler(
connection=self._http, scope=scope, stream_id=event.stream_id, transmit=self.transmit
)
self._handlers[event.stream_id] = handler
asyncio.create_task(handler.run_asgi(application))
elif isinstance(event, (HeadersReceived, DataReceived)) and event.stream_id in self._handlers:
self._handlers[event.stream_id].http_event_received(event)
elif isinstance(event, WebTransportStreamDataReceived):
self._handlers[event.session_id].http_event_received(event)
elif isinstance(event, DatagramReceived):
self._handlers[event.stream_id].http_event_received(event)
def quic_event_received(self, event: QuicEvent) -> None:
if isinstance(event, ProtocolNegotiated):
if event.alpn_protocol in H3_ALPN:
self._http = H3Connection(self._quic, enable_webtransport=True)
elif isinstance(event, DatagramFrameReceived):
pass # TODO: see what this can do
if self._http is not None:
for http_event in self._http.handle_event(event):
self.http_event_received(http_event)
async def main(host: str, port: int, configuration: QuicConfiguration) -> None:
await serve(host, port, configuration=configuration, create_protocol=Http3Protocol)
await asyncio.Future()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Minimal HTTP/3 QUIC server for ASGI apps")
parser.add_argument("app", type=str, nargs="?", default="gs_app:app",
help="ASGI application as <module>:<attribute>")
parser.add_argument("-c", "--certificate", type=str, required=True)
parser.add_argument("-k", "--private-key", type=str, required=True)
parser.add_argument("--host", type=str, default="::")
parser.add_argument("--port", type=int, default=4433)
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s %(message)s",
level=logging.DEBUG if args.verbose else logging.INFO,
)
module_str, attr_str = args.app.split(":", 1)
module = importlib.import_module(module_str)
application = getattr(module, attr_str)
config = QuicConfiguration(
alpn_protocols=H3_ALPN + ["siduck"],
is_client=False,
max_datagram_frame_size=65536,
max_datagram_size=1400, # safe default for many paths
)
config.load_cert_chain(args.certificate, args.private_key)
try:
asyncio.run(main(args.host, args.port, config))
except KeyboardInterrupt:
pass