HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.52.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Oct 15 06:39:08 EDT 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/lib/python3.9/site-packages/cockpit/__pycache__/protocol.cpython-39.pyc
a

�/�h&�@s�ddlZddlZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZe�
e�ZGdd�de�ZGdd�de�ZGdd	�d	ej�ZGd
d�de�ZdS)�N�)�	JsonError�
JsonObject�	JsonValue�
create_object�get_int�get_str�get_str_or_none�typecheckedcsFeZdZUdZeed<dededd��fdd�
Zed�d	d
�Z	�Z
S)�CockpitProblema�A type of exception that carries a problem code and a message.

    Depending on the scope, this is used to handle shutting down:

      - an individual channel (sends problem code in the close message)
      - peer connections (sends problem code in close message for each open channel)
      - the main stdio interaction with the bridge

    It is usually thrown in response to some violation of expected protocol
    when parsing messages, connecting to a peer, or opening a channel.
    �attrsN�JsonObject | None)�problem�msg�kwargs�returncs.||d<t||�|_t��t|jd|��dS)Nr�message)rr�super�__init__r)�selfrrr��	__class__��4/usr/lib/python3.9/site-packages/cockpit/protocol.pyr*szCockpitProblem.__init__�rcCsD|jddkr:|jdur:t|jt�|jj|j|jj�d�S|jSdS)Nrzinternal-error)�cause)r�	__cause__�dict�	traceback�format_exceptionr�
__traceback__�rrrr�	get_attrs/s

�zCockpitProblem.get_attrs)N)�__name__�
__module__�__qualname__�__doc__r�__annotations__�strrrr"�
__classcell__rrrrrs
rcs&eZdZdeed��fdd�
Z�ZS)�CockpitProtocolError�protocol-error)rrcst�j||d�dS)N)r)rr)rrrrrrr9szCockpitProtocolError.__init__)r+)r#r$r%r(rr)rrrrr*8sr*c@s8eZdZUdZdZded<dZdZeed<dZ	ded	<dd
�dd�Z
d
dd�dd�Zee
dd�dd�Zeee
dd�dd�Zeedd�dd�Zedd�dd�Zedd�dd�Zeed�d d!�Zejdd"�d#d$�Zd
dd�d%d&�Zd4d
dd�d'd(�Zeedd)�d*d+�Zd5d,edd-�d.d/�Zedd�d0d1�Zed
�d2d3�ZdS)6�CockpitProtocolz�A naive implementation of the Cockpit frame protocol

    We need to use this because Python's SelectorEventLoop doesn't supported
    buffered protocols.
    Nzasyncio.Transport | None�	transport�F�_closedzasyncio.Future[None] | None�_communication_donercCsdS�Nrr!rrr�do_readyHszCockpitProtocol.do_readyzException | None)�excrcCsdSr1r�rr3rrr�	do_closedKszCockpitProtocol.do_closed��commandrrcCst�dSr1��NotImplementedError�rr7rrrr�transport_control_receivedNsz*CockpitProtocol.transport_control_received)�channelr7rrcCst�dSr1r8)rr<r7rrrr�channel_control_receivedQsz(CockpitProtocol.channel_control_received)r<�datarcCst�dSr1r8)rr<r>rrr�channel_data_receivedTsz%CockpitProtocol.channel_data_received)�framercCsP|�d�\}}}|dkrB|�d�}t�dt|�|�|�||�n
