o
    h                     @   st  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; G dd dej<e=e=f Z>G dd dZ?G dd dZ@G dd dej<e=e=f ZAd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)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)lowerr/   Tr(   r)   .0kvr/    C/var/www/html/ai/venv/lib/python3.10/site-packages/httpx/_models.py
<listcomp>F       z$Headers.__init__.<locals>.<listcomp>c                    r1   r2   r4   r5   r9   r:   r;   r<   O   r=   )_list
isinstancer-   listr   items	_encoding)selfr.   r/   r:   r9   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)rB   rawdecodeUnicodeDecodeError)rC   r/   keyvaluer:   r:   r;   r/   Z   s   

	zHeaders.encodingrK   c                 C   
   || _ d S N)rB   rC   rK   r:   r:   r;   r/   s      
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:   )r6   raw_key_rK   r:   r:   r;   r<   |       zHeaders.raw.<locals>.<listcomp>r>   rC   r:   r:   r;   rG   w   s   zHeaders.rawc                    s    fdd j D  S )Nc                    s    i | ]\}}}|  jd qS rM   rH   r/   r6   rR   rJ   rK   rU   r:   r;   
<dictcomp>   s     z Headers.keys.<locals>.<dictcomp>)r>   keysrU   r:   rU   r;   rY   ~   s   zHeaders.keysc                 C   ^   i }| j D ]%\}}}|| j}|| j}||v r&||  d| 7  < q|||< q| S )N, )r>   rH   r/   valuesrC   values_dictrR   rJ   rK   str_key	str_valuer:   r:   r;   r\      s   
zHeaders.valuesc                 C   rZ   )z
        Return `(key, value)` items of headers. Concatenate headers
        into a single comma separated value when a key occurs multiple times.
        r[   )r>   rH   r/   rA   r]   r:   r:   r;   rA      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:   rV   rW   rU   r:   r;   r<      s    z'Headers.multi_items.<locals>.<listcomp>rT   rU   r:   rU   r;   multi_items   s   
zHeaders.multi_itemsrJ   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)rC   rJ   rb   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:   )r3   rH   r/   )r6   rR   item_key
item_valueget_header_keyrC   r:   r;   r<      s
    
z$Headers.get_list.<locals>.<listcomp>c                 S   s   g | ]}|  qS r:   )strip)r6   itemr:   r:   r;   r<      s    ,)r3   encoder/   r>   extendsplit)rC   rJ   re   r\   split_valuesrK   r:   rh   r;   get_list   s   zHeaders.get_listc                 C   s:   t |}| D ]}|| v r| | q| j|j d S rM   )r-   rY   popr>   rn   )rC   r.   rJ   r:   r:   r;   update   s   
zHeaders.updatec                 C   s   t | | jdS )Nr9   )r-   r/   rU   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:   rV   )r6   rR   
header_keyheader_valuenormalized_keyrC   r:   r;   r<      s
    
z'Headers.__getitem__.<locals>.<listcomp>r[   )r3   rm   r/   r>   joinrc   )rC   rJ   rA   r:   rw   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.
        rF   c                    s"   g | ]\}\}}}| kr|qS r:   r:   r6   idxrR   rf   
lookup_keyr:   r;   r<      s
    z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )rm   rB   r3   	enumerater>   reversedappend)rC   rJ   rK   set_key	set_value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:   )r3   r{   del_keyr:   r;   r<     s
    z'Headers.__delitem__.<locals>.<listcomp>N)r3   rm   r/   r   r>   rc   r   )rC   rJ   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:   )r6   rR   rJ   r:   r:   r;   r<     s    z(Headers.__contains__.<locals>.<listcomp>)r3   rm   r/   r>   )rC   rJ   ru   r:   r:   r;   __contains__  s   zHeaders.__contains__c                 C   s   t |  S rM   )iterrY   rU   r:   r:   r;   __iter__     zHeaders.__iter__c                 C   
   t | jS rM   )lenr>   rU   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:   rW   r:   r:   r;   r<     rS   z"Headers.__eq__.<locals>.<listcomp>c                 S   r   r:   r:   rW   r:   r:   r;   r<     rS   )r-   
