B
    îq\û  ã               @   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-MethodZOPTIONSzContent-LengthÚ0zAccess-Control-Allow-OriginzAccess-Control-Allow-MethodszAccess-Control-Allow-HeaderszContent-Typez Access-Control-Allow-CredentialsÚtrue)ZheadersÚgetZcommandÚsend_header)ÚselfÚmethod© r   ú7lib/python3.7/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rjt}|  dd¡ |  dd¡ |  dd t|ƒ¡¡ |  ¡  | j | 	d	¡¡ | j 
¡  d
}nd| j dkrÎt}|  dd¡ |  dd¡ |  dd t|ƒ¡¡ |  ¡  | j | 	d	¡¡ | j 
¡  d
}|S )NFz/crossdomain.xmléÈ   ÚOKzContent-Typeztext/x-cross-domain-policyzContent-Lengthz{0}zutf-8Tz/clientaccesspolicy.xmlztext/xml)ÚpathÚCROSS_DOMAINÚsend_responser   ÚformatÚlenr   ÚwfileÚwriteÚencodeÚflushÚCLIENT_ACCESS_POLICY)r   Zcross_domainZresponser   r   r   Ú_serve_cross_domain_xml/   s(    



z0WebProfileRequestHandler._serve_cross_domain_xmlc             C   s   |   ¡ rd S t | ¡S )N)r   r   Údo_POST)r   r   r   r   r    O   s    z WebProfileRequestHandler.do_POSTc             C   s$   |   ¡ s|  ¡  d S |  ¡ r d S d S )N)Úis_http_path_validÚ
report_404r   )r   r   r   r   Údo_HEADU   s
    z WebProfileRequestHandler.do_HEADc             C   s   |   dd¡ |  ¡  d S )Nr   r   )r   r   )r   r   r   r   Ú
do_OPTIONS^   s    z#WebProfileRequestHandler.do_OPTIONSc             C   s´   |   ¡ s|  ¡  d S | j d¡}|d dd„ | jjD ƒkr¤t|d ƒ}y@t|d d ƒ}|  dd¡ |  	¡  | j
 | ¡ ¡ | ¡  W n tk
r¢   |  ¡  d S X |  ¡ r°d S d S )	Nú?r   c             S   s   g | ]}d   |¡‘qS )z/translator/{})r   )Ú.0Úclidr   r   r   ú
<listcomp>k   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_GETc   s"    zWebProfileRequestHandler.do_GETc             C   s.   ddgdd„ | j jD ƒ }| j d¡d |kS )Nz/clientaccesspolicy.xmlz/crossdomain.xmlc             S   s   g | ]}d   |¡‘qS )z/translator/{})r   )r&   r'   r   r   r   r(   ~   s    z?WebProfileRequestHandler.is_http_path_valid.<locals>.<listcomp>r%   r   )r+   r,   r   r*   )r   Zvalid_pathsr   r   r   r!   {   s    z+WebProfileRequestHandler.is_http_path_validN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r#   r$   r0   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 )N)r,   r   Ú__init__)r   ZaddrÚlogZrequestHandlerZlogRequestsZ
allow_noneÚencodingr   r   r   r6   ‡   s    zWebProfileXMLRPCServer.__init__c             C   s   | j  |¡ d S )N)r,   Úappend)r   Ú	client_idr   r   r   Ú
add_clientŽ   s    z!WebProfileXMLRPCServer.add_clientc             C   s*   y| j  |¡ W n tk
r$   Y nX d S )N)r,   ÚremoveÚ
ValueError)r   r:   r   r   r   Úremove_client‘   s    z$WebProfileXMLRPCServer.remove_client)r1   r2   r3   r4   r   r6   r;   r>   r   r   r   r   r5   ‚   s
   r5   c             C   s^   d}t | d tƒr| d }n| d d }d || d ¡}t|ƒ tdƒ}| | ¡ dk¡ d S )NÚunknownr   z	samp.namea  A Web application which declares to be

Name: {}
Origin: {}

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>>> )ZyesÚy)Ú
isinstanceÚstrr   ÚprintÚinputZputÚlower)ZrequestZqueueZ	samp_nameÚtextZanswerr   r   r   Úweb_profile_text_dialog›   s    
rH   N)Zurllib.parser   Zurllib.requestr   Zastropy.utils.datar   Zstandard_profiler   r   Ú__all__r   r   r   r5   rH   r   r   r   r   Ú<module>   s   p