o
    Zh?                     @  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
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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= G dd dej>e?e?f Z@G dd dZAG dd dZBG dd dej>e?e?f ZCd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)is_known_encodingnormalize_header_keynormalize_header_valueobfuscate_sensitive_headersparse_content_type_charsetparse_header_linksc                   @  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 )WHeadersz9
    HTTP headers, as a case-insensitive multi-dict.
    NheadersHeaderTypes | Noneencoding
str | NonereturnNonec                   sj   |d u rg | _ n(t|trt|j | _ nt|tr& fdd| D | _ n
 fdd|D | _  | _d S )Nc                   4   g | ]\}}t |d  dt |d dt| fqS F)lowerr1   Tr)   r*   .0kvr1    D/var/www/html/lang_env/lib/python3.10/site-packages/httpx/_models.py
<listcomp>H       z$Headers.__init__.<locals>.<listcomp>c                   r5   r6   r8   r9   r=   r>   r?   r@   Q   rA   )_list
isinstancer.   listr   items	_encoding)selfr/   r1   r>   r=   r?   __init__>   s   




	
	zHeaders.__init__strc              
   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)asciiutf-8z
iso-8859-1)rF   rawdecodeUnicodeDecodeError)rG   r1   keyvaluer>   r>   r?   r1   \   s   

	zHeaders.encodingrP   c                 C  
   || _ d S N)rF   rG   rP   r>   r>   r?   r1   u      
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 r>   r>   )r:   Zraw_key_rP   r>   r>   r?   r@   ~       zHeaders.raw.<locals>.<listcomp>rB   rG   r>   r>   r?   rL   y   s   zHeaders.rawtyping.KeysView[str]c                   s    fdd j D  S )Nc                   s    i | ]\}}}|  jd qS rR   rM   r1   r:   rW   rO   rP   rZ   r>   r?   
<dictcomp>   s     z Headers.keys.<locals>.<dictcomp>)rB   keysrZ   r>   rZ   r?   r_      s   zHeaders.keystyping.ValuesView[str]c                 C  ^   i }| j D ]%\}}}|| j}|| j}||v r&||  d| 7  < q|||< q| S )N, )rB   rM   r1   valuesrG   Zvalues_dictrW   rO   rP   Zstr_keyZ	str_valuer>   r>   r?   rc      s   
zHeaders.valuestyping.ItemsView[str, str]c                 C  ra   )z
        Return `(key, value)` items of headers. Concatenate headers
        into a single comma separated value when a key occurs multiple times.
        rb   )rB   rM   r1   rE   rd   r>   r>   r?   rE      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 r>   r\   r]   rZ   r>   r?   r@      s    z'Headers.multi_items.<locals>.<listcomp>rY   rZ   r>   rZ   r?   multi_items   s   
zHeaders.multi_itemsrO   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)rG   rO   rh   r>   r>   r?   get   s
   
zHeaders.getFsplit_commasbool	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 r>   )r7   rM   r1   )r:   rW   item_keyZ
item_valueZget_header_keyrG   r>   r?   r@      s
    
z$Headers.get_list.<locals>.<listcomp>c                 S  s   g | ]}|  qS r>   )strip)r:   itemr>   r>   r?   r@      s    ,)r7   encoder1   rB   extendsplit)rG   rO   rl   rc   Zsplit_valuesrP   r>   rp   r?   get_list   s   zHeaders.get_listc                 C  s:   t |}| D ]}|| v r| | q| j|j d S rR   )r.   r_   poprB   ru   )rG   r/   rO   r>   r>   r?   update   s   
zHeaders.updatec                 C  s   t | | jdS )Nr=   )r.   r1   rZ   r>   r>   r?   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 r>   r\   )r:   rW   
header_keyheader_valueZnormalized_keyrG   r>   r?   r@      s
    
z'Headers.__getitem__.<locals>.<listcomp>rb   )r7   rt   r1   rB   joinrj   )rG   rO   rE   r>   r}   r?   __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.
        rK   c                   s"   g | ]\}\}}}| kr|qS r>   r>   r:   idxrW   ro   Z
lookup_keyr>   r?   r@      s
    z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )rt   rF   r7   	enumeraterB   reversedappend)rG   rO   rP   Zset_key	set_valueZfound_indexesr   r>   r   r?   __setitem__   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 r>   )r7   r   Zdel_keyr>   r?   r@     s
    z'Headers.__delitem__.<locals>.<listcomp>N)r7   rt   r1   r   rB   rj   r   )rG   rO   Zpop_indexesr   r>   r   r?   __delitem__   s   