ValueErrorr>   sorted)rC   r   other_headers	self_list
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 rE   z, encoding=())	__class____name__r/   r@   r*   ra   dictr   )rC   
class_nameencoding_stras_listas_dictno_duplicate_keysr:   r:   r;   __repr__"  s   
zHeaders.__repr__NNrM   )F)r0   r-   )'r   
__module____qualname____doc__typingOptionalr   strrD   propertyr/   setterListTuplebytesrG   KeysViewrY   
ValuesViewr\   	ItemsViewrA   ra   Anyrd   boolrq   rs   rt   rz   r   r   r   Iteratorr   intr   r   r   r:   r:   r:   r;   r-   7   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 dd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   r0   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 )NrE   )r   zcontent-type)content_type)r   r   r   r   boundary)r?   r   rH   upperr   r&   r   copy_merge_paramsr-   r.   r   Cookiesset_cookie_headerrd   r   r   rm   r/   _preparer   r   read)rC   r   r   r   r.   r   r   r   r   r   r   r   r   r:   r:   r;   rD   3  s<   





zRequest.__init__default_headersc                 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)rA   r3   r.   
setdefaultr   hostr   netlocr   r-   rG   )rC   r   rJ   rK   auto_headershas_hosthas_content_lengthr:   r:   r;   r   r  s   

zRequest._preparec                 C      t | dst | jS N_content)hasattrr   r   rU   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   Iterablery   r   r   rU   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 rM   r:   r6   partr:   r:   r;   r<         z!Request.aread.<locals>.<listcomp>N)r   r?   r   r   AsyncIterablery   r   r   rU   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   )rC   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:   r6   namerK   r:   r:   r;   rX     
    z(Request.__getstate__.<locals>.<dictcomp>__dict__rA   rU   r:   r:   r;   __getstate__     zRequest.__getstate__statec                 C   s0   |  D ]
\}}t| || qi | _t | _d S rM   )rA   setattrr   r   r   rC   r   r   rK   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!   rD   Dictr   r   r   r   r   r   r   r   r:   r:   r:   r;   r   2  sV    	


?"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 dd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ResponseNrF   )
r.   r   texthtmlr   r   requestr   historydefault_encodingstatus_coder.   r   r   r   r   r   r   r   r   r   r0   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.   _requestnext_requestr   r@   r   	is_closedis_stream_consumedr   r	   r   r   r?   r   r   _num_bytes_downloaded)rC   r   r.   r   r   r   r   r   r   r   r   r   r:   r:   r;   rD     s$   



zResponse.__init__r   c                 C   s<   |  D ]\}}| dkrd| jv rq| j|| qd S )Nr   zcontent-length)rA   r3   r.   r   )rC   r   rJ   rK   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  rU   r:   r:   r;   elapsed  s
   
zResponse.elapsedr  c                 C   rL   rM   )r  )rC   r  r:   r:   r;   r    rO   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  rU   r:   r:   r;   r     s
   
zResponse.requestrK   c                 C   rL   rM   )r   rN   r:   r:   r;   r     rO   c                 C   s2   z| j d }W n
 ty   Y dS w |jdddS )Nhttp_versionzHTTP/1.1rE   ignoreerrors)r   rc   rH   )rC   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_phraserE   r  r  )r   rc   r   get_reason_phraser   rH   )rC   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   rU   r:   r:   r;   r   -  s   zResponse.urlc                 C   r   r   )r   r   r   rU   r:   r:   r;   r   4  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   rF   r9   )r   r   r  r   r/   ry   rH   flush)rC   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.
        rB   Nr   rF   )r   charset_encodingr'   r?   r   r   r   rB   )rC   r/   r:   r:   r;   r/   E  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   rB   rN   r:   r:   r;   r/   [  s
   

