Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_events(self) -> list:
events = []
self.client.receive_data(self.transport.data)
while True:
event = self.client.next_event()
if event in (h11.NEED_DATA, h11.PAUSED):
break
events.append(event)
if isinstance(event, h11.ConnectionClosed):
break
return events
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 next_event(self):
while True:
event = self.conn.next_event()
if event is h11.NEED_DATA:
await self._read_from_peer()
continue
return event
async def _receive_event(self, timeout: TimeoutConfig) -> H11Event:
assert self._reader is not None
event = self._h11_state.next_event()
while event is h11.NEED_DATA:
try:
data = await asyncio.wait_for(
self._reader.read(2048), timeout.read_timeout
)
except asyncio.TimeoutError:
raise ReadTimeout()
self._h11_state.receive_data(data)
event = self._h11_state.next_event()
return event
def next_event(self, max_bytes_per_recv=200):
while True:
# If we already have a complete event buffered internally, just
# return that. Otherwise, read some data, add it to the internal
# buffer, and then try again.
event = self.conn.next_event()
if event is h11.NEED_DATA:
self.conn.receive_data(self.sock.recv(max_bytes_per_recv))
continue
return event
async def next_event(self):
while True:
event = self.conn.next_event()
if event is h11.NEED_DATA:
await self._read_from_peer()
continue
return event
A list of events that the remote peer triggered by sending
this data can be retrieved with :meth:`events`.
:param bytes data: Data received from the WebSocket peer.
"""
self._h11_connection.receive_data(data)
while True:
try:
event = self._h11_connection.next_event()
except h11.RemoteProtocolError:
raise RemoteProtocolError(
"Bad HTTP message", event_hint=RejectConnection()
)
if (
isinstance(event, h11.ConnectionClosed)
or event is h11.NEED_DATA
or event is h11.PAUSED
):
break
if self.client:
if isinstance(event, h11.InformationalResponse):
if event.status_code == 101:
self._events.append(self._establish_client_connection(event))
else:
self._events.append(
RejectConnection(
headers=event.headers,
status_code=event.status_code,
has_body=False,
)
)
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))
writer.write(data)
message = h11.EndOfMessage()
data = conn.send(message)
writer.write(data)
status_code = 0
headers = []
reason = b''
buffer = io.BytesIO()
while True:
event = conn.next_event()
event_type = type(event)
if event_type is h11.NEED_DATA:
data = await reader.read(2048)
conn.receive_data(data)
elif event_type is h11.Response:
status_code = event.status_code
headers = [(key.decode(), value.decode()) for key, value in event.headers]
reason = event.reason
elif event_type is h11.Data:
buffer.write(event.data)
elif event_type is h11.EndOfMessage:
buffer.seek(0)
break
writer.close()