o
    iG:                     @   s.  d e e d f  ddlZee Zee d ZddlZddlmZ ddlm	  m
Z 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 dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZmZm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& dddZ'dddZ(dd Z)G dd de"Z*dS )aY  The ``spyne.protocol.soap.soap11`` module contains the implementation of a
subset of the Soap 1.1 standard.

Except the binary optimizations (MtoM, attachments, etc) that are beta quality,
this protocol is production quality.

One must specifically enable the debug output for the Xml protocol to see the
actual document exchange. That's because the xml formatting code is run only
when explicitly enabled due to performance reasons. ::

    logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

Initially released in soaplib-0.8.0.

Logs valid documents to %r and invalid documents to %r.
z.invalid    N)chain)etree)XMLSyntaxError)	XMLParser)BODY_STYLE_WRAPPED)six)
DEFAULT_NS)HTTP_405HTTP_500)RequestNotAllowed)Fault)DateTimeDateTime)XmlDocument)collapse_swa)HttpTransportContextc                 K   s   | dtj}|rt| | | jd| krtdd| | jdd|id}| jdd|id}t|d	kr?t|d	kr?tdd
d}t|d	krM|d	  }d}t|d	krct|d	 d	krc|d	 d	 }||fS )z7Parses the xml string into the header and payload.
    ns{%s}EnvelopezClient.SoapErrorz"No {%s}Envelope element was found!ze:Headere)
namespacesze:Bodyr   zSoap envelope is empty!N)	popr   NS_SOAP11_ENVresolve_hrefstagr   xpathlengetchildren)in_envelope_xmlxmlidskwargsns_soapheader_envelopebody_envelopeheaderbody r&   Z/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/spyne/protocol/soap/soap11.py
_from_soap?   s,   

r(   c              
   C   s   t | } t| }t|tjrdt|f| }n	dt|f| }|r(||}z7z
t	||\}}W n" t
yU } ztd t	|||\}}W Y d }~n
d }~ww W ||fS W ||fS  tyy } ztd|| tdt|d }~ww )N     zbValueError: Deserializing from unicode strings with encoding declaration is not supported by lxml.z%r in string %rzClient.XMLSyntaxError)iternext
isinstancer   binary_typejoinr   decoder   XMLID
ValueErrorloggerdebugencoder   logger_invaliderrorr   str)
xml_stringparsercharsetchunkstringrootr   r   r&   r&   r'   _parse_xml_string^   s0   

"r?   c                    s   | D ]B   dr
q  dr?|  ddd }|d u rqt||  fdd| D   fdd| D  |j _qt | q| S )Nidhref#r*   c                    s   g | ]
\}}  ||qS r&   )set).0kvr   r&   r'   
<listcomp>   s    z!resolve_hrefs.<locals>.<listcomp>c                    s   g | ]}  |qS r&   )append)rD   childrG   r&   r'   rH      s    )getreplacer   itemsr   text)elementr   resolved_elementr&   rG   r'   r   {   s   



r   c                       st   e Zd ZdZdZeejZed e	j
Ze	jZ fddZdddZejfd	d
Zdd Zdd Zdd Z  ZS )Soap11a  The base implementation of a subset of the Soap 1.1 standard. The
    document is available here: http://www.w3.org/TR/soap11/

    :param app: The owner application instance.
    :param validator: One of (None, 'soft', 'lxml', 'schema',
                ProtocolBase.SOFT_VALIDATION, XmlDocument.SCHEMA_VALIDATION).
                Both ``'lxml'`` and ``'schema'`` values are equivalent to
                ``XmlDocument.SCHEMA_VALIDATION``.
    :param xml_declaration: Whether to add xml_declaration to the responses
        Default is 'True'.
    :param cleanup_namespaces: Whether to add clean up namespace declarations
        in the response document. Default is 'True'.
    :param encoding: The suggested string encoding for the returned xml
        documents. The transport can override this.
    :param pretty_print: When ``True``, returns the document in a pretty-printed
        format.
    ztext/xml; charset=utf-8)soapsoap11c                    sN   t t| j|i | dd | jt< dd | jt< | j| jt< | j	| jt< d S )Nc                 S      |  S N	isoformatclsvaluer&   r&   r'   <lambda>       z!Soap11.__init__.<locals>.<lambda>c                 S   rT   rU   rV   rX   r&   r&   r'   r[      r\   )
superrQ   __init___to_unicode_handlersr   r   date_from_unicode_iso_from_unicode_handlersr   datetime_from_unicode_iso)selfargsr    	__class__r&   r'   r^      s
   zSoap11.__init__Nc                 C   sz   t |jtr-|j }|j }|d u s|dkr t|j_tdt	|}t
