U
    {f"6                     @  sX  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rd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ddddZd`ddddddZdddddZddd d!d"Zdddd#d$d%Z dd&d'd(Z!dd)dd*d+Z"ddd,d-d.Z#d/d0hZ$d1d2d3d4d5Z%d6d7d8d9d:Z&d6d6dd;d<d=Z'd6d6dd>d?d@Z(dAd&dBdCZ)dadddddEdFZ*dbdddddGdHZ+ddIdIdJdKdLZ,ddddMdNZ-dddOdPdQZ.dRd7dSdTdUZ/G dVdW dWZ0G dXdY dYZ1dddZd[d\Z2dddZd]d^Z3dS )c    )annotationsN)Path)
getproxies   )PrimitiveDataURLz%22z\\)"\c                 C  s$   i | ]}|d krt |d|qS )   z%{:02X})chrformat.0c r   K/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/httpx/_utils.py
<dictcomp>   s       r       |c                 C  s   g | ]}t |qS r   )reescaper   r   r   r   
<listcomp>   s     r   zstr | bytesboolz
str | Nonebytes)valuelowerencodingreturnc                 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   Zbytes_valuer   r   r   normalize_header_key   s    
r"   )r   r   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   str)r   r   c                 C  s,   | dkrdS | dkrdS | dk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*   )r   r   c                 C  s*   zt |  W n tk
r$   Y dS X dS )z7
    Return `True` if `encoding` is a known codec.
    FT)codecslookupLookupError)r   r   r   r   is_known_encodingG   s
    r.   )namer   r   c                 C  s0   ddddd}t ||}|  d| d S )z;
    Encode a name/value pair within a multipart form.
    ztyping.Match[str]r%   )matchr   c                 S  s   t | d S )Nr   )!_HTML5_FORM_ENCODING_REPLACEMENTSgroup)r0   r   r   r   replacerW   s    z#format_form_param.<locals>.replacerz="r	   )_HTML5_FORM_ENCODING_REsubr!   )r/   r   r3   r   r   r   format_form_paramR   s    r6   r   c                  C  sT   dt jkr(tt jd } |  r(t| S dt jkrPtt jd }| rPt|S d S )NZSSL_CERT_FILEZSSL_CERT_DIR)osenvironr   is_filer%   is_dir)Zssl_fileZssl_pathr   r   r   get_ca_bundle_from_env^   s    

r<   zlist[dict[str, str]]c           	   
   C  s   g }d}|  |} | s|S td| D ]}z|dd\}}W n tk
r\   |d }}Y nX d| di}|dD ]F}z|d\}} W n tk
r   Y  qY nX |  ||| |< qv|| 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valr>   paramslinkparamkeyr   r   r   parse_header_linksj   s&    
rK   )content_typer   c                 C  s   t j }| |d< |jd dS )Nzcontent-type)failobj)emailmessageMessageget_content_charset)rL   msgr   r   r   parse_content_type_charset   s    
rS   authorizationzproxy-authorizationz4typing.Iterable[tuple[typing.AnyStr, typing.AnyStr]]z4typing.Iterator[tuple[typing.AnyStr, typing.AnyStr]])itemsr   c                 c  s8   | D ].\}}t | tkr(td|d}||fV  qd S )Nz[secure])match_type_of)to_strr   SENSITIVE_HEADERSto_bytes_or_str)rU   kvr   r   r   obfuscate_sensitive_headers   s    r\   r   z
int | None)r>   r   c                 C  s"   | j d k	r| j S ddd| jS )NP     )httphttps)portgetscheme)r>   r   r   r   port_or_default   s    
rd   )r>   otherr   c                 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.
    )rc   hostrd   )r>   re   r   r   r   same_origin   s
    
rg   )r>   locationr   c                 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'
    Fr_   r]   r`   r^   )rf   rc   rd   )r>   rh   r   r   r   is_https_redirect   s    


ri   zdict[str, str | None]c                  C  s   t  } i }dD ]6}| |r| | }d|kr0|nd| || d< qdd | dddD }|D ]}|d	krzi   S |rfd|krd
||< qft|rd
|d| < qft|rd
|d| d< qf| dkrd
|d| < qfd
|d| < qf|S )z+Gets proxy information from the environment)r_   r`   allz://zhttp://c                 S  s   g | ]}|  qS r   )r@   )r   rf   r   r   r   r      s     z+get_environment_proxies.<locals>.<listcomp>nor(   ,*Nzall://zall://[]	localhostzall://*)r   rb   rA   is_ipv4_hostnameis_ipv6_hostnamer   )Z
proxy_infoZmountsrc   hostnameZno_proxy_hostsr   r   r   get_environment_proxies   s,    