|�|�dS)N�
r.�asciiz.data received: %d bytes of data for channel %s)�	partition�decode�logger�debug�lenr?�control_received)rr@�header�_r>r<rrr�frame_receivedWs
zCockpitProtocol.frame_received)r>rc
Cs�zftt�|�t�}t|d�}t|dd�}|durLt�d|�|�|||�nt�d|�|�||�Wn:tj	t
fy�}ztd|���|�WYd}~n
d}~00dS)Nr7r<zchannel control received %sztransport control received %szcontrol message: )r
�json�loadsrrrErFr=r;ZJSONDecodeErrorrr*)rr>rr7r<r3rrrrHbs
z CockpitProtocol.control_receivedc
Cs�z|�d�}WnRty`}z:t|�dkrBt|�dWYd}~Std�|�WYd}~n
d}~00zt|d|��}Wn.ty�}ztd�|�WYd}~n
d}~00|d}||}|t|�kr�t|�|S|�|||��|S)z�Consumes a single frame from view.

        Returns positive if a number of bytes were consumed, or negative if no
        work can be done because of a given number of bytes missing.
        rA�
Nzsize line is too longzframe size is not an integerr)�index�
ValueErrorrGr*�intrK)rr>�newliner3�length�start�endrrr�consume_one_framers   z!CockpitProtocol.consume_one_frame)r-rcCsFt�d|�t|tj�sJ�||_|��|jrBt�d�|��dS)Nzconnection_made(%s)z;  but the protocol already was closed, so closing transport)	rErF�
isinstance�asyncioZ	Transportr-r2r/�close)rr-rrr�connection_made�s
zCockpitProtocol.connection_madecCs,t�d�|jdusJ�d|_|�|�dS)N�connection_lost)rErFr-rYr4rrrr[�s
zCockpitProtocol.connection_lostcCs.|jr
dSd|_|jr |j��|�|�dS)NT)r/r-rYr5r4rrrrY�s
zCockpitProtocol.close)r<�payloadrcCsbt|d�t|�}|�d|�d��d�}|jdurTt�d|j�|j�||�n
t�d�dS)z0Send a given payload (bytes) on channel (string)�
rBNzwriting to transport %sz cannot write to closed transport)rG�encoder-rErF�write)rr<r\Zframe_lengthrIrrr�write_channel_data�s
z"CockpitProtocol.write_channel_datar
)rrrcKs:t�d||�tjt||�dd�d}|�d|���dS)zCWrite a control message.  See jsonutil.create_object() for details.zsending control message %r %r�)�indentr]�N)rErFrL�dumpsrr`r^)rrrZprettyrrr�
write_control�szCockpitProtocol.write_controlc
CsxzD|j|7_|jrB|�|j�}|dkr0WdS|j|d�|_qWn.tyr}z|�|�WYd}~n
d}~00dS)Nr)�bufferrVr*rY)rr>�resultr3rrr�
data_received�szCockpitProtocol.data_receivedcCsdS)NFrr!rrr�eof_received�szCockpitProtocol.eof_received)N)N) r#r$r%r&r-r'rfr/�boolr0r2r5r(rr;r=�bytesr?rKrHrQrVrXZ
BaseTransportrZr[rYr`rrerhrirrrrr,=s(


r,c@s�eZdZUdZded<dZded<dZdd�dd	�Zedd
�dd�Z	ddedd
�dd�Z
eedd�dd�Zdd�dd�Z
dedeed�dd�Zdedeed�dd�Zedd
�dd�ZdS)�CockpitProtocolServerNz
str | None�	init_hostz,dict[str, asyncio.Future[JsonObject]] | None�authorizationsrrcCst�dSr1r8r!rrr�do_send_init�sz"CockpitProtocolServer.do_send_init)rrcCsdSr1r)rrrrr�do_init�szCockpitProtocolServer.do_init)�host�grouprrcCst�dSr1r8)rrqrrrrrr�do_kill�szCockpitProtocolServer.do_killr6cCs�|dkr6t|d�dkrtd��t|d�|_|�|�nL|dkr^|�t|dd�t|dd�|�n$|dkrr|�|�ntd	|�d
���dS)N�init�versionrzincorrect version numberrq�killrr�	authorizezunexpected control message z	 received)rr*rrmrprsr	�do_authorizer:rrrr;�s z0CockpitProtocolServer.transport_control_receivedcCs|��dSr1)ror!rrrr2�szCockpitProtocolServer.do_readyz
int | None)�	challenge�timeoutrrc	�s�|jduri|_t|j�}|jd7_t����}zB||j|<|jdd||d�|��t�||�IdHW|j�|�S|j�|�0dS)Nrrw)r7ry�cookie)N)	rnr(�next_auth_idrXZget_running_loopZ
create_futurere�wait_for�pop)rryrzrr{Zfuturerrr�request_authorization_object�s


�z2CockpitProtocolServer.request_authorization_objectc�s t|j||fi|��IdHd�S)NZresponse)rr)rryrzrrrr�request_authorization�sz+CockpitProtocolServer.request_authorizationcCs@t|d�}|jdus||jvr,t�d�dS|j|�|�dS)Nr{zno matching authorize request)rrnrEZwarningZ
set_result)rrr{rrrrx�s


z"CockpitProtocolServer.do_authorize)N)N)r#r$r%rmr'rnr|rorrprsr(r;r2rrr�rxrrrrrl�s&

����rl)rXrLZloggingrZjsonutilrrrrrrr	r
Z	getLoggerr#rE�	Exceptionrr*�Protocolr,rlrrrr�<module>s(