o
    bm                     @   s^  d Z ddlZzddlmZ ddlmZ ddlmZ W n e	y5   ddl
mZ ddl
mZ ddl
mZ Y nw zddlmZ ddlmZ W n e	yY   ddlmZ ddlmZ Y nw zddlZW n e	yo   ddl
mZ Y nw 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)parse_keqv_list)parse_http_list   )
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   R/var/www/secure340b-portal/env/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_decode0   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_qsc   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   fragmentZschnetpathparr   Zfrar   r   r   add_params_to_urim   s
   
r.      /c                 C   s   t tt| |S r   )r	   _quoter
   )ssafer   r   r   r   w   s   r   c                 C   s   t t| S r   )r	   _unquoter1   r   r   r   r   {   s   r   c                 C   s
   t | dS )Ns   ~@#$&()*!+=:;,.?/'r   r4   r   r   r   	quote_url   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_params   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urllibr   r0   r   r3   r   r   ImportErrorurllib.parseurllib2r   r   urllib.requestr   parseencodingr	   r
   Zalways_safer   r   compiler   r   r!   r'   r.   r5   r:   r?   r   r   r   r   <module>   sJ    
3



