o
    e'                     @   sx  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-m.Z.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< G dd dej=e>e>f Z?G dd dZ@G dd dZAG dd dej=e>e>f ZBdS )    N)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)guess_json_utf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		d3deje deje ddfddZe	defdd	Z
e
jd
eddfdd	Z
e	dejejeef  fddZdeje fddZdeje fddZdejeef fddZdejejeef  fddZd4dedejdejfddZd5dededeje fddZd4deje ddfddZd6d d!Zdedefd"d#Zded
eddfd$d%Zdeddfd&d'Zdejdefd(d)Z dej!ej fd*d+Z"de#fd,d-Z$d.ejdefd/d0Z%defd1d2Z&dS )7Headersz9
    HTTP headers, as a case-insensitive multi-dict.
    Nheadersencodingreturnc                    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)lowerr0   Tr)   r*   .0kvr0    =D:\Projects\ConvertPro\env\Lib\site-packages\httpx/_models.py
<listcomp>G       z$Headers.__init__.<locals>.<listcomp>c                    r2   r3   r5   r6   r:   r;   r<   r=   P   r>   )_list
isinstancer.   listr   items	_encoding)selfr/   r0   r;   r:   r<   __init__=   s   




	
	zHeaders.__init__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)asciiutf-8z
iso-8859-1)rC   rawdecodeUnicodeDecodeError)rD   r0   keyvaluer;   r;   r<   r0   [   s   

	zHeaders.encodingrL   c                 C   
   || _ d S NrC   rD   rL   r;   r;   r<   r0   t      
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;   )r7   Zraw_key_rL   r;   r;   r<   r=   }       zHeaders.raw.<locals>.<listcomp>r?   rD   r;   r;   r<   rH   x   s   zHeaders.rawc                    s    fdd j D  S )Nc                    s    i | ]\}}}|  jd qS rN   rI   r0   r7   rS   rK   rL   rV   r;   r<   
<dictcomp>   s     z Headers.keys.<locals>.<dictcomp>)r?   keysrV   r;   rV   r<   rZ      s   zHeaders.keysc                 C   ^   i }| j D ]%\}}}|| j}|| j}||v r&||  d| 7  < q|||< q| S )N, )r?   rI   r0   valuesrD   Zvalues_dictrS   rK   rL   Zstr_keyZ	str_valuer;   r;   r<   r]      s   
zHeaders.valuesc                 C   r[   )z
        Return `(key, value)` items of headers. Concatenate headers
        into a single comma separated value when a key occurs multiple times.
        r\   )r?   rI   r0   rB   r^   r;   r;   r<   rB      s   
