Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_protocol_handle_h2c_upgrade(protocol: H11Protocol) -> None:
with pytest.raises(H2CProtocolRequired) as exc_info:
await protocol.handle(
RawData(
data=(
b"GET / HTTP/1.1\r\nHost: hypercorn\r\n"
b"upgrade: h2c\r\nhttp2-settings: abcd\r\n\r\nbbb"
)
)
)
assert protocol.send.call_args_list == [
call(
RawData(
b"HTTP/1.1 101 \r\nupgrade: h2c\r\ndate: Thu, 01 Jan 1970 01:23:20 GMT\r\n"
b"server: hypercorn-h11\r\n\r\n"
)
)
]
assert exc_info.value.data == b"bbb"
assert exc_info.value.headers == [
(b":method", b"GET"),
(b":path", b"/"),
(b":authority", b"hypercorn"),
(b"host", b"hypercorn"),
(b"upgrade", b"h2c"),
(b"http2-settings", b"abcd"),
]
assert exc_info.value.settings == "abcd"
async def test_protocol_handle_h2_prior(protocol: H11Protocol) -> None:
with pytest.raises(H2ProtocolAssumed) as exc_info:
await protocol.handle(RawData(data=b"PRI * HTTP/2.0\r\n\r\nbbb"))
assert exc_info.value.data == b"PRI * HTTP/2.0\r\n\r\nbbb"
async def protocol_send(self, event: Event) -> None:
if isinstance(event, RawData):
async with self.send_lock:
try:
with trio.CancelScope() as cancel_scope:
cancel_scope.shield = True
await self.stream.send_all(event.data)
except (trio.BrokenResourceError, trio.ClosedResourceError):
await self.protocol.handle(Closed())
elif isinstance(event, Closed):
await self._close()
await self.protocol.handle(Closed())
elif isinstance(event, Updated):
pass # Triggers the keep alive timeout update
await self._update_keep_alive_timeout()
async def _read_data(self) -> None:
while True:
try:
data = await self.reader.read(MAX_RECV)
except (BrokenPipeError, ConnectionResetError):
await self.protocol.handle(Closed())
break
else:
if data == b"":
self._update_keep_alive_timeout()
break
await self.protocol.handle(RawData(data))
self._update_keep_alive_timeout()
await self.protocol.initiate()
if error.data != b"":
return await self.protocol.handle(RawData(data=error.data))
except H2CProtocolRequired as error:
self.protocol = H2Protocol(
self.config,
self.ssl,
self.client,
self.server,
self.send,
self.spawn_app,
self.event_class,
)
await self.protocol.initiate(error.headers, error.settings)
if error.data != b"":
return await self.protocol.handle(RawData(data=error.data))
async def protocol_send(self, event: Event) -> None:
if isinstance(event, RawData):
try:
self.writer.write(event.data)
await self.writer.drain()
except (BrokenPipeError, ConnectionResetError):
await self.protocol.handle(Closed())
elif isinstance(event, Closed):
await self._close()
await self.protocol.handle(Closed())
elif isinstance(event, Updated):
pass # Triggers the keep alive timeout update
self._update_keep_alive_timeout()
async def _read_data(self) -> None:
while True:
try:
data = await self.stream.receive_some(MAX_RECV)
except (trio.ClosedResourceError, trio.BrokenResourceError):
await self.protocol.handle(Closed())
break
else:
if data == b"":
await self._update_keep_alive_timeout()
break
await self.protocol.handle(RawData(data))
await self._update_keep_alive_timeout()
async def run(
self, task_status: trio._core._run._TaskStatus = trio.TASK_STATUS_IGNORED
) -> None:
task_status.started()
while True:
data, address = await self.socket.recvfrom(MAX_RECV)
await self.protocol.handle(RawData(data=data, address=address))
async def send_all(self, connection: QuicConnection) -> None:
for data, address in connection.datagrams_to_send(now=self.now()):
await self.send(RawData(data=data, address=address))
async def protocol_send(self, event: Event) -> None:
if isinstance(event, RawData):
await self.socket.sendto(event.data, event.address)