HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/twisted/protocols/haproxy/__pycache__/_wrapper.cpython-310.pyc
o

�b]�@s�dZddlmZmZddlmZddlmZddlm	Z	ddl
mZddlm
Z
dd	lmZdd
lmZGdd�de	j�ZGd
d�de	j�Zdejdefdd�ZdS)z@
Protocol wrapper that provides HAProxy PROXY protocol support.
�)�Optional�Union)�
interfaces)�_WrapperServerEndpoint)�policies�)�_info)�InvalidProxyHeader)�V1Parser)�V2Parsercs`eZdZdZdejdejf�fdd�Zde	ddfd	d
�Z
dejfdd�Zdejfd
d�Z
�ZS)�HAProxyProtocolWrappera
    A Protocol wrapper that provides HAProxy support.

    This protocol reads the PROXY stream header, v1 or v2, parses the provided
    connection data, and modifies the behavior of getPeer and getHost to return
    the data provided by the PROXY header.
    �factory�wrappedProtocolcst��||�d|_d|_dS�N)�super�__init__�
_proxyInfo�_parser)�selfr
r��	__class__��D/usr/lib/python3/dist-packages/twisted/protocols/haproxy/_wrapper.pyrs
zHAProxyProtocolWrapper.__init__�data�returnNcCs�|jdur|j�|�S|j}|durPt|�dkr4|dd�tjkr4t|dd��d@dkr4t�|_}nt|�dkrJ|dd�tj	krJt�|_}n|�
�dSz|�|�\|_}|rd|j�|�WdSWdStyt|�
�YdSw)N���
��� ��)
rr�dataReceivedr�lenr�PREFIX�ordr
�PROXYSTR�loseConnection�feedr	)rr�parser�	remainingrrrr"$s(
��z#HAProxyProtocolWrapper.dataReceivedcC�*|jr|jjr|jjS|jsJ�|j��Sr)r�source�	transport�getPeer�rrrrr.=�

zHAProxyProtocolWrapper.getPeercCr+r)r�destinationr-�getHostr/rrrr2Cr0zHAProxyProtocolWrapper.getHost)�__name__�
__module__�__qualname__�__doc__r�WrappingFactoryr�	IProtocolr�bytesr"�IAddressr.r2�
__classcell__rrrrrs��rc@s"eZdZdZeZdefdd�ZdS)�HAProxyWrappingFactoryzL
    A Factory wrapper that adds PROXY protocol support to connections.
    rcCs.tj�|j�r
|j��}n|jjj}|�d�S)z�
        Annotate the wrapped factory's log prefix with some text indicating
        the PROXY protocol is in use.

        @rtype: C{str}
        z (PROXY))r�ILoggingContext�
providedBy�wrappedFactory�	logPrefixrr3)rr@rrrr@Qs

z HAProxyWrappingFactory.logPrefixN)r3r4r5r6r�protocol�strr@rrrrr<Jsr<�wrappedEndpointrcCs
t|t�S)a�
    Wrap an endpoint with PROXY protocol support, so that the transport's
    C{getHost} and C{getPeer} methods reflect the attributes of the proxied
    connection rather than the underlying connection.

    @param wrappedEndpoint: The underlying listening endpoint.
    @type wrappedEndpoint: L{IStreamServerEndpoint}

    @return: a new listening endpoint that speaks the PROXY protocol.
    @rtype: L{IStreamServerEndpoint}
    )rr<)rCrrr�
proxyEndpoint_s
rDN)r6�typingrr�twisted.internetr�twisted.internet.endpointsr�twisted.protocolsr�r�_exceptionsr	�	_v1parserr
�	_v2parserr�ProtocolWrapperrr7r<�IStreamServerEndpointrDrrrr�<module>s 6��