o
    %zh"                     @   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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   Fc                 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rt|	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}|r|s|s|
d|	 7 }
t|jdd}t|jdd}t|jdd}|r|du r|du r|dkrd}|du rd}t||
|||f}t |tr|d}|S )z
    Encodes a unicode IRI into an ASCII byte string URI

    :param value:
        A unicode string of an IRI

    :param normalize:
        A bool that controls URI normalization

    :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   )value	normalizer*   real_prefixprefix_matchparsedr+   r-   r.   r/   netlocdefault_httpdefault_httpsr0   r1   r2   output r<   J/var/www/html/kangema/venv/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)rA   preserve&=)r    r	   r!   r   r   r   r*   decode_urlunquoter-   r.   r+   r/   r   r
   r0   r1   r2   r   )r3   r7   r*   r-   r.   r+   r/   r8   r0   r1   r2   r<   r<   r=   
uri_to_irix   s>   




rH   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   rT   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-8rU   rI   )r   r'   rF   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_escaperV   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]   rb   r;   re   r<   )ra   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   rI   rV   rU   )rc   rX   r,   r   rY   rF   items)	rZ   rA   rC   rS   preserve_unmapcharreplacementr;   originalr<   r<   r=   rG      s(   
"rG   )F)r   )NN)#__doc__
__future__r   r   r   r   	encodingsr   codecsr%   r"   _errorsr   _typesr	   r
   r   r   r   r#   urlparser   r   urllibr   rh   r   r   urllib.parser>   rH   rT   register_errorr)   rG   r<   r<   r<   r=   <module>   s$   

S4
4