
    ~h                         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 ddlmZ d	Z ee       ed
      z  Z ej"                  d      Zd Zd Zd ZddZddZd Zd Zd ZddefdZy)zWauthlib.util.urls.
~~~~~~~~~~~~~~~~~

Wrapper functions for URL encoding and decoding.
    Nquote)unquote)	urlencode   )to_bytes)
to_unicodezAABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-z=&;:%+~,*@!()/?z&%[^0-9A-Fa-f]|%[0-9A-Fa-f][^0-9A-Fa-f]c                     g }| D ]*  \  }}|j                  t        |      t        |      f       , t        t        |            S N)appendr   r	   
_urlencode)paramsencodedkvs       O/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/authlib/common/urls.py
url_encoder      sC    G 31Xa[123j)**    c                 B   | r4t        |       t        k  s"d}t        |t        |       t        z
  | fz        t        j	                  |       rt        d      t        j                  | d      }g }|D ]*  \  }}|j                  t        |      t        |      f       , |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   s         r   
url_decoder"      s     SZ:-% 	 #e*z"95!AABB !!%(@AA& >F G 71
1z!}567Nr   c                     t        |t              r|j                         }t        j                  | d      }|j                  |       t        |      S )z)Extend a query with a list of two-tuples.Tr   )
isinstancedictitemsr   r   extendr   )r   r   qss      r   add_params_to_qsr)   Q   s?    &$			ET	:BIIfb>r   c                     t        j                   |       \  }}}}}}|rt        ||      }nt        ||      }t        j                  ||||||f      S )z5Add a list of two-tuples to the uri query components.)r   r)   
urlunparse)	urir   fragmentschnetpathparr   fras	            r   add_params_to_urir3   [   sX    &.&7&7&<#CdCsF+ /S$UC@AAr   c                 >    t        t        t        |       |            S r   )r	   _quoter   )ssafes     r   r   r   e   s    fXa[$/00r   c                 *    t        t        |             S r   )r	   _unquoter6   s    r   r   r   i   s    hqk""r   c                     t        | d      S )Ns   ~@#$&()*!+=:;,.?/'r   r:   s    r   	quote_urlr<   m   s    )**r   c                 N   t        | t        t        f      r	 t        |       } t        | t              rAg }| j                         D ]*  \  }}|j                  t        |      t        |      f       , |S | sy	 t        |       S # t        t
        f$ r Y yw xY w# t
        $ r Y yw xY 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   s       r   extract_paramsrB   q   s     #e}%	s)C #tIIK 	:DAqMM:a=*Q-89	:# :& 		  s#   B 8
B BB	B$#B$urlc                     t        j                   |       }|j                  xr |j                  xr |xs |j                   S r   )r   schemehostnamer-   )rC   fragments_allowedparseds      r   is_valid_urlrI      s9    s#FX&//X/@/WDWr   )F)   /)T)__doc__reurllib.parseparser   r   r5   r   r9   r   r   encodingr   r	   always_safer   r   compiler   r   r"   r)   r3   r<   rB   strrI    r   r   <module>rT      s    
  ( , 0   Q$5 66
 bjj!JK +2jB1#+:c r   