
    h&                         S SK r S SKrS SKrS SKrSSKJrJrJrJrJ	r	J
r
JrJr  \R                  " \5      r " S S\5      r " S S\5      r " S S	\ R&                  5      r " S
 S\5      rg)    N   )	JsonError
JsonObject	JsonValuecreate_objectget_intget_strget_str_or_nonetypecheckedc            	       ^   ^  \ rS rSr% Sr\\S'   SS\SSS\S	S4U 4S
 jjjr	S	\4S jr
SrU =r$ )CockpitProblem   a  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problemmsgJsonObject | Nonekwargsreturnc                 v   > XS'   [        X#5      U l        [        TU ]  [	        U R                  SU5      5        g )Nr   message)r   r   super__init__r	   )selfr   r   r   	__class__s       2/usr/lib/python3/dist-packages/cockpit/protocol.pyr   CockpitProblem.__init__*   s1    #y"3/
Y@A    c                    U R                   S   S:X  ai  U R                  b\  [        U R                   [        R                  " U R                  R
                  U R                  U R                  R                  5      S9$ U R                   $ )Nr   zinternal-error)cause)r   	__cause__dict	tracebackformat_exceptionr   __traceback__r   s    r   	get_attrsCockpitProblem.get_attrs/   sh    ::i $449S

)*D*D(($..$..:V:V+   ::r   )r   N)__name__
__module____qualname____firstlineno____doc__r   __annotations__strr   r   r&   __static_attributes____classcell__r   s   @r   r   r      sP    
 B B*= BPY B^b B B
:  r   r   c                   8   ^  \ rS rSrSS\S\4U 4S jjjrSrU =r$ )CockpitProtocolError8   r   r   c                     > [         TU ]  X!S9  g )N)r   )r   r   )r   r   r   r   s      r   r   CockpitProtocolError.__init__9   s    2r    )zprotocol-error)r)   r*   r+   r,   r/   r   r0   r1   r2   s   @r   r4   r4   8   s    3 3c 3 3r   r4   c                   f   \ rS rSr% SrSrS\S'   SrSr\	\S'   Sr
S	\S
'   S%S jrS&S jrS\S\SS4S jrS\S\S\SS4S jrS\S\SS4S jrS\SS4S jrS\SS4S jrS\S\4S jrS\R0                  SS4S jrS&S jrS'S&S jjrS\S\SS4S jrS'SSS \SS4S! jjrS\SS4S" jrS\	4S# jr S$r!g)(CockpitProtocol=   zA naive implementation of the Cockpit frame protocol

