
    XBe                         d Z ddlZddlZddlZddlZddlmZ ddlmZ d Z	 G d dej                  j                        Zy)z9Tornado websocket handler to serve a terminal interface.
    N)gen)run_on_executorc                 H    t        | t              r| j                  d      S | S )Nzutf-8)
isinstancebytesdecode)ss    3lib/python3.12/site-packages/terminado/websocket.py_cast_unicoder      s     !Uxx  H    c                        e Zd ZdZd ZddZd fd	Zd Zd Ze	j                  d        Zd	 Zd
 ZddeddfdZ ed      d        Z xZS )
TermSocketz Handler for a terminal websocketc                 
   || _         d| _        d| _        d | _        |j                  | _        t        j                  t              | _	        d| _
        t        j                  t        j                  dd            dk(  | _        y )N )NNLOG_TERMINAL_OUTPUTfalsetrue)term_manager	term_namesizeterminalblocking_io_executor_blocking_io_executorlogging	getLogger__name___logger_user_commandstrlowerosgetenv_enable_output_logging)selfr   s     r
   
initializezTermSocket.initialize   sm    ( 	%1%F%F"((2 '*ii		:OQX0Y&Z^d&d#r   Nc                 r    | j                  |xs% | j                  j                  j                  d            S )z1Deprecated: backward-compat for terminado <= 0.5.Origin)check_originrequestheadersget)r$   origins     r
   origin_checkzTermSocket.origin_check&   s-      !M4<<+?+?+C+CH+MNNr   c                 ,   t         |   |       | j                  j                  d|       t	        |      }|xs d| _        | j                  j                  |      | _        | j                  j                  j                  |        | j                  di g       | j                  j                  d| j
                         d}| j                  j                  j                         }	 |sn|j                         }||z  }|r| j                  |       yy)zWebsocket connection opened.

        Call our terminal manager to get a terminal, and connect to it as a
        client.
        zTermSocket.open: %sttysetupzTermSocket.open: Opened %sr   N)superopenr   infor   r   r   get_terminalr   clientsappendsend_json_messageread_buffercopypoplefton_pty_read)r$   url_componentbufferedpreopen_bufferr	   	__class__s        r
   r2   zTermSocket.open*   s     	]#/?%m4&/%))66}E$$T*}-6G22779!&&(AMH	 
 X& r   c                 *    | j                  d|g       y)z$Data read from pty; send to frontendstdoutN)r7   r$   texts     r
   r;   zTermSocket.on_pty_readG   s    $/0r   c                     t        j                  |      }| j                  |       | j                  r5|d   dk(  r,t	        |d   t
              r| j                  d|d           y y y y )Nr   rA      zSTDOUT: )jsondumpswrite_messager#   r   r   log_terminal_output)r$   contentjson_msgs      r
   r7   zTermSocket.send_json_messageK   sg    ::g&8$&&qzX%*WQZ*E((8GAJ<)@A +F% 'r   c              #     K   t        j                  |      }|d   }| j                  J |dk(  rj| j                  |d          | j                  rG|d   dk(  r&| j                  d| j                          d| _        y| xj                  |d   z  c_        yy|dk(  r%|dd	 | _        | j                  j                          yyw)
zHandle incoming websocket message

        We send JSON arrays, where the first element is a string indicating
        what kind of message this is. Data associated with the message follows.
        r   NstdinrE   zSTDIN: r   set_size   )	rF   loadsr   stdin_to_ptyprocr#   rI   r   r   resize_to_smallest)r$   messagecommandmsg_types       r
   
on_messagezTermSocket.on_messageS   s      **W%1:}}((w''
33**1:%,,wt7I7I6J-KL)+D&&&'!*4& + #!DIMM,,. $s   CCc                    | j                   j                  d       | j                  r?| j                  j                  j	                  |        | j                  j                          | j                  j                  |        y)zHandle websocket closing.

        Disconnect from our terminal, and tell the terminal manager we're
        disconnecting.
        zWebsocket closedN)r   r3   r   r5   removerS   r   client_disconnectedr$   s    r
   on_closezTermSocket.on_closej   sZ     	,-==MM!!((.MM,,.--d3r   c                 X    | j                  ddg       | j                          d| _        y)z9Terminal closed: tell the frontend, and close the socket.
disconnectrE   N)r7   closer   r[   s    r
   on_pty_diedzTermSocket.on_pty_diedv   s%    a01

r   logreturnc                 :    | j                   j                  |       y)zg
        Logs the terminal input/output
        :param log: log line to write
        :return:
        N)r   debug)r$   ra   s     r
   rI   zTermSocket.log_terminal_output|   s     	3r   r   )executorc                 h    | j                   &| j                   j                  j                  |       yy)a   Handles stdin messages sent on the websocket.

        This is a blocking call that should NOT be performed inside the
        server primary event loop thread. Messages must be handled
        asynchronously to prevent blocking on the PTY buffer.
        N)r   ptyprocwriterB   s     r
   rR   zTermSocket.stdin_to_ptyproc   s+     ==$MM!!''- %r   )N)r   )r   
__module____qualname____doc__r%   r-   r2   r;   r7   r   	coroutinerW   r\   r`   r   rI   r   rR   __classcell__)r?   s   @r
   r   r      sq    *eO':1B 	]]/ /,
4 s  D   56. 7.r   r   )rk   rF   r   r!   tornado.websockettornador   tornado.concurrentr   r   	websocketWebSocketHandlerr    r   r
   <module>rt      s=      	   .w.""33 w.r   