HEX
Server: LiteSpeed
System: Linux srv146.niagahoster.com 4.18.0-553.30.1.lve.el8.x86_64 #1 SMP Tue Dec 3 01:21:19 UTC 2024 x86_64
User: kodi1989 (1633)
PHP: 8.1.34
Disabled: symlink,shell_exec,exec,popen,system,dl,passthru,escapeshellarg,escapeshellcmd,show_source,pcntl_exec
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/bmemcached/__pycache__/protocol.cpython-37.pyc
B

��f���@s�ddlmZmZddlZddlZddlZddlZddlZyddlmZWn e	k
rhddl
mZYnXddlZddlm
Z
ddlZddlmZmZddlmZddlmZmZmZddlmZe�e�ZGd	d
�d
ej�ZdS)�)�datetime�	timedeltaN)�	splitport)�BytesIO)�binary_type�	text_type)�long)�AuthenticationNotSupported�InvalidCredentials�MemcachedException)�str_to_bytescs�eZdZdZdZdZddd�Zddd	�d
dd	�ddd	�dd
d	�dd
d	�dd
d	�dd
d	�dd
d	�ddd	�ddd	�ddd	�ddd	�ddd	�ddiddiddd	�d�Zdddddd d!�Zdddddd"�Z	d#Z
dZdf�fd%d&�	Zd'd(�Z
ed)d*��Zd+d,�Zd-d.�Zd/d0�Zed1d2��Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Zdgd>d?�Zd@dA�ZdBdC�ZdDdE�ZdFdG�ZdhdHdI�ZdidJdK�Z djdLdM�Z!dkdNdO�Z"dldPdQ�Z#dmdSdT�Z$dUdV�Z%dndXdY�Z&dodZd[�Z'dpd\d]�Z(d^d_�Z)d`da�Z*dqdbdc�Z+ddde�Z,�Z-S)r�ProtocolaD
    This class is used by Client class to communicate with server.

    Reference https://github.com/memcached/memcached/wiki/BinaryProtocolRevamped ::

        Header structure
        Byte/     0       |       1       |       2       |       3       |
           /              |               |               |               |
          |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
          +---------------+---------------+---------------+---------------+
         0| Magic         | Opcode        | Key length                    |
          +---------------+---------------+---------------+---------------+
         4| Extras length | Data type     | vbucket id                    |
          +---------------+---------------+---------------+---------------+
         8| Total body length                                             |
          +---------------+---------------+---------------+---------------+
        12| Opaque                                                        |
          +---------------+---------------+---------------+---------------+
        16| CAS                                                           |
          |                                                               |
          +---------------+---------------+---------------+---------------+
          Total 24 bytes
    z
!BBHBBHLLQ���)�request�responserz%ds)�command�struct��
�zLL%ds%ds������zQQL%ds���I�
�r�� �!z%ds%ds)�get�getk�getkq�set�setq�add�addq�replace�delete�incr�decr�flush�noop�stat�auth_negotiation�auth_requestl��)�success�
key_not_found�
key_exists�
auth_error�unknown_command�server_disconnected)�object�integerr�
compressed�binaryl��Nc

s�tt|���||_||_||_|dkr,tn||_d|_d|_	||_
||_||_||_
|	|_d|_|�d�s�|�|j�\|_|_|�d�nd|_|_|�d�dS)NF�/rr)�superr
�__init__�server�	_username�	_password�zlib�compression�
connection�
authenticated�socket_timeout�pickle_protocol�pickler�	unpickler�tls_context�reconnects_deferred_until�
startswith�split_host_port�host�port�set_retry_delay)
�selfrC�username�passwordrGrJrKrLrMrN)�	__class__��D/opt/alt/python37/lib/python3.7/site-packages/bmemcached/protocol.pyrBes$
zProtocol.__init__cCsd�|j|j|j�S)Nz{}_{}_{})�formatrCrDrE)rUrYrYrZ�__str__~szProtocol.__str__cCs
|jdkS)N)rR)rUrYrYrZ�server_uses_unix_socket�sz Protocol.server_uses_unix_socketcCs
||_dS)N)�retry_delay)rU�valuerYrYrZrT�szProtocol.set_retry_delaycCs�|jr
dSd|_|jr(|jt��kr(dSy�|jr�t�tjtj�|_|j�	|j
�|j�|j|jf�|j
r�|j
j|j|jd�|_n t�tjtj�|_|j�|j�|��Wn.tjk
r�t��t|jd�|_�YnXdS)NF)�server_hostname)�seconds)rHrIrOr�nowrR�socket�AF_INET�SOCK_STREAM�
settimeoutrJ�connectrSrN�wrap_socket�AF_UNIXrC�_send_authentication�errorrr^)rUrYrYrZ�_open_connection�s(zProtocol._open_connectioncCs|��dS)N)�
disconnect)rU�	exceptionrYrYrZ�_connection_error�szProtocol._connection_errorcCsBt|�\}}|dkrd}t|�}t�d|�r:t�dd|�}||fS)z�
        Return (host, port) from server.

        Port defaults to 11211.

        >>> split_host_port('127.0.0.1:11211')
        ('127.0.0.1', 11211)
        >>> split_host_port('127.0.0.1')
        ('127.0.0.1', 11211)
        Ni�+z:.*$r")r�int�re�search�sub)�clsrCrRrSrYrYrZrQ�szProtocol.split_host_portcCsVt�}x2t|�|kr8|j�|t|��}|s.P||7}qWt|�|krNt���t|�S)z{
        Reads data from socket.

        :param size: Size in bytes to be read.
        :return: Data from socket
        )�	bytearray�lenrH�recvrcrk�bytes)rU�sizer_�datarYrYrZ�_read_socket�szProtocol._read_socketc
