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 test_protocol_handle_request(protocol: H11Protocol) -> None:
client = h11.Connection(h11.CLIENT)
await protocol.handle(
RawData(data=client.send(h11.Request(method="GET", target="/?a=b", headers=BASIC_HEADERS)))
)
protocol.stream.handle.assert_called()
assert protocol.stream.handle.call_args_list == [
call(
Request(
stream_id=1,
headers=[(b"host", b"hypercorn"), (b"connection", b"close")],
http_version="1.1",
method="GET",
raw_path=b"/?a=b",
)
),
call(EndBody(stream_id=1)),
]
async def test_http1_request(nursery: trio._core._run.Nursery) -> None:
client_stream, server_stream = trio.testing.memory_stream_pair()
server_stream.socket = MockSocket()
server = TCPServer(sanity_framework, Config(), server_stream)
nursery.start_soon(server.run)
client = h11.Connection(h11.CLIENT)
await client_stream.send_all(
client.send(
h11.Request(
method="GET",
target="/",
headers=[
(b"host", b"hypercorn"),
(b"connection", b"close"),
(b"content-length", b"%d" % len(SANITY_BODY)),
],
)
)
)
await client_stream.send_all(client.send(h11.Data(data=SANITY_BODY)))
await client_stream.send_all(client.send(h11.EndOfMessage()))
events = []
def __init__(self, serving_app: Quart, event_loop: asyncio.AbstractEventLoop) -> None:
self.transport = MockTransport()
self.client = h11.Connection(h11.CLIENT)
self.server = H11Server( # type: ignore
serving_app, event_loop, self.transport, None, '', 5,
)
tunnel_port=None,
tunnel_headers=None,
):
self.is_verified = False
self._backend = backend or SyncBackend()
self._host = host
self._port = port
self._socket_options = (
socket_options if socket_options is not _DEFAULT_SOCKET_OPTIONS else self.default_socket_options
)
self._source_address = source_address
self._tunnel_host = tunnel_host
self._tunnel_port = tunnel_port
self._tunnel_headers = tunnel_headers
self._sock = None
self._state_machine = h11.Connection(our_role=h11.CLIENT)
def connect(self, ssl_context=None,
fingerprint=None, assert_hostname=None, connect_timeout=None):
"""
Connect this socket to the server, applying the source address, any
relevant socket options, and the relevant connection timeout.
"""
if self._sock is not None:
# We're already connected, move on.
return
self._state_machine = h11.Connection(our_role=h11.CLIENT)
self._selector = selectors.DefaultSelector()
extra_kw = {}
if self._source_address:
extra_kw['source_address'] = self._source_address
if self._socket_options:
extra_kw['socket_options'] = self._socket_options
conn = self._do_socket_connect(connect_timeout, extra_kw)
if ssl_context is not None:
if self._tunnel_host is not None:
self._tunnel(conn)
conn = self._wrap_socket(
def _send_http1_request(self, stream, context):
logger.debug("replay http1 request: {0}".format(context.request))
Http1Connection(h11.CLIENT, stream).send_request(context.request)
async def _tunnel(self, sock):
"""
This method establishes a CONNECT tunnel shortly after connection.
"""
# Basic sanity check that _tunnel is only called at appropriate times.
assert self._state_machine.our_state is h11.IDLE
tunnel_request = _build_tunnel_request(
self._tunnel_host, self._tunnel_port, self._tunnel_headers
)
tunnel_state_machine = h11.Connection(our_role=h11.CLIENT)
h11_response = await _start_http_request(
tunnel_request, tunnel_state_machine, sock
)
# XX this is wrong -- 'self' here will try to iterate using
# self._state_machine, not tunnel_state_machine. Also, we need to
# think about how this failure case interacts with the pool's
# connection lifecycle management.
tunnel_response = _response_from_h11(h11_response, self)
if h11_response.status_code != 200:
sock.forceful_close()
raise FailedTunnelError(
"Unable to establish CONNECT tunnel", tunnel_response
)
def __attrs_post_init__(self):
self.connection.set_client(self)
self._parser = h11.Connection(our_role=h11.CLIENT)
def initiate_upgrade_connection(self, headers: Headers, path: str) -> None:
"""Initiate an upgrade connection.
This should be used if the request has already be received and
parsed.
:param list headers: HTTP headers represented as a list of 2-tuples.
:param str path: A URL path.
"""
if self.client:
raise LocalProtocolError(
"Cannot initiate an upgrade connection when acting as the client"
)
upgrade_request = h11.Request(method=b"GET", target=path, headers=headers)
h11_client = h11.Connection(h11.CLIENT)
self.receive_data(h11_client.send(upgrade_request))
def _tunnel(self, conn):
"""
This method establishes a CONNECT tunnel shortly after connection.
"""
# Basic sanity check that _tunnel is only called at appropriate times.
assert self._state_machine.our_state is h11.IDLE
tunnel_request = _build_tunnel_request(
self._tunnel_host, self._tunnel_port, self._tunnel_headers
)
tunnel_state_machine = h11.Connection(our_role=h11.CLIENT)
h11_response = _start_http_request(
tunnel_request, tunnel_state_machine, conn
)
# XX this is wrong -- 'self' here will try to iterate using
# self._state_machine, not tunnel_state_machine. Also, we need to
# think about how this failure case interacts with the pool's
# connection lifecycle management.
tunnel_response = _response_from_h11(h11_response, self)
if h11_response.status_code != 200:
conn.forceful_close()
raise FailedTunnelError(
"Unable to establish CONNECT tunnel", tunnel_response
)