o
    9e                     @  s  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 d dlm	Z	 d dlm
Z
 d dlmZ dd	lmZ d/ddZede d0ddZeg edddR  ZedeZeded Zeded Zeded  Zd1d"d#Zd2d%d&Zd2d'd(Zd3d*d+Zd4d-d.ZdS )5    )annotationsN)quote)unquote)	urlencode)urlsplit)
urlunsplit   )iter_multi_itemseUnicodeErrorreturntuple[str, int]c                 C  s$   t | j| j| j dd}|| jfS )zRUsed in :func:`uri_to_iri` after unquoting to re-quote any
    invalid bytes.
     safe)r   objectstartend)r
   out r   -lib/python3.10/site-packages/werkzeug/urls.py_codec_error_url_quote   s   
r   werkzeug.url_quotenamestrcharst.Callable[[str], str]c                   sL   d dd t|D }td| dtj d fd	d
}d|  |_|S )zCreate a function that unquotes all percent encoded characters except those
    given. This allows working with unquoted characters if possible while not changing
    the meaning of a given part of a URL.
    |c                 s  s    | ]	}t |d V  qdS )Z02XN)ord).0cr   r   r   	<genexpr>!   s    z%_make_unquote_part.<locals>.<genexpr>z((?:%(?:z))+)valuer   r   c                   sH   t  | }g }|D ]}|t|dd |t|d qd|S )Nzutf-8r   r   )itersplitappendr   nextjoin)r"   partsr   partpatternr   r   _unquote_partial$   s   
z,_make_unquote_part.<locals>._unquote_partialZ	_unquote_N)r"   r   r   r   )r'   sortedrecompileI__name__)r   r   choicesr,   r   r*   r   _make_unquote_part   s
   
r3   !   %      fragmentqueryz&=+#pathz/?#userz:@/?#uric                 C  s   t | }t|j}t|j}t|j}|jrt|j}nd}d|v r(d| d}|j	r3| d|j	 }|j
rQt|j
}|jrJt|j}| d| }| d| }t|j||||fS )a-  Convert a URI to an IRI. All valid UTF-8 characters are unquoted,
    leaving all reserved and invalid characters quoted. If the URL has
    a domain, it is decoded from Punycode.

    >>> uri_to_iri("http://xn--n3h.net/p%C3%A5th?q=%C3%A8ry%DF")
    'http://\u2603.net/p\xe5th?q=\xe8ry%DF'

    :param uri: The URI to convert.

    .. versionchanged:: 3.0
        Passing a tuple or bytes, and the ``charset`` and ``errors`` parameters,
        are removed.

    .. versionchanged:: 2.3
        Which characters remain quoted is specific to each part of the URL.

    .. versionchanged:: 0.15
        All reserved and invalid characters remain quoted. Previously,
        only some reserved characters were preserved, and invalid bytes
        were replaced instead of left quoted.

    .. versionadded:: 0.6
    r   :[]@)r   _unquote_pathr9   _unquote_queryr8   _unquote_fragmentr7   hostname_decode_idnaportusername_unquote_userpasswordr   scheme)r;   r(   r9   r8   r7   netlocauthrH   r   r   r   
uri_to_iri<   s$   




rL   iric                 C  s   t | }t|jdd}t|jdd}t|jdd}|jr&|jdd}nd}d|v r2d	| d
}|jr=| d|j }|j	r_t|j	dd}|j
rXt|j
dd}| d| }| d| }t|j||||fS )a,  Convert an IRI to a URI. All non-ASCII and unsafe characters are
    quoted. If the URL has a domain, it is encoded to Punycode.

    >>> iri_to_uri('http://\u2603.net/p\xe5th?q=\xe8ry%DF')
    'http://xn--n3h.net/p%C3%A5th?q=%C3%A8ry%DF'

    :param iri: The IRI to convert.

    .. versionchanged:: 3.0
        Passing a tuple or bytes, the ``charset`` and ``errors`` parameters,
        and the ``safe_conversion`` parameter, are removed.

    .. versionchanged:: 2.3
        Which characters remain unquoted is specific to each part of the URL.

    .. versionchanged:: 0.15
        All reserved characters remain unquoted. Previously, only some reserved
        characters were left unquoted.

    .. versionchanged:: 0.9.6
       The ``safe_conversion`` parameter was added.

    .. versionadded:: 0.6
    z%!$&'()*+,/:;=@r   z%!$&'()*+,/:;=?@z%!#$&'()*+,/:;=?@idnaasciir   r<   r=   r>   z%!$&'()*+,;=r?   )r   r   r9   r8   r7   rC   encodedecoderE   rF   rH   r   rI   )rM   r(   r9   r8   r7   rJ   rK   rH   r   r   r   
iri_to_urip   s$   rR   c                 C  sH   z|  d W n ty   Y t| S w t| dddkr | S t| S )a  The URL scheme ``itms-services://`` must contain the ``//`` even though it does
    not have a host component. There may be other invalid schemes as well. Currently,
    responses will always call ``iri_to_uri`` on the redirect ``Location`` header, which
    removes the ``//``. For now, if the IRI only contains ASCII and does not contain
    spaces, pass it on as-is. In Werkzeug 3.0, this should become a
    ``response.process_location`` flag.

    :meta private:
    rO   Nr   )rP   r   lenr$   rR   )rM   r   r   r   _invalid_iri_to_uri   s   
rT   domainc              	   C  s   z|  d}W n ty   |  Y S w z|dW S  ty"   Y nw g }|dD ]}z
||d W q* tyG   ||d Y q*w d|S )NrO   rN      ..)rP   UnicodeEncodeErrorrQ   UnicodeDecodeErrorr$   r%   r'   )rU   datar(   r)   r   r   r   rD      s$   
rD   1t.Mapping[str, str] | t.Iterable[tuple[str, str]]c                 C  s   dd t | D }t|ddS )Nc                 S  s   g | ]
}|d  dur|qS )r   Nr   )r   xr   r   r   
<listcomp>   s    z_urlencode.<locals>.<listcomp>z!$'()*,/:;?@r   )r	   r   )r8   itemsr   r   r   
_urlencode   s   r_   )r
   r   r   r   )r   r   r   r   r   r   )r;   r   r   r   )rM   r   r   r   )rU   r   r   r   )r8   r[   r   r   )Z
__future__r   codecsr.   typingturllib.parser   r   r   r   r   Zdatastructuresr	   r   register_errorr3   bytesrangerQ   Z_always_unsaferB   rA   r@   rG   rL   rR   rT   rD   r_   r   r   r   r   <module>   s.    





4
7