Cs�y�|��|jdkrt�d��|�|j�}t�|j|�\	}}}}}}}}	}
||j	dks\t
�d}|rn|�|�}||||||||	|
|f
Stjk
r�}z6|�|�t|�}
|j	ddddd|j
dddd|
f
Sd}~XYnXdS)z�
        Get memcached response from socket.

        :return: A tuple with binary values from memcached.
        :rtype: tuple
        NzDelaying reconnection attemptr���rr;)rlrHrcrkr{�HEADER_SIZEr�unpack�
HEADER_STRUCT�MAGIC�AssertionErrorro�str�STATUS)rU�header�magic�opcode�keylen�extlen�datatype�status�bodylen�opaque�cas�
extra_content�e�messagerYrYrZ�
_get_response�s 

 


zProtocol._get_responsec
CsZy&|��|jdkrdS|j�|�Wn.tjk
rT}z|�|�Wdd}~XYnXdS)N)rlrH�sendallrcrkro)rUrzr�rYrYrZ�_send�s
zProtocol._sendcCs"||_||_|��|��|jS)a�
        Authenticate user on server.

        :param username: Username used to be authenticated.
        :type username: six.string_types
        :param password: Password used to be authenticated.
        :type password: six.string_types
        :return: True if successful.
        :raises: InvalidCredentials, AuthenticationNotSupported, MemcachedException
        :rtype: bool
        )rDrErmrlrI)rUrVrWrYrYrZ�authenticate�s
zProtocol.authenticatecCs�|jr|jsdSt�d|j�|�t�|j|jd|j	ddddddddd�
�|�
�\
}}}}}}}}}	}
||jdkr�dS||jdkr�t�d	�d
|_d
S|
}d|kr�t
d|��d}d
|j|jf}
t|
t�r�|
��}
|�t�|j|j	ddt|�t|
�f|jd|j	ddt|�dddt|�t|
�dd||
��|�
�\
}}}}}}}}}	}
||jdk�rtdS||jdk�r�td|��||jdk�r�td||
f|��t�d||
�d
|_d
S)NFzAuthenticating as %srr4rrr;r:z(Server does not requires authentication.TsPLAINz-This module only supports PLAIN auth for now.z%s%sr5rr9zIncorrect username or passwordr6zCode: %d Message: %szAuth OK. Code: %d Message: %s)rDrE�logger�debugr�r�packrr��COMMANDSr�r�rIr	�
isinstancer�encodervr
r)rUr�r�r�r�r�r�r�r�r�r��methods�method�authrYrYrZrjsH

(
zProtocol._send_authenticationr|cCsDd}t|t�r||jdO}n�t|t�r4|�d�}n�t|t�rdt|t�dkrd||jdO}t|�}ndt|t�r�t|t�dkr�||jdO}t|�}n4||jdO}t	�}|�
||j�}|�|�|�
�}|dk�r<t|�|jk�r<|dk	�r|dk�r|j�||�}n|j�|�}|�r<t|�t|�k�r<|}||jd	O}||fS)
a�
        Serializes a value based on its type.

        :param value: Something to be serialized
        :type value: six.string_types, int, long, object
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: Serialized type
        :rtype: str
        rr?�utf8Fr=rr<Nr>)r�r�FLAGSrr�rp�boolr�rrrLrK�dump�getvaluerv�COMPRESSION_THRESHOLDrG�compress)rUr_�compress_level�flags�bufrLZcompressed_valuerYrYrZ�	serializeIs0





