
    Xh                        d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	Z
ddl
mZ ddlmZ erddlmZmZ ddZ G d	 d
e
j$                  j&                        Zy)z9Tornado websocket handler to serve a terminal interface.
    )annotationsN)TYPE_CHECKINGAny)gen)run_on_executor)PtyWithClientsTermManagerBasec                H    t        | t              r| j                  d      S | S )Nzutf-8)
isinstancebytesdecode)ss    G/var/www/html/myenv/lib/python3.12/site-packages/terminado/websocket.py_cast_unicoder      s     !Uxx  H    c                       e Zd ZdZddZdddZdd fdZddZddZe	j                  dd       ZddZdd	Zddd
Z ed      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)zInitialize the handler. )NNN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    ( 	/3%1%F%F"((2 '*ii		:OQX0Y&Z^d&d#r   c                    |xs& | j                   j                  j                  dd      }|J | j                  |      S )z1Deprecated: backward-compat for terminado <= 0.5.Originr   )requestheadersgetcheck_origin)r)   origins     r   origin_checkzTermSocket.origin_check,   s@    A4<<//33HbA!!!  ((r   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   r7   zTermSocket.open2   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)r<   r)   texts     r   r@   zTermSocket.on_pty_readO   s    $/0r   c                    t        j                  |      }| j                  |       | j                  r5|d   dk(  r,t	        |d   t
              r| j                  d|d           yyyy)z"Send a json message on the socket.r   rF      zSTDOUT: N)jsondumpswrite_messager(   r   r$   log_terminal_output)r)   contentjson_msgs      r   r<   zTermSocket.send_json_messageS   si    ::g&8$&&71:+AjQXYZQ[]`Fa$$x
|%<= Gb+A&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stdinrJ   zSTDIN: r   set_size   )	rK   loadsr   stdin_to_ptyprocr(   rN   r#   r   resize_to_smallest)r)   messagecommandmsg_types       r   
on_messagezTermSocket.on_message[   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"   r8   r   r:   removerX   r   client_disconnectedr)   s    r   on_closezTermSocket.on_closer   sZ     	,-==MM!!((.MM,,.--d3r   c                X    | j                  ddg       | j                          d| _        y)z9Terminal closed: tell the frontend, and close the socket.
disconnectrJ   N)r<   closer   r`   s    r   on_pty_diedzTermSocket.on_pty_died~   s%    a01

r   c                :    | j                   j                  |       y)zg
        Logs the terminal input/output
        :param log: log line to write
        :return:
        N)r"   debug)r)   logs     r   rN   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writerG   s     r   rW   zTermSocket.stdin_to_ptyproc   s+     ==$MM!!''- %r   )r   r	   returnNone)N)r1   z
str | Nonerm   bool)rA   r   rm   rn   )rH   r$   rm   rn   )rO   r   rm   rn   )rY   r$   rm   rn   )rm   rn   )r   )rh   r$   rm   rn   )r!   
__module____qualname____doc__r*   r2   r7   r@   r<   r   	coroutiner\   ra   re   rN   r   rW   __classcell__)rD   s   @r   r   r      sa    *e)':1> 	]]/ /,
4  56. 7.r   r   )r   zstr | bytesrm   r$   )rr   
__future__r   rK   r   r&   typingr   r   tornado.websockettornador   tornado.concurrentr   terminado.managementr   r	   r   	websocketWebSocketHandlerr    r   r   <module>r~      sI   
 #   	 %   .Dz.""33 z.r   