o
    "`^h"6                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dl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ZddlmZ e
jrEddlmZ dd	d
Zedd edD  eddd e D Z	d^d_ddZd^d`ddZdadd Zdbd!d"Zdcd$d%Z ddd&d'Z!ded)d*Z"dfd,d-Z#d.d/hZ$dgd3d4Z%dhd8d9Z&did;d<Z'djd>d?Z(dkdAdBZ)dldmdDdEZ*dldndFdGZ+dodJdKZ,dpdLdMZ-dqdOdPZ.drdSdTZ/G dUdV dVZ0G dWdX dXZ1dsdZd[Z2dsd\d]Z3dS )t    )annotationsN)Path)
getproxies   )PrimitiveDataURLz%22z\\)"\c                 C  s$   i | ]}|d krt |d|qS )   z%{:02X})chrformat.0c r   J/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/httpx/_utils.py
<dictcomp>   s   $ r       |c                 C  s   g | ]}t |qS r   )reescaper   r   r   r   
<listcomp>   s    r   valuestr | byteslowerboolencoding
str | Nonereturnbytesc                 C  s.   t | tr| }n| |pd}|r| S |S )zE
    Coerce str/bytes into a strictly byte-wise HTTP header key.
    ascii)
isinstancer    encoder   )r   r   r   bytes_valuer   r   r   normalize_header_key   s   
r%   c                 C  s   t | tr| S | |pdS )zG
    Coerce str/bytes into a strictly byte-wise HTTP header value.
    r!   )r"   r    r#   r   r   r   r   r   normalize_header_value/   s   
r'   r   strc                 C  s,   | du rdS | du rdS | du rdS t | S )z
    Coerce a primitive data type into a string value.

    Note that we prefer JSON-style 'true'/'false' for boolean values here.
    TtrueFfalseN )r(   r   r   r   r   primitive_value_to_str8   s   r-   c                 C  s&   zt |  W dS  ty   Y dS w )z7
    Return `True` if `encoding` is a known codec.
    FT)codecslookupLookupError)r   r   r   r   is_known_encodingG   s   r1   namec                 C  s*   d
dd}t ||}|  d| d S )z;
    Encode a name/value pair within a multipart form.
    matchtyping.Match[str]r   r(   c                 S  s   t | d S )Nr   )!_HTML5_FORM_ENCODING_REPLACEMENTSgroup)r3   r   r   r   replacerW      z#format_form_param.<locals>.replacerz="r	   N)r3   r4   r   r(   )_HTML5_FORM_ENCODING_REsubr#   )r2   r   r7   r   r   r   format_form_paramR   s   
r;   c                  C  sT   dt jv rtt jd } |  rt| S dt jv r(tt jd }| r(t|S d S )NSSL_CERT_FILESSL_CERT_DIR)osenvironr   is_filer(   is_dir)ssl_filessl_pathr   r   r   get_ca_bundle_from_env^   s   

rD   list[dict[str, str]]c           	   
   C  s   g }d}|  |} | s|S td| D ]M}z
|dd\}}W n ty-   |d}}Y nw d| di}|dD ] }z	|d\}} W n
 tyO   Y  nw |  ||| |< q:|| q|S )	a7  
    Returns a list of parsed link headers, for more info see:
    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link
    The generic syntax of those is:
    Link: < uri-reference >; param1=value1; param2="value2"
    So for instance:
    Link; '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;'
    would return
        [
            {"url": "http:/.../front.jpeg", "type": "image/jpeg"},
            {"url": "http://.../back.jpeg"},
        ]
    :param value: HTTP Link entity-header field
    :return: list of parsed link headers
    z '"z, *<;r   r+   urlz<> '"=)stripr   split
ValueErrorappend)	r   linksreplace_charsvalrG   paramslinkparamkeyr   r   r   parse_header_linksj   s*   
rT   content_typec                 C  s   t j }| |d< |jd dS )Nzcontent-type)failobj)emailmessageMessageget_content_charset)rU   msgr   r   r   parse_content_type_charset   s   
r\   authorizationzproxy-authorizationitems4typing.Iterable[tuple[typing.AnyStr, typing.AnyStr]]4typing.Iterator[tuple[typing.AnyStr, typing.AnyStr]]c                 c  s:    | D ]\}}t | tv rtd|d}||fV  qd S )Nz[secure])match_type_of)to_strr   SENSITIVE_HEADERSto_bytes_or_str)r^   kvr   r   r   obfuscate_sensitive_headers   s   rg   rG   r   
int | Nonec                 C  s"   | j d ur| j S ddd| jS )NP     )httphttps)portgetscheme)rG   r   r   r   port_or_default   s   
rp   otherc                 C  s(   | j |j ko| j|jkot| t|kS )z@
    Return 'True' if the given URLs share the same origin.
    )ro   hostrp   )rG   rq   r   r   r   same_origin   s
   
rs   locationc                 C  s<   | j |j krdS | jdkot| dko|jdkot|dkS )zA
    Return 'True' if 'location' is a HTTPS upgrade of 'url'
    Frk   ri   rl   rj   )rr   ro   rp   )rG   rt   r   r   r   is_https_redirect   s   


ru   dict[str, str | None]c                  C  s   t  } i }dD ]}| |r"| | }d|v r|nd| || d< qdd | dddD }|D ]C}|d	kr=i   S |rvd|v rHd
||< q3t|rTd
|d| < q3t|rad
|d| d< q3| dkrod
|d| < q3d
|d| < q3|S )z+Gets proxy information from the environment)rk   rl   allz://zhttp://c                 S  s   g | ]}|  qS r   )rI   )r   rr   r   r   r   r      s    z+get_environment_proxies.<locals>.<listcomp>nor+   ,*Nzall://zall://[]	localhostzall://*)r   rn   rJ   is_ipv4_hostnameis_ipv6_hostnamer   )
proxy_infomountsro   hostnameno_proxy_hostsr   r   r   get_environment_proxies   s0   

r   utf-8c                 C  s   t | tr
| |S | S Nr"   r(   r#   r&   r   r   r   to_bytes      r   c                 C  s   t | tr| S | |S r   )r"   r(   decoder&   r   r   r   rb      r   rb   ra   typing.AnyStrc                 C  s   t |tr| S |  S r   r   )r   ra   r   r   r   rd         rd   c                 C  s0   | d | d   krdkrn n| dd S | S )Nr   r	   r   r   r,   r   r   r   unquote   s   0r   filenamec                 C  s   | rt | d p
dS d S )Nr   zapplication/octet-stream)	mimetypes
guess_type)r   r   r   r   guess_content_type  s   r   stream
typing.Anyc                 C  sr   z|   }t|j}W |S  ttfy8   z|  }| dtj}| | W Y |S  ttfy7   Y Y dS w w )zs
    Given a file-like stream object, return its length in number of bytes
    without reading it into memory.
    r   N)	filenor>   fstatst_sizeAttributeErrorOSErrortellseekSEEK_END)r   fdlengthoffsetr   r   r   peek_filelike_length
  s   r   c                   @  s>   e Zd ZdddZdddZddd	Zdd
dZdddZdS )Timerr   floatc                   s6   t  }|dkrdd l}| S dd l}|  S )Ntrior   )sniffiocurrent_async_libraryr   current_timeasyncioget_event_looptime)selflibraryr   r   r   r   r   	_get_time$  s   zTimer._get_timeNonec                 C  s   t  | _d S r   r   perf_counterstartedr   r   r   r   
sync_start/  r8   zTimer.sync_startc                   s   |   I d H | _d S r   r   r   r   r   r   r   async_start2  s   zTimer.async_startc                 C  s   t  }|| j S r   r   r   nowr   r   r   sync_elapsed5  s   
zTimer.sync_elapsedc                   s   |   I d H }|| j S r   r   r   r   r   r   async_elapsed9  s   
zTimer.async_elapsedN)r   r   )r   r   )__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   #  s    



r   c                   @  sP   e Zd ZdZdddZdddZedddZdddZdddZ	dddZ
dS )
URLPatterna  
    A utility class currently used for making lookups against proxy keys...

    # Wildcard matching...
    >>> pattern = URLPattern("all://")
    >>> pattern.matches(httpx.URL("http://example.com"))
    True

    # Witch scheme matching...
    >>> pattern = URLPattern("https://")
    >>> pattern.matches(httpx.URL("https://example.com"))
    True
    >>> pattern.matches(httpx.URL("http://example.com"))
    False

    # With domain matching...
    >>> pattern = URLPattern("https://example.com")
    >>> pattern.matches(httpx.URL("https://example.com"))
    True
    >>> pattern.matches(httpx.URL("http://example.com"))
    False
    >>> pattern.matches(httpx.URL("https://other.com"))
    False

    # Wildcard scheme, with domain matching...
    >>> pattern = URLPattern("all://example.com")
    >>> pattern.matches(httpx.URL("https://example.com"))
    True
    >>> pattern.matches(httpx.URL("http://example.com"))
    True
    >>> pattern.matches(httpx.URL("https://other.com"))
    False

    # With port matching...
    >>> pattern = URLPattern("https://example.com:1234")
    >>> pattern.matches(httpx.URL("https://example.com:1234"))
    True
    >>> pattern.matches(httpx.URL("https://example.com"))
    False
    patternr(   r   r   c                 C  s  ddl m} |rd|vrtd| d| d||}|| _|jdkr%dn|j| _|jd	kr0dn|j| _|j| _|jr@|jd	krEd | _d S |jd
rat	
|jdd  }t	d| d| _d S |jd	r}t	
|jdd  }t	d| d| _d S t	
|j}t	d| d| _d S )Nr   r   :zUProxy keys should use proper URL forms rather than plain scheme strings. Instead of "z", use "z://"rw   r+   rz   z*.   z^.+\.$z^(.+\.)?^)_urlsr   rK   r   ro   rr   rm   
host_regex
startswithr   r   compile)r   r   r   rG   domainr   r   r   __init__h  s0   
zURLPattern.__init__rq   r   r   c                 C  sV   | j r| j |j krdS | jr| jd ur| j|jsdS | jd ur)| j|jkr)dS dS )NFT)ro   rr   r   r3   rm   r   rq   r   r   r   matches  s   
zURLPattern.matchestuple[int, int, int]c                 C  s4   | j durdnd}t| j }t| j }|||fS )z
        The priority allows URLPattern instances to be sortable, so that
        we can match from most specific to least specific.
        Nr   r   )rm   lenrr   ro   )r   port_priorityhost_priorityscheme_priorityr   r   r   priority  s   
zURLPattern.priorityintc                 C  s
   t | jS r   )hashr   r   r   r   r   __hash__  s   
zURLPattern.__hash__c                 C  s   | j |j k S r   )r   r   r   r   r   __lt__  s   zURLPattern.__lt__r   c                 C  s   t |to
| j|jkS r   )r"   r   r   r   r   r   r   __eq__  r   zURLPattern.__eq__N)r   r(   r   r   )rq   r   r   r   )r   r   )r   r   )rq   r   r   r   )rq   r   r   r   )r   r   r   __doc__r   r   propertyr   r   r   r   r   r   r   r   r   >  s    
)


r   r   c                 C  0   zt | dd  W dS  ty   Y dS w N/r   FT)	ipaddressIPv4AddressrJ   	Exceptionr   r   r   r   r}        r}   c                 C  r   r   )r   IPv6AddressrJ   r   r   r   r   r   r~     r   r~   r   )r   r   r   r   r   r   r   r    )r   r   r   r   r   r    )r   r   r   r(   )r   r(   r   r   )r2   r(   r   r(   r   r    )r   r   )r   r(   r   rE   )rU   r(   r   r   )r^   r_   r   r`   )rG   r   r   rh   )rG   r   rq   r   r   r   )rG   r   rt   r   r   r   )r   rv   )r   )r   r   r   r(   r   r    )r   r   r   r(   r   r(   )r   r(   ra   r   r   r   )r   r(   r   r(   )r   r   r   r   )r   r   r   rh   )r   r(   r   r   )4
__future__r   r.   email.messagerW   r   r   r>   r   r   typingpathlibr   urllib.requestr   r   _typesr   TYPE_CHECKINGr   r   r5   updateranger   joinkeysr9   r%   r'   r-   r1   r;   rD   rT   r\   rc   rg   rp   rs   ru   r   r   rb   rd   r   r   r   r   r   r}   r~   r   r   r   r   <module>   s^    

	




%

	


1




m