o
    h                     @  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mZ d dlmZmZ ddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& dd	l'm(Z( dd
l)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; g dZ<ddhZ=d3ddZ>d4d5ddZ?d4d6ddZ@d7d!d"ZAd8d$d%ZBd9d)d*ZCG d+d, d,e	jDeEeEf ZFG d-d. d.ZGG d/d0 d0ZHG d1d2 d2e	jDeEeEf ZIdS ):    )annotationsN)Mapping)Cookie	CookieJar   )
ByteStreamUnattachedStreamencode_requestencode_response)SUPPORTED_DECODERSByteChunkerContentDecoderIdentityDecoderLineDecoderMultiDecoderTextChunkerTextDecoder)CookieConflictHTTPStatusErrorRequestNotReadResponseNotReadStreamClosedStreamConsumedrequest_context)(get_multipart_boundary_from_content_type)codes)AsyncByteStreamCookieTypesHeaderTypesQueryParamTypesRequestContentRequestDataRequestExtensionsRequestFilesResponseContentResponseExtensionsSyncByteStream)URL)to_bytes_or_strto_str)CookiesHeadersRequestResponseauthorizationzproxy-authorizationencodingstrreturnboolc                 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/    r7   a/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/httpx/_models.py_is_known_encoding8   s   r9   keystr | bytes
str | Nonebytesc                 C  s   t | tr| S | |pdS )zE
    Coerce str/bytes into a strictly byte-wise HTTP header key.
    ascii)
isinstancer=   encode)r:   r/   r7   r7   r8   _normalize_header_keyC   s   rA   valuec                 C  s8   t | tr| S t | tstdt|  | |pdS )zG
    Coerce str/bytes into a strictly byte-wise HTTP header value.
    z'Header value must be str or bytes, not r>   )r?   r=   r0   	TypeErrortyper@   )rB   r/   r7   r7   r8   _normalize_header_valueJ   s
   

rE   content_typec                 C  s   t j }| |d< |jd dS )Ncontent-type)failobj)emailmessageMessageget_content_charset)rF   msgr7   r7   r8   _parse_content_type_charsetU   s   
rN   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    urlz<> '"=)stripresplit
ValueErrorappend)	rB   linksreplace_charsvalrR   paramslinkparamr:   r7   r7   r8   _parse_header_links]   s*   
r_   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)r)   lowerSENSITIVE_HEADERSr(   )r`   kvr7   r7   r8   _obfuscate_sensitive_headers   s   rh   c                   @  s   e Zd ZdZ		dAdBd	d
ZedCddZejdDddZedEddZdFddZ	dGddZ
dHddZdIddZdJdKd"d#ZdLdMd(d)ZdJdNd*d+ZdOd,d-ZdPd.d/ZdQd0d1ZdRd2d3ZdSd4d5ZdTd7d8ZdUd:d;ZdVd=d>ZdCd?d@ZdS )Wr+   z9
    HTTP headers, as a case-insensitive multi-dict.
    NheadersHeaderTypes | Noner/   r<   r1   Nonec                 C  s   g | _ t|trt|j | _ nDt|tr3| D ]\}}t||}t||}| j ||	 |f qn |d urS|D ]\}}t||}t||}| j ||	 |f q9|| _
d S N)_listr?   r+   listr   r`   rA   rE   rX   rd   	_encoding)selfri   r/   rf   rg   	bytes_keybytes_valuer7   r7   r8   __init__   s   






zHeaders.__init__r0   c              
   C  sj   | j du r2dD ]'}| jD ]\}}z|| || W q ty&   Y  nw || _  | j S qd| _ | j S )zn
        Header encoding is mandated as ascii, but we allow fallbacks to utf-8
        or iso-8859-1.
        N)r>   utf-8z
iso-8859-1)ro   rawdecodeUnicodeDecodeError)rp   r/   r:   rB   r7   r7   r8   r/      s   

	zHeaders.encodingrB   c                 C  
   || _ d S rl   )ro   rp   rB   r7   r7   r8   r/         
list[tuple[bytes, bytes]]c                 C     dd | j D S )zH
        Returns a list of the raw header items, as byte pairs.
        c                 S  s   g | ]	\}}}||fqS r7   r7   ).0raw_key_rB   r7   r7   r8   
