Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
],
)
h0_client.send_data(stream_id=stream_id, data=b"", end_stream=True)
# receive request
events = h0_transfer(quic_client, h0_server)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(
events[0].headers, [(b":method", b"GET"), (b":path", b"/")]
)
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)
# send response
h0_server.send_headers(
stream_id=stream_id,
headers=[
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
],
)
h0_server.send_data(
stream_id=stream_id,
data=b"hello",
end_stream=True,
(b":scheme", b"https"),
(b":authority", b"localhost"),
(b":path", b"/app.js"),
],
push_id=1,
stream_id=stream_id,
),
HeadersReceived(
headers=[
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
],
stream_id=stream_id,
stream_ended=False,
),
DataReceived(
data=b"hello",
stream_id=stream_id,
stream_ended=True,
),
HeadersReceived(
headers=[(b":status", b"200"), (b"content-type", b"text/css")],
push_id=0,
stream_id=push_stream_id_css,
stream_ended=False,
),
DataReceived(
data=b"body { color: pink }",
push_id=0,
stream_id=push_stream_id_css,
stream_ended=True,
),
events = h3_transfer(quic_client, h3_server)
self.assertEqual(
events,
[
HeadersReceived(
headers=[
(b":method", b"GET"),
(b":scheme", b"https"),
(b":authority", b"localhost"),
(b":path", b"/"),
(b"x-foo", b"client"),
],
stream_id=stream_id,
stream_ended=False,
),
DataReceived(data=b"", stream_id=stream_id, stream_ended=True),
],
)
# send response
h3_server.send_headers(
stream_id=stream_id,
headers=[
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
(b"x-foo", b"server"),
],
)
h3_server.send_data(
stream_id=stream_id,
data=b"hello",
end_stream=True,
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
],
end_stream=True,
)
# receive response
events = h0_transfer(quic_server, h0_client)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(events[0].headers, [])
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)
(b":scheme", b"https"),
(b":authority", b"localhost"),
(b":path", b"/app.txt"),
],
push_id=0,
stream_id=stream_id,
),
HeadersReceived(
headers=[
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
],
stream_id=0,
stream_ended=False,
),
DataReceived(data=b"h", stream_id=0, stream_ended=False),
DataReceived(data=b"t", stream_id=0, stream_ended=False),
DataReceived(data=b"m", stream_id=0, stream_ended=False),
DataReceived(data=b"l", stream_id=0, stream_ended=False),
DataReceived(data=b"", stream_id=0, stream_ended=True),
HeadersReceived(
headers=[(b":status", b"200"), (b"content-type", b"text/plain")],
stream_id=15,
stream_ended=False,
push_id=0,
),
DataReceived(data=b"t", stream_id=15, stream_ended=False, push_id=0),
DataReceived(data=b"e", stream_id=15, stream_ended=False, push_id=0),
DataReceived(data=b"x", stream_id=15, stream_ended=False, push_id=0),
DataReceived(data=b"t", stream_id=15, stream_ended=False, push_id=0),
DataReceived(data=b"", stream_id=15, stream_ended=True, push_id=0),
],
)
)
else:
method, path = data.rstrip().split(b" ", 1)
http_events.append(
HeadersReceived(
headers=[(b":method", method), (b":path", path)],
stream_ended=False,
stream_id=event.stream_id,
)
)
data = b""
self._headers_received[event.stream_id] = True
http_events.append(
DataReceived(
data=data, stream_ended=event.end_stream, stream_id=event.stream_id
)
)
return http_events
self._request_waiter[stream_id] = waiter
self.transmit()
# process response
events: Deque[H3Event] = await asyncio.shield(waiter)
content = b""
headers = []
status_code = None
for event in events:
if isinstance(event, HeadersReceived):
for header, value in event.headers:
if header == b":status":
status_code = int(value.decode())
elif header[0:1] != b":":
headers.append((header.decode(), value.decode()))
elif isinstance(event, DataReceived):
content += event.data
return AsyncResponse(
status_code=status_code,
protocol="HTTP/3",
headers=headers,
content=content,
# on_close=on_close,
request=request,
)
start = time.time()
async with connect(
server.host,
server.port,
configuration=configuration,
create_protocol=HttpClient,
) as protocol:
protocol = cast(HttpClient, protocol)
http_events = await protocol.get(
"https://{}:{}{}".format(server.host, server.port, path)
)
quic_elapsed = time.time() - start
quic_octets = 0
for http_event in http_events:
if isinstance(http_event, DataReceived):
quic_octets += len(http_event.data)
assert quic_octets == size, "HTTP/QUIC response size mismatch"
print(" - HTTP/TCP completed in %.3f s" % tcp_elapsed)
print(" - HTTP/QUIC completed in %.3f s" % quic_elapsed)
if quic_elapsed > 1.1 * tcp_elapsed:
failures += 1
print(" => FAIL")
else:
print(" => PASS")
if failures == 0:
server.result |= Result.T
def http_event_received(self, event: H3Event):
if isinstance(event, (HeadersReceived, DataReceived)):
stream_id = event.stream_id
if stream_id in self._request_events:
# http
self._request_events[event.stream_id].append(event)
if event.stream_ended:
request_waiter = self._request_waiter.pop(stream_id)
request_waiter.set_result(self._request_events.pop(stream_id))
elif stream_id in self._websockets:
# websocket
websocket = self._websockets[stream_id]
websocket.http_event_received(event)
elif event.push_id in self.pushes:
# push
self.pushes[event.push_id].append(event)
):
server.result |= Result.d
# check push support
if server.push_path is not None:
protocol.pushes.clear()
await protocol.get(
"https://{}:{}{}".format(server.host, server.port, server.push_path)
)
await asyncio.sleep(0.5)
for push_id, events in protocol.pushes.items():
if (
len(events) >= 3
and isinstance(events[0], PushPromiseReceived)
and isinstance(events[1], HeadersReceived)
and isinstance(events[2], DataReceived)
):
protocol._quic._logger.info(
"Push promise %d for %s received (status %s)",
push_id,
dict(events[0].headers)[b":path"].decode("ascii"),
int(dict(events[1].headers)[b":status"]),
)
server.result |= Result.p