zProtocol.serializecCs�|j}||d@r|j�|�}||d@r.|S||d@rBt|�S||d@rVt|�S||d@r|t|�}|�|�}|��Stj	r�|�
d�Sy|�
d�Wn4tk
r�y
|�
d�Stk
r�|SXYnX|SdS)	a>
        Deserialized values based on flags or just return it if it is not serialized.

        :param value: Serialized or not value.
        :type value: six.string_types, int
        :param flags: Value flags
        :type flags: int
        :return: Deserialized value
        :rtype: six.string_types|int
        r>r?r=rr<r��asciiN)r�rG�
decompressrprrrM�load�six�PY3�decode�UnicodeDecodeError)rUr_r�r�r�rMrYrYrZ�deserializevs.


zProtocol.deserializec
Cst�d|�t|�}t�|j|jddt|�f|jd|jddt|�dddt|�dd|�}|�	|�|�
�\
}}}}}}	}
}}}
t�d||
|�|	|jdkr�|	|jd	kr�t�d
|
�dS|	|jdkr�dStd
|	|
f|	��t�
d|
df|
�\}}|�||�|fS)z�
        Get a key and its CAS value from server.  If the value isn't cached, return
        (None, None).

        :param key: Key's name
        :type key: six.string_types
        :return: Returns (value, cas).
        :rtype: object
        zGetting key %sr&rrrrz0Value Length: %d. Body length: %d. Data type: %dr6r7zKey not found. Message: %s)NNr;zCode: %d Message: %sz!L%dsr)r�r�rrr�rr�rvr�r�r�r�rr~r�)rU�key�keybytesrzr�r�r�r�r�r�r�r�r�r�r�r_rYrYrZr&�s(


zProtocol.getcCs�t�d�t�|j|jdd|jd|jddddddddd�
}|�|�|��\
}}}}}}}}	}
}t�d|||�||j	dkr�t�d	||f�t
|�S)
z_
        Send a NOOP command

        :return: Returns the status.
        :rtype: int
        zSending NOOPr2rrrrz0Value Length: %d. Body length: %d. Data type: %dr6z'NOOP failed (status is %d). Message: %s)r�r�rr�rr�r�r�r�r�rp)rUrzr�r�r�r�r�r�r�r�r�r�rYrYrZr2�s


z
Protocol.noopcCs�t|�}|dkriSt�}x~t|�D]r\}}t|�}|j||dkrHdnd}|t�|j|dt|�f|jd|dt|�dddt|�dd|�7}q$W|�	|�i}d}	x�|	|jddk�r\|�
�\
}
}	}}}
}}}}}||jd	k�r&t�d
|||df|�\}}}|�
||�|f||<q�||jdk�r:Pq�||jd
kr�td||f|��q�Wi}x,|D]$}t|�}||k�rh||||<�qhW|S)ai
        Get multiple keys from server.

        Since keys are converted to b'' when six.PY3 the keys need to be decoded back
        into string . e.g key='test' is read as b'test' and then decoded back to 'test'
        This encode/decode does not work when key is already a six.binary_type hence
        this function remembers which keys were originally sent as str so that
        it only decoded those keys back to string which were sent as string

        :param keys: A list of keys to from server.
        :type keys: Collection
        :return: A dict with all requested keys.
        :rtype: dict
        rrr'r(rrrr|r6z!L%ds%dsrr;r7zCode: %d Message: %s)rvru�	enumeraterr�rr�rr�r�r�r�r~r�r)rU�keys�n�msg�ir�r�r�dr�r�r�r�r�r�r�r�r�r�r�r_�retrYrYrZ�	get_multi�s@
"


zProtocol.get_multicCsH|dkr|n|j}t�d|�|j||d�\}}t�dt|��t|t�rT|�d�}t|�}|�	t
�|j|j
|dt|�t|�f|jd|j
|dt|�d	ddt|�t|�d	d|||||��|��\
}	}
}}}
}}}}}||jd
k�rD||jdk�r
dS||jd
k�rdS||jdk�r2dStd||f|��dS)a�
        Function to set/add/replace commands.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param cas: The CAS value that must be matched for this operation to complete, or 0 for no CAS.
        :type cas: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True in case of success and False in case of failure
        :rtype: bool
        rz Setting/adding/replacing key %s.)r�zValue bytes %s.r�rrrrr6r8Fr7r;zCode: %d Message: %sT)�MAXIMUM_EXPIRE_TIMEr�r�r�rvr�rr�rr�rr�rr�r�r�r�r)rUrr�r_�timer�r�r�r�r�r�r�r�r�r�r�r�r�rYrYrZ�_set_add_replaces.