zHeaders.__delitem__c                 C  s$   |  | j}|dd | jD v S )Nc                 S  s   g | ]\}}}|qS r>   r>   )r:   rW   rO   r>   r>   r?   r@     s    z(Headers.__contains__.<locals>.<listcomp>)r7   rt   r1   rB   )rG   rO   r{   r>   r>   r?   __contains__  s   zHeaders.__contains__typing.Iterator[typing.Any]c                 C  s   t |  S rR   )iterr_   rZ   r>   r>   r?   __iter__     zHeaders.__iter__intc                 C  
   t | jS rR   )lenrB   rZ   r>   r>   r?   __len__     
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 r>   r>   r]   r>   r>   r?   r@      rX   z"Headers.__eq__.<locals>.<listcomp>c                 S  r   r>   r>   r]   r>   r>   r?   r@   !  rX   )r.   
ValueErrorrB   sorted)rG   r   Zother_headersZ	self_listZ
other_listr>   r>   r?   __eq__  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 )N rJ   z, encoding=())	__class____name__r1   rD   r+   rg   dictr   )rG   
class_nameZencoding_stras_listas_dictZno_duplicate_keysr>   r>   r?   __repr__$  s   
zHeaders.__repr__NN)r/   r0   r1   r2   r3   r4   r3   rI   rP   rI   r3   r4   )r3   rU   )r3   r[   )r3   r`   )r3   re   )r3   rf   rR   )rO   rI   rh   ri   r3   ri   )F)rO   rI   rl   rm   r3   rn   )r/   r0   r3   r4   )r3   r.   )rO   rI   r3   rI   rO   rI   rP   rI   r3   r4   )rO   rI   r3   r4   )rO   ri   r3   rm   )r3   r   r3   r   )r   ri   r3   rm   )r   
__module____qualname____doc__rH   propertyr1   setterrL   r_   rc   rE   rg   rk   rw   ry   rz   r   r   r   r   r   r   r   r   r>   r>   r>   r?   r.   9   s6    













r.   c                
   @  sv   e Zd Zdddddddddd	d1ddZd2dd Zed3d"d#Zd3d$d%Zd3d&d'Zd4d)d*Z	d5d,d-Z
d6d/d0ZdS )7RequestN)	paramsr/   cookiescontentdatafilesjsonstream
extensionsmethodstr | bytesurl	URL | strr   QueryParamTypes | Noner/   r0   r   CookieTypes | Noner   RequestContent | Noner   RequestData | Noner   RequestFiles | Noner   typing.Any | Noner   'SyncByteStream | AsyncByteStream | Noner   RequestExtensions | Noner3   r4   c       	      	   C  s   t |tr|d n| | _t|| _|d ur"| jj|d| _t|| _	|d u r-i n|| _
|r9t||  |
d u ro| j	d}t||||	t|rR|| j	jnd dd\}}
| | |
| _t |
trm|   d S d S |
| _d S )NrJ   )r   zcontent-type)content_type)r   r   r   r   boundary)rC   bytesrM   upperr   r'   r   Zcopy_merge_paramsr.   r/   r   Cookiesset_cookie_headerrk   r	   r   rt   r1   _preparer   r   read)rG   r   r   r   r/   r   r   r   r   r   r   r   r   r>   r>   r?   rH   5  s<   





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)rE   r7   r/   
setdefaultr   hostr   netlocr   r.   rL   )rG   r   rO   rP   Zauto_headersZhas_hostZhas_content_lengthr>   r>   r?   r   u  s   

zRequest._preparer   c                 C     t | dst | jS N_content)hasattrr   r   rZ   r>   r>   r?   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   rC   r   typingIterabler~   r   r   rZ   r>   r>   r?   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 rR   r>   r:   partr>   r>   r?   r@         z!Request.aread.<locals>.<listcomp>N)r   rC   r   r   AsyncIterabler~   r   r   rZ   r>   r>   r?   aread  s   
zRequest.areadrI   c                 C  s,   | j j}t| j}d| d| jd|dS )N<r   rb   z)>)r   r   rI   r   r   )rG   r   r   r>   r>   r?   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   r>   r:   namerP   r>   r>   r?   r^     
    z(Request.__getstate__.<locals>.<dictcomp>__dict__rE   rZ   r>   r>   r?   __getstate__     zRequest.__getstate__statec                 C  s0   |  D ]
\}}t| || qi | _t | _d S rR   )rE   setattrr   r   r   rG   r   r   rP   r>   r>   r?   __setstate__  s   zRequest.__setstate__)r   r   r   r   r   r   r/   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r4   r   r   r3   r4   r3   r   r   r3   r   r   r   r3   r4   )r   r   r   rH   r   r   r   r   r   r   r   r   r>   r>   r>   r?   r   4  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 )ResponseNrK   )
r/   r   texthtmlr   r   requestr   historydefault_encodingstatus_coder   r/   r0   r   ResponseContent | Noner   r2   r   r   ri   r   r   r   Request | Noner   ResponseExtensions | Noner   list[Response] | Noner   #str | typing.Callable[[bytes], str]r3   r4   c       
         C  s   || _ t|| _|| _d | _|	d u ri n|	| _|
d u rg nt|
| _d| _d| _	|| _
|d u rJt||||\}}| | || _t|trI|   n|| _d| _d S )NFr   )r   r.   r/   _requestZnext_requestr   rD   r   	is_closedis_stream_consumedr   r
   r   r   rC   r   r   _num_bytes_downloaded)rG   r   r/   r   r   r   r   r   r   r   r   r   r>   r>   r?   rH     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)rE   r7   r/   r   )rG   r   rO   rP   r>   r>   r?   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   RuntimeErrorr  rZ   r>   r>   r?   elapsed  s
   
zResponse.elapsedr
  c                 C  rQ   rR   )r  )rG   r
  r>   r>   r?   r
    rT   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.)r  r	  rZ   r>   r>   r?   r     s
   
zResponse.requestrP   c                 C  rQ   rR   )r  rS   r>   r>   r?   r     rT   rI   c                 C  s2   z| j d }W n
 ty   Y dS w |jdddS )Nhttp_versionzHTTP/1.1rJ   ignoreerrors)r   rj   rM   )rG   r  r>   r>   r?   r    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_phraserJ   r  r  )r   rj   r   Zget_reason_phraser   rM   )rG   r  r>   r>   r?   r  '  s   zResponse.reason_phraser'   c                 C  s   | j jS )zA
        Returns the URL for which the request was made.
        )r   r   rZ   r>   r>   r?   r   0  s   zResponse.urlr   c                 C  r   r   )r   r   r   rZ   r>   r>   r?   r   7  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_textr   rK   r=   )r   r   r  r   r1   r~   rM   flush)rG   r   decoderr>   r>   r?   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.
        rF   Nr   rK   )r   charset_encodingr(   rC   r   rI   r   rF   )rG   r1   r>   r>   r?   r1   H  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   r   rF   rS   r>   r>   r?   r1   ^  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)r/   rk   r,   )rG   r   r>   r>   r?   r  l  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)rl   r   r   )children)r   r/   rw   rq   r7   r   r   rj   r   r  r   r   )rG   Zdecodersrc   rP   Zdecoder_clsr>   r>   r?   _get_content_decoderw  s&   

zResponse._get_content_decoderrm   c                 C     t | jS )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   is_informationalr   rZ   r>   r>   r?   r       zResponse.is_informationalc                 C  r  )zU
        A property which is `True` for 2xx status codes, `False` otherwise.
        )r   
is_successr   rZ   r>   r>   r?   r    r  zResponse.is_successc                 C  r  )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   rZ   r>   r>   r?   r    s   
zResponse.is_redirectc                 C  r  )zU
        A property which is `True` for 4xx status codes, `False` otherwise.
        )r   is_client_errorr   rZ   r>   r>   r?   r    r  zResponse.is_client_errorc                 C  r  )zU
        A property which is `True` for 5xx status codes, `False` otherwise.
        )r   is_server_errorr   rZ   r>   r>   r?   r    r  zResponse.is_server_errorc                 C  r  )z]
        A property which is `True` for 4xx and 5xx status codes, `False` otherwise.
        )r   is_errorr   rZ   r>   r>   r?   r    r  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.
        ZLocation)r   r   MOVED_PERMANENTLYFOUND	SEE_OTHERTEMPORARY_REDIRECTPERMANENT_REDIRECTr/   rZ   r>   r>   r?   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)r  r	  r  r$  r   rk   formatr   )rG   r   messageZstatus_classZerror_typesr)  r>   r>   r?   raise_for_status  s*   
zResponse.raise_for_statuskwargsc                 K  s   t j| jfi |S rR   )jsonlibloadsr   )rG   r.  r>   r>   r?   r     s   zResponse.jsonr   c                 C  s$   t | dst | _| j|  | jS )N_cookies)r   r   r1  extract_cookiesrZ   r>   r>   r?   r     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
        linkNc                 S  s"   i | ]}| d p| d|qS )relr   )rk   )r:   r4  r>   r>   r?   r^     s    z"Response.links.<locals>.<dictcomp>)r/   rk   r-   )rG   headerr>   r>   r?   links  s   zResponse.linksc                 C  s   | j S rR   )r  rZ   r>   r>   r?   num_bytes_downloaded  s   zResponse.num_bytes_downloadedc                 C  s   d| j  d| j dS )Nz<Response [ ]>)r   r  rZ   r>   r>   r?   r     s   zResponse.__repr__r   c                 C  r   )Nc                 S  r   ))r   r   r  r  r>   r   r>   r>   r?   r^     r   z)Response.__getstate__.<locals>.<dictcomp>r   rZ   r>   r>   r?   r     r   zResponse.__getstate__r   c                 C  s6   |  D ]
\}}t| || qd| _i | _t | _d S )NT)rE   r   r  r   r   r   r   r>   r>   r?   r   !  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   rZ   r>   r>   r?   r   (  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, and brotli encoded responses.
        r   Nr   r   r=  r   )r   r   r   rangemaxr  r   r   r  iter_rawrM   r  rG   r=  ir  chunkerZ	raw_bytesdecodedchunkr>   r>   r?   r<  0  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.
        rK   r=   rA  rB  N)r   r1   r   r   r  r<  rM   r  rG   r=  r  rH  Zbyte_contentZtext_contentrJ  r>   r>   r?   	iter_textG  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 NrB  )r   r   r  rN  rM   r  rG   r  r   liner>   r>   r?   
iter_linesZ  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   rA  rB  N)r  r   r  r   rC   r   r&   r	  r  r   r   r  r   rM   r  closerG   r=  rH  Zraw_stream_bytesrJ  r>   r>   r?   rE  c  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.TrB  N)rC   r   r&   r	  r  r   r  rT  rZ   r>   r>   r?   rT  }  s   "zResponse.closec                   s2   t | dsddd |  2 I dH | _| jS )r;  r   r   c                   r   rR   r>   r   r>   r>   r?   r@     r   z"Response.aread.<locals>.<listcomp>N)r   r~   aiter_bytesr   rZ   r>   r>   r?   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 r@  )r   r   r   rC  rD  r  r   r   r  	aiter_rawrM   r  rF  r>   r>   r?   rW    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 rL  )r   r1   r   r   r  rW  rM   r  rM  r>   r>   r?   
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 rO  )r   r   r  r[  rM   r  rP  r>   r>   r?   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 )rS  z6Attempted to call an async iterator on an sync stream.Tr   rA  rB  N)r  r   r  r   rC   r   r   r	  r  r   r   r  r   rM   r  acloserU  r>   r>   r?   rY    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 )rV  z3Attempted to call an async close on an sync stream.TrB  N)rC   r   r   r	  r  r   r  r]  rZ   r>   r>   r?   r]    s   "zResponse.aclose)r   r   r/   r0   r   r   r   r2   r   r2   r   ri   r   r   r   r   r   r   r   r  r   r  r3   r4   r   )r3   r  )r
  r  r3   r4   )r3   r   )rP   r   r3   r4   r   )r3   r'   r   )r3   r2   r   )r3   r   r3   rm   )r3   r   )r.  ri   r3   ri   )r3   r   )r3   r3  r   r   r   rR   )r=  r>  r3   r?  )r=  r>  r3   rK  r3   rK  )r3   r4   )r=  r>  r3   rX  )r=  r>  r3   rZ  )r3   rZ  ),r   r   r   rH   r   r   r
  r   r   r  r  r   r   r   r1   r  r  r  r  r  r  r  r  r$  r-  r   r   r7  r8  r   r   r   r   r<  rN  rR  rE  rT  r   rW  r[  r\  rY  r]  r>   r>   r>   r?   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   r3   r4   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 rR   )	rC   r   r   jarrE   setrD   r   
set_cookie)rG   r   rO   rP   cookier>   r>   r?   rH     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`  r2  )rG   r*  Zurllib_responseurllib_requestr>   r>   r?   r2  	  s   
zCookies.extract_cookiesr   r   c                 C  s   |  |}| j| dS )zM
        Sets an appropriate 'Cookie:' HTTP header on the `Request`.
        N)re  r`  add_cookie_header)rG   r   rf  r>   r>   r?   r     s   
zCookies.set_cookie_headerr   /r   rI   rP   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   rP   portNport_specifiedFri  domain_specifieddomain_initial_dot.rj  path_specifiedsecureexpiresdiscardTcommentcomment_urlrestHttpOnlyrfc2109r>   )rm   
startswithr   r`  rb  )rG   r   rP   ri  rj  r.  rc  r>   r>   r?   ra    sJ   
	
