o
    i-                     @   s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZmZmZmZmZ G d	d
 d
eZdddZG dd de	ZG dd deZG dd deZdS )    )defaultdict)utils)encode_rfc2231)	tspecials)TransportContextMethodDescriptorMethodContextRedirect)
ServerBase)HttpPattern)gen_body_redirectHTTP_301HTTP_302HTTP_303HTTP_307c                       s*   e Zd Zdef fdd	Zdd Z  ZS )HttpRedirectNc                    s2   t t| j|||d || _|| _|| _|| _d S )N)orig_exc)superr   __init__ctxlocationr   code)selfr   r   r   r   	__class__ Q/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/spyne/server/http.pyr   "   s   
zHttpRedirect.__init__c                 C   sD   t | jjts| jd ur| jt| jj| jjj| j| jd d S )N)r   )	
isinstancer   	transportHttpTransportContextr   	TypeErrorrespondr   r   r   r   r   r   do_redirect+   s
   
zHttpRedirect.do_redirect)__name__
__module____qualname__r   r   r#   __classcell__r   r   r   r   r   !   s    	r   NTc                 C   s   |du s
t |dkr| S t|tr'| d7 } t|d |d |d }d| |f S z|d W n tyG   | d7 } t|dd	}d| |f  Y S w |sOt|rXd
| t	|f S d| |f S )a~  Convenience function to format and return a key=value pair.

    This will quote the value if needed or if quote is true.  If value is a
    three tuple (charset, language, value), it will be encoded according
    to RFC2231 rules.  If it contains non-ascii characters it will likewise
    be encoded according to RFC2231 rules, using the utf-8 charset and
    a null language.
    Nr   *      z%s=%sasciizutf-8 z%s="%s")
lenr   tupler   encodeUnicodeEncodeErrorr   searchr   quote)paramvaluer2   r   r   r   _formatparam;   s    	
r5   c                       s   e Zd ZdZ f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d Zdd Zdd Zedd Zdd Zedd dd Z	 eZ  ZS )r   zThe abstract base class that is used in the transport attribute of the
    :class:`HttpMethodContext` class and its subclasses.c                    sD   t t| ||d || _	 i | _	 || _d | _	 d | _	 d | _d S )Nhttp)	r   r   r   reqresp_headers	mime_type	resp_codewsdl
wsdl_error)r   parentr   requestcontent_typer   r   r   r   d   s   zHttpTransportContext.__init__c                 C   s   | j dd S NzContent-Type)r8   getr"   r   r   r   get_mime_typey      z"HttpTransportContext.get_mime_typec                 C   s   || j d< d S r@   )r8   r   whatr   r   r   set_mime_type|   rC   z"HttpTransportContext.set_mime_typec                 K   sP   || _ |ttttfv r"|d}|| jd< t||g| j_	d| _
d S g | j_	d S )Nr   Locationz	text/html)r:   r   r   r   r   popr8   r   r=   
out_stringr9   )r   r:   kwargslr   r   r   r!      s   


zHttpTransportContext.respondc                 C      t  NNotImplementedErrorr"   r   r   r   get_url      zHttpTransportContext.get_urlc                 C   rL   rM   rN   r"   r   r   r   get_path   rQ   zHttpTransportContext.get_pathc                 C   rL   rM   rN   r"   r   r   r   get_request_method   rQ   z'HttpTransportContext.get_request_methodc                 C   rL   rM   rN   r"   r   r   r   get_request_content_type   rQ   z-HttpTransportContext.get_request_content_typec                 C   rL   rM   rN   r"   r   r   r   get_path_and_qs   rQ   z$HttpTransportContext.get_path_and_qsc                 C   rL   rM   rN   )r   keyr   r   r   
get_cookie   rQ   zHttpTransportContext.get_cookiec                 C   rL   rM   rN   r"   r   r   r   get_peer   rQ   zHttpTransportContext.get_peerc                 K   sh   g }|  D ]\}}|d u r||dd q|t|dd| q| d ur/|d|  d|S )N_-r   z; )itemsappendreplacer5   insertjoin)_valuerJ   partskvr   r   r   
gen_header   s   
zHttpTransportContext.gen_headerc                 K   s   | j |fi || j|< dS )u  Extended header setting.

        name is the header field to add.  keyword arguments can be used to set
        additional parameters for the header field, with underscores converted
        to dashes.  Normally the parameter will be added as key="value" unless
        value is None, in which case only the key will be added.  If a
        parameter value contains non-ASCII characters it can be specified as a
        three-tuple of (charset, language, value), in which case it will be
        encoded according to RFC2231 rules.  Otherwise it will be encoded using
        the utf-8 charset and a language of ''.

        Examples:

        msg.add_header('content-disposition', 'attachment', filename='bud.gif')
        msg.add_header('content-disposition', 'attachment',
                       filename=('utf-8', '', Fußballer.ppt'))
        msg.add_header('content-disposition', 'attachment',
                       filename='Fußballer.ppt'))
        N)rd   r8   )r   _namer`   rJ   r   r   r   
add_header   s   zHttpTransportContext.add_headerc                 C   s   |   S rM   )rB   r"   r   r   r   <lambda>   s    zHttpTransportContext.<lambda>c                 C   s
   |  |S rM   )rF   rD   r   r   r   rg      s   
 )r$   r%   r&   __doc__r   rB   rF   r!   rP   rR   rS   rT   rU   rW   rX   staticmethodrd   rf   propertyr9   r?   r'   r   r   r   r   r   `   s.    
