
    Vh                         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	             Zy
)zD
IProxyParser implementation for version one of the PROXY protocol.
    )TupleUnion)implementer)address   )_info_interfaces)InvalidNetworkProtocolInvalidProxyHeaderMissingAddressDataconvertErrorc                       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
edeeej                  ef   ed   f   fdZededej                  fd       Zy)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                     d| _         y )N    )buffer)selfs    S/home/dcms/DCMS/lib/python3.12/site-packages/twisted/protocols/haproxy/_v1parser.py__init__zV1Parser.__init__,   s	    r   dataNNc                    | xj                   |z  c_         t        | j                         dkD  r"| j                  | j                   vr
t               | j                   j	                  | j                  d      }t        |      dkD  syd| _         |j                         }|j                         }| j                  |      }||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infos         r   feedzV1Parser.feed/   s    " 	tt{{c!dll$++&E$&&##DLL!45zA~IIK	zz&!i  r   linec                    |}d}d}d}d}d}d}t        t        t              5  |j                  dd      \  }}ddd       || j                  k7  r
t               t        t        t
              5  |j                  dd      \  }}ddd       || j                  vr
t               || j                  k(  rt        j                  |dd      S t        t        t              5  |j                  dd      \  }}ddd       t        t        t              5  |j                  dd      \  }}ddd       t        t        t              5  |j                  dd      \  }}ddd       t        t        t              5  |j                  d      d   }ddd       || j                  k(  rot        j                  |t        j                  d|j                         t        |            t        j                  d|j                         t        |                  S t        j                  |t        j                   d|j                         t        |            t        j                   d|j                         t        |                  S # 1 sw Y   GxY w# 1 sw Y   	xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   \xY w# 1 sw Y   8xY w)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destPorts	            r   r   zV1Parser.parseL   si   ( 

*&89 	1!ZZa0NHd	1 s||#$&&*&<= 	8$(JJtQ$7!OT	8 #"8"88(**c///??<t<<*&89 	3#zz$2J	3 *&89 	1!ZZa0NHd	1 *&89 	3#zz$2J	3 *&89 	+zz$'*H	+ cnn,??##E:+<+<+>JP##E8??+<c(mL  z'8'8':C
OLx'8#h-H
 	
E	1 	1	8 	8	3 	3	1 	1	3 	3	+ 	+sG   I>.JJJ%J24J?>JJJ"%J/2J<?K	)r   N)__name__
__module____qualname____doc__r*   r,   r.   
TCP6_PROTOr+   r   r   bytesr   r   r   r-   r$   classmethodr    r   r   r   r      s     HMJJ
 G!!	uU__e+,eJ.??	@!: A
 A
5?? A
 A
r   r   N)r>   typingr   r   zope.interfacer   twisted.internetr    r   r	   _exceptionsr
   r   r   r   IProxyParserr   rB   r   r   <module>rI      sI      & $    [%%&v
 v
 'v
r   