File: //lib/python3/dist-packages/twisted/protocols/__pycache__/basic.cpython-310.pyc
o
�b�{ � @ sd d Z ddlZddlZddlmZ ddlmZmZmZ ddl m
Z
ddlmZm
Z
mZ ddlmZ dd � Zd
e_ dZG dd� de�ZG d
d� de�ZG dd� dej�ZG dd� dej�ZG dd� d�ZG dd� deje�ZG dd� de�ZG dd� d�ZG dd� deje�ZG dd� de�Z G dd � d e�Z!G d!d"� d"e�Z"G d#d$� d$�Z#e
e
j$�G d%d&� d&��Z%dS )'zN
Basic protocols, such as line-oriented, netstring, and int prefixed strings.
� N)�BytesIO)�calcsize�pack�unpack)�implementer)�defer�
interfaces�protocol)�logc C s d� tt| ���d�d| dg�S )N� �ascii� :� ,)�join�str�len�encode)�data� r �9/usr/lib/python3/dist-packages/twisted/protocols/basic.py�_formatNetstring s r z_
Convert some C{bytes} into netstring format.
@param data: C{bytes} that will be reformatted.
c @ � e Zd ZdZdS )�NetstringParseErrorz=
The incoming data is not in valid Netstring format.
N��__name__�
__module__�__qualname__�__doc__r r r r r ) � r c @ r )�IncompleteNetstringz2
Not enough data to complete a netstring.
Nr r r r r r / r r c @ s� e Zd ZdZdZe�d�Ze�d�ZdZ dZ
dZdZe
d �\ZZd
d� Zdd
� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zd d!� Zd"d#� Zd$d%� Zd&d'� Zd(d)� Zd*d+� Z d,d-� Z!d.S )/�NetstringReceiveraM
A protocol that sends and receives netstrings.
See U{http://cr.yp.to/proto/netstrings.txt} for the specification of
netstrings. Every netstring starts with digits that specify the length
of the data. This length specification is separated from the data by
a colon. The data is terminated with a comma.
Override L{stringReceived} to handle received netstrings. This
method is called with the netstring payload as a single argument
whenever a complete netstring is received.
Security features:
1. Messages are limited in size, useful if you don't want
someone sending you a 500MB netstring (change C{self.MAX_LENGTH}
to the maximum length you wish to accept).
2. The connection is lost if an illegal message is received.
@ivar MAX_LENGTH: Defines the maximum length of netstrings that can be
received.
@type MAX_LENGTH: C{int}
@ivar _LENGTH: A pattern describing all strings that contain a netstring
length specification. Examples for length specifications are C{b'0:'},
C{b'12:'}, and C{b'179:'}. C{b'007:'} is not a valid length
specification, since leading zeros are not allowed.
@type _LENGTH: C{re.Match}
@ivar _LENGTH_PREFIX: A pattern describing all strings that contain
the first part of a netstring length specification (without the
trailing comma). Examples are '0', '12', and '179'. '007' does not
start a netstring length specification, since leading zeros are
not allowed.
@type _LENGTH_PREFIX: C{re.Match}
@ivar _PARSING_LENGTH: Indicates that the C{NetstringReceiver} is in
the state of parsing the length portion of a netstring.
@type _PARSING_LENGTH: C{int}
@ivar _PARSING_PAYLOAD: Indicates that the C{NetstringReceiver} is in
the state of parsing the payload portion (data and trailing comma)
of a netstring.
@type _PARSING_PAYLOAD: C{int}
@ivar brokenPeer: Indicates if the connection is still functional
@type brokenPeer: C{int}
@ivar _state: Indicates if the protocol is consuming the length portion
(C{PARSING_LENGTH}) or the payload (C{PARSING_PAYLOAD}) of a netstring
@type _state: C{int}
@ivar _remainingData: Holds the chunk of data that has not yet been consumed
@type _remainingData: C{string}
@ivar _payload: Holds the payload portion of a netstring including the
trailing comma
@type _payload: C{BytesIO}
@ivar _expectedPayloadSize: Holds the payload size plus one for the trailing
comma.
@type _expectedPayloadSize: C{int}
韆 s (0|[1-9]\d*)(:)s
(0|[1-9]\d*)$zBThe received netstring does not start with a length specification.zpThe length specification of the received netstring cannot be represented in Python - it causes an OverflowError!zQThe received netstring is longer than the maximum %s specified by self.MAX_LENGTHz4The received netstring is not terminated by a comma.� c C s: t j�| |� d| _d| _t� | _| j| _d| _ d| _
dS )z+
Initializes the protocol.
r r N)r �Protocol�makeConnection�_remainingData�_currentPayloadSizer �_payload�_PARSING_LENGTH�_state�_expectedPayloadSize�
brokenPeer)�self� transportr r r r$ � s
z NetstringReceiver.makeConnectionc C s | j �t|�� dS )a1
Sends a netstring.
Wraps up C{string} by adding length information and a
trailing comma; writes the result to the transport.
@param string: The string to send. The necessary framing (length
prefix, etc) will be added.
@type string: C{bytes}
N)r- �writer �r, �stringr r r �
sendString� s zNetstringReceiver.sendStringc C s^ | j |7 _ | j r-z| �� W n ty Y dS ty' | �� Y dS w | j s
dS dS )a=
Receives some characters of a netstring.
Whenever a complete netstring is received, this method extracts
its payload and calls L{stringReceived} to process it.
@param data: A chunk of data representing a (possibly partial)
netstring
@type data: C{bytes}
N)r% �_consumeDatar r �_handleParseError�r, r r r r �dataReceived� s ��zNetstringReceiver.dataReceivedc C s t � �)a^
Override this for notification when each complete string is received.
@param string: The complete string which was received with all
framing (length prefix, etc) removed.
@type string: C{bytes}
@raise NotImplementedError: because the method has to be implemented
by the child class.
��NotImplementedErrorr/ r r r �stringReceived� s z NetstringReceiver.stringReceivedc C s t �t �| j��d S )z�
Calculate and return the string size of C{self.MAX_LENGTH}.
@return: The size of the string representation for C{self.MAX_LENGTH}
@rtype: C{float}
� )�math�ceil�log10�
MAX_LENGTH�r, r r r �_maxLengthSize� s z NetstringReceiver._maxLengthSizec C s8 | j | jkr| �� | �� | j | jkr| �� dS dS )a0
Consumes the content of C{self._remainingData}.
@raise IncompleteNetstring: if C{self._remainingData} does not
contain enough data to complete the current netstring.
@raise NetstringParseError: if the received data do not
form a valid netstring.
N)r) r( �_consumeLength�_prepareForPayloadConsumption�_PARSING_PAYLOAD�_consumePayloadr>