a
    f	g%<                     @   s  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r~ddlmZ ddd	Zed
d edD  eddd e D ZdXejeef eeje edddZdYejeef eje edddZ dedddZ!eedddZ"eeedddZ#d Z$e$d! Z%e$d" Z&eeje d#d$d%Z'eje d&d'd(Z(eej)ej*eef  dd)d*Z+eeje d+d,d-Z,d.d/hZ-ej.ej/ej0ej0f  ej1ej/ej0ej0f  d0d1d2Z2d3eje3 d4d5d6Z4d3d3ed7d8d9Z5d3d3ed:d;d<Z6ej*eeje f d&d=d>Z7dZejeef eedd@dAZ8d[ejeef eeddBdCZ9eej0ej0dDdEdFZ:eeddGdHZ;eje eje dIdJdKZ<ej=eje3 dLdMdNZ>G dOdP dPZ?G dQdR dRZ@eedSdTdUZAeedSdVdWZBdS )\    N)Path)
getproxies   )PrimitiveDataURLz%22z\\)"\c                 C   s$   i | ]}|d krt |d|qS )   z%{:02X})chrformat.0c r   O/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/httpx/_utils.py
<dictcomp>       r       |c                 C   s   g | ]}t |qS r   )reescaper   r   r   r   
<listcomp>   r   r   )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)
isinstancebytes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   )r   r   c                 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 )strr   r   r   r   primitive_value_to_str8   s    r)   )r   r   c                 C   s(   zt |  W n ty"   Y dS 0 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   s6   t jt tddd}t||}|  d| d S )z;
    Encode a name/value pair within a multipart form.
    )matchr   c                 S   s   t | d S )Nr   )!_HTML5_FORM_ENCODING_REPLACEMENTSgroup)r/   r   r   r   replacerW   s    z#format_form_param.<locals>.replacerz="r   )typingMatchr'   _HTML5_FORM_ENCODING_REsubr    )r.   r   r2   r   r   r   format_form_paramR   s    r7             )datar   c                 C   s   | d d }|t jt jfv r dS |d d t jkr6dS |d d t jt jfv rRdS |t}|dkrhdS |dkr|d d d tkrd	S |d
d d tkrdS |dkr|d d t	krdS |d
d  t	krdS d S )N   zutf-32r:   z	utf-8-sigr9   zutf-16r   utf-8z	utf-16-ber   z	utf-16-lez	utf-32-bez	utf-32-le)
r*   BOM_UTF32_LEBOM_UTF32_BEBOM_UTF8BOM_UTF16_LEBOM_UTF16_BEcount_null_null2_null3)r;   sample	nullcountr   r   r   guess_json_utfd   s*    
rI   r   c                  C   sT   dt jv r(tt jd } |  r(t| S dt jv 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    

rO   c           	   
   C   s   g }d}|  |} | s|S td| D ]}z|dd\}}W n tyZ   |d }}Y n0 d| di}|dD ]D}z|d\}} W n ty   Y  qY n0 |  ||| |< qt|| 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valrQ   paramslinkparamkeyr   r   r   parse_header_links   s&    
r^   )content_typer   c                 C   s   t j }| |d< |jd dS )Nzcontent-type)failobj)emailmessageMessageget_content_charset)r_   msgr   r   r   parse_content_type_charset   s    
rf   authorizationzproxy-authorization)itemsr   c                 c   s8   | 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)rh   kvr   r   r   obfuscate_sensitive_headers   s    ro   r   )rQ   r   c                 C   s"   | j d ur| j S ddd| jS )NP     )httphttps)portgetscheme)rQ   r   r   r   port_or_default   s    
rw   )rQ   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.
    )rv   hostrw   )rQ   rx   r   r   r   same_origin   s
    