r   c                       s<   e Zd ZdZdZeZ fddZdd Zee	j
eZ  ZS )HttpMethodContextzThe Http-Specific method context. Http-Specific information is stored in
    the transport attribute using the :class:`HttpTransportContext` class.
    Nc                    s*   t t| |tj | | |||| _d S rM   )r   rk   r   r   SERVERr   r   )r   r   req_envr?   r   r   r   r      s
   zHttpMethodContext.__init__c                 C   sB   || _ | j jd u r| j | j t| jtr| j|j d S d S rM   )_out_protocolappset_appr   r   r   rF   r9   rD   r   r   r   set_out_protocol   s   z"HttpMethodContext.set_out_protocol)r$   r%   r&   rh   r   r   r   rq   rj   r   get_out_protocolout_protocolr'   r   r   r   r   rk      s    rk   c                       sj   e Zd ZdZdZdZ			d fdd	Zed	d
 Zedd Z	edd Z
dddZedd Z  ZS )HttpBasez$http://schemas.xmlsoap.org/soap/http/z/%sF        c           	         s   t t| | || _|| _|| _t | _| jj	j
 D ]\}}|d }|jD ]}t|tr4| j| q'qttt| jdd d| _d S )Nr   c                 S   s   | j | jfS rM   )addresshost)xr   r   r   rg     s    z#HttpBase.__init__.<locals>.<lambda>)rV   )r   rt   r   chunkedmax_content_lengthblock_lengthset_http_patternsro   	interfaceservice_method_mapr[   patternsr   r   addlistreversedsorted)	r   ro   r{   r|   r}   rb   rc   p_method_descriptorpattr   r   r   r      s   


zHttpBase.__init__c                 C      |j S rM   )verb_reclsr   r   r   r   get_patt_verb     zHttpBase.get_patt_verbc                 C   r   rM   )host_rer   r   r   r   get_patt_host  r   zHttpBase.get_patt_hostc                 C   r   rM   )
address_rer   r   r   r   get_patt_address  r   zHttpBase.get_patt_addressr,   c                 C   s  | | js| j|f }tt}| jD ]}t|tsJ |jdurK| 	|
|}|du r.q| dt|fks9q|  D ]\}}	|| |	 q?|jdurz| |
|}|du r]q| dt|fkshq|  D ]\}}	|| |	 qn|jdu r|| jd |jjkrqn*| |
|}|du rq| dt|fksq|  D ]\}}	|| |	 q|j}
t|
tsJ |
j|_ |S |S )a  Sets ctx.method_request_string if there's a match. It's O(n) which
        means you should keep your number of patterns as low as possible.

        :param ctx: A MethodContext instance
        :param method: The verb in the HTTP Request (GET, POST, etc.)
        :param host: The contents of the ``Host:`` header
        :param path: Path but not the arguments. (i.e. stuff before '?', if it's
            there)
        Nr   )
startswithSLASHSLASHPERr   r   r   r   r   verbr   matchspanr-   	groupdictr[   r\   ry   r   rx   splitendpointnamer   r   method_request_string)r   r   methodpathry   paramsr   r   rb   rc   dr   r   r   match_pattern  sJ   



zHttpBase.match_patternc                 C   s   t | jdkS )Nr   )r-   r   r"   r   r   r   has_patternsV  s   zHttpBase.has_patterns)Frv   rw   )r,   r,   r,   )r$   r%   r&   r   r   r   r   classmethodr   r   r   r   rj   r   r'   r   r   r   r   rt      s"    



=rt   )NT)collectionsr   emailr   email.utilsr   email.messager   spyner   r   r   r	   spyne.serverr
   spyne.protocol.httpr   spyne.const.httpr   r   r   r   r   r   r5   r   rk   rt   r   r   r   r   <module>   s   
%s