<listcomp>       zHeaders.raw.<locals>.<listcomp>rm   rp   r7   r7   r8   ru      s   zHeaders.rawtyping.KeysView[str]c                   s    fdd j D  S )Nc                   s    i | ]\}}}|  jd qS rl   rv   r/   r}   r   r:   rB   r   r7   r8   
<dictcomp>   s     z Headers.keys.<locals>.<dictcomp>)rm   keysr   r7   r   r8   r      s   zHeaders.keystyping.ValuesView[str]c                 C  ^   i }| j D ]%\}}}|| j}|| j}||v r&||  d| 7  < q|||< q| S )N, )rm   rv   r/   valuesrp   values_dictr   r:   rB   str_key	str_valuer7   r7   r8   r      s   
zHeaders.valuestyping.ItemsView[str, str]c                 C  r   )z
        Return `(key, value)` items of headers. Concatenate headers
        into a single comma separated value when a key occurs multiple times.
        r   )rm   rv   r/   r`   r   r7   r7   r8   r`      s   
zHeaders.itemslist[tuple[str, str]]c                   s    fdd j D S )z
        Return a list of `(key, value)` pairs of headers. Allow multiple
        occurrences of the same key without concatenating into a single
        comma separated value.
        c                   s*   g | ]\}}}|  j|  jfqS r7   r   r   r   r7   r8   r      s    z'Headers.multi_items.<locals>.<listcomp>r   r   r7   r   r8   multi_items   s   
zHeaders.multi_itemsr:   default
typing.Anyc                 C  s"   z| | W S  t y   | Y S w )z
        Return a header value. If multiple occurrences of the header occur
        then concatenate them together with commas.
        )KeyError)rp   r:   r   r7   r7   r8   get   s
   
zHeaders.getFsplit_commasr2   	list[str]c                   sZ   |  j  fddjD }|s|S g }|D ]}|dd |dD  q|S )z
        Return a list of all header values for a given key.
        If `split_commas=True` is passed, then any comma separated header
        values are split into multiple return strings.
        c                   s*   g | ]\}}}|   kr|jqS r7   )rd   rv   r/   )r}   r   item_key
item_valueget_header_keyrp   r7   r8   r     s
    
z$Headers.get_list.<locals>.<listcomp>c                 S  s   g | ]}|  qS r7   )rT   )r}   itemr7   r7   r8   r     s    ,)rd   r@   r/   rm   extendrV   )rp   r:   r   r   split_valuesrB   r7   r   r8   get_list   s   zHeaders.get_listc                 C  s:   t |}| D ]}|| v r| | q| j|j d S rl   )r+   r   poprm   r   )rp   ri   r:   r7   r7   r8   update  s   
zHeaders.updatec                 C  s   t | | jdS )Nr6   )r+   r/   r   r7   r7   r8   copy  s   zHeaders.copyc                   s<   |  j  fddjD }|rd|S t|)z
        Return a single header value.

        If there are multiple headers with the same key, then we concatenate
        them with commas. See: https://tools.ietf.org/html/rfc7230#section-3.2.2
        c                   s&   g | ]\}}}| kr| jqS r7   r   )r}   r   
header_keyheader_valuenormalized_keyrp   r7   r8   r   %  s
    
z'Headers.__getitem__.<locals>.<listcomp>r   )rd   r@   r/   rm   joinr   )rp   r:   r`   r7   r   r8   __getitem__  s   
zHeaders.__getitem__c                   s   | | jpd}| | jpd}|   fddt| jD }t|dd D ]}| j|= q(|r?|d }| |f| j|< dS | j| |f dS )zs
        Set the header `key` to `value`, removing any duplicate entries.
        Retains insertion order.
        rt   c                   s"   g | ]\}\}}}| kr|qS r7   r7   r}   idxr   r   
lookup_keyr7   r8   r   9  s
    z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )r@   ro   rd   	enumeraterm   reversedrX   )rp   r:   rB   set_key	set_valuefound_indexesr   r7   r   r8   __setitem__0  s   

zHeaders.__setitem__c                   sN   |  | j  fddt| jD }|st|t|D ]}| j|= qdS )z*
        Remove the header `key`.
        c                   s&   g | ]\}\}}}|   kr|qS r7   )rd   r   del_keyr7   r8   r   N  s
    z'Headers.__delitem__.<locals>.<listcomp>N)rd   r@   r/   r   rm   r   r   )rp   r:   pop_indexesr   r7   r   r8   __delitem__H  s   

zHeaders.__delitem__c                 C  s$   |  | j}|dd | jD v S )Nc                 S  s   g | ]\}}}|qS r7   r7   )r}   r   r:   r7   r7   r8   r   \  s    z(Headers.__contains__.<locals>.<listcomp>)rd   r@   r/   rm   )rp   r:   r   r7   r7   r8   __contains__Z  s   zHeaders.__contains__typing.Iterator[typing.Any]c                 C  s   t |  S rl   )iterr   r   r7   r7   r8   __iter__^     zHeaders.__iter__intc                 C  
   t | jS rl   )lenrm   r   r7   r7   r8   __len__a     
