← Back
Editing: basic.cpython-38.pyc
U W[=} � @ s� d Z ddlmZmZ ddlZddlmZmZmZ ddl m Z ddlZddlm Z ddlmZ ddlmZmZmZ dd lmZ er�d d� Zndd� Zd e_ dZG dd� de�ZG dd� de�ZG dd� dej�ZG dd� dej�ZG dd� d�ZG dd� deje�Z G dd� de!�Z"G dd� de#�Z$G dd� deje�Z%G d d!� d!e%�Z&G d"d#� d#e%�Z'G d$d%� d%e%�Z(G d&d'� d'�Z)e ej*�G d(d)� d)��Z+dS )*zN Basic protocols, such as line-oriented, netstring, and int prefixed strings. � )�absolute_import�divisionN)�pack�unpack�calcsize)�BytesIO)�implementer)�_PY3)�protocol�defer� interfaces)�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 c C s dt | �| f S )Ns %d:%s,)r r r r r r s z_ Convert some C{bytes} into netstring format. @param data: C{bytes} that will be reformatted. c @ s e Zd ZdZdS )�NetstringParseErrorz= The incoming data is not in valid Netstring format. N��__name__� __module__�__qualname__�__doc__r r r r r , s r c @ s e Zd ZdZdS )�IncompleteNetstringz2 Not enough data to complete a netstring. Nr r r r r r! 3 s 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&