c                 C   s    | j d}|du rdS t|S )zO
        Return the encoding, as specified by the Content-Type header.
        zContent-TypeN)r.   rd   r+   )rC   r   r:   r:   r;   r  i  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)re   r   r   )children)r   r.   rq   rj   r3   r
   r   rc   r   r  r   r   )rC   decodersr\   rK   decoder_clsr:   r:   r;   _get_content_decodert  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   rU   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   rU   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   rU   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   rU   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   rU   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   rU   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.
        Location)r   r   MOVED_PERMANENTLYFOUND	SEE_OTHERTEMPORARY_REDIRECTPERMANENT_REDIRECTr.   rU   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   rd   formatr   )rC   r   messagestatus_classerror_typesr(  r:   r:   r;   raise_for_status  s*   
zResponse.raise_for_statuskwargsc                 K   s   t j| jfi |S rM   )jsonlibloadsr   )rC   r/  r:   r:   r;   r     s   zResponse.jsonr   c                 C   s$   t | dst | _| j|  | jS )N_cookies)r   r   r2  extract_cookiesrU   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.   rd   r,   )rC   headerldictlinksr4  rJ   r:   r:   r;   r8    s   
zResponse.linksc                 C   s   | j S rM   )r  rU   r:   r:   r;   num_bytes_downloaded  s   zResponse.num_bytes_downloadedc                 C   s   d| j  d| j dS )Nz<Response [ ]>)r   r
  rU   r:   r:   r;   r     s   zResponse.__repr__c                 C   r   )Nc                 S   r   ))r   r   r   r  r:   r   r:   r:   r;   rX     r   z)Response.__getstate__.<locals>.<dictcomp>r   rU   r:   r:   r;   r     r   zResponse.__getstate__r   c                 C   s6   |  D ]
\}}t| || qd| _i | _t | _d S )NT)rA   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   ry   
iter_bytesr   rU   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_rawrH   r  rC   r>  ir  chunker	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.
        rF   r9   r@  rA  N)r   r/   r   r   r   r=  rH   r  rC   r>  r  rG  byte_contenttext_contentrJ  r:   r:   r;   	iter_textF  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 NrA  )r   r   r   rO  rH   r  rC   r  r   liner:   r:   r;   
iter_lines[  s   "zResponse.iter_linesc                 c   s    | j rt | jrt t| jtstdd| _ d| _t	|d}t
| jd" | jD ]}|  jt|7  _||D ]}|V  q<q,W d   n1 sMw   Y  | D ]}|V  qV|   dS )@
        A byte-iterator over the raw response content.
        z5Attempted to call a sync iterator on an async stream.Tr   r@  rA  N)r  r   r   r   r?   r   r%   r  r  r   r   r   r   rH   r  closerC   r>  rG  raw_stream_bytesrJ  r:   r:   r;   rD  d  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.TrA  N)r?   r   r%   r  r   r   r   rU  rU   r:   r:   r;   rU    s   "zResponse.closec                    s2   t | dsddd |  2 I dH | _| jS )r<  r   r   c                    r   rM   r:   r   r:   r:   r;   r<     r   z"Response.aread.<locals>.<listcomp>N)r   ry   aiter_bytesr   rU   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   rB  rC  r  r   r   r   	aiter_rawrH   r  rE  r:   r:   r;   rY    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 rK  )r   r/   r   r   r   rY  rH   r  rL  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 rP  )r   r   r   r[  rH   r  rQ  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 )rT  z6Attempted to call an async iterator on an sync stream.Tr   r@  rA  N)r  r   r   r   r?   r   r   r  r  r   r   r   r   rH   r  acloserV  r:   r:   r;   rZ    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 )rX  z3Attempted to call an async close on an sync stream.TrA  N)r?   r   r   r  r   r   r   r]  rU   r:   r:   r;   r]    s   "zResponse.aclose)r0   r   )r0   r   rM   )r0   N)Cr   r   r   r   r   r   r   r#   r   r   r   r%   r   r   r$   r   Callabler   rD   r   r   r   datetime	timedeltar  r   r   r  r
  r&   r   r   r   r/   r  r   r  r   r  r  r  r  r  r  r#  r.  r   r   r8  r9  r   r   r   r   r   r=  rO  rS  rD  rU  r   AsyncIteratorrY  r[  r\  rZ  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   r0   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 rM   )	r?   r   r   jarrA   setr@   r   