zHeaders.__len__otherc                 C  sR   zt |}W n
 ty   Y dS w dd | jD }dd |jD }t|t|kS )NFc                 S     g | ]	\}}}||fqS r7   r7   r   r7   r7   r8   r   j  r   z"Headers.__eq__.<locals>.<listcomp>c                 S  r   r7   r7   r   r7   r7   r8   r   k  r   )r+   rW   rm   sorted)rp   r   other_headers	self_list
other_listr7   r7   r8   __eq__d  s   zHeaders.__eq__c                 C  sv   | j j}d}| jdkrd| j}tt|  }t|}t|t|k}|r1| d|| dS | d|| dS )NrQ   r>   z, encoding=())	__class____name__r/   rn   rh   r   dictr   )rp   
class_nameencoding_stras_listas_dictno_duplicate_keysr7   r7   r8   __repr__n  s   
zHeaders.__repr__NN)ri   rj   r/   r<   r1   rk   r1   r0   rB   r0   r1   rk   )r1   r{   )r1   r   )r1   r   )r1   r   )r1   r   rl   )r:   r0   r   r   r1   r   )F)r:   r0   r   r2   r1   r   )ri   rj   r1   rk   )r1   r+   )r:   r0   r1   r0   r:   r0   rB   r0   r1   rk   )r:   r0   r1   rk   )r:   r   r1   r2   )r1   r   r1   r   )r   r   r1   r2   )r   
__module____qualname____doc__rs   propertyr/   setterru   r   r   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   r+      s6    













r+   c                
   @  sv   e Zd Zdddddddddd	d0ddZd1dd Zed2d"d#Zd2d$d%Zd2d&d'Zd3d(d)Z	d4d+d,Z
d5d.d/ZdS )6r,   N)	r\   ri   cookiescontentdatafilesjsonstream
extensionsmethodr0   rR   	URL | strr\   QueryParamTypes | Noneri   rj   r   CookieTypes | Noner   RequestContent | Noner   RequestData | Noner   RequestFiles | Noner   typing.Any | Noner   'SyncByteStream | AsyncByteStream | Noner   RequestExtensions | Noner1   rk   c       	      	   C  s   |  | _|d u rt|nt||d| _t|| _|d u ri nt|| _|r-t|	|  |
d u rc| j
d}t||||	t|rF|| jjnd dd\}}
| | |
| _t|
tra|   d S d S |
| _d S )N)r\   rG   )rF   )r   r   r   r   boundary)upperr   r'   rR   r+   ri   r   r   r*   set_cookie_headerr   r	   r   r@   r/   _preparer   r?   r   read)rp   r   rR   r\   ri   r   r   r   r   r   r   r   rF   r7   r7   r8   rs     s2   





zRequest.__init__default_headersdict[str, str]c                 C  s   |  D ]\}}| dkrd| jv rq| j|| qg }d| jv }d| jv p,d| jv }|s<| jjr<|d| jjf |sH| jdv rH|d t	|| jj
 | _d S )Ntransfer-encodingzContent-LengthHostzTransfer-Encodings   Host)POSTPUTPATCH)s   Content-Length   0)r`   rd   ri   
setdefaultrR   hostrX   netlocr   r+   ru   )rp   r   r:   rB   auto_headershas_hosthas_content_lengthr7   r7   r8   r     s   

zRequest._preparer=   c                 C     t | dst | jS N_content)hasattrr   r  r   r7   r7   r8   r        
zRequest.contentc                 C  sH   t | ds!t| jtjsJ d| j| _t| jts!t| j| _| jS )6
        Read and return the request content.
        r      )r  r?   r   typingIterabler   r  r   r   r7   r7   r8   r     s   
zRequest.readc                   sZ   t | ds*t| jtjsJ ddd | j2 I dH | _t| jts*t| j| _| jS )r	  r  r
  c                      g | z3 d H W }|q6 S rl   r7   r}   partr7   r7   r8   r         z!Request.aread.<locals>.<listcomp>N)r  r?   r   r  AsyncIterabler   r  r   r   r7   r7   r8   aread  s   
zRequest.areadc                 C  s,   | j j}t| j}d| d| jd|dS )N<r   r   z)>)r   r   r0   rR   r   )rp   r   rR   r7   r7   r8   r     s   
zRequest.__repr__dict[str, typing.Any]c                 C     dd | j  D S )Nc                 S     i | ]\}}|d vr||qS ))r   r   r7   r}   namerB   r7   r7   r8   r     
    z(Request.__getstate__.<locals>.<dictcomp>__dict__r`   r   r7   r7   r8   __getstate__     zRequest.__getstate__statec                 C  s0   |  D ]
