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 get_events(self) -> list:
events = []
self.client.receive_data(await self.client_stream.receive_some(2 ** 16))
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
async def get_events(self) -> list:
events = []
self.client.receive_data(await self.client_stream.receive_some(2 ** 16))
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
if self.connection.our_state in {h11.IDLE, h11.SEND_RESPONSE}:
await self._send_error_response(400)
await self.send(Closed())
break
else:
if isinstance(event, h11.Request):
await self._check_protocol(event)
await self._create_stream(event)
elif isinstance(event, h11.Data):
await self.stream.handle(Body(stream_id=STREAM_ID, data=event.data))
elif isinstance(event, h11.EndOfMessage):
await self.stream.handle(EndBody(stream_id=STREAM_ID))
elif isinstance(event, Data):
# WebSocket pass through
await self.stream.handle(event)
elif event is h11.PAUSED:
await self.send(Updated())
await self.can_read.clear()
await self.can_read.wait()
elif isinstance(event, h11.ConnectionClosed) or event is h11.NEED_DATA:
break
status_code=101,
headers=[(b"upgrade", b"h2c")] + self.response_headers(),
)
)
raise error
self.task = self.loop.create_task(self.handle_request(event))
self.task.add_done_callback(self.recycle_or_close)
elif isinstance(event, h11.EndOfMessage):
self.app_queue.put_nowait(
{"type": "http.request", "body": b"", "more_body": False}
)
elif isinstance(event, h11.Data):
self.app_queue.put_nowait(
{"type": "http.request", "body": event.data, "more_body": True}
)
elif event is h11.PAUSED:
self.transport.pause_reading()
break
elif isinstance(event, h11.ConnectionClosed) or event is h11.NEED_DATA:
break
def _check(self) -> None:
"""
Process pending events from the h11 parser.
"""
event = None
while event is not h11.NEED_DATA:
try:
event = self._parser.next_event()
except h11.RemoteProtocolError:
# Something went wrong. Let's bail.
self._done.errback(Failure())
return
if event is h11.PAUSED:
self._done.errback(Failure(Exception("????")))
return
else:
if isinstance(event, h11.EndOfMessage):
return self._finish()
self._response.append(event)
def _get_response(self):
# type: () -> Optional[HttpResponse]
ret = self._connection.next_event()
if not self._current_response:
self._current_response = HttpResponse()
while ret != h11.NEED_DATA:
if ret == h11.PAUSED or isinstance(ret, h11.EndOfMessage):
try:
self._connection.start_next_cycle()
except h11.ProtocolError:
self._connection = h11.Connection(our_role=h11.CLIENT)
response = self._current_response
self._current_response = None
response.mark_as_received()
return response
elif isinstance(ret, h11.InformationalResponse):
pass
elif isinstance(ret, h11.Response):
self._current_response.add_response(ret)
elif isinstance(ret, h11.Data):
self._current_response.add_data(ret.data)
ret = self._connection.next_event()
log.log("process_chunk: reading chunk of data from async reader")
buf = await request_reader.read(constants.ASYNC_IO_READ_BUFFER)
log.log("process_chunk: buffer filled")
connection.receive_data(buf)
log.log("process_chunk: sending data to h11")
while True:
event = connection.next_event()
log.log("process_chunk: event type {0}"
.format(type(event)))
if isinstance(event, h11.Request):
request = event
if isinstance(event, h11.Data):
body.write(event.data)
if isinstance(event, h11.EndOfMessage):
return request, body
if isinstance(event, (h11.NEED_DATA, h11.PAUSED)):
log.log("requiring more data or connection paused")
break
if isinstance(event, h11.ConnectionClosed):
raise Exception("connection closed")
if isinstance(event, h11.Request):
headers = CIMultiDict()
if event.http_version < b'1.1':
headers.setdefault('host', self.app.config['SERVER_NAME'] or '')
for name, value in event.headers:
headers.add(name.decode().title(), value.decode())
if 'Upgrade' in headers:
self._handle_upgrade_request(headers, event)
self.handle_request(
0, event.method.decode().upper(), event.target.decode(), headers,
)
elif isinstance(event, h11.EndOfMessage):
self.streams[0].complete()
elif isinstance(event, h11.Data):
self.streams[0].append(event.data)
elif event is h11.NEED_DATA or event is h11.PAUSED:
break
elif isinstance(event, h11.ConnectionClosed):
break
if self.connection.our_state is h11.MUST_CLOSE:
self.close()
async def _receive_response_data(
self, timeout: Timeout
) -> typing.AsyncIterator[bytes]:
"""
Read the response data from the network.
"""
while True:
event = await self._receive_event(timeout)
if isinstance(event, h11.Data):
yield bytes(event.data)
else:
assert isinstance(event, h11.EndOfMessage) or event is h11.PAUSED
break # pragma: no cover
def handle_events(self):
while True:
try:
event = self.conn.next_event()
except h11.RemoteProtocolError as exc:
msg = "Invalid HTTP request received."
self.logger.warning(msg)
self.transport.close()
return
event_type = type(event)
if event_type is h11.NEED_DATA:
break
elif event_type is h11.PAUSED:
# This case can occur in HTTP pipelining, so we need to
# stop reading any more data, and ensure that at the end
# of the active request/response cycle we handle any
# events that have been buffered up.
self.flow.pause_reading()
break
elif event_type is h11.Request:
self.headers = [(key.lower(), value) for key, value in event.headers]
raw_path, _, query_string = event.target.partition(b"?")
self.scope = {
"type": "http",
"http_version": event.http_version.decode("ascii"),
"server": self.server,
"client": self.client,
"scheme": self.scheme,