U
    e»{j½  ã                   @   st   d dl mZ d dlmZ d dlmZ ddlmZmZ g Z	edƒZ
edƒZG dd	„ d	eƒZG d
d„ deƒZdd„ ZdS )é    )Úparse_qs)Úurlopen)Úget_pkg_data_contentsé   )ÚSAMPSimpleXMLRPCRequestHandlerÚThreadingXMLRPCServerzdata/crossdomain.xmlzdata/clientaccesspolicy.xmlc                   @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚWebProfileRequestHandlerzG
    Handler of XMLRPC requests performed through the Web Profile.
    c                 C   s    | j  d¡d k	rœ| j  d¡}|rp| jdkrp|  dd¡ |  d| j  d¡¡ |  d|¡ |  dd	¡ |  d
d¡ n,|  d| j  d¡¡ |  dd	¡ |  d
d¡ d S )NZOriginzAccess-Control-Request-MethodÚOPTIONSúContent-LengthÚ0zAccess-Control-Allow-OriginzAccess-Control-Allow-MethodszAccess-Control-Allow-HeadersúContent-Typez Access-Control-Allow-CredentialsÚtrue)ÚheadersÚgetÚcommandÚsend_header)ÚselfÚmethod© r   úW/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/samp/web_profile.pyÚ_send_CORS_header   s"     
ÿ 
ÿz*WebProfileRequestHandler._send_CORS_headerc                 C   s   |   ¡  t | ¡ d S ©N)r   r   Úend_headers©r   r   r   r   r   *   s    z$WebProfileRequestHandler.end_headersc                 C   sÊ   d}| j dkrft}|  dd¡ |  dd¡ |  dt|ƒ› ¡ |  ¡  | j | d¡¡ | j 	¡  d	}n`| j d
krÆt
}|  dd¡ |  dd¡ |  dt|ƒ› ¡ |  ¡  | j | d¡¡ | j 	¡  d	}|S )NFú/crossdomain.xmléÈ   ÚOKr   ztext/x-cross-domain-policyr
   zutf-8Tú/clientaccesspolicy.xmlztext/xml)ÚpathÚCROSS_DOMAINÚsend_responser   Úlenr   ÚwfileÚwriteÚencodeÚflushÚCLIENT_ACCESS_POLICY)r   Zcross_domainÚresponser   r   r   Ú_serve_cross_domain_xml.   s(    



z0WebProfileRequestHandler._serve_cross_domain_xmlc                 C   s   |   ¡ rd S t | ¡S r   )r(   r   Údo_POSTr   r   r   r   r)   K   s    z WebProfileRequestHandler.do_POSTc                 C   s$   |   ¡ s|  ¡  d S |  ¡ r d S d S r   )Úis_http_path_validÚ
report_404r(   r   r   r   r   Údo_HEADQ   s
    z WebProfileRequestHandler.do_HEADc                 C   s   |   dd¡ |  ¡  d S )Nr   r   )r    r   r   r   r   r   Ú
do_OPTIONSY   s    z#WebProfileRequestHandler.do_OPTIONSc                 C   s¶   |   ¡ s|  ¡  d S | j d¡}|d dd„ | jjD ƒkr¦t|d ƒ}z@t|d d ƒ}|  dd¡ |  	¡  | j
 | ¡ ¡ | ¡  W n tk
r¤   |  ¡  Y d S X |  ¡ r²d S d S )	Nú?r   c                 S   s   g | ]}d |› �‘qS ©z/translator/r   ©Ú.0Zclidr   r   r   Ú
<listcomp>d   s     z3WebProfileRequestHandler.do_GET.<locals>.<listcomp>r   Úrefr   r   )r*   r+   r   ÚsplitÚserverÚclientsr   r   r    r   r"   r#   ÚreadÚcloseÚOSErrorr(   )r   Z
split_pathZurlpathZ	proxyfiler   r   r   Údo_GET]   s"    zWebProfileRequestHandler.do_GETc                 C   s.   ddgdd„ | j jD ƒ }| j d¡d |kS )Nr   r   c                 S   s   g | ]}d |› �‘qS r/   r   r0   r   r   r   r2   u   s    z?WebProfileRequestHandler.is_http_path_valid.<locals>.<listcomp>r.   r   )r5   r6   r   r4   )r   Zvalid_pathsr   r   r   r*   t   s    ÿz+WebProfileRequestHandler.is_http_path_validN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r)   r,   r-   r:   r*   r   r   r   r   r      s   r   c                   @   s4   e Zd ZdZdedddfdd„Zdd„ Zdd	„ ZdS )
ÚWebProfileXMLRPCServerz8
    XMLRPC server supporting the SAMP Web Profile.
    NTc              	   C   s    g | _ t | ||||||¡ d S r   )r6   r   Ú__init__)r   ÚaddrÚlogZrequestHandlerZlogRequestsÚ
allow_noneÚencodingr   r   r   r@   €   s    	      ÿzWebProfileXMLRPCServer.__init__c                 C   s   | j  |¡ d S r   )r6   Úappend©r   Z	client_idr   r   r   Ú
add_clientŽ   s    z!WebProfileXMLRPCServer.add_clientc                 C   s*   z| j  |¡ W n tk
r$   Y nX d S r   )r6   ÚremoveÚ
ValueErrorrF   r   r   r   Úremove_client‘   s    z$WebProfileXMLRPCServer.remove_client)r;   r<   r=   r>   r   r@   rG   rJ   r   r   r   r   r?   {   s   ù
r?   c                 C   sd   d}t | d tƒr| d }n| d d }d|› d| d › d�}t|ƒ tdƒ}| | ¡ d	k¡ d S )
NÚunknownr   z	samp.namez.A Web application which declares to be

Name: z	
Origin: é   zÛ

is requesting to be registered with the SAMP Hub.
Pay attention that if you permit its registration, such
application will acquire all current user privileges, like
file read/write.

Do you give your consent? [yes|no]z>>> )ÚyesÚy)Ú
isinstanceÚstrÚprintÚinputÚputÚlower)ÚrequestÚqueueZ	samp_nameÚtextZanswerr   r   r   Úweb_profile_text_dialog›   s    
þýrX   N)Úurllib.parser   Úurllib.requestr   Zastropy.utils.datar   Zstandard_profiler   r   Ú__all__r   r&   r   r?   rX   r   r   r   r   Ú<module>   s   j 