a
    ߙfb                     @   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 u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OPTIONSContent-Length0zAccess-Control-Allow-OriginzAccess-Control-Allow-MethodszAccess-Control-Allow-HeadersContent-Typez Access-Control-Allow-Credentialstrue)ZheadersgetZcommandsend_header)selfmethod r   7lib/python3.9/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rt
}| 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_domainZ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%   O   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_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 v rt|d }z@t|d d }| dd | 	  | j
|  |  W n ty   |   Y d S 0 |  rd S d S )	N?r   c                 S   s   g | ]}d | qS z/translator/r   .0Zclidr   r   r   
<listcomp>k       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 |v S )Nr   r   c                 S   s   g | ]}d | qS r+   r   r,   r   r   r   r.   ~   r/   z?WebProfileRequestHandler.is_http_path_valid.<locals>.<listcomp>r*   r   )r2   r3   r   r1   )r   Zvalid_pathsr   r   r   r&   {   s    z+WebProfileRequestHandler.is_http_path_validN)__name__
__module____qualname____doc__r   r   r$   r%   r(   r)   r7   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   )r3   r   __init__)r   ZaddrlogZrequestHandlerZlogRequestsZ
allow_noneencodingr   r   r   r=      s    zWebProfileXMLRPCServer.__init__c                 C   s   | j | d S r   )r3   appendr   Z	client_idr   r   r   
add_client   s    z!WebProfileXMLRPCServer.add_clientc                 C   s(   z| j | W n ty"   Y n0 d S r   )r3   remove
ValueErrorrA   r   r   r   remove_client   s    z$WebProfileXMLRPCServer.remove_client)r8   r9   r:   r;   r   r=   rB   rE   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	v  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>>> )Zyesy)
isinstancestrprintinputZputlower)ZrequestZqueueZ	samp_nametextZanswerr   r   r   web_profile_text_dialog   s    
rO   N)Zurllib.parser   Zurllib.requestr   Zastropy.utils.datar   Zstandard_profiler   r   __all__r   r#   r   r<   rO   r   r   r   r   <module>   s   p