o
    q¨Êh´!  ã                   @   sè   d Z ddlmZmZmZmZ ddlmZ ddlZddl	Z	ddl
Z
ddlmZ ddlmZmZmZmZmZ e
jdk rJdd	lmZmZ dd
lmZmZ nddlmZmZmZmZ dd„ Zdd„ Zdd„ Ze  de¡ ddd„Z!ddd„Z"dS )z‡
Functions to convert unicode IRIs into ASCII byte string URIs and back. Exports
the following items:

 - iri_to_uri()
 - uri_to_iri()
é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_function)ÚidnaNé   )Úunwrap)Úbyte_clsÚstr_clsÚ	type_nameÚbytes_to_listÚ	int_types)é   )ÚurlsplitÚ
urlunsplit)ÚquoteÚunquote)r   Úunquote_to_bytesr   r   c                 C   sü  t | tƒsttdt| ƒƒƒ‚d}tjdk rO|  d¡sO|  d¡sOd}t 	d| ¡}|r8| 
d¡}d| t|ƒd…  } t| ƒ}|rN|| dd…  } t|dd	… ƒ}nt| ƒ}|du r\t|jƒ}|j}|durh| d
¡}t|jdd�}t|jdd�}|j}|dur„t|ƒ d¡}d}	|durš|	|7 }	|r–|	d| 7 }	|	d7 }	|dur¢|	|7 }	|durÀ|dko­|dk}
|dkoµ|dk}|
sÀ|sÀ|	d| 7 }	t|jdd�}t|jdd�}t|jdd�}|du rã|du rã|dkrãd}|du réd}t||	|||fƒ}t |tƒrü| d¡}|S )z¿
    Normalizes and encodes a unicode IRI into an ASCII byte string URI

    :param value:
        A unicode string of an IRI

    :return:
        A byte string of the ASCII-encoded URI
    z@
            value must be a unicode string, not %s
            N)é   é   zhttp://zhttps://z	^[^:]*://r   r   éýÿÿÿr   z!$&'()*+,;=©ÚsafeÚasciió    ó   :ó   @s   https   80s   httpss   443z/!$&'()*+,;=@:z/?!$&'()*+,;=@:ó   /Ú Úlatin1)Ú
isinstancer
   Ú	TypeErrorr   r   ÚsysÚversion_infoÚ
startswithÚreÚmatchÚgroupÚlenr   Ú	_urlquoteÚschemeÚhostnameÚencodeÚusernameÚpasswordÚportÚpathÚqueryÚfragmentr   )Úvaluer*   Úreal_prefixÚprefix_matchÚparsedr+   r-   r.   r/   ÚnetlocÚdefault_httpÚdefault_httpsr0   r1   r2   Úoutput© r;   úA/var/www/html/env/lib/python3.10/site-packages/asn1crypto/_iri.pyÚ
iri_to_uri%   sh   
ü
€



r=   c                 C   s(  t | tƒsttdt| ƒƒƒ‚t| ƒ}|j}|dur| d¡}t|j	ddgd�}t|j
ddgd�}|j}|r:| d¡}|j}|rIt |tƒsI| d¡}d}|dur_||7 }|r[|d| 7 }|d7 }|durg||7 }|durs|dt|ƒ 7 }t|jd	gd
d�}t|jddgd
d�}	t|jƒ}
t||||	|
fƒS )z³
    Converts an ASCII URI byte string into a unicode IRI

    :param value:
        An ASCII-encoded byte string of the URI

    :return:
        A unicode string of the IRI
    z=
            value must be a byte string, not %s
            Nr   ú:ú@)Úremapr   r   ú/T)r@   Úpreserveú&ú=)r    r	   r!   r   r   r   r*   ÚdecodeÚ_urlunquoter-   r.   r+   r/   r   r
   r0   r1   r2   r   )r3   r6   r*   r-   r.   r+   r/   r7   r0   r1   r2   r;   r;   r<   Ú
uri_to_iriu   s>   
ü