zHeaders.itemsc                    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;   rW   rX   rV   r;   r<   r=      s    z'Headers.multi_items.<locals>.<listcomp>rU   rV   r;   rV   r<   multi_items   s   
zHeaders.multi_itemsrK   defaultc                 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)rD   rK   r`   r;   r;   r<   get   s
   
zHeaders.getFsplit_commasc                    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;   )r4   rI   r0   )r7   rS   item_keyZ
item_valueZget_header_keyrD   r;   r<   r=      s
    
z$Headers.get_list.<locals>.<listcomp>c                 S   s   g | ]}|  qS r;   )strip)r7   itemr;   r;   r<   r=      s    ,)r4   encoder0   r?   extendsplit)rD   rK   rc   r]   Zsplit_valuesrL   r;   re   r<   get_list   s   zHeaders.get_listc                 C   s:   t |}| D ]}|| v r| | q| j|j d S rN   )r.   rZ   popr?   rj   )rD   r/   rK   r;   r;   r<   update   s   
zHeaders.updatec                 C   s   t | | jdS )Nr:   )r.   r0   rV   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;   rW   )r7   rS   
header_keyheader_valueZnormalized_keyrD   r;   r<   r=      s
    
z'Headers.__getitem__.<locals>.<listcomp>r\   )r4   ri   r0   r?   joinra   )rD   rK   rB   r;   rr   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.
        rG   c                    s"   g | ]\}\}}}| kr|qS r;   r;   r7   idxrS   rd   Z
lookup_keyr;   r<   r=      s
    z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )ri   rC   r4   	enumerater?   reversedappend)rD   rK   rL   Zset_key	set_valueZfound_indexesrv   r;   rw   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;   )r4   ru   Zdel_keyr;   r<   r=     s
    z'Headers.__delitem__.<locals>.<listcomp>N)r4   ri   r0   rx   r?   ra   ry   )rD   rK   Zpop_indexesrv   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;   )r7   rS   rK   r;   r;   r<   r=     s    z(Headers.__contains__.<locals>.<listcomp>)r4   ri   r0   r?   )rD   rK   rp   r;   r;   r<   __contains__  s   zHeaders.__contains__c                 C   s   t |  S rN   )iterrZ   rV   r;   r;   r<   __iter__     zHeaders.__iter__c                 C   
   t | jS rN   )lenr?   rV   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;   rX   r;   r;   r<   r=     rT   z"Headers.__eq__.<locals>.<listcomp>c                 S   r   r;   r;   rX   r;   r;   r<   r=      rT   )r.   
ValueErrorr?   sorted)rD   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 rF   z, encoding=())	__class____name__r0   rA   r+   r_   dictr   )rD   
class_nameZencoding_stras_listas_dictZno_duplicate_keysr;   r;   r<   __repr__#  s   
zHeaders.__repr__NNrN   )F)r1   r.   )'r   
__module____qualname____doc__typingOptionalr   strrE   propertyr0   setterListTuplebytesrH   KeysViewrZ   
ValuesViewr]   	ItemsViewrB   r_   Anyrb   boolrl   rn   ro   rt   r|   r~   r   Iteratorr   intr   r   r   r;   r;   r;   r<   r.   8   sB    
 


r.   c                   @   s:  e Zd Zdddddddddd	dejeef dejdef deje deje	 deje
 d	eje d
eje deje dejej dejeedf deje fddZdejeef ddfddZedefddZdefddZdefddZdefddZdejeejf fddZdejeejf ddfd d!ZdS )"RequestN)	paramsr/   cookiescontentdatafilesjsonstream
extensionsmethodurlr&   r   r/   r   r   r   r   r   r   r   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 )NrF   )r   zcontent-type)content_type)r   r   r   r   boundary)r@   r   rI   upperr   r&   r   Zcopy_merge_paramsr.   r/   r   Cookiesset_cookie_headerrb   r   r   ri   r0   _preparer   r   read)rD   r   r   r   r/   r   r   r   r   r   r   r   r   r;   r;   r<   rE   4  s<   





zRequest.__init__default_headersr1   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)rB   r4   r/   
setdefaultr   hostrz   netlocr   r.   rH   )rD   r   rK   rL   Zauto_headersZhas_hostZhas_content_lengthr;   r;   r<   r   s  s   

zRequest._preparec                 C      t | dst | jS N_content)hasattrr   r   rV   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   r@   r   r   Iterablers   r   r   rV   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 rN   r;   r7   partr;   r;   r<   r=         z!Request.aread.<locals>.<listcomp>N)r   r@   r   r   AsyncIterablers   r   r   rV   r;   r;   r<   aread  s   
zRequest.areadc                 C   s,   | j j}t| j}d| d| jd|dS )N<r   r\   z)>)r   r   r   r   r   )rD   r   r   r;   r;   r<   r     s   
zRequest.__repr__c                 C      dd | j  D S )Nc                 S      i | ]\}}|d vr||qS ))r   r   r;   r7   namerL   r;   r;   r<   rY     
    z(Request.__getstate__.<locals>.<dictcomp>__dict__rB   rV   r;   r;   r<   __getstate__     zRequest.__getstate__statec                 C   s0   |  D ]
\}}t| || qi | _t | _d S rN   )rB   setattrr   r   r   rD   r   r   rL   r;   r;   r<   __setstate__  s   zRequest.__setstate__)r   r   r   r   Unionr   r   r   r   r   r   r   r    r"   r   r%   r   r!   rE   Dictr   r   r   r   r   r   r   r   r;   r;   r;   r<   r   3  sR    	


?"r   c                   @   s&  e Zd Zddddddddddd
dedeje deje deje deje d	ej	d
ej
eedf deje deje dejejd   dej
eejegef f fddZdejeef ddfddZedejfddZejdejddfddZedefddZejdeddfddZedefddZedefdd Zedefd!d"Zedefd#d$Zedefd%d&Z edeje fd'd(Z!e!jdeddfd)d(Z!edeje fd*d+Z"de#fd,d-Z$ede%fd.d/Z&ede%fd0d1Z'ede%fd2d3Z(ede%fd4d5Z)ede%fd6d7Z*ede%fd8d9Z+ede%fd:d;Z,dhd<d=Z-d>ej	dej	fd?d@Z.edidBdCZ/edejeje ejeef f fdDdEZ0edefdFdGZ1defdHdIZ2dejeej	f fdJdKZ3dLejeej	f ddfdMdNZ4defdOdPZ5	djdQeje dej6e fdRdSZ7	djdQeje dej6e fdTdUZ8dej6e fdVdWZ9	djdQeje dej6e fdXdYZ:dkdZd[Z;defd\d]Z<	djdQeje dej=e fd^d_Z>	djdQeje dej=e fd`daZ?dej=e fdbdcZ@	djdQeje dej=e fdddeZAdkdfdgZBdS )lResponseNrG   )
r/   r   texthtmlr   r   requestr   historydefault_encodingstatus_coder/   r   r   r   r   r   r   r   r   r   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   rA   r   	is_closedis_stream_consumedr   r	   r   r   r@   r   r   _num_bytes_downloaded)rD   r   r/   r   r   r   r   r   r   r   r   r   r;   r;   r<   rE     s$   