set_cookie)rC   r   rJ   rK   cookier:   r:   r;   rD     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  r3  )rC   r)  urllib_responseurllib_requestr:   r:   r;   r3    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)rC   r   ri  r:   r:   r;   r     s   
zCookies.set_cookie_headerr   /r   rK   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   rK   portNport_specifiedFrl  domain_specifieddomain_initial_dot.rm  path_specifiedsecureexpiresdiscardTcommentcomment_urlrestHttpOnlyrfc2109r:   )r   
startswithr   rb  rd  )rC   r   rK   rl  rm  r/  re  r:   r:   r;   rc    sJ   
	
zCookies.setrb   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   rl  rm  r   rK   )rC   r   rb   rl  rm  rK   re  r+  r:   r:   r;   rd   6  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 rM   )r   rl  rm  r6   re  rl  r   rm  r:   r;   r<   \  s    
z"Cookies.delete.<locals>.<listcomp>)rb  clearrl  rm  r   )rC   r   rl  rm  removere  r:   r  r;   deleteO  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   rb  r  )rC   rl  rm  argsr:   r:   r;   r  g  s   

zCookies.clearc                 C   s$   t |}|jD ]}| j| qd S rM   )r   rb  rd  )rC   r   re  r:   r:   r;   rs   v  s   
zCookies.updatec                 C   s   |  ||S rM   )rc  rC   r   rK   r:   r:   r;   r   {  r   zCookies.__setitem__c                 C   s   |  |}|d u rt||S rM   )rd   rc   r  r:   r:   r;   rz   ~  s   
zCookies.__getitem__c                 C   s
   |  |S rM   )r  )rC   r   r:   r:   r;   r     r   zCookies.__delitem__c                 C   r   rM   )r   rb  rU   r:   r:   r;   r     r   zCookies.__len__c                 C   rP   )Nc                 s   s    | ]}|j V  qd S rM   )r   r~  r:   r:   r;   	<genexpr>  s    z#Cookies.__iter__.<locals>.<genexpr>rb  rU   r:   r:   r;   r     s   zCookies.__iter__c                 C   s   | j D ]} dS dS )NTFr  )rC   rR   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   rK   rl  r~  r:   r:   r;   r<     s    z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r;  )ry   rb  )rC   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   r0   Nc                    s*   t  jt|jt|j|jd || _d S )N)r   r.   r   )superrD   r   r   r   r.   r   r   )rC   r   r   r:   r;   rD     s   
z%Cookies._CookieCompatRequest.__init__rJ   rK   c                    s   t  || || jj|< d S rM   )r  add_unredirected_headerr   r.   )rC   rJ   rK   r  r:   r;   r    s   z4Cookies._CookieCompatRequest.add_unredirected_header)	r   r   r   r   r   rD   r   r  __classcell__r:   r:   r  r;   rg    s    "rg  c                   @   s4   e Zd ZdZdeddfddZdejjfddZ	dS )	zCookies._CookieCompatResponser  r)  r0   Nc                 C   rL   rM   )r)  )rC   r)  r:   r:   r;   rD     r   z&Cookies._CookieCompatResponse.__init__c                 C   s,   t j }| jj D ]\}}|||< q|S rM   )emailr+  Messager)  r.   ra   )rC   inforJ   rK   r:   r:   r;   r    s   

z"Cookies._CookieCompatResponse.info)
r   r   r   r   r   rD   r  r+  r  r  r:   r:   r:   r;   rf    s    rf  rM   )r   rk  )NNNr   ) r   r   r   r   r   r   r   rD   r   r3  r   r   r   rc  rd   r  r  rs   r   rz   r   r   r   r   r   r   r  r   urllibr   rg  rf  r:   r:   r:   r;   r     sb    	 



r   )Br_  email.messager  r   r0  r   urllib.requestr  collections.abcr   http.cookiejarr   r   r   r   r   r   r	   	_decodersr
   r   r   r   r   r   r   r   _exceptionsr   r   r   r   r   r   r   
_multipartr   _status_codesr   _typesr   r   r   r   r   r    r!   r"   r#   r$   r%   _urlsr&   _utilsr'   r(   r)   r*   r+   r,   MutableMappingr   r-   r   r   r   r:   r:   r:   r;   <module>   s2    ($
	4 
 |     >