a
    4_bö  ã                   @   s„   d Z ddlmZmZ ddlZzddlmZ W n eyJ   ddlmZ Y n0 ddlZddl	Z	ddl
Zdd„ ZG dd„ dejjƒZdS )	z9Tornado websocket handler to serve a terminal interface.
é    )Úabsolute_importÚprint_functionN)Úurlparsec                 C   s   t | tƒr|  d¡S | S )Nzutf-8)Ú
isinstanceÚbytesÚdecode)Ús© r	   ú2lib/python3.9/site-packages/terminado/websocket.pyÚ_cast_unicode   s    

r   c                       sl   e Zd ZdZdd„ Zddd„Zd‡ fdd„	Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdedœdd„Z‡  ZS )Ú
TermSocketz Handler for a terminal websocketc                 C   sF   || _ d| _d| _d | _t t¡| _d| _t	 
t dd¡¡dk| _d S )NÚ )NNZLOG_TERMINAL_OUTPUTZfalseÚtrue)Úterm_managerÚ	term_nameÚsizeÚterminalÚloggingZ	getLoggerÚ__name__Ú_loggerÚ_user_commandÚstrÚlowerÚosÚgetenvÚ_enable_output_logging)Úselfr   r	   r	   r
   Ú
initialize    s    zTermSocket.initializeNc                 C   s   |   |p| jj d¡¡S )z1Deprecated: backward-compat for terminado <= 0.5.ZOrigin)Zcheck_originZrequestZheadersÚget)r   Úoriginr	   r	   r
   Úorigin_check,   s    zTermSocket.origin_checkc                    s¤   t t| ƒ |¡ | j d|¡ t|ƒ}|p,d| _| j |¡| _	| j	j
 | ¡ |  di g¡ | j d| j¡ d}| j	j ¡ }|s€q’| ¡ }||7 }qz|r |  |¡ dS )z‚Websocket connection opened.

        Call our terminal manager to get a terminal, and connect to it as a
        client.
        zTermSocket.open: %sZttyZsetupzTermSocket.open: Opened %sr   N)Úsuperr   Úopenr   Úinfor   r   r   Zget_terminalr   ÚclientsÚappendÚsend_json_messageZread_bufferÚcopyÚpopleftÚon_pty_read)r   Zurl_componentZbufferedZpreopen_bufferr   ©Ú	__class__r	   r
   r"   0   s     

zTermSocket.openc                 C   s   |   d|g¡ dS )z$Data read from pty; send to frontendÚstdoutN)r&   )r   Útextr	   r	   r
   r)   M   s    zTermSocket.on_pty_readc                 C   sL   t  |¡}|  |¡ | jrH|d dkrHt|d tƒrH|  d|d › ¡ d S )Nr   r,   é   zSTDOUT: )ÚjsonÚdumpsZwrite_messager   r   r   Úlog_terminal_output)r   ZcontentZjson_msgr	   r	   r
   r&   Q   s
    

zTermSocket.send_json_messagec                 C   s   t  |¡}|d }|dkrl| jj |d ¡ | jrŒ|d dkrX|  d| j› ¡ d| _qŒ|  j|d 7  _n |dkrŒ|dd… | _| j 	¡  d	S )
zÇHandle 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   Ústdinr.   úzSTDIN: r   Zset_sizeé   N)
r/   Úloadsr   ZptyprocÚwriter   r1   r   r   Úresize_to_smallest)r   ÚmessageZcommandZmsg_typer	   r	   r
   Ú
on_messageY   s    
zTermSocket.on_messagec                 C   s:   | j  d¡ | jr*| jj | ¡ | j ¡  | j | ¡ dS )z„Handle websocket closing.

        Disconnect from our terminal, and tell the terminal manager we're
        disconnecting.
        zWebsocket closedN)r   r#   r   r$   Úremover7   r   Zclient_disconnected©r   r	   r	   r
   Úon_closeo   s
    
zTermSocket.on_closec                 C   s    |   ddg¡ |  ¡  d| _dS )zBTerminal closed: tell the frontend, and close the socket.
        Z
disconnectr.   N)r&   Úcloser   r;   r	   r	   r
   Úon_pty_died{   s    zTermSocket.on_pty_diedr   )Úlogc                 C   s   | j  |¡ dS )zg
        Logs the terminal input/output
        :param log: log line to write
        :return:
        N)r   Údebug)r   r?   r	   r	   r
   r1   ‚   s    zTermSocket.log_terminal_output)N)N)r   )r   Ú
__module__Ú__qualname__Ú__doc__r   r    r"   r)   r&   r9   r<   r>   r   r1   Ú__classcell__r	   r	   r*   r
   r      s   
r   )rC   Z
__future__r   r   r   Zurllib.parser   ÚImportErrorr/   r   Ztornado.websocketZtornador   Z	websocketZWebSocketHandlerr   r	   r	   r	   r
   Ú<module>   s   