rz   )rQ   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'
    Frr   rp   rs   rq   )ry   rv   rw   )rQ   r{   r   r   r   is_https_redirect   s    


r|   c                  C   s   t  } i }dD ]6}| |r| | }d|v r0|nd| || d< qdd | dddD }|D ]t}|d	krzi   S |rft|rd
|d| < qft|rd
|d| d< qf| dkrd
|d| < qfd
|d| < qf|S )z+Gets proxy information from the environment)rr   rs   allz://zhttp://c                 S   s   g | ]}|  qS r   )rS   )r   ry   r   r   r   r      r   z+get_environment_proxies.<locals>.<listcomp>nor&   ,*Nzall://zall://[]	localhostzall://*)r   ru   rT   is_ipv4_hostnameis_ipv6_hostnamer   )Z
proxy_infoZmountsrv   hostnameZno_proxy_hostsr   r   r   get_environment_proxies   s(    
r   r=   c                 C   s   t | tr| |S | S Nr   r'   r    r"   r   r   r   to_bytes  s    r   c                 C   s   t | tr| S | |S r   )r   r'   decoder"   r   r   r   rj     s    rj   )r   ri   r   c                 C   s   t |tr| S |  S r   r   )r   ri   r   r   r   rl     s    rl   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    r   )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   )streamr   c                 C   st   z|   }t|j}W nV ttfyn   z$|  }| dtj}| | W n ttfyh   Y Y dS 0 Y n0 |S )zs
    Given a file-like stream object, return its length in number of bytes
    without reading it into memory.
    r   N)	filenorK   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edddZddddZddddZedd	d
ZedddZdS )TimerrJ   c                    sZ   t  }|dkr dd l}| S |dkrFdd l}tt| I d H S dd l	}|
  S )Ntrior   curio)sniffioZcurrent_async_libraryr   current_timer   r3   castfloatZclockasyncioZget_event_looptime)selflibraryr   r   r   r   r   r   	_get_timeE  s    zTimer._get_timeNc                 C   s   t  | _d S r   r   perf_counterstartedr   r   r   r   
sync_startT  s    zTimer.sync_startc                    s   |   I d H | _d S r   r   r   r   r   r   r   async_startW  s    zTimer.async_startc                 C   s   t  }|| j S r   r   r   nowr   r   r   sync_elapsedZ  s    zTimer.sync_elapsedc                    s   |   I d H }|| j S r   r   r   r   r   r   async_elapsed^  s    zTimer.async_elapsed)	__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   D  s
   r   c                   @   s~   e Zd ZdZeddddZdeddd	Zee	j
eeef d
ddZed
ddZd edddZe	je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
    N)patternr   c                 C   s  ddl m} |r.d|v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://"r}   r&   r   z*.r9   z^.+\.$z^(.+\.)?^)_urlsr   rU   r   rv   ry   rt   
host_regex
startswithr   r   compile)r   r   r   rQ   domainr   r   r   __init__  s0    zURLPattern.__init__r   )rx   r   c                 C   sV   | j r| j |j krdS | jr8| jd ur8| j|js8dS | jd urR| j|jkrRdS dS )NFT)rv   ry   r   r/   rt   r   rx   r   r   r   matches  s    zURLPattern.matchesrJ   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   )rt   lenry   rv   )r   Zport_priorityZhost_priorityZscheme_priorityr   r   r   priority  s    zURLPattern.priorityc                 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__c                 C   s   t |to| j|jkS r   )r   r   r   r   r   r   r   __eq__  s    zURLPattern.__eq__)r   r   r   __doc__r'   r   boolr   propertyr3   Tupleintr   r   r   Anyr   r   r   r   r   r   c  s   )r   )r   r   c                 C   s2   zt | dd  W n ty,   Y dS 0 dS N/r   FT)	ipaddressIPv4AddressrT   	Exceptionr   r   r   r   r     s
    r   c                 C   s2   zt | dd  W n ty,   Y dS 0 dS r   )r   IPv6AddressrT   r   r   r   r   r   r     s
    r   )N)N)r=   )r=   )Cr*   email.messagera   r   r   rK   r   r   r3   pathlibr   urllib.requestr   r   _typesr   TYPE_CHECKINGr   r   r0   updateranger   joinkeysr5   Unionr'   r   r   Optionalr!   r#   r)   r-   r7   rD   rE   rF   rI   rO   ListDictr^   rf   rk   Iterabler   AnyStrIteratorro   r   rw   rz   r|   r   r   rj   rl   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sx   
   %	/m