rs   utf-8c                 C  s   t | tr| |S | S Nr    r%   r!   r#   r   r   r   to_bytes   s    rw   c                 C  s   t | tr| S | |S ru   )r    r%   decoder#   r   r   r   rW      s    rW   ztyping.AnyStr)r   rV   r   c                 C  s   t |tr| S |  S ru   rv   )r   rV   r   r   r   rY      s    rY   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    rz   )filenamer   c                 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~   
typing.Any)streamr   c                 C  sx   z|   }t|j}W nZ ttfk
rr   z$|  }| dtj}| | W n ttfk
rl   Y Y dS X Y nX |S )zs
    Given a file-like stream object, return its length in number of bytes
    without reading it into memory.
    r   N)	filenor8   fstatst_sizeAttributeErrorOSErrortellseekSEEK_END)r   fdlengthoffsetr   r   r   peek_filelike_length
  s    r   c                   @  sR   e Zd ZddddZddddZdddd	Zddd
dZddddZdS )Timerfloatr7   c                   s8   t  }|dkr dd l}| S dd l}|  S d S )Ntrior   )sniffioZ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 ru   r   perf_counterstartedr   r   r   r   
sync_start/  s    zTimer.sync_startc                   s   |   I d H | _d S ru   r   r   r   r   r   r   async_start2  s    zTimer.async_startc                 C  s   t  }|| j S ru   r   r   nowr   r   r   sync_elapsed5  s    zTimer.sync_elapsedc                   s   |   I d H }|| j S ru   r   r   r   r   r   async_elapsed9  s    zTimer.async_elapsedN)__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ddZddd	d
dZeddddZddddZd dd	ddZ	dd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
    r%   r   )patternr   c                 C  s  ddl m} |r.d|kr.td| d| d||}|| _|jdkrJdn|j| _|jd	kr`dn|j| _|j| _|jr|jd	krd | _n|jd
rt	
|jdd  }t	d| d| _nV|jd	rt	
|jdd  }t	d| d| _n 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://"rj   r(   rm   z*.   z^.+\.$z^(.+\.)?^)_urlsr   rB   r   rc   rf   ra   
host_regex
startswithr   r   compile)r   r   r   r>   domainr   r   r   __init__h  s(    zURLPattern.__init__r   r   )re   r   c                 C  sV   | j r| j |j krdS | jr8| jd k	r8| j|js8dS | jd k	rR| j|jkrRdS dS )NFT)rc   rf   r   r0   ra   r   re   r   r   r   matches  s    zURLPattern.matchesztuple[int, int, int]r7   c                 C  s4   | j dk	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   )ra   lenrf   rc   )r   Zport_priorityZhost_priorityZscheme_priorityr   r   r   priority  s    zURLPattern.priorityintc                 C  s
   t | jS ru   )hashr   r   r   r   r   __hash__  s    zURLPattern.__hash__c                 C  s   | j |j k S ru   )r   r   r   r   r   __lt__  s    zURLPattern.__lt__r   c                 C  s   t |to| j|jkS ru   )r    r   r   r   r   r   r   __eq__  s    zURLPattern.__eq__N)r   r   r   __doc__r   r   propertyr   r   r   r   r   r   r   r   r   >  s   )r   )rr   r   c                 C  s4   zt | dd  W n tk
r.   Y dS X dS N/r   FT)	ipaddressIPv4AddressrA   	Exceptionrr   r   r   r   rp     s
    rp   c                 C  s4   zt | dd  W n tk
r.   Y dS X dS r   )r   IPv6AddressrA   r   r   r   r   r   rq     s
    rq   )N)N)rt   )rt   )4
__future__r   r+   email.messagerN   r   r|   r8   r   r   typingpathlibr   urllib.requestr   r   _typesr   TYPE_CHECKINGr   r   r1   updateranger   joinkeysr4   r"   r$   r*   r.   r6   r<   rK   rS   rX   r\   rd   rg   ri   rs   rw   rW   rY   rz   r~   r   r   r   rp   rq   r   r   r   r   <module>   s\   
 	%	1m