We need to use this because Python's SelectorEventLoop doesn't supported
buffered protocols.
Nzasyncio.Transport | None	transportr   F_closedzasyncio.Future[None] | None_communication_doner   c                     g r(   r8   r%   s    r   do_readyCockpitProtocol.do_readyH       r   c                     g r(   r8   r   excs     r   	do_closedCockpitProtocol.do_closedK   rB   r   commandr   c                     [         er(   NotImplementedErrorr   rH   r   s      r   transport_control_received*CockpitProtocol.transport_control_receivedN       !!r   channelc                     [         er(   rJ   )r   rP   rH   r   s       r   channel_control_received(CockpitProtocol.channel_control_receivedQ   rO   r   datac                     [         er(   rJ   )r   rP   rT   s      r   channel_data_received%CockpitProtocol.channel_data_receivedT   rO   r   framec                     UR                  S5      u  p#nUS:w  aC  UR                  S5      n[        R                  S[	        U5      U5        U R                  XT5        g U R                  U5        g )N   
r   asciiz.data received: %d bytes of data for channel %s)	partitiondecodeloggerdebuglenrV   control_received)r   rX   header_rT   rP   s         r   frame_receivedCockpitProtocol.frame_receivedW   s]    //%04S=mmG,GLLI3t9V]^&&w5 !!$'r   c                     [        [        R                  " U5      [        5      n[	        US5      n[	        USS 5      nUb)  [
        R                  SU5        U R                  XCU5        g [
        R                  SU5        U R                  X25        g ! [        R                  [        4 a  n[        SU< 35      UeS nAff = f)NrH   rP   zchannel control received %sztransport control received %szcontrol message: )r   jsonloadsr!   r	   r^   r_   rR   rM   JSONDecodeErrorr   r4   )r   rT   r   rH   rP   rE   s         r   ra    CockpitProtocol.control_receivedb   s    	M!$**T"2D9Ggy1Ggy$7G":GD--gH<gF//A$$i0 	M&):3''BCL	Ms   A(B +'B C-B==Cc                 v    UR                  S5      n [	        USU 5      nUS-   nXT-   nU[        U5      :  a  [        U5      U-
  $ U R                  XU 5        U$ ! [         a3  n[        U5      S:  a  [        U5      S-
  s SnA$ [        S5      UeSnAff = f! [         a  n[        S5      UeSnAff = f)zConsumes 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.
rZ   
   Nzsize line is too longzframe size is not an integerr   )index
ValueErrorr`   r4   intrd   )r   rT   newlinerE   lengthstartends          r   consume_one_frame!CockpitProtocol.consume_one_framer   s    	Ijj'G	Phw(F !nT?t9s?" 	DsO,
)  	I4y2~4y2~%&'>?SH		I  	P&'EFCO	Ps:   A B 
B'BB	BB
B8'B33B8c                    [         R                  SU5        [        U[        R                  5      (       d   eXl        U R                  5         U R                  (       a&  [         R                  S5        UR                  5         g g )Nzconnection_made(%s)z;  but the protocol already was closed, so closing transport)	r^   r_   
isinstanceasyncio	Transportr<   r@   r=   close)r   r<   s     r   connection_madeCockpitProtocol.connection_made   s[    *I6)W%6%67777"<<LLVWOO r   c                 |    [         R                  S5        U R                  c   eS U l        U R                  U5        g )Nconnection_lost)r^   r_   r<   rz   rD   s     r   r~   CockpitProtocol.connection_lost   s1    &'~~)))

3r   c                     U R                   (       a  g SU l         U R                  (       a  U R                  R                  5         U R                  U5        g )NT)r=   r<   rz   rF   rD   s     r   rz   CockpitProtocol.close   s6    <<>>NN  "sr   payloadc                 &   [        US-   5      [        U5      -   nU SU S3R                  S5      nU R                  b>  [        R	                  SU R                  5        U R                  R                  XB-   5        g[        R	                  S5        g)z0Send a given payload (bytes) on channel (string)
r[   Nzwriting to transport %sz cannot write to closed transport)r`   encoder<   r^   r_   write)r   rP   r   frame_lengthrb   s        r   write_channel_data"CockpitProtocol.write_channel_data   sw     7T>*S\9 >G9B/66w?>>%LL2DNNCNN  !12LL;<r   r   r   r   c                     [         R                  SX5        [        R                  " [	        X5      SS9S-   nU R                  SUR                  5       5        g)zCWrite a control message.  See jsonutil.create_object() for details.zsending control message %r %r   )indentr    N)r^   r_   rg   dumpsr   r   r   )r   r   r   prettys       r   write_controlCockpitProtocol.write_control   sA    4cBM#6qADHFMMO4r   c                 6    U =R                   U-  sl         U R                   (       aJ  U R                  U R                   5      nUS::  a  g U R                   US  U l         U R                   (       a  MI  g g ! [         a  nU R                  U5         S nAg S nAff = f)Nr   )bufferrt   r4   rz   )r   rT   resultrE   s       r   data_receivedCockpitProtocol.data_received   sv    	KK4K++//<Q;"kk&'2	 +++
 $ 	JJsOO	s   AA3 
%A3 3
B=BBc                     g)NFr8   r%   s    r   eof_receivedCockpitProtocol.eof_received   s    r   )r=   r   r<   r   N)rE   zException | Noner   Nr(   )"r)   r*   r+   r,   r-   r<   r.   r   r=   boolr>   r@   rF   r/   r   rM   rR   bytesrV   rd   ra   ro   rt   rx   BaseTransportr{   r~   rz   r   r   r   r   r   r0   r8   r   r   r:   r:   =   sC   
 -1I)0FGT9=6="# "
 "t "" "c "J "[_ ""S " "$ "	(E 	(d 	(MU Mt M e  >)>)> 4 	=# 	= 	=$ 	=5!4 5y 5UY 5	% 	D 	d r   r:   c            	           \ rS rSr% SrS\S'   SrS\S'   SrSS	 jrS
\	SS4S jr
SSSSS
\	SS4S jrS\S
\	SS4S jrSS jr SS\SSS\S\	4S jjr SS\SSS\S\4S jjrS
\	SS4S jrSrg)CockpitProtocolServer   Nz
str | None	init_hostz,dict[str, asyncio.Future[JsonObject]] | Noneauthorizationsr   r   c                     [         er(   rJ   r%   s    r   do_send_init"CockpitProtocolServer.do_send_init   rO   r   r   c                     g r(   r8   )r   r   s     r   do_initCockpitProtocolServer.do_init   rB   r   hostgroupc                     [         er(   rJ   )r   r   r   r   s       r   do_killCockpitProtocolServer.do_kill   rO   r   rH   c                 8   US:X  a>  [        US5      S:w  a  [        S5      e[        US5      U l        U R	                  U5        g US:X  a*  U R                  [        USS 5      [        USS 5      U5        g US:X  a  U R                  U5        g [        S	U S
35      e)Ninitversionr   zincorrect version numberr   killr   	authorizezunexpected control message z	 received)r   r4   r	   r   r   r   r
   do_authorizerL   s      r   rM   0CockpitProtocolServer.transport_control_received   s    fw	*a/*+EFF$Wf5DNLL!LL&$?QXZacgAhjqr#g&&)DWIY'WXXr   c                 $    U R                  5         g r(   )r   r%   s    r   r@   CockpitProtocolServer.do_ready   s    r   	challengetimeoutz
int | Noner   c                   #    U R                   c  0 U l         [        U R                  5      nU =R                  S-  sl        [        R                  " 5       R                  5       n XPR                   U'   U R                  " SSXS.UD6  [        R                  " XR5      I S h  vN U R                   R                  U5        $  N! U R                   R                  U5        f = f7f)Nr   r   )rH   r   cookier(   )	r   r/   next_auth_idrx   get_running_loopcreate_futurer   wait_forpop)r   r   r   r   r   futures         r   request_authorization_object2CockpitProtocolServer.request_authorization_object   s      &"$DT&&'Q))+99;	,*0'g[Ig`fg ))&::##F+ ;##F+s0   A"C#%<C !C"C %C#C C  C#c                 X   #    [        U R                  " X40 UD6I S h  vN S5      $  N	7f)Nresponse)r	   r   )r   r   r   r   s       r   request_authorization+CockpitProtocolServer.request_authorization   s,      T>>y\U[\\^hii\s   *(
*c                     [        US5      nU R                  b  X R                  ;  a  [        R                  S5        g U R                  U   R	                  U5        g )Nr   zno matching authorize request)r	   r   r^   warning
set_result)r   r   r   s      r   r   "CockpitProtocolServer.do_authorize   sN    (+&&8K8K*KNN:;F#..w7r   )r   r   r   r(   )r)   r*   r+   r,   r   r.   r   r   r   r   r   r   r/   rM   r@   r   r   r   r   r0   r8   r   r   r   r      s    "I|"EINBIL"z d "L " "
 "W[ "Y# Y
 Yt Y
 7;,,'3,FO,	,  7;jj'3jFOj	j
8J 84 8r   r   )rx   rg   loggingr"   jsonutilr   r   r   r   r   r	   r
   r   	getLoggerr)   r^   	Exceptionr   r4   Protocolr:   r   r8   r   r   <module>r      se   $     u u u			8	$Y 83> 3
Kg&& K^:8O :8r   