Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_handshake_extra_headers() -> None:
response, nonce = _make_handshake([], accept_headers=[(b"X-Foo", b"bar")])
response.headers = sorted(response.headers) # For test determinism
assert response == h11.InformationalResponse(
status_code=101,
headers=[
(b"connection", b"Upgrade"),
(b"sec-websocket-accept", generate_accept_token(nonce)),
(b"upgrade", b"WebSocket"),
(b"x-foo", b"bar"),
],
target="/",
subprotocols=subprotocols or [],
extensions=extensions or [],
)
)
)
request = server.next_event()
if auto_accept_key:
full_request_headers = normed_header_dict(request.headers)
response_headers.append(
(
b"Sec-WebSocket-Accept",
generate_accept_token(full_request_headers[b"sec-websocket-key"]),
)
)
response = h11.InformationalResponse(
status_code=response_status, headers=response_headers
)
client.receive_data(server.send(response))
return list(client.events())
def _send_response(self, interim=False):
self._response.finalize()
self._logger.info('< %s', self._response.status_line)
self._log_headers(self._response.raw_headers)
cls = h11.InformationalResponse if interim else h11.Response
self._handler.send_event(cls(
http_version=self._response.http_version,
status_code=self._response.status_code,
reason=force_bytes(self._response.reason),
headers=_encode_headers(self._response.raw_headers),
))
def receive(self, data, raise_exception=False):
try:
logger.debug("data received from {0} with length {1}".format(self.conn_type, len(data)))
self.receive_data(data)
while True:
event = self.next_event()
self._log_event(event)
if isinstance(event, Request):
if self._req: # pragma: no cover
# NOTE: guess that never happen because h11 should help us handle http state
raise ProtocolError("http1 connection had received request")
self._req = event
elif isinstance(event, InformationalResponse):
self.on_info_response(HttpResponse(
version=self._parse_version(event),
reason=event.reason,
code=str(event.status_code),
headers=event.headers))
elif isinstance(event, Response):
self._resp = event
if self.our_state is h11.SWITCHED_PROTOCOL:
self.on_response(HttpResponse(
version=self._parse_version(self._resp),
reason=self._resp.reason,
code=str(self._resp.status_code),
headers=self._resp.headers,
body=b"".join(self._body_chunks)))
self._cleanup_after_received()
elif isinstance(event, Data):
def consume_bytes(data):
state_machine.receive_data(data)
while True:
event = state_machine.next_event()
if event is h11.NEED_DATA:
break
elif isinstance(event, h11.InformationalResponse):
# Ignore 1xx responses
continue
elif isinstance(event, h11.Response):
# We have our response! Save it and get out of here.
context['h11_response'] = event
raise LoopAbort
else:
# Can't happen
raise RuntimeError("Unexpected h11 event {}".format(event))
async def _read_from_peer(self):
if self.conn.they_are_waiting_for_100_continue:
self.info("Sending 100 Continue")
go_ahead = h11.InformationalResponse(
status_code=100,
headers=self.basic_headers())
await self.send(go_ahead)
try:
data = await self.stream.receive_some(MAX_RECV)
except ConnectionError:
# They've stopped listening. Not much we can do about it here.
data = b""
self.conn.receive_data(data)
async def _read_from_peer(self):
if self.conn.they_are_waiting_for_100_continue:
self.info("Sending 100 Continue")
go_ahead = h11.InformationalResponse(
status_code=100,
headers=self.basic_headers())
await self.send(go_ahead)
try:
data = await self.stream.receive_some(MAX_RECV)
except ConnectionError:
# They've stopped listening. Not much we can do about it here.
data = b""
self.conn.receive_data(data)
def _handle_events(self) -> None:
if self.connection.they_are_waiting_for_100_continue:
self._send(
h11.InformationalResponse(status_code=100, headers=self.response_headers),
)
while True:
try:
event = self.connection.next_event()
except h11.RemoteProtocolError:
self._handle_error()
self.close()
break
else:
if isinstance(event, h11.Request):
headers = CIMultiDict()
for name, value in event.headers:
headers.add(name.decode().title(), value.decode())
self.handle_request(
0, event.method.decode().upper(), event.target.decode(), headers,
)
async def _read_from_peer(self):
if self._conn.they_are_waiting_for_100_continue:
go_ahead = h11.InformationalResponse(status_code=100, headers=self.basic_headers())
await self.send_h11(go_ahead)
try:
data = await self.socket.recv(settings.MAX_RECV)
except ConnectionError:
data = b""
self._conn.receive_data(data)