
    c                     x    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dS )z9Tornado websocket handler to serve a terminal interface.
    N)gen)run_on_executorc                 Z    t          | t                    r|                     d          S | S )Nzutf-8)
isinstancebytesdecode)ss    3lib/python3.11/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                              t          j        dd                    dk    | _        d S )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   st    ( 	%1%F"(22 '*ii	:OQX0Y0Y&Z&Z^d&d###r   Nc                 j    |                      |p| 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+MNNNr   c                 H   t                                          |           | j                            d|           t	          |          }|pd| _        | j                            |          | _        | j        j	        
                    |            |                     di g           | j                            d| j                   d}| j        j                                        }	 |sn|                                }||z  }|r|                     |           dS dS )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   TN)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4&/%)66}EE$$T***}---6GGG27799	! &&((AMH		
  	'X&&&&&	' 	'r   c                 4    |                      d|g           dS )z$Data read from pty; send to frontendstdoutN)r7   r$   texts     r
   r;   zTermSocket.on_pty_readG   s!    $/00000r   c                     t          j        |          }|                     |           | j        rG|d         dk    r=t	          |d         t
                    r$|                     d|d                     d S d S d S d S )Nr   rA      zSTDOUT: )jsondumpswrite_messager#   r   r   log_terminal_output)r$   contentjson_msgs      r
   r7   zTermSocket.send_json_messageK   s    :g&&8$$$& 	BqzX% B*WQZ*E*E B(()@GAJ)@)@AAAAA	B 	BB B B Br   c              #     K   t          j        |          }|d         }| j        J |dk    rp|                     |d                   V  | j        rJ|d         dk    r&|                     d| j                    d| _        dS | xj        |d         z  c_        dS dS |dk    r*|dd	         | _        | j                                         dS dS )
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333* 51:% 5,,-Kt7I-K-KLLL)+D&&&&&'!*4&&&&5 5 # 	/!DIM,,.....	/ 	/r   c                     | j                             d           | j        r8| j        j                            |            | j                                         | j                            |            dS )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   so     	,---= 	/M!((...M,,...--d33333r   c                 j    |                      ddg           |                                  d| _        dS )z9Terminal closed: tell the frontend, and close the socket.
disconnectrE   N)r7   closer   r[   s    r
   on_pty_diedzTermSocket.on_pty_diedv   s3    a0111

r   r   logreturnc                 :    | j                             |           dS )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                 V    | j         !| j         j                            |           dS dS )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   s7     = 	.M!''-----	. 	.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      s       **e e eO O O O' ' ' ' ' ':1 1 1B B B 	]/ / ]/,
4 
4 
4     s  D         _5666. . 76. . . . .r   r   )rk   rF   r   r!   tornado.websockettornador   tornado.concurrentr   r   	websocketWebSocketHandlerr    r   r
   <module>rt      s       				           . . . . . .  w. w. w. w. w."3 w. w. w. w. w.r   