||| j|_t|jtdi | j||_d S )NPOSTzHYou must issue a POST request with the Content-Type header properly set.r&   )r-   	transportr   get_request_content_typeget_request_methodr	   	resp_coder   cgiparse_headerr   ns_soap_env	in_stringr?   r   parser_kwargsin_document)rc   ctxr;   content_type	http_verbr&   r&   r'   create_in_document   s   



zSoap11.create_in_documentc                 C   sd   |j \}}t||| jd\}}||_ |jd| j kr||_d S ||_||_|jj|_| || d S )N)r   	{%s}Fault)rq   r(   rn   r   in_body_docin_header_docmethod_request_stringvalidate_body)rc   rr   messageenvelope_xmlr   header_documentbody_documentr&   r&   r'   decompose_incoming_envelope   s   



z"Soap11.decompose_incoming_envelopec                 C   sl  || j | jfv s
J | jd| |jjd| j kr'd|_| |t	|j|_
n|| j u r5|jj}|jj}n|| ju rB|jj}|jj}|jdur|durdgt| }tdd |jD }t|D ]%\}}|t|k rd|j|jf }	||	d}
|
dur| |||
||< q`t|dkr|d |_n||_|jdu rdgt|j |_n	| |||j|_| jd	| dS )
zTakes a MethodContext instance and a string containing ONE soap
        message.
        Returns the corresponding native python object

        Not meant to be overridden.
        before_deserializerv   Nc                 S   s   g | ]}|j |fqS r&   )r   )rD   rO   r&   r&   r'   rH      s    z&Soap11.deserialize.<locals>.<listcomp>z{%s}%s   r   after_deserialize)REQUESTRESPONSEevent_manager
fire_eventrw   r   rn   	in_objectfrom_elementr   in_error
descriptor	in_header
in_message
out_headerout_messagerx   r   dict	enumerate__namespace____type_name__rK   
_type_info)rc   rr   r{   header_class
body_classheadersin_header_dicti
head_classnsval
header_docr&   r&   r'   deserialize   sH   



zSoap11.deserializec              
   C   s  || j | jfv s
J | jd| | jjj}tjd| j	 |d|_
|jdurFtj|j
d| j	 |d |_}| ||jj|j|| jj  n|| j u rT|jj}|jj}n|| ju ra|jj}|jj}td| j	  |_}|jjtu r|j}| }| |}	t|}
t|j}	 zt|
}W n	 ty   Y nw zt|}W n ty   d}Y nw |||||	 q| |||||  n1|jd }|j j!}|du r| }|t"u r| jj }|j j#}|du r|$ }| |||||| |jdur1|dur1t|j
d| j	  |_%}t&|jt't(fr|j}n|jf}t)||D ]\}}| ||||| |$  q|j
*|j | j+rBt+|j
 | jd	| dS )
zUses ctx.out_object, ctx.out_header or ctx.out_error to set
        ctx.out_body_doc, ctx.out_header_doc and ctx.out_document as an
        lxml.etree._Element instance.

        Not meant to be overridden.
        before_serializer   )nsmapNz{%s}BodyTr   z
{%s}Headerafter_serialize),r   r   r   r   app	interfacer   r   Elementrn   out_document	out_error
SubElementout_body_doc	to_parentrf   get_tnsr   r   r   r   r   
body_styler   r   get_cls_attrsr+   
out_objectr,   StopIteration	_safe_setget_namespace
Attributessub_nsr   sub_nameget_type_nameout_header_docr-   listtupleziprI   cleanup_namespaces)rc   rr   r{   r   r   header_message_classbody_message_classout_type_infor   bm_attrskeysvaluesrE   rF   r   r   soap_header_eltout_headersr   r   r&   r&   r'   	serialize  s   











zSoap11.serializec                 C   s   t S rU   )r
   )rc   faultr&   r&   r'   fault_to_http_response_codez  s   z"Soap11.fault_to_http_response_coderU   )__name__
__module____qualname____doc__	mime_typerC   r   typeupdater   r   rn   NS_SOAP11_ENCns_soap_encr^   ru   r   r   r   r   r   __classcell__r&   r&   re   r'   rQ      s    


6erQ   rU   )+r   logging	getLoggerr3   r6   rl   	itertoolsr   spyne.const.xmlconstxmlr   lxmlr   
lxml.etreer   r   spyner   
spyne.utilr   r   spyne.const.httpr	   r
   spyne.errorr   spyne.model.faultr   spyne.model.primitiver   r   r   spyne.protocol.xmlr   spyne.protocol.soap.mimer   spyne.server.httpr   r(   r?   r   rQ   r&   r&   r&   r'   <module>   s4   



