U
    
W[                     @   sj   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZ ddl
mZ eejG dd	 d	eZd
S )zD
IProxyParser implementation for version one of the PROXY protocol.
    )implementer)address   )convertErrorInvalidProxyHeaderInvalidNetworkProtocolMissingAddressData)_info)_interfacesc                   @   sJ   e Zd ZdZdZdZdZdZeeefZdZ	dd Z
d	d
 Ze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   
c                 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__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   )NNr   )r   lenNEWLINEr   splitpopparse)r   datalinesZ	remainingheaderinfor   r   r   feed/   s    
zV1Parser.feedc           	   
   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rt || jkrt	|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rlt	|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	   Z	ProxyInfor   
TCP4_PROTOr   ZIPv4AddressintZIPv6Address)	clslineZoriginalLineZproxyStrZnetworkProtocolZ
sourceAddrZ
sourcePortZdestAddrZdestPortr   r   r   r   K   sH    


zV1Parser.parseN)__name__
__module____qualname____doc__r   r!   r"   Z
TCP6_PROTOr    r   r   r   classmethodr   r   r   r   r   r      s   r   N)r)   Zzope.interfacer   Ztwisted.internetr   _exceptionsr   r   r   r    r	   r
   ZIProxyParserobjectr   r   r   r   r   <module>   s   