o
    ¯b�  ã                   @   sp   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZ ee
jƒG dd	„ d	ƒƒZd
S )zD
IProxyParser implementation for version one of the PROXY protocol.
é    )ÚTupleÚUnion)Úimplementer)Úaddressé   )Ú_infoÚ_interfaces)ÚInvalidNetworkProtocolÚInvalidProxyHeaderÚMissingAddressDataÚconvertErrorc                   @   sx   e Zd ZdZdZdZdZdZeeefZdZ	dd	d
„Z
dedeeejef ed f fdd„Zededejfdd„ƒZdS )ÚV1Parserzõ
    PROXY protocol version one header parser.

    Version one of the PROXY protocol is a human readable format represented
    by a single, newline delimited binary string that contains all of the
    relevant source and destination data.
    s   PROXYs   UNKNOWNs   TCP4s   TCP6s   
ÚreturnNc                 C   s
   d| _ d S )Nó    )Úbuffer)Úself© r   úE/usr/lib/python3/dist-packages/twisted/protocols/haproxy/_v1parser.pyÚ__init__,   s   
zV1Parser.__init__Údata©NNc                 C   sv   |  j |7  _ t| j ƒdkr| j| j vrtƒ ‚| j  | jd¡}t|ƒdks'dS d| _ | ¡ }| ¡ }|  |¡}||fS )aî  
        Consume a chunk of data and attempt to parse it.

        @param data: A bytestring.
        @type data: L{bytes}

        @return: A two-tuple containing, in order, a
            L{_interfaces.IProxyInfo} and any bytes fed to the
            parser that followed the end of the header.  Both of these values
            are None until a complete header is parsed.

        @raises InvalidProxyHeader: If the bytes fed to the parser create an
            invalid PROXY header.
        ék   r   r   r   )r   ÚlenÚNEWLINEr
   ÚsplitÚpopÚparse)r   r   ÚlinesÚ	remainingÚheaderÚinfor   r   r   Úfeed/   s   
zV1Parser.feedÚlinec           	   
   C   s  |}d}d}d}d}d}d}t ttƒ� | dd¡\}}W d  ƒ n1 s&w   Y  || jkr3tƒ ‚t ttƒ� | dd¡\}}W d  ƒ n1 sKw   Y  || jvrXtƒ ‚|| jkrdt 	|dd¡S t tt
ƒ� | dd¡\}}W d  ƒ n1 s|w   Y  t tt
ƒ� | dd¡\}}W d  ƒ n1 s™w   Y  t tt
ƒ� | dd¡\}}W d  ƒ n1 s¶w   Y  t tt
ƒ� | d¡d }W d  ƒ n1 sÒw   Y  || jkrõt 	|t d| ¡ t|ƒ¡t d| ¡ t|ƒ¡¡S t 	|t d| ¡ t|ƒ¡t d| ¡ t|ƒ¡¡S )a�  
        Parse a bytestring as a full PROXY protocol header line.

        @param line: A bytestring that represents a valid HAProxy PROXY
            protocol header line.
        @type line: bytes

        @return: A L{_interfaces.IProxyInfo} containing the parsed data.

        @raises InvalidProxyHeader: If the bytestring does not represent a
            valid PROXY header.

        @raises InvalidNetworkProtocol: When no protocol can be parsed or is
            not one of the allowed values.

        @raises MissingAddressData: When the protocol is TCP* but the header
            does not contain a complete set of addresses and ports.
        Nó    r   r   ÚTCP)r   Ú
ValueErrorr
   r   ÚPROXYSTRr	   ÚALLOWED_NET_PROTOSÚUNKNOWN_PROTOr   Ú	ProxyInfor   Ú
TCP4_PROTOr   ÚIPv4AddressÚdecodeÚintÚIPv6Address)	Úclsr"   ÚoriginalLineÚproxyStrÚnetworkProtocolÚ
sourceAddrÚ
sourcePortÚdestAddrÚdestPortr   r   r   r   L   sT   ÿ
ÿ

ÿÿÿÿ
ýýzV1Parser.parse)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r(   r*   Ú
TCP6_PROTOr'   r   r   Úbytesr   r   r   r)   r!   Úclassmethodr   r   r   r   r   r      s&    ý
ÿ
þr   N)r:   Útypingr   r   Úzope.interfacer   Útwisted.internetr   Ú r   r   Ú_exceptionsr	   r
   r   r   ÚIProxyParserr   r   r   r   r   Ú<module>   s   