\}}t| || qi | _t | _d S rl   )r`   setattrr   r   r   rp   r  r  rB   r7   r7   r8   __setstate__  s   zRequest.__setstate__)r   r0   rR   r   r\   r   ri   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   rk   r   r   r1   rk   r1   r=   r   r1   r  r  r  r1   rk   )r   r   r   rs   r   r   r   r   r  r   r  r!  r7   r7   r7   r8   r,   ~  s&    
:



r,   c                   @  s8  e Zd Zddddddddddd
dddZddd Zedd"d#Zejdd%d#Zedd'd(Zejdd*d(Zedd,d-Z	edd.d/Z
edd1d2Zedd4d5Zedd6d7Zedd8d9Zejdd:d9Zedd;d<Zdd>d?ZeddAdBZeddCdDZeddEdFZeddGdHZeddIdJZeddKdLZeddMdNZddOdPZddRdSZeddUdVZeddXdYZeddZd[Zdd\d]Zdd_d`ZddbdcZddddeZ dddidjZ!dddldmZ"ddndoZ#dddpdqZ$ddrdsZ%ddtduZ&	dddwdxZ'	dddzd{Z(dd|d}Z)	ddd~dZ*dddZ+dS )r-   Nrt   )
ri   r   texthtmlr   r   requestr   historydefault_encodingstatus_coder   ri   rj   r   ResponseContent | Noner&  r<   r'  r   r   r   r   r(  Request | Noner   ResponseExtensions | Noner)  list[Response] | Noner*  #str | typing.Callable[[bytes], str]r1   rk   c       
         C  s   || _ t|| _|| _d | _|	d u ri nt|	| _|
d u rg nt|
| _d| _	d| _
|| _|d u rLt||||\}}| | || _t|trK|   n|| _d| _d S )NFr   )r+  r+   ri   _requestnext_requestr   r   rn   r)  	is_closedis_stream_consumedr*  r
   r   r   r?   r   r   _num_bytes_downloaded)rp   r+  ri   r   r&  r'  r   r   r(  r   r)  r*  r7   r7   r8   rs     s$   



