Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(":scheme", "https"),
("sec-websocket-version", "13"),
],
)
await client_stream.send_all(h2_client.data_to_send())
events = h2_client.receive_data(await client_stream.receive_some(1024))
await client_stream.send_all(h2_client.data_to_send())
events = h2_client.receive_data(await client_stream.receive_some(1024))
if not isinstance(events[-1], h2.events.ResponseReceived):
events = h2_client.receive_data(await client_stream.receive_some(1024))
assert events[-1].headers == [
(b":status", b"200"),
(b"date", b"Thu, 01 Jan 1970 01:23:20 GMT"),
(b"server", b"hypercorn-h2"),
]
client = wsproto.connection.Connection(wsproto.ConnectionType.CLIENT)
h2_client.send_data(stream_id, client.send(wsproto.events.BytesMessage(data=SANITY_BODY)))
await client_stream.send_all(h2_client.data_to_send())
events = h2_client.receive_data(await client_stream.receive_some(1024))
client.receive_data(events[0].data)
assert list(client.events()) == [wsproto.events.TextMessage(data="Hello & Goodbye")]
h2_client.send_data(stream_id, client.send(wsproto.events.CloseConnection(code=1000)))
await client_stream.send_all(h2_client.data_to_send())
events = h2_client.receive_data(await client_stream.receive_some(1024))
client.receive_data(events[0].data)
assert list(client.events()) == [wsproto.events.CloseConnection(code=1000, reason="")]
await client_stream.send_all(b"")
def __init__(self, path: str, *, framework: ASGIFramework = echo_framework) -> None:
self.client_stream, server_stream = trio.testing.memory_stream_pair()
server_stream.socket = MockSocket()
self.server = WebsocketServer(framework, Config(), server_stream)
self.connection = WSConnection(ConnectionType.CLIENT)
self.server.connection.receive_data(
self.connection.send(Request(target=path, host="hypercorn"))
)
def __init__(self):
super().__init__()
self.socket = curio.socket.socket(
curio.socket.AF_INET, curio.socket.SOCK_STREAM)
self.protocol = WSConnection(ConnectionType.CLIENT)
async def test_http1_websocket(event_loop: asyncio.AbstractEventLoop) -> None:
server = TCPServer(
sanity_framework, event_loop, Config(), MemoryReader(), MemoryWriter() # type: ignore
)
asyncio.ensure_future(server.run())
client = wsproto.WSConnection(wsproto.ConnectionType.CLIENT)
await server.reader.send( # type: ignore
client.send(wsproto.events.Request(host="hypercorn", target="/"))
)
client.receive_data(await server.writer.receive()) # type: ignore
assert list(client.events()) == [
wsproto.events.AcceptConnection(
extra_headers=[
(b"date", b"Thu, 01 Jan 1970 01:23:20 GMT"),
(b"server", b"hypercorn-h11"),
]
)
]
await server.reader.send( # type: ignore
client.send(wsproto.events.BytesMessage(data=SANITY_BODY))
)
client.receive_data(await server.writer.receive()) # type: ignore
def __init__(
self,
path: str,
event_loop: asyncio.AbstractEventLoop,
*,
framework: ASGIFramework = echo_framework,
) -> None:
self.transport = MockTransport()
self.server = WebsocketServer( # type: ignore
framework, event_loop, Config(), self.transport
)
self.connection = WSConnection(ConnectionType.CLIENT)
self.server.data_received(self.connection.send(Request(target=path, host="hypercorn")))
def __init__(self, socket):
super().__init__()
self.socket = socket
self.protocol = WSConnection(ConnectionType.SERVER)
def handle_connection(stream: socket.socket) -> None:
"""
Handle a connection.
The server operates a request/response cycle, so it performs a synchronous
loop:
1) Read data from network into wsproto
2) Get new events and handle them
3) Send data from wsproto to network
:param stream: a socket stream
"""
ws = WSConnection(ConnectionType.SERVER)
running = True
while running:
# 1) Read data from network
in_data = stream.recv(RECEIVE_BYTES)
print("Received {} bytes".format(len(in_data)))
ws.receive_data(in_data)
# 2) Get new events and handle them
out_data = b""
for event in ws.events():
if isinstance(event, Request):
# Negotiate new WebSocket connection
print("Accepting WebSocket upgrade")
out_data += ws.send(AcceptConnection())
elif isinstance(event, CloseConnection):
def __init__(
self,
app: ASGIFramework,
config: Config,
stream: trio.abc.Stream,
*,
upgrade_request: Optional[h11.Request] = None,
) -> None:
super().__init__(stream, "wsproto")
self.app = app
self.config = config
self.connection = WSConnection(ConnectionType.SERVER)
self.response: Optional[dict] = None
self.scope: Optional[dict] = None
self.send_lock = trio.Lock()
self.state = ASGIWebsocketState.HANDSHAKE
self.buffer = WebsocketBuffer(self.config.websocket_max_message_size)
self.app_send_channel, self.app_receive_channel = trio.open_memory_channel(10)
if upgrade_request is not None:
self.connection.initiate_upgrade_connection(
upgrade_request.headers, upgrade_request.target
)