$zProtocol._set_add_replacecCs|jd||||d�S)aA
        Set a value for a key on server.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True in case of success and False in case of failure
        :rtype: bool
        r))r�)r�)rUr�r_r�r�rYrYrZr)LszProtocol.setcCsF|dkstd��|dkr,|jd||||d�S|jd|||||d�SdS)ah
        Add a key/value to server ony if it does not exist.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True if key is added False if key already exists and has a different CAS
        :rtype: bool
        rz0 is an invalid CAS valueNr+)r�r))r�r�)r�r�)rUr�r_r�r�r�rYrYrZr�_szProtocol.cascCs|jd||||d�S)aP
        Add a key/value to server ony if it does not exist.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True if key is added False if key already exists
        :rtype: bool
        r+)r�)r�)rUr�r_r�r�rYrYrZr+|szProtocol.addcCs|jd||||d�S)aS
        Replace a key/value to server ony if it does exist.

        :param key: Key's name
        :type key: six.string_types
        :param value: A value to be stored on server.
        :type value: object
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: True if key is replace False if key does not exists
        :rtype: bool
        r-)r�)r�)rUr�r_r�r�rYrYrZr-�szProtocol.replace�dcCs�t|���}t�}x�t|�D]�\}\}}t|t�r<|\}}nd}|dkrNd}	nd}	t|�}
|j||d�\}}|t�	|j
|j|	dt|
�t|�f|j
d|j|	dt|
�d	ddt|
�t|�d	||p�d|||
|�7}qW|t�	|j
|jd
d|j
d|jd
dddddddd�
7}|�|�d}g}
x�||jd
dk�r�|��\
}}}}}}}}}}||jdk�r�td
d�|D��S||jdk�r4||\}}t|t��r�|
�|d|f�n
|
�|��q4W|
S)aY
        Set multiple keys with its values on server.

        If a key is a (key, cas) tuple, insert as if cas(key, value, cas) had
        been called.

        :param mappings: A dict with keys/values
        :type mappings: dict
        :param time: Time in seconds that your key will expire.
        :type time: int
        :param compress_level: How much to compress.
            0 = no compression, 1 = fastest, 9 = slowest but best,
            -1 = default compression level.
        :type compress_level: int
        :return: List of keys that failed to be set.
        :rtype: list
        Nrr,r*)r�rrrrr2r|r;css|]\}}|VqdS)NrY)�.0r�r_rYrYrZ�	<genexpr>�sz%Protocol.set_multi.<locals>.<genexpr>r6)�list�itemsrur�r��tuplerr�rr�rr�rvr�r�r�r��append)rUZmappingsr�r�r�r�r�r_r�rr�r�r��failedr�r�r�r�r�r�r�rYrYrZ�	set_multi�sH


 

zProtocol.set_multicCs�t|�}|dkr|n|j}|�t�|j|j|dt|�|jd|j|dt|�dddt|�ddd||||��|�	�\
}}}	}
}}}
}}}||j
d|j
dfkr�td||f|��||j
dkr�dSt�d	|�dS)
a�
        Function which increments and decrements.

        :param key: Key's name
        :type key: six.string_types
        :param value: Number to be (de|in)cremented
        :type value: int
        :param default: Default value if key does not exist.
        :type default: int
        :param time: Time in seconds to expire key.
        :type time: int
        :return: Actual value of the key on server
        :rtype: int
        rrrr�r6r;zCode: %d Message: %sz!Q)
rr�r�rr�rr�rvr�r�r�rr~)rUrr�r_�defaultr�r�r�r�r�r�r�r�r�r�r�r�rYrYrZ�
_incr_decr�szProtocol._incr_decr�@BcCs|�d||||�S)a�
        Increment a key, if it exists, returns its actual value, if it doesn't, return 0.

        :param key: Key's name
        :type key: six.string_types
        :param value: Number to be incremented
        :type value: int
        :param default: Default value if key does not exist.
        :type default: int
        :param time: Time in seconds to expire key.
        :type time: int
        :return: Actual value of the key on server
        :rtype: int
        r/)r�)rUr�r_r�r�rYrYrZr/