zResponse.__init__r   r   c                 C  s<   |  D ]\}}| dkrd| jv rq| j|| qd S )Nr   zcontent-length)r`   rd   ri   r   )rp   r   r:   rB   r7   r7   r8   r   ;  s
   zResponse._preparedatetime.timedeltac                 C  s   t | ds	td| jS )ze
        Returns the time taken for the complete request/response
        cycle to complete.
        _elapsedzK'.elapsed' may only be accessed after the response has been read or closed.)r  RuntimeErrorr7  r   r7   r7   r8   elapsedB  s
   
zResponse.elapsedr9  c                 C  rx   rl   )r7  )rp   r9  r7   r7   r8   r9  O  rz   r,   c                 C  s   | j du r	td| j S )zR
        Returns the request instance associated to the current response.
        Nz7The request instance has not been set on this response.)r1  r8  r   r7   r7   r8   r(  S  s
   
zResponse.requestrB   c                 C  rx   rl   )r1  ry   r7   r7   r8   r(  ^  rz   r0   c                 C  s2   z| j d }W n
 ty   Y dS w |jdddS )Nhttp_versionzHTTP/1.1r>   ignoreerrors)r   r   rv   )rp   r:  r7   r7   r8   r:  b  s   zResponse.http_versionc                 C  s<   z| j d }W n ty   t| j Y S w |jdddS )Nreason_phraser>   r;  r<  )r   r   r   get_reason_phraser+  rv   )rp   r>  r7   r7   r8   r>  k  s   zResponse.reason_phraser'   c                 C  s   | j jS )zA
        Returns the URL for which the request was made.
        )r(  rR   r   r7   r7   r8   rR   t  s   zResponse.urlr=   c                 C  r  r  )r  r   r  r   r7   r7   r8   r   {  r  zResponse.contentc                 C  sR   t | ds&| j}|sd| _| jS t| jpdd}d|| j| g| _| jS )N_textrQ   rt   r6   )r  r   r@  r   r/   r   rv   flush)rp   r   decoderr7   r7   r8   r&    s   
zResponse.textc                 C  sZ   t | ds*| j}|du st|s%t| jtr| j}nt | dr%| | j}|p(d| _| jS )a  
        Return an encoding to use for decoding the byte content into text.
        The priority for determining this is given by...

        * `.encoding = <>` has been set explicitly.
        * The encoding as specified by the charset parameter in the Content-Type header.
        * The encoding as determined by `default_encoding`, which may either be
          a string like "utf-8" indicating the encoding to use, or may be a callable
          which enables charset autodetection.
        ro   Nr  rt   )r  charset_encodingr9   r?   r*  r0   r  ro   )rp   r/   r7   r7   r8   r/     s   


zResponse.encodingc                 C  s   t | dr	td|| _dS )z
        Set the encoding to use for decoding the byte content into text.

        If the `text` attribute has been accessed, attempting to set the
        encoding will throw a ValueError.
        r@  z?Setting encoding after `text` has been accessed is not allowed.N)r  rW   ro   ry   r7   r7   r8   r/     s
   

c                 C  s    | j d}|du rdS t|S )zO
        Return the encoding, as specified by the Content-Type header.
        zContent-TypeN)ri   r   rN   )rp   rF   r7   r7   r8   rC    s   zResponse.charset_encodingr   c              	   C  s   t | dsPg }| jjddd}|D ]}|  }zt| }||  W q ty.   Y qw t|dkr=|d | _	| j	S t|dkrLt
|d| _	| j	S t | _	| j	S )z
        Returns a decoder instance which can be used to decode the raw byte
        content, depending on the Content-Encoding used in the response.
        _decoderzcontent-encodingT)r   r   r   )children)r  ri   r   rT   rd   r   rX   r   r   rD  r   r   )rp   decodersr   rB   decoder_clsr7   r7   r8   _get_content_decoder  s&   

zResponse._get_content_decoderr2   c                 C     t | jS )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   is_informationalr+  r   r7   r7   r8   rJ       zResponse.is_informationalc                 C  rI  )zU
        A property which is `True` for 2xx status codes, `False` otherwise.
        )r   
is_successr+  r   r7   r7   r8   rL    rK  zResponse.is_successc                 C  rI  )a  
        A property which is `True` for 3xx status codes, `False` otherwise.

        Note that not all responses with a 3xx status code indicate a URL redirect.

        Use `response.has_redirect_location` to determine responses with a properly
        formed URL redirection.
        )r   is_redirectr+  r   r7   r7   r8   rM    s   
zResponse.is_redirectc                 C  rI  )zU
        A property which is `True` for 4xx status codes, `False` otherwise.
        )r   is_client_errorr+  r   r7   r7   r8   rN    rK  zResponse.is_client_errorc                 C  rI  )zU
        A property which is `True` for 5xx status codes, `False` otherwise.
        )r   is_server_errorr+  r   r7   r7   r8   rO    rK  zResponse.is_server_errorc                 C  rI  )z]
        A property which is `True` for 4xx and 5xx status codes, `False` otherwise.
        )r   is_errorr+  r   r7   r7   r8   rP    rK  zResponse.is_errorc                 C  s(   | j tjtjtjtjtjfv od| jv S )zs
        Returns True for 3xx responses with a properly formed URL redirection,
        `False` otherwise.
        Location)r+  r   MOVED_PERMANENTLYFOUND	SEE_OTHERTEMPORARY_REDIRECTPERMANENT_REDIRECTri   r   r7   r7   r8   has_redirect_location  s   zResponse.has_redirect_locationc                 C  sp   | j }|du rtd| jr| S | jrd}nd}| jd }dddd	d
}||d}|j| |d}t||| d)z>
        Raise the `HTTPStatusError` if one occurred.
        NzYCannot call `raise_for_status` as the request instance has not been set on this response.z{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'
Redirect location: '{0.headers[location]}'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}z{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}d   zInformational responsezRedirect responsezClient errorzServer error)r            zInvalid status code)
error_type)r(  response)r1  r8  rL  rW  r+  r   formatr   )rp   r(  rJ   status_classerror_typesr\  r7   r7   r8   raise_for_status  s*   
zResponse.raise_for_statuskwargsc                 K  s   t j| jfi |S rl   )jsonlibloadsr   )rp   rb  r7   r7   r8   r   ?  s   zResponse.jsonr*   c                 C  s$   t | dst | _| j|  | jS )N_cookies)r  r*   re  extract_cookiesr   r7   r7   r8   r   B  s   
zResponse.cookies dict[str | None, dict[str, str]]c                 C  s*   | j d}|du ri S dd t|D S )zI
        Returns the parsed header links of the response, if any
        r]   Nc                 S  s"   i | ]}| d p| d|qS )relrR   )r   )r}   r]   r7   r7   r8   r   R  s    z"Response.links.<locals>.<dictcomp>)ri   r   r_   )rp   headerr7   r7   r8   rY   I  s   zResponse.linksc                 C  s   | j S rl   )r5  r   r7   r7   r8   num_bytes_downloadedW  s   zResponse.num_bytes_downloadedc                 C  s   d| j  d| j dS )Nz<Response [ ]>)r+  r>  r   r7   r7   r8   r   [  s   zResponse.__repr__r  c                 C  r  )Nc                 S  r  ))r   r   r3  rD  r7   r  r7   r7   r8   r   _  r  z)Response.__getstate__.<locals>.<dictcomp>r  r   r7   r7   r8   r  ^  r  zResponse.__getstate__r  c                 C  s6   |  D ]
\}}t| || qd| _i | _t | _d S )NT)r`   r  r3  r   r   r   r   r7   r7   r8   r!  e  s
   zResponse.__setstate__c                 C  s    t | dsd|  | _| jS )7
        Read and return the response content.
        r  r
  )r  r   