zResponse.__init__r   r1   c                 C   s<   |  D ]\}}| dkrd| jv rq| j|| qd S )Nr   zcontent-length)rB   r4   r/   r   )rD   r   rK   rL   r;   r;   r<   r     s
   zResponse._preparec                 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   rV   r;   r;   r<   elapsed  s
   
zResponse.elapsedr   c                 C   rM   rN   )r   )rD   r   r;   r;   r<   r   	  rQ   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   rV   r;   r;   r<   r     s
   
zResponse.requestrL   c                 C   rM   rN   )r   rP   r;   r;   r<   r     rQ   c                 C   s2   z| j d }W n
 ty   Y dS w |jdddS )Nhttp_versionzHTTP/1.1rF   ignoreerrors)r   ra   rI   )rD   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_phraserF   r   r   )r   ra   r   Zget_reason_phraser   rI   )rD   r   r;   r;   r<   r   %  s   zResponse.reason_phrasec                 C   s   | j jS )zA
        Returns the URL for which the request was made.
        )r   r   rV   r;   r;   r<   r   .  s   zResponse.urlc                 C   r   r   )r   r   r   rV   r;   r;   r<   r   5  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   rG   r:   )r   r   r   r   r0   rs   rI   flush)rD   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.
        rC   Nr   rG   )r   charset_encodingr(   r@   r   r   r   rC   )rD   r0   r;   r;   r<   r0   F  s   


