U
    ª€lj‹  ã                   @   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	d
„Z
eeeejef ed f dœdd„Zeeejdœ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   
N)Úreturnc                 C   s
   d| _ d S )Nó    )Úbuffer)Úself© r   úG/tmp/pip-unpacked-wheel-aghyhbke/twisted/protocols/haproxy/_v1parser.pyÚ__init__,   s    zV1Parser.__init__©NN)Údatar   c                 C   sv   |  j |7  _ t| j ƒdkr.| j| j kr.tƒ ‚| j  | jd¡}t|ƒdksNd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)Úliner   c           	   
   C   s¦  |}d}d}d}d}d}d}t ttƒ� | dd¡\}}W 5 Q R X || jkrRtƒ ‚t ttƒ� | dd¡\}}W 5 Q R X || jkrˆtƒ ‚|| jkr t 	|dd¡S t tt
ƒ� | dd¡\}}W 5 Q R X t tt
ƒ� | dd¡\}}W 5 Q R X t tt
ƒ� | dd¡\}}W 5 Q R X t tt
ƒ� | d¡d }W 5 Q R X || jk�rtt 	|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   ZTCP)r   Ú
ValueErrorr
   r   ÚPROXYSTRr	   ÚALLOWED_NET_PROTOSÚUNKNOWN_PROTOr   Ú	ProxyInfor   Ú
TCP4_PROTOr   ÚIPv4AddressÚdecodeÚintÚIPv6Address)	Úclsr"   ZoriginalLineZproxyStrZnetworkProtocolZ
sourceAddrZ
sourcePortZdestAddrZdestPortr   r   r   r   L   sH    


ýýzV1Parser.parse)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r'   r)   Z
TCP6_PROTOr&   r   r   Úbytesr   r   r   r(   r!   Úclassmethodr   r   r   r   r   r      s    ýþr   N)r2   Útypingr   r   Zzope.interfacer   Ztwisted.internetr   Ú r   r   Ú_exceptionsr	   r
   r   r   ZIProxyParserr   r   r   r   r   Ú<module>   s   