zCookies.setrh   r2   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   ri  rj  r   rP   )rG   r   rh   ri  rj  rP   rc  r,  r>   r>   r?   rk   3  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 rR   )r   ri  rj  r:   rc  ri  r   rj  r>   r?   r@   Y  s    
z"Cookies.delete.<locals>.<listcomp>)r`  clearri  rj  r   )rG   r   ri  rj  removerc  r>   r|  r?   deleteL  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)r   r`  r}  )rG   ri  rj  argsr>   r>   r?   r}  d  s   

zCookies.clearc                 C  s$   t |}|jD ]}| j| qd S rR   )r   r`  rb  )rG   r   rc  r>   r>   r?   ry   q  s   
zCookies.updatec                 C  s   |  ||S rR   )ra  rG   r   rP   r>   r>   r?   r   v  r   zCookies.__setitem__c                 C  s   |  |}|d u rt||S rR   )rk   rj   r  r>   r>   r?   r   y  s   
zCookies.__getitem__c                 C  s
   |  |S rR   )r  )rG   r   r>   r>   r?   r     r   zCookies.__delitem__r   c                 C  r   rR   )r   r`  rZ   r>   r>   r?   r     r   zCookies.__len__rK  c                 C  rV   )Nc                 s  s    | ]}|j V  qd S rR   )r   r{  r>   r>   r?   	<genexpr>  s    z#Cookies.__iter__.<locals>.<genexpr>r`  rZ   r>   r>   r?   r     s   zCookies.__iter__rm   c                 C  s   | j D ]} dS dS )NTFr  )rG   rW   r>   r>   r?   __bool__  s   
zCookies.__bool__c                 C  s"   d dd | jD }d| dS )Nrb   c              	   S  s*   g | ]}d |j  d|j d|j dqS )z<Cookie =z for z />)r   rP   ri  r{  r>   r>   r?   r@     s    z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r:  )r~   r`  )rG   Zcookies_reprr>   r>   r?   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   r3   r4   c                   s*   t  jt|jt|j|jd || _d S )N)r   r/   r   )superrH   rI   r   r   r/   r   r   )rG   r   r   r>   r?   rH     s   
z%Cookies._CookieCompatRequest.__init__rO   rI   rP   c                   s   t  || || jj|< d S rR   )r  add_unredirected_headerr   r/   )rG   rO   rP   r  r>   r?   r    s   z4Cookies._CookieCompatRequest.add_unredirected_headerr   r   r3   r4   r   )r   r   r   r   rH   r  __classcell__r>   r>   r  r?   re    s    re  c                   @  s$   e Zd ZdZdddZdd	d
ZdS )zCookies._CookieCompatResponser  r*  r   r3   r4   c                 C  rQ   rR   )r*  )rG   r*  r>   r>   r?   rH     r   z&Cookies._CookieCompatResponse.__init__email.message.Messagec                 C  s,   t j }| jj D ]\}}|||< q|S rR   )emailr,  Messager*  r/   rg   )rG   inforO   rP   r>   r>   r?   r    s   

z"Cookies._CookieCompatResponse.infoNr*  r   r3   r4   )r3   r  )r   r   r   r   rH   r  r>   r>   r>   r?   rd    s    
rd  rR   )r   r   r3   r4   r  r  )r   rh  )
r   rI   rP   rI   ri  rI   rj  rI   r3   r4   )NNN)
r   rI   rh   r2   ri  r2   rj  r2   r3   r2   r   )r   rI   ri  r2   rj  r2   r3   r4   )ri  r2   rj  r2   r3   r4   )r   rI   rP   rI   r3   r4   )r   rI   r3   rI   )r   rI   r3   r4   r   r_  r^  r   )r   r   r   r   rH   r2  r   ra  rk   r  r}  ry   r   r   r   r   r   r  r   urllibr   r   re  rd  r>   r>   r>   r?   r     s0    

	







r   )D
__future__r   datetimeemail.messager  r   r/  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   Z
_multipartr   Z_status_codesr   _typesr   r   r   r   r    r!   r"   r#   r$   r%   r&   Z_urlsr'   _utilsr(   r)   r*   r+   r,   r-   MutableMappingrI   r.   r   r   r   r>   r>   r>   r?   <module>   s4    ($
	4 
 |     8