sz
Protocol.incrcCs|�d||||�S)a

        Decrement a key, if it exists, returns its actual value, if it doesn't, return 0.
        Minimum value of decrement return is 0.

        :param key: Key's name
        :type key: six.string_types
        :param value: Number to be decremented
        :type value: int
        :param default: Default value if key does not exist.
        :type default: int
        :param time: Time in seconds to expire key.
        :type time: int
        :return: Actual value of the key on server
        :rtype: int
        r0)r�)rUr�r_r�r�rYrYrZr0sz
Protocol.decrc
Cs�t�d|�t|�}|�t�|j|jddt|�f|j	d|jddt|�dddt|�d||��|�
�\
}}}}}}	}
}}}|	|jdkr�dS|	|jd	kr�|	|jd
|jdfkr�td|	|f|	��t�d
|�|	|jdkS)al
        Delete a key/value from server. If key existed and was deleted, return True.

        :param key: Key's name to be deleted
        :type key: six.string_types
        :param cas: If set, only delete the key if its CAS value matches.
        :type cas: int
        :return: True in case o success and False in case of failure.
        :rtype: bool
        zDeleting key %sr.rrrrr;Fr6r7r8zCode: %d message: %szKey deleted %s)
r�r�rr�rr�rr�rvr�r�r�r)
rUr�r�r�r�r�r�r�r�r�r�r�r�rYrYrZr.-s&zProtocol.deletecCs,t�d|�t�}xh|D]`}t|�}|t�|j|jddt|�f|j	d|jddt|�dddt|�dd|�7}qW|t�|j|jdd|j	d|jddddddddd�
7}|�
|�d}d	}xX||jddk�r&|��\
}}}}	}
}}}
}}||jd
k�rd}||jdkr�Pq�W|S)
z�
        Delete multiple keys from server in one command.

        :param keys: A list of keys to be deleted
        :type keys: list
        :return: True in case of success and False in case of failure.
        :rtype: bool
        zDeleting keys %rr.rrrrr2r|Tr6Fr;)
r�r�rurrr�rr�rvr�r�r�r�)rUr�r�r�r�r��retvalr�r�r�r�r�r�r�r�r�rYrYrZ�delete_multiKs4	
"
zProtocol.delete_multicCs�t�d�|�t�|j|jdd|jd|jddddddddd|��|��\
}}}}}}}}	}
}||j	d|j	d	fkr�t
d
||f|��t�d�dS)
z�
        Send a command to server flush|delete all keys.

        :param time: Time to wait until flush in seconds.
        :type time: int
        :return: True in case of success, False in case of failure
        :rtype: bool
        zFlushing memcachedr1rrrrrr6r;zCode: %d message: %szMemcached flushedT)r��infor�rr�rr�r�r�r�rr�)rUr�r�r�r�r�r�r�r�r�r�r�rYrYrZ�	flush_allts	

zProtocol.flush_allc

Cs |dk	r\t|t�rt|�}t|�}t�|jd||jd|jdd|ddd|dd|�}n.t�|j|jd|jddddddddd�
}|�	|�i}x�|�
�}|d}||jdkr�P|d	}|d
}|dkr�|dkr�P|d}|d|�}|||�}	|	|t|t��r|�
�n|<q�W|S)z�
        Return server stats.

        :param key: Optional if you want status from a key.
        :type key: six.string_types
        :return: A dict with server stats
        :rtype: dict
        Nz%dsrr3rrrr;rrr|)r�rrrvrr�rr�r�r�r�r�rxr�)
rUr�r��packedr_rr�r�r��bodyrYrYrZ�stats�s<


 zProtocol.statscCs|jr|j��d|_dS)z�
        Disconnects from server.  A new connection will be established the next time a request is made.

        :return: Nothing
        :rtype: None
        N)rH�close)rUrYrYrZrm�s
zProtocol.disconnect)NNNNNNNN)r|)rr|)r|)r|)r|)r|)r�r|)rr�)rr�)r)N).�__name__�
__module__�__qualname__�__doc__rr}r�r�r�r�r�r�rBr\�propertyr]rTrlro�classmethodrQr{r�r�r�rjr�r�r&r2r�r�r)r�r+r-r�r�r/r0r.r�r�r�rm�
__classcell__rYrY)rXrZr
s�"
6
-,'9
0




E#


)
2r
)rr�loggingrqrcr�	threading�urllibr�ImportError�urllib.parserF�iorr�rrZbmemcached.compatrZbmemcached.exceptionsr	r
rZbmemcached.utilsr�	getLoggerr�r��localr
rYrYrYrZ�<module>s$