rG   c                 C   s4   t | j| j| j… ƒ}dd„ |D ƒ}d |¡| jfS )a>  
    Error handler for decoding UTF-8 parts of a URI into an IRI. Leaves byte
    sequences encoded in %XX format, but as part of a unicode string.

    :param exc:
        The UnicodeDecodeError exception

    :return:
        A 2-element tuple of (replacement unicode string, integer index to
        resume at)
    c                 S   s   g | ]}d | ‘qS )ú%%%02xr;   )Ú.0Únumr;   r;   r<   Ú
<listcomp>·   s    z,_iri_utf8_errors_handler.<locals>.<listcomp>r   )r   ÚobjectÚstartÚendÚjoin)ÚexcÚbytes_as_intsÚreplacementsr;   r;   r<   Ú_iri_utf8_errors_handler©   s   rS   Úiriutf8r   c                    s®   | du s| dkr
dS g ‰ t  d| ¡r,‡fdd„}t  d|| ¡} ‡ fdd„}t  d|| ¡} t|  d	¡ˆ d	¡d
�}t|tƒsB| d¡}tˆ ƒdkrU‡ fdd„}t  d||¡}|S )a  
    Quotes a unicode string for use in a URL

    :param string:
        A unicode string

    :param safe:
        A unicode string of character to not encode

    :return:
        None (if string is None) or an ASCII byte string of the quoted string
    Nr   z%[0-9a-fA-F]{2}c                    s@   t |  d¡ƒ}| dd¡}tˆ ƒD ]}| |dt|ƒ ¡}q|S )Nr   úutf-8rT   rH   )r   r'   rE   ÚlistÚreplaceÚord)r&   Úbyte_stringÚunicode_stringÚ	safe_charr   r;   r<   Ú_try_unescapeÖ   s
   z _urlquote.<locals>._try_unescapez(?:%[0-9a-fA-F]{2})+c                    s   ˆ   |  d¡ d¡¡ dS )Nr   r   ú )Úappendr'   r,   )r&   ©Úescapesr;   r<   Ú_extract_escapeà   s   z"_urlquote.<locals>._extract_escaperU   r   r   r   c                    s
   ˆ   d¡S )Nr   )Úpop)Ú_r_   r;   r<   Ú_return_escapeë   s   
z!_urlquote.<locals>._return_escapes   %00)r%   ÚsearchÚsubÚurlquoter,   r    r	   r(   )Ústringr   r\   ra   r:   rd   r;   )r`   r   r<   r)   ¾   s   

r)   c           	      C   sÄ   | du r| S | dkrdS |r.g d¢}i }|D ]}|  d¡}|||< |  | d¡| d¡¡} qt| ƒ} |rI|D ]}|  | d¡dt|ƒ  d¡¡} q6|  dd	¡}|r`| ¡ D ]
\}}| ||¡}qU|S )
a  
    Unquotes a URI portion from a byte string into unicode using UTF-8

    :param byte_string:
        A byte string of the data to unquote

    :param remap:
        A list of characters (as unicode) that should be re-mapped to a
        %XX encoding. This is used when characters are not valid in part of a
        URL.

    :param preserve:
        A bool - indicates that the chars to be remapped if they occur in
        non-hex form, should be preserved. E.g. / for URL path.

    :return:
        A unicode string
    Nr   r   )úúúúúr   r   rH   rU   rT   )rb   rW   r,   r   rX   rE   Úitems)	rY   r@   rB   rR   Úpreserve_unmapÚcharÚreplacementr:   Úoriginalr;   r;   r<   rF   ò   s(   
"rF   )r   )NN)#Ú__doc__Ú
__future__r   r   r   r   Ú	encodingsr   Úcodecsr%   r"   Ú_errorsr   Ú_typesr	   r
   r   r   r   r#   Úurlparser   r   Úurllibr   rg   r   r   Úurllib.parser=   rG   rS   Úregister_errorr)   rF   r;   r;   r;   r<   Ú<module>   s$   
P4
4