iter_bytesr  r   r7   r7   r8   r   l  s   
zResponse.read
chunk_size
int | Nonetyping.Iterator[bytes]c                 c  s    t | dr,|du rt| jn|}tdt| jt|dD ]}| j|||  V  qdS |  }t|d}t| jd9 | 	 D ]}|
|}|
|D ]}|V  qLq@| }|
|D ]}|V  q\| D ]}|V  qfW d   dS 1 sww   Y  dS z
        A byte-iterator over the decoded response content.
        This allows us to handle gzip, deflate, brotli, and zstd encoded responses.
        r  Nr   r   ro  r(  )r  r   r  rangemaxrH  r   r   r1  iter_rawrv   rA  rp   ro  irB  chunker	raw_bytesdecodedchunkr7   r7   r8   rn  t  s*   


"zResponse.iter_bytestyping.Iterator[str]c                 c  s    t | jpdd}t|d}t| jd9 |  D ]}||}||D ]}|V  q%q| }||D ]}|V  q5| D ]}|V  q?W d   dS 1 sPw   Y  dS z
        A str-iterator over the decoded response content
        that handles both gzip, deflate, etc but also detects the content's
        string encoding.
        rt   r6   rs  rt  N)r   r/   r   r   r1  rn  rv   rA  rp   ro  rB  rz  byte_contenttext_contentr}  r7   r7   r8   	iter_text  s    

"zResponse.iter_textc                 c  sr    t  }t| jd% |  D ]}||D ]}|V  qq| D ]}|V  q!W d    d S 1 s2w   Y  d S Nrt  )r   r   r1  r  rv   rA  rp   rB  r&  liner7   r7   r8   
iter_lines  s   "zResponse.iter_linesc                 c  s    | j rt | jrt t| jtstdd| _ d| _t	|d}t
| jd" | jD ]}|  jt|7  _||D ]}|V  q<q,W d   n1 sMw   Y  | D ]}|V  qV|   dS )@
        A byte-iterator over the raw response content.
        z5Attempted to call a sync iterator on an async stream.Tr   rs  rt  N)r4  r   r3  r   r?   r   r&   r8  r5  r   r   r1  r   rv   rA  closerp   ro  rz  raw_stream_bytesr}  r7   r7   r8   rw    s*   

zResponse.iter_rawc                 C  s`   t | jts
td| js.d| _t| jd | j  W d   dS 1 s'w   Y  dS dS )
        Close the response and release the connection.
        Automatically called if the response body is read to completion.
        z3Attempted to call an sync close on an async stream.Trt  N)r?   r   r&   r8  r3  r   r1  r  r   r7   r7   r8   r    s   "zResponse.closec                   s2   t | dsddd |  2 I dH | _| jS )rm  r  r
  c                   r  rl   r7   r  r7   r7   r8   r     r  z"Response.aread.<locals>.<listcomp>N)r  r   aiter_bytesr  r   r7   r7   r8   r    s   
 zResponse.areadtyping.AsyncIterator[bytes]c                 C s  t | dr,|du rt| jn|}tdt| jt|dD ]}| j|||  V  qdS |  }t|d}t| jd> | 	 2 z3 dH W }|
|}|
|D ]}|V  qPq@6 | }|
|D ]}|V  qa| D ]}|V  qkW d   dS 1 s|w   Y  dS rr  )r  r   r  ru  rv  rH  r   r   r1  	aiter_rawrv   rA  rx  r7   r7   r8   r    s,   


"zResponse.aiter_bytestyping.AsyncIterator[str]c                 C s   t | jpdd}t|d}t| jd> |  2 z3 dH W }||}||D ]}|V  q)q6 | }||D ]}|V  q:| D ]}|V  qDW d   dS 1 sUw   Y  dS r  )r   r/   r   r   r1  r  rv   rA  r  r7   r7   r8   
aiter_text  s"   

"zResponse.aiter_textc                 C s|   t  }t| jd* |  2 z3 d H W }||D ]}|V  qq6 | D ]}|V  q&W d    d S 1 s7w   Y  d S r  )r   r   r1  r  rv   rA  r  r7   r7   r8   aiter_lines  s   "zResponse.aiter_linesc                 C s   | j rt | jrt t| jtstdd| _ d| _t	|d}t
| jd' | j2 z3 dH W }|  jt|7  _||D ]}|V  q@q,6 W d   n1 sRw   Y  | D ]}|V  q[|  I dH  dS )r  z6Attempted to call an async iterator on an sync stream.Tr   rs  rt  N)r4  r   r3  r   r?   r   r   r8  r5  r   r   r1  r   rv   rA  acloser  r7   r7   r8   r    s*   
zResponse.aiter_rawc                   sh   t | jtstd| js2d| _t| jd | j I dH  W d   dS 1 s+w   Y  dS dS )r  z3Attempted to call an async close on an sync stream.Trt  N)r?   r   r   r8  r3  r   r1  r  r   r7   r7   r8   r  )  s   "zResponse.aclose)r+  r   ri   rj   r   r,  r&  r<   r'  r<   r   r   r   r   r(  r-  r   r.  r)  r/  r*  r0  r1   rk   r"  )r1   r6  )r9  r6  r1   rk   )r1   r,   )rB   r,   r1   rk   r   )r1   r'   r#  )r1   r<   r   )r1   r   r1   r2   )r1   r-   )rb  r   r1   r   )r1   r*   )r1   rg  r   r$  r%  rl   )ro  rp  r1   rq  )ro  rp  r1   r~  r1   r~  )r1   rk   )ro  rp  r1   r  )ro  rp  r1   r  )r1   r  ),r   r   r   rs   r   r   r9  r   r(  r:  r>  rR   r   r&  r/   rC  rH  rJ  rL  rM  rN  rO  rP  rW  ra  r   r   rY   rj  r   r  r!  r   rn  r  r  rw  r  r  r  r  r  r  r  r7   r7   r7   r8   r-     s    
7





%




	

	

r-   c                   @  s   e Zd ZdZd9d:ddZd;ddZd<ddZd=d>ddZ			d?d@ddZ		dAdBddZ	dAdCd d!Z
d9d:d"d#ZdDd$d%ZdEd&d'ZdFd(d)ZdGd+d,ZdHd.d/ZdId1d2ZdJd3d4ZG d5d6 d6ejjZG d7d8 d8ZdS )Kr*   z-
    HTTP Cookies, as a mutable mapping.
    Nr   r   r1   rk   c                 C  s   |d u s	t |tr%t | _t |tr!| D ]\}}| || qd S d S t |tr=t | _|D ]
\}}| || q0d S t |trTt | _|jD ]}| j| qId S || _d S rl   )	r?   r   r   jarr`   setrn   r*   
set_cookie)rp   r   r:   rB   cookier7   r7   r8   rs   <  s$   




zCookies.__init__r]  r-   c                 C  s(   |  |}| |j}| j|| dS )zO
        Loads any cookies based on the response `Set-Cookie` headers.
        N)_CookieCompatResponse_CookieCompatRequestr(  r  rf  )rp   r]  urllib_responseurllib_requestr7   r7   r8   rf  M  s   
zCookies.extract_cookiesr(  r,   c                 C  s   |  |}| j| dS )zM
        Sets an appropriate 'Cookie:' HTTP header on the `Request`.
        N)r  r  add_cookie_header)rp   r(  r  r7   r7   r8   r   V  s   
zCookies.set_cookie_headerrQ   /r  r0   rB   domainpathc                 C  s   i ddd|d|ddddd	|d
t |d|dd|dt |dddddddddddddidd}tdi |}| j| dS )zU
        Set a cookie value by name. May optionally include domain and path.
        versionr   r  rB   portNport_specifiedFr  domain_specifieddomain_initial_dot.r  path_specifiedsecureexpiresdiscardTcommentcomment_urlrestHttpOnlyrfc2109r7   )r2   
startswithr   r  r  )rp   r  rB   r  r  rb  r  r7   r7   r8   r  ]  sJ   
	
zCookies.setr   r<   c                 C  sn   d}| j D ])}|j|kr.|du s|j|kr.|du s|j|kr.|dur+d| }t||j}q|du r5|S |S )z
        Get a cookie by name. May optionally include domain and path
        in order to specify exactly which cookie to retrieve.
        Nz!Multiple cookies exist with name=)r  r  r  r  r   rB   )rp   r  r   r  r  rB   r  rJ   r7   r7   r8   r   w  s   


zCookies.getc                   s\    durdur| j  S  fdd| j D }|D ]}| j |j|j|j qdS )z
        Delete a cookie by name. May optionally include domain and path
        in order to specify exactly which cookie to delete.
        Nc                   s>   g | ]}|j kr d u s|j krd u s|jkr|qS rl   )r  r  r  r}   r  r  r  r  r7   r8   r     s    
z"Cookies.delete.<locals>.<listcomp>)r  clearr  r  r  )rp   r  r  r  remover  r7   r  r8   delete  s   
zCookies.deletec                 C  sD   g }|dur| | |dur|dusJ | | | jj|  dS )z
        Delete all cookies. Optionally include a domain and path in
        order to only delete a subset of all the cookies.
        N)rX   r  r  )rp   r  r  argsr7   r7   r8   r    s   

zCookies.clearc                 C  s$   t |}|jD ]}| j| qd S rl   )r*   r  r  )rp   r   r  r7   r7   r8   r     s   
zCookies.updatec                 C  s   |  ||S rl   )r  rp   r  rB   r7   r7   r8   r     r   zCookies.__setitem__c                 C  s   |  |}|d u rt||S rl   )r   r   r  r7   r7   r8   r     s   
zCookies.__getitem__c                 C  s
   |  |S rl   )r  )rp   r  r7   r7   r8   r     r   zCookies.__delitem__r   c                 C  r   rl   )r   r  r   r7   r7   r8   r     r   zCookies.__len__r~  c                 C  r|   )Nc                 s  s    | ]}|j V  qd S rl   )r  r  r7   r7   r8   	<genexpr>  s    z#Cookies.__iter__.<locals>.<genexpr>r  r   r7   r7   r8   r     s   zCookies.__iter__r2   c                 C  s   | j D ]} dS dS )NTFr  )rp   r   r7   r7   r8   __bool__  s   
zCookies.__bool__c                 C  s"   d dd | jD }d| dS )Nr   c              	   S  s*   g | ]}d |j  d|j d|j dqS )z<Cookie rS   z for z />)r  rB   r  r  r7   r7   r8   r     s    z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[rl  )r   r  )rp   cookies_reprr7   r7   r8   r     s   zCookies.__repr__c                      s0   e Zd ZdZd fddZd fddZ  ZS )zCookies._CookieCompatRequest
        Wraps a `Request` instance up in a compatibility interface suitable
        for use with `CookieJar` operations.
        r(  r,   r1   rk   c                   s*   t  jt|jt|j|jd || _d S )N)rR   ri   r   )superrs   r0   rR   r   ri   r   r(  )rp   r(  r   r7   r8   rs     s   
z%Cookies._CookieCompatRequest.__init__r:   r0   rB   c                   s   t  || || jj|< d S rl   )r  add_unredirected_headerr(  ri   )rp   r:   rB   r  r7   r8   r    s   z4Cookies._CookieCompatRequest.add_unredirected_headerr(  r,   r1   rk   r   )r   r   r   r   rs   r  __classcell__r7   r7   r  r8   r    s    r  c                   @  s$   e Zd ZdZdddZdd	d
ZdS )zCookies._CookieCompatResponser  r]  r-   r1   rk   c                 C  rx   rl   )r]  )rp   r]  r7   r7   r8   rs     r   z&Cookies._CookieCompatResponse.__init__email.message.Messagec                 C  s,   t j }| jj D ]\}}|||< q|S rl   )rI   rJ   rK   r]  ri   r   )rp   infor:   rB   r7   r7   r8   r    s   

z"Cookies._CookieCompatResponse.infoNr]  r-   r1   rk   )r1   r  )r   r   r   r   rs   r  r7   r7   r7   r8   r    s    
r  rl   )r   r   r1   rk   r  r  )rQ   r  )
r  r0   rB   r0   r  r0   r  r0   r1   rk   )NNN)
r  r0   r   r<   r  r<   r  r<   r1   r<   r   )r  r0   r  r<   r  r<   r1   rk   )r  r<   r  r<   r1   rk   )r  r0   rB   r0   r1   rk   )r  r0   r1   r0   )r  r0   r1   rk   r   r  r  r   )r   r   r   r   rs   rf  r   r  r   r  r  r   r   r   r   r   r   r  r   urllibr(  r,   r  r  r7   r7   r7   r8   r*   7  s0    

	







r*   )r/   r0   r1   r2   rl   )r:   r;   r/   r<   r1   r=   )rB   r;   r/   r<   r1   r=   )rF   r0   r1   r<   )rB   r0   r1   rO   )r`   ra   r1   rb   )J
__future__r   r3   datetimeemail.messagerI   r   rc  rU   r  urllib.requestr  collections.abcr   http.cookiejarr   r   r  r   r   r	   r
   	_decodersr   r   r   r   r   r   r   r   _exceptionsr   r   r   r   r   r   r   
_multipartr   _status_codesr   _typesr   r   r   r   r    r!   r"   r#   r$   r%   r&   _urlsr'   _utilsr(   r)   __all__re   r9   rA   rE   rN   r_   rh   MutableMappingr0   r+   r,   r-   r*   r7   r7   r7   r8   <module>   sH    ($
	4



%	 t     8