zResponse.encodingc                 C   rM   rN   rO   rP   r;   r;   r<   r0   \  rQ   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/   rb   r,   )rD   r   r;   r;   r<   r   `  s   zResponse.charset_encodingc              	   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)rc   r   r   )children)r   r/   rl   rf   r4   r
   rz   ra   r   r   r   r   )rD   Zdecodersr]   rL   Zdecoder_clsr;   r;   r<   _get_content_decoderk  s&   

zResponse._get_content_decoderc                 C      t | jS )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   is_informationalr   rV   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   rV   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   rV   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   rV   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   rV   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   rV   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/   rV   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   rb   formatr   )rD   r   messageZstatus_classZerror_typesr  r;   r;   r<   raise_for_status  s*   
zResponse.raise_for_statuskwargsc                 K   s^   | j d u r%| jr%t| jdkr%t| j}|d ur%tj| j|fi |S tj| jfi |S )Nr  )r   r   r   r'   jsonlibloadsrI   r   )rD   r  r0   r;   r;   r<   r     s
   
zResponse.jsonr   c                 C   s$   t | dst | _| j|  | jS )N_cookies)r   r   r  extract_cookiesrV   r;   r;   r<   r     s   
zResponse.cookiesc                 C   sF   | j d}i }|r!t|}|D ]}|dp|d}|||< q|S )zI
        Returns the parsed header links of the response, if any
        linkrelr   )r/   rb   r-   )rD   headerZldictlinksr  rK   r;   r;   r<   r    s   
zResponse.linksc                 C   s   | j S rN   )r   rV   r;   r;   r<   num_bytes_downloaded  s   zResponse.num_bytes_downloadedc                 C   s   d| j  d| j dS )Nz<Response [ ]>)r   r   rV   r;   r;   r<   r     s   zResponse.__repr__c                 C   r   )Nc                 S   r   ))r   r   r   r   r;   r   r;   r;   r<   rY     r   z)Response.__getstate__.<locals>.<dictcomp>r   rV   r;   r;   r<   r     r   zResponse.__getstate__r   c                 C   s6   |  D ]
\}}t| || qd| _i | _t | _d S )NT)rB   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   rs   
iter_bytesr   rV   r;   r;   r<   r      s   
zResponse.read
chunk_sizec                 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_rawrI   r   rD   r"  ir   chunkerZ	raw_bytesdecodedchunkr;   r;   r<   r!  (  s*   


"zResponse.iter_bytesc                 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.
        rG   r:   r$  r%  N)r   r0   r   r   r   r!  rI   r   rD   r"  r   r+  Zbyte_contentZtext_contentr-  r;   r;   r<   	iter_textA  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 Nr%  )r   r   r   r0  rI   r   rD   r   r   liner;   r;   r<   
iter_linesV  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   r$  r%  N)r   r   r   r   r@   r   r%   r   r   r   r   r   r   rI   r   closerD   r"  r+  Zraw_stream_bytesr-  r;   r;   r<   r(  _  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.Tr%  N)r@   r   r%   r   r   r   r   r6  rV   r;   r;   r<   r6  {  s   "zResponse.closec                    s2   t | dsddd |  2 I dH | _| jS )r   r   r   c                    r   rN   r;   r   r;   r;   r<   r=     r   z"Response.aread.<locals>.<listcomp>N)r   rs   aiter_bytesr   rV   r;   r;   r<   r     s   
 zResponse.areadc                 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   r&  r'  r   r   r   r   	aiter_rawrI   r   r)  r;   r;   r<   r9    s,   


"zResponse.aiter_bytesc                 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   r0   r   r   r   r9  rI   r   r/  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 r1  )r   r   r   r;  rI   r   r2  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 )r5  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   rI   r   acloser7  r;   r;   r<   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 )r8  z3Attempted to call an async close on an sync stream.Tr%  N)r@   r   r   r   r   r   r   r=  rV   r;   r;   r<   r=    s   "zResponse.aclose)r1   r   )r1   r   rN   )r1   N)Cr   r   r   r   r   r   r   r#   r   r   r   r%   r   r   r$   r   Callabler   rE   r   r   r   datetime	timedeltar   r   r   r   r   r&   r   r   r   r0   r   r   r   r   r   r   r  r  r  r  r
  r  r   r   r  r  r   r   r   r   r   r!  r0  r4  r(  r6  r   AsyncIteratorr9  r;  r<  r:  r=  r;   r;   r;   r<   r     s    	

7



%*	




	



r   c                   @   s  e Zd ZdZd0deje ddfddZdeddfdd	Z	d
e
ddfddZd1dededededdf
ddZ			d2dedeje deje deje deje f
ddZ		d3dedeje deje ddfddZ	d3deje deje ddfddZd0deje ddfddZdededdfddZdedefd d!Zdeddfd"d#Zdefd$d%Zdeje fd&d'Zdefd(d)Zdefd*d+ZG d,d- d-ejj
ZG d.d/ d/ZdS )4r   z-
    HTTP Cookies, as a mutable mapping.
    Nr   r1   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 rN   )	r@   r   r   jarrB   setrA   r   
set_cookie)rD   r   rK   rL   cookier;   r;   r<   rE     s$   




zCookies.__init__r  c                 C   s(   |  |}| |j}| j|| dS )zO
        Loads any cookies based on the response `Set-Cookie` headers.
        N)_CookieCompatResponse_CookieCompatRequestr   rB  r  )rD   r  Zurllib_responseurllib_requestr;   r;   r<   r    s   
zCookies.extract_cookiesr   c                 C   s   |  |}| j| dS )zM
        Sets an appropriate 'Cookie:' HTTP header on the `Request`.
        N)rG  rB  add_cookie_header)rD   r   rH  r;   r;   r<   r     s   
zCookies.set_cookie_headerr   /r   rL   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   rL   portNport_specifiedFrK  domain_specifieddomain_initial_dot.rL  path_specifiedsecureexpiresdiscardTcommentcomment_urlrestHttpOnlyrfc2109r;   )r   
startswithr   rB  rD  )rD   r   rL   rK  rL  r  rE  r;   r;   r<   rC    sJ   
	
zCookies.setr`   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=)rB  r   rK  rL  r   rL   )rD   r   r`   rK  rL  rL   rE  r  r;   r;   r<   rb   1  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 rN   )r   rK  rL  r7   rE  rK  r   rL  r;   r<   r=   W  s    
z"Cookies.delete.<locals>.<listcomp>)rB  clearrK  rL  r   )rD   r   rK  rL  removerE  r;   r^  r<   deleteJ  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)rz   rB  r_  )rD   rK  rL  argsr;   r;   r<   r_  b  s   

zCookies.clearc                 C   s$   t |}|jD ]}| j| qd S rN   )r   rB  rD  )rD   r   rE  r;   r;   r<   rn   q  s   
zCookies.updatec                 C   s   |  ||S rN   )rC  rD   r   rL   r;   r;   r<   r|   v  r   zCookies.__setitem__c                 C   s   |  |}|d u rt||S rN   )rb   ra   rc  r;   r;   r<   rt   y  s   
zCookies.__getitem__c                 C   s
   |  |S rN   )ra  )rD   r   r;   r;   r<   r~     r   zCookies.__delitem__c                 C   r   rN   )r   rB  rV   r;   r;   r<   r     r   zCookies.__len__c                 C   rR   )Nc                 s   s    | ]}|j V  qd S rN   )r   r]  r;   r;   r<   	<genexpr>  s    z#Cookies.__iter__.<locals>.<genexpr>rB  rV   r;   r;   r<   r     s   zCookies.__iter__c                 C   s   | j D ]} dS dS )NTFre  )rD   rS   r;   r;   r<   __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 =z for z />)r   rL   rK  r]  r;   r;   r<   r=     s    z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r  )rs   rB  )rD   Zcookies_reprr;   r;   r<   r     s   zCookies.__repr__c                       sD   e Zd ZdZdeddf fddZdededdf 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   r1   Nc                    s*   t  jt|jt|j|jd || _d S )N)r   r/   r   )superrE   r   r   r   r/   r   r   )rD   r   r   r;   r<   rE     s   
z%Cookies._CookieCompatRequest.__init__rK   rL   c                    s   t  || || jj|< d S rN   )ri  add_unredirected_headerr   r/   )rD   rK   rL   rj  r;   r<   rk    s   z4Cookies._CookieCompatRequest.add_unredirected_header)	r   r   r   r   r   rE   r   rk  __classcell__r;   r;   rj  r<   rG    s    "rG  c                   @   s0   e Zd ZdZdefddZdejjfddZ	dS )	zCookies._CookieCompatResponserh  r  c                 C   rM   rN   )r  )rD   r  r;   r;   r<   rE     r   z&Cookies._CookieCompatResponse.__init__r1   c                 C   s,   t j }| jj D ]\}}|||< q|S rN   )emailr  Messager  r/   r_   )rD   inforK   rL   r;   r;   r<   ro    s   

z"Cookies._CookieCompatResponse.infoN)
r   r   r   r   r   rE   rm  r  rn  ro  r;   r;   r;   r<   rF    s    rF  rN   )r   rJ  )NNNr   ) r   r   r   r   r   r   r   rE   r   r  r   r   r   rC  rb   ra  r_  rn   r|   rt   r~   r   r   r   r   r   rf  r   urllibr   rG  rF  r;   r;   r;   r<   r     sb    	 



r   )Cr?  email.messagerm  r   r  r   urllib.requestrp  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,   r-   MutableMappingr   r.   r   r   r   r;   r;   r;   r<   <module>   s2    ($
	4$ |     8