← Back
Editing: _v1parser.cpython-38.pyc
U W[� � @ sj d Z ddlmZ ddlmZ ddlmZmZmZm Z ddl mZ ddl mZ eej �G dd � d e��Zd S )zD IProxyParser implementation for version one of the PROXY protocol. � )�implementer)�address� )�convertError�InvalidProxyHeader�InvalidNetworkProtocol�MissingAddressData)�_info)�_interfacesc @ sJ e Zd ZdZdZdZdZdZeeefZdZ dd� Z d d � Zedd� �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 zV1Parser.__init__c C sv | j |7 _ t| j �dkr.| j| j kr.t� �| j �| jd�}t|�dksNdS d| _ |�� }|�� }| �|�}||fS )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 tt�� |�dd�\}}W 5 Q R X || jkrRt� �t tt�� |�dd�\}}W 5 Q R X || jkr�t� �|| jkr�t� |dd�S t tt �� |�dd�\}}W 5 Q R X t tt �� |�dd�\}}W 5 Q R X t tt �� |�dd�\}}W 5 Q R X t tt �� |�d�d }W 5 Q R X || jk�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 ZTCP)r � ValueErrorr r �PROXYSTRr �ALLOWED_NET_PROTOS� UNKNOWN_PROTOr Z ProxyInfor � TCP4_PROTOr ZIPv4Address�intZIPv6Address) �cls�lineZoriginalLineZproxyStrZnetworkProtocolZ sourceAddrZ sourcePortZdestAddrZdestPortr r r r K sH ��zV1Parser.parseN)�__name__� __module__�__qualname__�__doc__r r! r"