
     iI              	           S r SSKrSSKr " S S\5      r " S S5      rSS\S\S	\S
\	4S jjr
SS\S\	S
\	4S jjrS\S\S
\	4S jrg)a  
Control Socket Protocol

JSON-based protocol with length-prefixed framing for the control interface.

Message Format:
    +----------------+------------------+
    | Length (4B BE) |  JSON Payload    |
    +----------------+------------------+

Request Format:
    {"id": 1, "command": "show", "args": ["workers"]}

Response Format:
    {"id": 1, "status": "ok", "data": {...}}
    {"id": 1, "status": "error", "error": "message"}
    Nc                       \ rS rSrSrSrg)ProtocolError   zProtocol-level error. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       G/var/www/ias/venv/lib/python3.13/site-packages/gunicorn/ctl/protocol.pyr   r      s    r   r   c                       \ rS rSrSrSr\S\S\4S j5       r	\S\S\4S j5       r
\S\4S j5       r\S\4S	 j5       r\S\4S
 j5       r\S\4S j5       rSrg)ControlProtocol   z{
Protocol implementation for control socket communication.

Uses 4-byte big-endian length prefix followed by JSON payload.
i   datareturnc                     [         R                  " U 5      R                  S5      n[        R                  " S[        U5      5      nX!-   $ )zs
Encode a message for transmission.

Args:
    data: Dictionary to encode

Returns:
    Length-prefixed JSON bytes
utf-8>I)jsondumpsencodestructpacklen)r   payloadlengths      r   encode_messageControlProtocol.encode_message)   s:     **T"))'2T3w<0r   c                    [        U 5      S:  a  [        S5      e[        R                  " SU SS 5      S   n[        U 5      SU-   :  a  [        S5      eU SSU-    n[        R
                  " UR                  S5      5      $ )zy
Decode a message from bytes.

Args:
    data: Raw bytes (length prefix + JSON payload)

Returns:
    Decoded dictionary
   zMessage too shortr   Nr   zIncomplete messager   )r   r   r   unpackr   loadsdecode)r   r   r   s      r   decode_messageControlProtocol.decode_message8   sz     t9q= 344tT"1X.q1t9q6z! 455qV$zz'..122r   c                    Sn[        U5      S:  aV  U R                  S[        U5      -
  5      nU(       d  U(       d  [        S5      e[        S5      eX-  n[        U5      S:  a  MV  [        R
                  " SU5      S   nU[        R                  :  a  [        SU 35      eSn[        U5      U:  aN  U R                  [        U[        U5      -
  S5      5      nU(       d  [        S	5      eXB-  n[        U5      U:  a  MN   [        R                  " UR                  S
5      5      $ ! [        R                   a  n[        SU 35      eSnAff = f)z
Read one message from a socket.

Args:
    sock: Socket to read from

Returns:
    Decoded message dictionary

Raises:
    ProtocolError: If message is malformed
    ConnectionError: If connection is closed
r   r"   zConnection closedzIncomplete length prefixr   r   Message too large: i   zIncomplete payloadr   Invalid JSON: N)r   recvConnectionErrorr   r   r#   r   MAX_MESSAGE_SIZEminr   r$   r%   JSONDecodeError)socklength_datachunkr   payload_dataes         r   read_messageControlProtocol.read_messageM   s;     +"IIa#k"223E")*=>>#$>?? K +" t[1!4O444"5fX >?? ,&(IIc&3|+<"<eDEE#$899!L	 ,&(	6::l11':;;## 	6. 455	6s   $D) )E=EEc                 P    [         R                  U5      nU R                  U5        g)zi
Write one message to a socket.

Args:
    sock: Socket to write to
    data: Message dictionary to send
N)r   r   sendall)r0   r   messages      r   write_messageControlProtocol.write_messagex   s      "006Wr   c                   #    U R                  S5      I Sh  vN n[        R                  " SU5      S   nU[        R                  :  a  [        SU 35      eU R                  U5      I Sh  vN n [        R                  " UR                  S5      5      $  N N,! [        R                   a  n[        SU 35      eSnAff = f7f)zy
Read one message from an async reader.

Args:
    reader: asyncio StreamReader

Returns:
    Decoded message dictionary
r"   Nr   r   r)   r   r*   )
readexactlyr   r#   r   r-   r   r   r$   r%   r/   )readerr1   r   r3   r4   s        r   read_message_async"ControlProtocol.read_message_async   s      #..q11t[1!4O444"5fX >?? $//77	6::l11':;; 2 8 ## 	6. 455	6sF   CBAC,B-C2$B CCC/B==CCc                    #    [         R                  U5      nU R                  U5        U R                  5       I Sh  vN   g N7f)zt
Write one message to an async writer.

Args:
    writer: asyncio StreamWriter
    data: Message dictionary to send
N)r   r   writedrain)writerr   r9   s      r   write_message_async#ControlProtocol.write_message_async   s2      "006Wllns   :AAAr   N)r   r   r	   r
   r   r-   staticmethoddictbytesr   r&   r5   r:   r?   rE   r   r   r   r   r   r      s     ( T  e     3U 3t 3 3( (6d (6 (6T 	$ 	 	 6D 6 62 
 
 
r   r   
request_idcommandargsr   c                      U UU=(       d    / S.$ )z
Create a request message.

Args:
    request_id: Unique request identifier
    command: Command name (e.g., "show workers")
    args: Optional list of arguments

Returns:
    Request dictionary
)idrK   rL   r   )rJ   rK   rL   s      r   make_requestrO      s     
 r   r   c                      U SU=(       d    0 S.$ )z
Create a success response message.

Args:
    request_id: Request identifier being responded to
    data: Response data

Returns:
    Response dictionary
ok)rN   statusr   r   )rJ   r   s     r   make_responserS      s     
 r   errorc                     U SUS.$ )z
Create an error response message.

Args:
    request_id: Request identifier being responded to
    error: Error message

Returns:
    Error response dictionary
rT   )rN   rR   rT   r   )rJ   rT   s     r   make_error_responserV      s      r   )N)r   r   r   	Exceptionr   r   intstrlistrH   rO   rS   rV   r   r   r   <module>r[      s   
$   I  J JZS 3 d d &c   $C   r   