o
    Df                     @   s   d 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mZ dZeeed	B Zed
Zdd Zdd Zdd ZdddZd ddZdd Zdd Zdd Zdd ZdS )!zc
    authlib.util.urls
    ~~~~~~~~~~~~~~~~~

    Wrapper functions for URL encoding and decoding.
    Nquote)unquote)	urlencode   )
to_unicodeto_byteszAABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-z=&;:%+~,*@!()/?z&%[^0-9A-Fa-f]|%[0-9A-Fa-f][^0-9A-Fa-f]c                 C   s4   g }| D ]\}}| t|t|f qtt|S N)appendr   r   
_urlencode)paramsencodedkv r   L/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/common/urls.py
url_encode   s   r   c                 C   sx   | rt | tksd}t|t | t | f t| rtdtj| dd}g }|D ]\}}|t|t|f q*|S )a  Decode a query string in x-www-form-urlencoded format into a sequence
    of two-element tuples.

    Unlike urlparse.parse_qsl(..., strict_parsing=True) urldecode will enforce
    correct formatting of the query string by validation. If validation fails
    a ValueError will be raised. urllib.parse_qsl will only raise errors if
    any of name-value pairs omits the equals sign.
    zError trying to decode a non urlencoded string. Found invalid characters: %s in the string: '%s'. Please ensure the request/response body is x-www-form-urlencoded.z%Invalid hex encoding in query string.Tkeep_blank_values)	set
urlencoded
ValueErrorINVALID_HEX_PATTERNsearchurlparse	parse_qslr
   r   )queryerrorr   decodedr   r   r   r   r   
url_decode    s   

r   c                 C   s2   t |tr	| }tj| dd}|| t|S )z)Extend a query with a list of two-tuples.Tr   )
isinstancedictitemsr   r   extendr   )r   r   qsr   r   r   add_params_to_qsS   s
   

r%   Fc           	      C   sF   t  | \}}}}}}|rt||}nt||}t ||||||fS )z5Add a list of two-tuples to the uri query components.)r   r%   
urlunparse)	urir   fragmentschnetpathparr   frar   r   r   add_params_to_uri]   s
   
r.      /c                 C   s   t tt| |S r	   )r   _quoter   )ssafer   r   r   r   g   s   r   c                 C   s   t t| S r	   )r   _unquoter1   r   r   r   r   k   s   r   c                 C   s
   t | dS )Ns   ~@#$&()*!+=:;,.?/'r   r4   r   r   r   	quote_urlo   s   
r5   c              	   C   s   t | ttfrzt| } W n ttfy   Y dS w t | tr7g }|  D ]\}}|t|t|f q%|S | s;dS zt	| W S  tyJ   Y dS w )a*  Extract parameters and return them as a list of 2-tuples.

    Will successfully extract parameters from urlencoded query strings,
    dicts, or lists of 2-tuples. Empty strings/dicts/lists will return an
    empty list of parameters. Any other input will result in a return
    value of None.
    N)
r    listtupler!   	TypeErrorr   r"   r
   r   r   )rawr   r   r   r   r   r   extract_paramss   s$   

r:   c                 C   s   t  | }|jo
|jS r	   )r   schemehostname)urlparsedr   r   r   is_valid_url   s   
r?   )F)r/   )__doc__reurllib.parser   r0   r   r3   r   r   parser   encodingr   r   always_safer   r   compiler   r   r   r%   r.   r5   r:   r?   r   r   r   r   <module>   s(    
3



