Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self):
self._conn = h2.connection.H2Connection(client_side=False)
self._recv_buffer = {}
self._handlers = {}
self._handlers['ConnectionMade'] = self.on_connection_made_default
self._handlers['DataReceived'] = self.on_data_received_default
self._handlers['WindowUpdated'] = self.on_window_update_default
self._handlers['RequestReceived'] = self.on_request_received_default
self._handlers['SendDone'] = self.on_send_done_default
self._handlers['ConnectionLost'] = self.on_connection_lost
self._handlers['PingAcknowledged'] = self.on_ping_acknowledged_default
self._stream_status = {}
self._send_remaining = {}
self._outstanding_pings = 0
def test_can_receive_response(self, frame_factory):
"""
After upgrading, we can safely receive a response.
"""
c = h2.connection.H2Connection()
c.initiate_upgrade_connection()
c.clear_outbound_data_buffer()
f1 = frame_factory.build_headers_frame(
stream_id=1,
headers=self.example_response_headers,
)
f2 = frame_factory.build_data_frame(
stream_id=1,
data=b'some data',
flags=['END_STREAM']
)
events = c.receive_data(f1.serialize() + f2.serialize())
assert len(events) == 3
assert isinstance(events[0], h2.events.ResponseReceived)
def __init__(self, serving_app: Quart, event_loop: asyncio.AbstractEventLoop) -> None:
self.transport = MockTransport()
self.server = H2Server( # type: ignore
serving_app, event_loop, self.transport, None, '', 5,
)
self.connection = h2.connection.H2Connection()
def handle(self):
config = h2.config.H2Configuration(
client_side=False,
validate_outbound_headers=False,
validate_inbound_headers=False)
h2_conn = h2.connection.H2Connection(config)
preamble = self.rfile.read(24)
h2_conn.initiate_connection()
h2_conn.receive_data(preamble)
self.wfile.write(h2_conn.data_to_send())
self.wfile.flush()
if 'h2_server_settings' in self.kwargs:
h2_conn.update_settings(self.kwargs['h2_server_settings'])
self.wfile.write(h2_conn.data_to_send())
self.wfile.flush()
done = False
while not done:
try:
raw = b''.join(http2.read_raw_frame(self.rfile))
def test_flow_control_initializes_properly(self):
"""
The flow control window for a stream should initially be the default
flow control value.
"""
c = h2.connection.H2Connection()
c.send_headers(1, self.example_request_headers)
assert c.local_flow_control_window(1) == self.DEFAULT_FLOW_WINDOW
assert c.remote_flow_control_window(1) == self.DEFAULT_FLOW_WINDOW
def test_reset_stream_keeps_flow_control_correct(self,
close_id,
other_id,
frame_factory):
"""
A stream that has been reset still affects the connection flow control
window.
"""
c = h2.connection.H2Connection()
c.initiate_connection()
c.send_headers(stream_id=1, headers=self.example_request_headers)
c.send_headers(stream_id=3, headers=self.example_request_headers)
# Record the initial window size.
initial_window = c.remote_flow_control_window(stream_id=other_id)
f = frame_factory.build_headers_frame(
headers=self.example_response_headers, stream_id=close_id
)
c.receive_data(f.serialize())
c.reset_stream(stream_id=close_id)
c.clear_outbound_data_buffer()
f = frame_factory.build_data_frame(
data=b'some data!',
def __init__(self, sock, root):
config = h2.config.H2Configuration(
client_side=False, header_encoding='utf-8'
)
self.sock = sock
self.conn = h2.connection.H2Connection(config=config)
self.root = root
self.flow_control_events = {}
StateMachine = collections.namedtuple(
'StateMachine', ['fqdn', 'machine', 'states', 'inputs', 'transitions']
)
# This is all the state machines we currently know about and will render.
# If any new state machines are added, they should be inserted here.
STATE_MACHINES = [
StateMachine(
fqdn='h2.connection.H2ConnectionStateMachine',
machine=h2.connection.H2ConnectionStateMachine,
states=h2.connection.ConnectionState,
inputs=h2.connection.ConnectionInputs,
transitions=h2.connection.H2ConnectionStateMachine._transitions,
),
StateMachine(
fqdn='h2.stream.H2StreamStateMachine',
machine=h2.stream.H2StreamStateMachine,
states=h2.stream.StreamState,
inputs=h2.stream.StreamInputs,
transitions=h2.stream._transitions,
),
]
def quote(s):
return '"{}"'.format(s.replace('"', r'\"'))
def html(s):
async def handle_request(self, event: h2.events.RequestReceived) -> None:
await self.streams[event.stream_id].handle_request(
event, self.scheme, self.client, self.server
)
if (
self.connection.state_machine.state is not h2.connection.ConnectionState.CLOSED
and event.stream_id in self.connection.streams
and not self.connection.streams[event.stream_id].closed
):
# The connection is not closed and there has been an error
# preventing the stream from closing correctly.
self.connection.reset_stream(event.stream_id)
await self.streams[event.stream_id].close()
del self.streams[event.stream_id]
from h2 import events
import queue
from mitmproxy import connections # noqa
from mitmproxy import exceptions
from mitmproxy import http
from mitmproxy.proxy.protocol import base
from mitmproxy.proxy.protocol import http as httpbase
import mitmproxy.net.http
from mitmproxy.net import tcp
from mitmproxy.coretypes import basethread
from mitmproxy.net.http import http2, headers
from mitmproxy.utils import human
class SafeH2Connection(connection.H2Connection):
def __init__(self, conn, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conn = conn
self.lock = threading.RLock()
def safe_acknowledge_received_data(self, acknowledged_size: int, stream_id: int):
if acknowledged_size == 0:
return
with self.lock:
self.acknowledge_received_data(acknowledged_size, stream_id)
self.conn.send(self.data_to_send())
def safe_reset_stream(self, stream_id: int, error_code: int):
with self.lock: