U
    nËjœ®  ã                   @  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; ddddgZ<ddhZ=dddœdd„Z>d2ddddœdd„Z?d3ddddœd d!„Z@ddd"œd#d$„ZAdd%d&œd'd(„ZBd)d*d+œd,d-„ZCG d.d„ de	jDeEeEf ƒZFG d/d„ dƒZGG d0d„ dƒZHG d1d„ de	jDeEeEf ƒZIdS )4é    )Ú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ÚstrÚbool)ÚencodingÚreturnc                 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©r1   © r7   ú1/tmp/pip-unpacked-wheel-7ig9zwdf/httpx/_models.pyÚ_is_known_encoding8   s
    r9   zstr | bytesú
str | NoneÚbytes)Úkeyr1   r2   c                 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<   r1   r7   r7   r8   Ú_normalize_header_keyC   s    r@   )Úvaluer1   r2   c                 C  s8   t | tƒr| S t | tƒs*tdt| ƒ› �ƒ‚|  |p4d¡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;   r/   Ú	TypeErrorÚtyper?   )rA   r1   r7   r7   r8   Ú_normalize_header_valueJ   s
    

rD   )Úcontent_typer2   c                 C  s   t j ¡ }| |d< |jd d�S )Núcontent-type)Úfailobj)ÚemailÚmessageÚMessageÚget_content_charset)rE   Úmsgr7   r7   r8   Ú_parse_content_type_charsetU   s    
rM   zlist[dict[str, str]]©rA   r2   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  q¾Y 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   Ú Úurlz<> '"ú=)ÚstripÚreÚsplitÚ
ValueErrorÚappend)	rA   ÚlinksÚreplace_charsÚvalrQ   ÚparamsÚlinkÚparamr<   r7   r7   r8   Ú_parse_header_links]   s&    
r^   z4typing.Iterable[tuple[typing.AnyStr, typing.AnyStr]]z4typing.Iterator[tuple[typing.AnyStr, typing.AnyStr]])Úitemsr2   c                 c  s8   | D ].\}}t | ¡ ƒtkr(td|d�}||fV  qd S )Nz[secure])Zmatch_type_of)r)   ÚlowerÚSENSITIVE_HEADERSr(   )r_   ÚkÚvr7   r7   r8   Ú_obfuscate_sensitive_headers‚   s    rd   c                   @  sZ  e Zd ZdZdBddddœdd„Zed	d
œdd„ƒZejd	ddœdd„ƒZe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dCd	dddœd d!„ZdDd	d#d$d%œd&d'„ZdEddd(œd)d*„Zd d
œd+d,„Zd	d	d-œd.d/„Zd	d	dd0œd1d2„Zd	dd-œd3d4„Zdd#d-œd5d6„Zd7d
œd8d9„Zd:d
œd;d<„Zdd#d=œd>d?„Zd	d
œd@dA„ZdS )Fr+   z9
    HTTP headers, as a case-insensitive multi-dict.
    NúHeaderTypes | Noner:   ÚNone)Úheadersr1   r2   c                 C  s°   g | _ t|tƒrt|j ƒ| _ nˆt|tƒrf| ¡ D ]2\}}t||ƒ}t||ƒ}| j  || 	¡ |f¡ q0n@|d k	r¦|D ]2\}}t||ƒ}t||ƒ}| j  || 	¡ |f¡ qr|| _
d S ©N)Ú_listr>   r+   Úlistr   r_   r@   rD   rW   r`   Ú	_encoding)Úselfrg   r1   rb   rc   Z	bytes_keyZbytes_valuer7   r7   r8   Ú__init__�   s    





zHeaders.__init__r/   ©r2   c              
   C  sn   | j dkrhdD ]R}| jD ]<\}}z| |¡ | |¡ W q tk
rR   Y  qY qX q|| _  qhq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)rk   ÚrawÚdecodeÚUnicodeDecodeError)rl   r1   r<   rA   r7   r7   r8   r1   ¦   s    

zHeaders.encodingrN   c                 C  s
   || _ d S rh   )rk   ©rl   rA   r7   r7   r8   r1   ¿   s    zlist[tuple[bytes, bytes]]c                 C  s   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   )Ú.0Zraw_keyÚ_rA   r7   r7   r8   Ú
<listcomp>È   s     zHeaders.raw.<locals>.<listcomp>©ri   ©rl   r7   r7   r8   rp   Ã   s    zHeaders.rawztyping.KeysView[str]c                   s   ‡ fdd„ˆ j D ƒ ¡ S )Nc                   s    i | ]\}}}|  ˆ j¡d “qS rh   ©rq   r1   ©rt   ru   r<   rA   rx   r7   r8   Ú
<dictcomp>Ë   s     
 z Headers.keys.<locals>.<dictcomp>)ri   Úkeysrx   r7   rx   r8   r|   Ê   s    zHeaders.keysztyping.ValuesView[str]c                 C  s^   i }| j D ]J\}}}| | j¡}| | j¡}||krL||  d|› �7  < q
|||< q
| ¡ S )Nú, )ri   rq   r1   Úvalues©rl   Zvalues_dictru   r<   rA   Zstr_keyZ	str_valuer7   r7   r8   r~   Í   s    
zHeaders.valuesztyping.ItemsView[str, str]c                 C  s^   i }| j D ]J\}}}| | j¡}| | j¡}||krL||  d|› �7  < q
|||< q
| ¡ S )z›
        Return `(key, value)` items of headers. Concatenate headers
        into a single comma separated value when a key occurs multiple times.
        r}   )ri   rq   r1   r_   r   r7   r7   r8   r_   Ø   s    
zHeaders.itemsz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   ry   rz   rx   r7   r8   rv   í   s   ÿz'Headers.multi_items.<locals>.<listcomp>rw   rx   r7   rx   r8   Úmulti_itemsç   s    
þzHeaders.multi_itemsú
typing.Any)r<   Údefaultr2   c                 C  s(   z
| | W S  t k
r"   | Y S X dS )zˆ
        Return a header value. If multiple occurrences of the header occur
        then concatenate them together with commas.
        N)ÚKeyError)rl   r<   r‚   r7   r7   r8   Úgetò   s    
zHeaders.getFr0   z	list[str])r<   Úsplit_commasr2   c                   sZ   |  ¡  ˆj¡‰ ‡ ‡fdd„ˆjD ƒ}|s.|S g }|D ]}| dd„ | d¡D ƒ¡ q6|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   )r`   rq   r1   )rt   ru   Úitem_keyZ
item_value©Zget_header_keyrl   r7   r8   rv     s   þz$Headers.get_list.<locals>.<listcomp>c                 S  s   g | ]}|  ¡ ‘qS r7   )rS   )rt   Úitemr7   r7   r8   rv     s     ú,)r`   r?   r1   ri   ÚextendrU   )rl   r<   r…   r~   Zsplit_valuesrA   r7   r‡   r8   Úget_listü   s    þzHeaders.get_list)rg   r2   c                 C  s:   t |ƒ}| ¡ D ]}|| kr|  |¡ q| j |j¡ d S rh   )r+   r|   Úpopri   rŠ   )rl   rg   r<   r7   r7   r8   Úupdate  s
    zHeaders.updatec                 C  s   t | | jd�S )Nr6   )r+   r1   rx   r7   r7   r8   Úcopy  s    zHeaders.copy)r<   r2   c                   s@   |  ¡  ˆj¡‰ ‡ ‡fdd„ˆjD ƒ}|r4d |¡S t|ƒ‚dS )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   ry   )rt   ru   Ú
header_keyÚheader_value©Znormalized_keyrl   r7   r8   rv   %  s   þz'Headers.__getitem__.<locals>.<listcomp>r}   N)r`   r?   r1   ri   Újoinrƒ   )rl   r<   r_   r7   r‘   r8   Ú__getitem__  s    þ
zHeaders.__getitem__©r<   rA   r2   c                   s’   |  | jpd¡}|  | jpd¡}| ¡ ‰ ‡ fdd„t| jƒD ƒ}t|dd… ƒD ]}| j|= qP|r||d }|ˆ |f| j|< n| j |ˆ |f¡ dS )zs
        Set the header `key` to `value`, removing any duplicate entries.
        Retains insertion order.
        ro   c                   s"   g | ]\}\}}}|ˆ kr|‘qS r7   r7   ©rt   Úidxru   r†   ©Z
lookup_keyr7   r8   rv   9  s   þz'Headers.__setitem__.<locals>.<listcomp>r   Nr   )r?   rk   r`   Ú	enumerateri   ÚreversedrW   )rl   r<   rA   Zset_keyÚ	set_valueZfound_indexesr–   r7   r—   r8   Ú__setitem__0  s    
þ
zHeaders.__setitem__c                   sN   |  ¡  | j¡‰ ‡ fdd„t| jƒD ƒ}|s4t|ƒ‚t|ƒD ]}| j|= q<dS )z*
        Remove the header `key`.
        c                   s&   g | ]\}\}}}|  ¡ ˆ kr|‘qS r7   )r`   r•   ©Zdel_keyr7   r8   rv   N  s   þz'Headers.__delitem__.<locals>.<listcomp>N)r`   r?   r1   r˜   ri   rƒ   r™   )rl   r<   Zpop_indexesr–   r7   rœ   r8   Ú__delitem__H  s    
þzHeaders.__delitem__c                 C  s$   |  ¡  | j¡}|dd„ | jD ƒkS )Nc                 S  s   g | ]\}}}|‘qS r7   r7   )rt   ru   r<   r7   r7   r8   rv   \  s     z(Headers.__contains__.<locals>.<listcomp>)r`   r?   r1   ri   )rl   r<   r�   r7   r7   r8   Ú__contains__Z  s    zHeaders.__contains__ztyping.Iterator[typing.Any]c                 C  s   t |  ¡ ƒS rh   )Úiterr|   rx   r7   r7   r8   Ú__iter__^  s    zHeaders.__iter__Úintc                 C  s
   t | jƒS rh   )Úlenri   rx   r7   r7   r8   Ú__len__a  s    zHeaders.__len__)Úotherr2   c                 C  sT   zt |ƒ}W n tk
r"   Y dS X dd„ | jD ƒ}dd„ |jD ƒ}t|ƒt|ƒkS )NFc                 S  s   g | ]\}}}||f‘qS r7   r7   rz   r7   r7   r8   rv   j  s     z"Headers.__eq__.<locals>.<listcomp>c                 S  s   g | ]\}}}||f‘qS r7   r7   rz   r7   r7   r8   rv   k  s     )r+   rV   ri   Úsorted)rl   r¤   Zother_headersZ	self_listZ
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}|rb|› d|›|› d�S |› d|›|› d�S )NrP   r=   z, encoding=ú(ú))Ú	__class__Ú__name__r1   rj   rd   r€   Údictr¢   )rl   Ú
class_nameZencoding_strZas_listZas_dictZno_duplicate_keysr7   r7   r8   Ú__repr__n  s    
zHeaders.__repr__)NN)N)F)N)rª   Ú
__module__Ú__qualname__Ú__doc__rm   Úpropertyr1   Úsetterrp   r|   r~   r_   r€   r„   r‹   r�   rŽ   r“   r›   r�   rž   r    r£   r¦   r­   r7   r7   r7   r8   r+   ‹   s4     ý

c                   @  s°   e Zd Zddddddddddœ	ddddddd	d
dd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„Zddœdd„Z	d dœd!d"„Z
d dd#œd$d%„ZdS )&r,   N)	r[   rg   ÚcookiesÚcontentÚdataÚfilesÚjsonÚstreamÚ
extensionsr/   z	URL | strzQueryParamTypes | Nonere   úCookieTypes | NonezRequestContent | NonezRequestData | NonezRequestFiles | Noneztyping.Any | Noneú'SyncByteStream | AsyncByteStream | NonezRequestExtensions | Nonerf   )ÚmethodrQ   r[   rg   r³   r´   rµ   r¶   r·   r¸   r¹   r2   c       	      	   C  sÊ   |  ¡ | _|d krt|ƒn
t||d�| _t|ƒ| _|d kr>i nt|ƒ| _|rZt|ƒ 	| ¡ |
d krÀ| j 
d¡}t||||	t|rŒ| | jj¡nd d�d�\}}
|  |¡ |
| _t|
tƒrÆ|  ¡  n|
| _d S )N)r[   rF   )rE   )r´   rµ   r¶   r·   Úboundary)Úupperr¼   r'   rQ   r+   rg   r«   r¹   r*   Úset_cookie_headerr„   r	   r   r?   r1   Ú_preparer¸   r>   r   Úread)rl   r¼   rQ   r[   rg   r³   r´   rµ   r¶   r·   r¸   r¹   rE   r7   r7   r8   rm     s0    

ÿýû



zRequest.__init__údict[str, str]©Údefault_headersr2   c                 C  s¦   |  ¡ D ].\}}| ¡ dkr(d| jkr(q| j ||¡ qg }d| jk}d| jkpXd| jk}|sx| jjrx| d| jjf¡ |s�| jdkr�| d¡ t	|| jj
 ƒ| _d S )Nútransfer-encodingzContent-LengthÚHostzTransfer-Encodings   Host)ÚPOSTÚPUTÚPATCH)s   Content-Lengthó   0)r_   r`   rg   Ú
setdefaultrQ   ÚhostrW   Únetlocr¼   r+   rp   )rl   rÄ   r<   rA   Zauto_headersZhas_hostZhas_content_lengthr7   r7   r8   rÀ   ¹  s    
ÿ
zRequest._preparer;   rn   c                 C  s   t | dƒstƒ ‚| jS ©NÚ_content)Úhasattrr   rÏ   rx   r7   r7   r8   r´   Î  s    
zRequest.contentc                 C  sH   t | dƒsBt| jtjƒst‚d | j¡| _t| jtƒsBt| jƒ| _| jS )ú6
        Read and return the request content.
        rÏ   ó    )	rÐ   r>   r¸   ÚtypingÚIterableÚAssertionErrorr’   rÏ   r   rx   r7   r7   r8   rÁ   Ô  s    
zRequest.readc                 Ã  sX   t | dƒsRt| jtjƒst‚d dd„ | j2 ƒI dH ¡| _t| jtƒsRt| jƒ| _| jS )rÑ   rÏ   rÒ   c                 Ó  s   g | z3 d H W }|‘q6 S rh   r7   ©rt   Úpartr7   r7   r8   rv   è  s     z!Request.aread.<locals>.<listcomp>N)	rÐ   r>   r¸   rÓ   ÚAsyncIterablerÕ   r’   rÏ   r   rx   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ª   r/   rQ   r¼   )rl   r¬   rQ   r7   r7   r8   r­   ð  s    
zRequest.__repr__údict[str, typing.Any]c                 C  s   dd„ | j  ¡ D ƒS )Nc                 S  s   i | ]\}}|d kr||“qS ))r¹   r¸   r7   ©rt   ÚnamerA   r7   r7   r8   r{   ö  s   þ z(Request.__getstate__.<locals>.<dictcomp>©Ú__dict__r_   rx   r7   r7   r8   Ú__getstate__õ  s    þzRequest.__getstate__©Ústater2   c                 C  s0   |  ¡ D ]\}}t| ||ƒ qi | _tƒ | _d S rh   )r_   Úsetattrr¹   r   r¸   ©rl   râ   rÝ   rA   r7   r7   r8   Ú__setstate__ü  s    zRequest.__setstate__)rª   r®   r¯   rm   rÀ   r±   r´   rÁ   rÙ   r­   rà   rå   r7   r7   r7   r8   r,   ~  s$   ó(:c                   @  s  e Zd Zdddddddddddœ
ddddddd	d
dddddœdd„Zdddœdd„Zeddœdd„ƒZejdddœdd„ƒZeddœdd„ƒZejdddœd d„ƒZed!dœd"d#„ƒZ	ed!dœd$d%„ƒZ
ed&dœd'd(„ƒZed)dœd*d+„ƒZed!dœd,d-„ƒZeddœd.d/„ƒZejd!ddœd0d/„ƒZeddœd1d2„ƒZd3dœd4d5„Zed6dœd7d8„ƒZed6dœd9d:„ƒZed6dœd;d<„ƒZed6dœd=d>„ƒZed6dœd?d@„ƒZed6dœdAdB„ƒZed6dœdCdD„ƒZd dœdEdF„ZdddGœdHdI„ZedJdœdKdL„ƒZedMdœdNdO„ƒZeddœdPdQ„ƒZd!dœdRdS„ZdTdœdUdV„ZdTddWœdXdY„Zd)dœdZd[„Z dxd\d]d^œd_d`„Z!dyd\dad^œdbdc„Z"dadœddde„Z#dzd\d]d^œdfdg„Z$ddœdhdi„Z%d)dœdjdk„Z&d{d\dld^œdmdn„Z'd|d\dod^œdpdq„Z(dodœdrds„Z)d}d\dld^œdtdu„Z*ddœdvdw„Z+dS )~r-   Nro   )
rg   r´   ÚtextÚhtmlr·   r¸   Úrequestr¹   ÚhistoryÚdefault_encodingr¡   re   zResponseContent | Noner:   r�   r»   zRequest | NonezResponseExtensions | Nonezlist[Response] | Nonez#str | typing.Callable[[bytes], str]rf   )Ústatus_coderg   r´   ræ   rç   r·   r¸   rè   r¹   ré   rê   r2   c       
         C  s¨   || _ t|ƒ| _|| _d | _|	d kr(i nt|	ƒ| _|
d kr>g nt|
ƒ| _d| _	d| _
|| _|d kr˜t||||ƒ\}}|  |¡ || _t|tƒrž|  ¡  n|| _d| _d S )NFr   )rë   r+   rg   Ú_requestZnext_requestr«   r¹   rj   ré   Ú	is_closedÚis_stream_consumedrê   r
   rÀ   r¸   r>   r   rÁ   Ú_num_bytes_downloaded)rl   rë   rg   r´   ræ   rç   r·   r¸   rè   r¹   ré   rê   r7   r7   r8   rm     s"    



zResponse.__init__rÂ   rÃ   c                 C  s<   |  ¡ D ].\}}| ¡ dkr(d| jkr(q| j ||¡ qd S )NrÅ   zcontent-length)r_   r`   rg   rË   )rl   rÄ   r<   rA   r7   r7   r8   rÀ   ;  s    zResponse._preparezdatetime.timedeltarn   c                 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Ð   ÚRuntimeErrorrð   rx   r7   r7   r8   ÚelapsedB  s
    
ÿzResponse.elapsed)rò   r2   c                 C  s
   || _ d S rh   )rð   )rl   rò   r7   r7   r8   rò   O  s    r,   c                 C  s   | j dk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.)rì   rñ   rx   r7   r7   r8   rè   S  s
    
ÿzResponse.requestrN   c                 C  s
   || _ d S rh   )rì   rs   r7   r7   r8   rè   ^  s    r/   c                 C  s8   z| j d }W n tk
r$   Y dS X |jddd�S d S )NÚhttp_versionzHTTP/1.1r=   Úignore©Úerrors)r¹   rƒ   rq   )rl   ró   r7   r7   r8   ró   b  s
    zResponse.http_versionc                 C  sB   z| j d }W n  tk
r.   t | j¡ Y S X |jddd�S d S )NÚreason_phraser=   rô   rõ   )r¹   rƒ   r   Zget_reason_phraserë   rq   )rl   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è   rQ   rx   r7   r7   r8   rQ   t  s    zResponse.urlr;   c                 C  s   t | dƒstƒ ‚| jS rÎ   )rÐ   r   rÏ   rx   r7   r7   r8   r´   {  s    
zResponse.contentc                 C  sN   t | dƒsH| j}|sd| _n,t| jp&dd�}d | | j¡| ¡ g¡| _| jS )NÚ_textrP   ro   r6   )rÐ   r´   rø   r   r1   r’   rq   Úflush)rl   r´   Údecoderr7   r7   r8   ræ   �  s    
zResponse.textc                 C  sZ   t | dƒsT| j}|dks t|ƒsJt| jtƒr4| j}nt | dƒrJ|  | j¡}|pPd| _| 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.
        rk   NrÏ   ro   )rÐ   Úcharset_encodingr9   r>   rê   r/   rÏ   rk   )rl   r1   r7   r7   r8   r1   Œ  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Ð   rV   rk   rs   r7   r7   r8   r1   ¢  s
    
ÿc                 C  s    | j  d¡}|dkrdS t|ƒS )zO
        Return the encoding, as specified by the Content-Type header.
        zContent-TypeN)rg   r„   rM   )rl   rE   r7   r7   r8   rû   °  s    zResponse.charset_encodingr   c              	   C  s¦   t | dƒs g }| jjddd�}|D ]B}| ¡  ¡ }zt| }| |ƒ ¡ W q" tk
rb   Y q"Y q"X q"t|ƒdkr~|d | _	n"t|ƒdkr˜t
|d�| _	n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Ð   rg   r‹   rS   r`   r   rW   rƒ   r¢   rü   r   r   )rl   Zdecodersr~   rA   Zdecoder_clsr7   r7   r8   Ú_get_content_decoder»  s     
zResponse._get_content_decoderr0   c                 C  s   t  | j¡S )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   Úis_informationalrë   rx   r7   r7   r8   rÿ   Ô  s    zResponse.is_informationalc                 C  s   t  | j¡S )zU
        A property which is `True` for 2xx status codes, `False` otherwise.
        )r   Ú
is_successrë   rx   r7   r7   r8   r   Û  s    zResponse.is_successc                 C  s   t  | j¡S )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ë   rx   r7   r7   r8   r  â  s    
zResponse.is_redirectc                 C  s   t  | j¡S )zU
        A property which is `True` for 4xx status codes, `False` otherwise.
        )r   Úis_client_errorrë   rx   r7   r7   r8   r  î  s    zResponse.is_client_errorc                 C  s   t  | j¡S )zU
        A property which is `True` for 5xx status codes, `False` otherwise.
        )r   Úis_server_errorrë   rx   r7   r7   r8   r  õ  s    zResponse.is_server_errorc                 C  s   t  | j¡S )z]
        A property which is `True` for 4xx and 5xx status codes, `False` otherwise.
        )r   Úis_errorrë   rx   r7   r7   r8   r  ü  s    zResponse.is_errorc                 C  s(   | j tjtjtjtjtjfko&d| jkS )zs
        Returns True for 3xx responses with a properly formed URL redirection,
        `False` otherwise.
        ZLocation)rë   r   ÚMOVED_PERMANENTLYÚFOUNDÚ	SEE_OTHERÚTEMPORARY_REDIRECTÚPERMANENT_REDIRECTrg   rx   r7   r7   r8   Úhas_redirect_location  s    öÿòzResponse.has_redirect_locationc                 C  st   | j }|dkrtdƒ‚| jr | S | jr,d}nd}| jd }dddd	d
œ}| |d¡}|j| |d�}t||| d�‚dS )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)rì   rñ   r   r
  rë   r„   Úformatr   )rl   rè   rI   Zstatus_classZerror_typesr  r7   r7   r8   Úraise_for_status  s*    ÿÿÿ
üzResponse.raise_for_status)Úkwargsr2   c                 K  s   t j| jf|ŽS rh   )ÚjsonlibÚloadsr´   )rl   r  r7   r7   r8   r·   ?  s    zResponse.jsonr*   c                 C  s$   t | dƒstƒ | _| j | ¡ | jS )NÚ_cookies)rÐ   r*   r  Úextract_cookiesrx   r7   r7   r8   r³   B  s    
zResponse.cookiesz dict[str | None, dict[str, str]]c                 C  s*   | j  d¡}|dk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 )ÚrelrQ   )r„   )rt   r\   r7   r7   r8   r{   R  s   ÿ z"Response.links.<locals>.<dictcomp>)rg   r„   r^   )rl   Úheaderr7   r7   r8   rX   I  s    þzResponse.linksc                 C  s   | j S rh   )rï   rx   r7   r7   r8   Únum_bytes_downloadedW  s    zResponse.num_bytes_downloadedc                 C  s   d| j › d| j› d�S )Nz<Response [ú ú]>)rë   r÷   rx   r7   r7   r8   r­   [  s    zResponse.__repr__rÛ   c                 C  s   dd„ | j  ¡ D ƒS )Nc                 S  s   i | ]\}}|d kr||“qS ))r¹   r¸   rí   rü   r7   rÜ   r7   r7   r8   r{   _  s   þ z)Response.__getstate__.<locals>.<dictcomp>rÞ   rx   r7   r7   r8   rà   ^  s    þzResponse.__getstate__rá   c                 C  s6   |  ¡ D ]\}}t| ||ƒ qd| _i | _tƒ | _d S )NT)r_   rã   rí   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Ï   rx   r7   r7   r8   rÁ   l  s    
zResponse.readz
int | Noneztyping.Iterator[bytes])Ú
chunk_sizer2   c              	   c  sâ   t | dƒrT|dkrt| jƒn|}tdt| jƒt|dƒƒD ]}| j||| … V  q8nŠ|  ¡ }t|d�}t| jd��f |  	¡ D ]$}| 
|¡}| 
|¡D ]
}|V  q”q|| ¡ }| 
|¡D ]
}|V  q´| ¡ D ]
}|V  qÈW 5 Q R X 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   ©r  ©rè   )rÐ   r¢   rÏ   ÚrangeÚmaxrþ   r   r   rì   Úiter_rawrq   rù   ©rl   r  Úirú   ÚchunkerZ	raw_bytesÚdecodedÚchunkr7   r7   r8   r  t  s     



zResponse.iter_bytesútyping.Iterator[str]c              	   c  s–   t | jp
dd�}t|d�}t| jd��f |  ¡ D ]$}| |¡}| |¡D ]
}|V  qHq0| ¡ }| |¡D ]
}|V  qh| ¡ D ]
}|V  q|W 5 Q R X 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.
        ro   r6   r!  r"  N)r   r1   r   r   rì   r  rq   rù   ©rl   r  rú   r(  Zbyte_contentZtext_contentr*  r7   r7   r8   Ú	iter_text‹  s    


zResponse.iter_textc              	   c  sZ   t ƒ }t| jd��> |  ¡ D ]}| |¡D ]
}|V  q*q| ¡ D ]
}|V  q@W 5 Q R X d S ©Nr"  )r   r   rì   r.  rq   rù   ©rl   rú   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  qvqVW 5 Q R X | ¡ D ]
}|V  q–|  ¡  dS )ú@
        A byte-iterator over the raw response content.
        z5Attempted to call a sync iterator on an async stream.Tr   r!  r"  N)rî   r   rí   r   r>   r¸   r&   rñ   rï   r   r   rì   r¢   rq   rù   Úclose©rl   r  r(  Zraw_stream_bytesr*  r7   r7   r8   r%  §  s"    

zResponse.iter_rawc              	   C  sF   t | jtƒstdƒ‚| jsBd| _t| jd�� | j ¡  W 5 Q R X 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.Tr"  N)r>   r¸   r&   rñ   rí   r   rì   r4  rx   r7   r7   r8   r4  Á  s    zResponse.closec                 Ã  s0   t | dƒs*d dd„ |  ¡ 2 ƒI dH ¡| _| jS )r  rÏ   rÒ   c                 Ó  s   g | z3 d H W }|‘q6 S rh   r7   rÖ   r7   r7   r8   rv   Ó  s     z"Response.aread.<locals>.<listcomp>N)rÐ   r’   Úaiter_bytesrÏ   rx   r7   r7   r8   rÙ   Î  s    
 zResponse.areadztyping.AsyncIterator[bytes]c              	   C sì   t | dƒrT|dkrt| jƒn|}tdt| jƒt|dƒƒD ]}| j||| … V  q8n”|  ¡ }t|d�}t| jd��p |  	¡ 2 z,3 dH W }| 
|¡}| 
|¡D ]
}|V  qœq|6 | ¡ }| 
|¡D ]
}|V  q¾| ¡ D ]
}|V  qÒW 5 Q R X dS r   )rÐ   r¢   rÏ   r#  r$  rþ   r   r   rì   Ú	aiter_rawrq   rù   r&  r7   r7   r8   r7  Ö  s     


zResponse.aiter_bytesztyping.AsyncIterator[str]c              	   C s    t | jp
dd�}t|d�}t| jd��p |  ¡ 2 z,3 dH W }| |¡}| |¡D ]
}|V  qPq06 | ¡ }| |¡D ]
}|V  qr| ¡ D ]
}|V  q†W 5 Q R X dS r,  )r   r1   r   r   rì   r7  rq   rù   r-  r7   r7   r8   Ú
aiter_textï  s    

zResponse.aiter_textc              	   C sd   t ƒ }t| jd��H |  ¡ 2 z"3 d H W }| |¡D ]
}|V  q2q6 | ¡ D ]
}|V  qJW 5 Q R X d S r/  )r   r   rì   r9  rq   rù   r0  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��D | j2 z43 dH W }|  jt|ƒ7  _| |¡D ]
}|V  q~qV6 W 5 Q R X | ¡ D ]
}|V  q |  ¡ I dH  dS )r3  z6Attempted to call an async iterator on an sync stream.Tr   r!  r"  N)rî   r   rí   r   r>   r¸   r   rñ   rï   r   r   rì   r¢   rq   rù   Úacloser5  r7   r7   r8   r8    s"    
zResponse.aiter_rawc              	   Ã  sL   t | jtƒstdƒ‚| jsHd| _t| jd�� | j ¡ I dH  W 5 Q R X dS )r6  z3Attempted to call an async close on an sync stream.Tr"  N)r>   r¸   r   rñ   rí   r   rì   r;  rx   r7   r7   r8   r;  )  s    zResponse.aclose)N)N)N)N)N)N),rª   r®   r¯   rm   rÀ   r±   rò   r²   rè   ró   r÷   rQ   r´   ræ   r1   rû   rþ   rÿ   r   r  r  r  r  r
  r  r·   r³   rX   r  r­   rà   rå   rÁ   r  r.  r2  r%  r4  rÙ   r7  r9  r:  r8  r;  r7   r7   r7   r8   r-     sš   ó(7


%		 ÿ ÿ
 ÿc                   @  s:  e Zd ZdZ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dœdd„Zd<ddddddœdd„Zd=dddddœdd„Z	d>ddddœdd„Z
d?dddœd d!„Zdddd"œd#d$„Zddd%œd&d'„Zddd%œd(d)„Zd*d+œd,d-„Zd.d+œd/d0„Zd1d+œd2d3„Zdd+œd4d5„ZG d6d7„ d7ejjƒZG d8d9„ d9ƒZdS )@r*   z-
    HTTP Cookies, as a mutable mapping.
    Nrº   rf   )r³   r2   c                 C  s¨   |d kst |tƒrDtƒ | _t |tƒr¤| ¡ D ]\}}|  ||¡ q,n`t |tƒrrtƒ | _|D ]\}}|  ||¡ qZn2t |tƒržtƒ | _|jD ]}| j |¡ qŠn|| _d S rh   )	r>   r«   r   Újarr_   Úsetrj   r*   Ú
set_cookie)rl   r³   r<   rA   Úcookier7   r7   r8   rm   <  s    



zCookies.__init__r-   ©r  r2   c                 C  s(   |   |¡}|  |j¡}| j ||¡ dS )zO
        Loads any cookies based on the response `Set-Cookie` headers.
        N)Ú_CookieCompatResponseÚ_CookieCompatRequestrè   r<  r  )rl   r  Zurllib_responseÚurllib_requestr7   r7   r8   r  M  s    
zCookies.extract_cookiesr,   ©rè   r2   c                 C  s   |   |¡}| j |¡ dS )zM
        Sets an appropriate 'Cookie:' HTTP header on the `Request`.
        N)rB  r<  Úadd_cookie_header)rl   rè   rC  r7   r7   r8   r¿   V  s    
zCookies.set_cookie_headerrP   ú/r/   )rÝ   rA   ÚdomainÚpathr2   c                 C  sT   d||dd|t |ƒ| d¡|t |ƒdddddddiddœ}tf |Ž}| j |¡ dS )zU
        Set a cookie value by name. May optionally include domain and path.
        r   NFÚ.TÚHttpOnly)ÚversionrÝ   rA   ÚportÚport_specifiedrG  Údomain_specifiedÚdomain_initial_dotrH  Úpath_specifiedÚsecureÚexpiresÚdiscardÚcommentÚcomment_urlÚrestÚrfc2109)r0   Ú
startswithr   r<  r>  )rl   rÝ   rA   rG  rH  r  r?  r7   r7   r8   r=  ]  s(    ï
zCookies.setr:   )rÝ   r‚   rG  rH  r2   c                 C  sn   d}| j D ]R}|j|kr
|dks*|j|kr
|dks<|j|kr
|dk	rVd|› �}t|ƒ‚|j}q
|dkrj|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Ý   rG  rH  r   rA   )rl   rÝ   r‚   rG  rH  rA   r?  rI   r7   r7   r8   r„   w  s    


zCookies.get)rÝ   rG  rH  r2   c                   s\   ˆ dk	r ˆdk	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 | ]6}|j ˆkrˆ d ks$|jˆ krˆd ks6|jˆkr|‘qS rh   )rÝ   rG  rH  ©rt   r?  ©rG  rÝ   rH  r7   r8   rv   �  s   
 
 
üz"Cookies.delete.<locals>.<listcomp>)r<  ÚclearrG  rH  rÝ   )rl   rÝ   rG  rH  Úremover?  r7   rZ  r8   Údelete�  s    
þzCookies.delete)rG  rH  r2   c                 C  sD   g }|dk	r|  |¡ |dk	r4|dk	s*t‚|  |¡ | 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)rW   rÕ   r<  r[  )rl   rG  rH  Úargsr7   r7   r8   r[  ¨  s    

zCookies.clearc                 C  s$   t |ƒ}|jD ]}| j |¡ qd S rh   )r*   r<  r>  )rl   r³   r?  r7   r7   r8   r�   µ  s    
zCookies.update)rÝ   rA   r2   c                 C  s   |   ||¡S rh   )r=  ©rl   rÝ   rA   r7   r7   r8   r›   º  s    zCookies.__setitem__)rÝ   r2   c                 C  s   |   |¡}|d krt|ƒ‚|S rh   )r„   rƒ   r_  r7   r7   r8   r“   ½  s    
zCookies.__getitem__c                 C  s
   |   |¡S rh   )r]  )rl   rÝ   r7   r7   r8   r�   Ã  s    zCookies.__delitem__r¡   rn   c                 C  s
   t | jƒS rh   )r¢   r<  rx   r7   r7   r8   r£   Æ  s    zCookies.__len__r+  c                 C  s   dd„ | j D ƒS )Nc                 s  s   | ]}|j V  qd S rh   )rÝ   rY  r7   r7   r8   Ú	<genexpr>Ê  s     z#Cookies.__iter__.<locals>.<genexpr>©r<  rx   r7   r7   r8   r    É  s    zCookies.__iter__r0   c                 C  s   | j D ]} dS dS )NTFra  )rl   ru   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 rR   z for z />)rÝ   rA   rG  rY  r7   r7   r8   rv   Ó  s   ÿz$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r  )r’   r<  )rl   Zcookies_reprr7   r7   r8   r­   Ñ  s    þÿzCookies.__repr__c                      s>   e Zd ZdZdddœ‡ fdd„Zddd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,   rf   rD  c                   s*   t ƒ jt|jƒt|jƒ|jd� || _d S )N)rQ   rg   r¼   )Úsuperrm   r/   rQ   r«   rg   r¼   rè   )rl   rè   ©r©   r7   r8   rm   á  s    ýz%Cookies._CookieCompatRequest.__init__r/   r”   c                   s   t ƒ  ||¡ || jj|< d S rh   )rd  Úadd_unredirected_headerrè   rg   )rl   r<   rA   re  r7   r8   rf  é  s    z4Cookies._CookieCompatRequest.add_unredirected_header)rª   r®   r¯   r°   rm   rf  Ú__classcell__r7   r7   re  r8   rB  Û  s   rB  c                   @  s.   e Zd ZdZdddœdd„Zddœd	d
„ZdS )zCookies._CookieCompatResponserc  r-   rf   r@  c                 C  s
   || _ d S rh   )r  )rl   r  r7   r7   r8   rm   ó  s    z&Cookies._CookieCompatResponse.__init__zemail.message.Messagern   c                 C  s,   t j ¡ }| jj ¡ D ]\}}|||< q|S rh   )rH   rI   rJ   r  rg   r€   )rl   Úinfor<   rA   r7   r7   r8   rh  ö  s    

z"Cookies._CookieCompatResponse.infoN)rª   r®   r¯   r°   rm   rh  r7   r7   r7   r8   rA  í  s   rA  )N)rP   rF  )NNN)NN)NN)N)rª   r®   r¯   r°   rm   r  r¿   r=  r„   r]  r[  r�   r›   r“   r�   r£   r    rb  r­   Úurllibrè   r,   rB  rA  r7   r7   r7   r8   r*   7  s.   	   û  ü
)N)N)JÚ
__future__r   r3   ÚdatetimeÚemail.messagerH   r·   r  rT   rÓ   Úurllib.requestri  Ú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   Z
_multipartr   Z_status_codesr   Ú_typesr   r   r   r   r    r!   r"   r#   r$   r%   r&   Z_urlsr'   Ú_utilsr(   r)   Ú__all__ra   r9   r@   rD   rM   r^   rd   ÚMutableMappingr/   r+   r,   r-   r*   r7   r7   r7   r8   Ú<module>   sF   (
$	4%	 t     8