U
    —ùiÚC  ã                   @  s@  d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	 ej
rjd dlmZ d dlmZ G dd	„ d	eƒZd
dgZe d¡Ze d¡Ze d¡Zejdejeef ejejeef  d	f ZG dd„ deƒZdddœdd„ZG dd
„ d
ejeef ejeef ƒZG dd„ dejejeef  ƒZ G dd„ dejeef ƒZ!dS )é    )ÚannotationsN)ÚOrderedDict)ÚEnumÚauto)ÚRLock)ÚProtocol)ÚSelfc                   @  s*   e Zd Zddœdd„Zdddœdd„Zd	S )
ÚHasGettableStringKeysútyping.Iterator[str]©Úreturnc                 C  s   d S ©N© ©Úselfr   r   ú8/tmp/pip-unpacked-wheel-z24cd2a5/urllib3/_collections.pyÚkeys   s    zHasGettableStringKeys.keysÚstr©Úkeyr   c                 C  s   d S r   r   ©r   r   r   r   r   Ú__getitem__   s    z!HasGettableStringKeys.__getitem__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r	      s   r	   ÚRecentlyUsedContainerÚHTTPHeaderDictÚ_KTÚ_VTÚ_DTc                   @  s   e Zd Zeƒ ZdS )Ú	_SentinelN)r   r   r   r   Ú
not_passedr   r   r   r   r    )   s   r    ÚobjectúValidHTTPHeaderSource | None)Ú	potentialr   c                 C  s€   t | tƒr| S t | tjƒr0t tjttf | ¡S t | tjƒrXt tjtjttf  | ¡S t| dƒrxt| dƒrxt d| ¡S d S d S )Nr   r   r	   )	Ú
isinstancer   ÚtypingÚMappingÚcastr   ÚIterableÚTupleÚhasattr)r$   r   r   r   Ú%ensure_can_construct_http_header_dict-   s    
r,   c                      s¸   e Zd ZU dZded< ded< ded< ded	< d%ddddœ‡ fdd„Zdddœdd„Zddddœdd„Zdddœdd„Zddœdd„Z	ddœdd„Z
ddœd d!„Zd"dœd#d$„Z‡  ZS )&r   a‘  
    Provides a thread-safe dict-like container which maintains up to
    ``maxsize`` keys while throwing away the least-recently-used keys beyond
    ``maxsize``.

    :param maxsize:
        Maximum number of recent elements to retain.

    :param dispose_func:
        Every time an item is evicted from the container,
        ``dispose_func(value)`` is called.  Callback which will get called
    ztyping.OrderedDict[_KT, _VT]Ú
_containerÚintÚ_maxsizez#typing.Callable[[_VT], None] | NoneÚdispose_funcr   Úlocké
   NÚNone)Úmaxsizer0   r   c                   s*   t ƒ  ¡  || _|| _tƒ | _tƒ | _d S r   )ÚsuperÚ__init__r/   r0   r   r-   r   r1   )r   r4   r0   ©Ú	__class__r   r   r6   T   s
    
zRecentlyUsedContainer.__init__r   r   r   c              
   C  s8   | j �( | j |¡}|| j|< |W  5 Q R £ S Q R X d S r   )r1   r-   Úpop)r   r   Úitemr   r   r   r   _   s    
z!RecentlyUsedContainer.__getitem__)r   Úvaluer   c              	   C  s–   d }| j �b z|| j |¡f}|| j|< W n< tk
rf   || j|< t| jƒ| jkrb| jjdd�}Y nX W 5 Q R X |d k	r’| jr’|\}}|  |¡ d S )NF)Úlast)r1   r-   r9   ÚKeyErrorÚlenr/   Úpopitemr0   )r   r   r;   Zevicted_itemÚ_Úevicted_valuer   r   r   Ú__setitem__f   s    
z!RecentlyUsedContainer.__setitem__c              	   C  s2   | j � | j |¡}W 5 Q R X | jr.|  |¡ d S r   )r1   r-   r9   r0   )r   r   r;   r   r   r   Ú__delitem__   s    z!RecentlyUsedContainer.__delitem__r   c              
   C  s(   | j � t| jƒW  5 Q R £ S Q R X d S r   )r1   r>   r-   r   r   r   r   Ú__len__†   s    zRecentlyUsedContainer.__len__ztyping.NoReturnc                 C  s   t dƒ‚d S )Nz7Iteration over this class is unlikely to be threadsafe.)ÚNotImplementedErrorr   r   r   r   Ú__iter__Š   s    ÿzRecentlyUsedContainer.__iter__c              	   C  sH   | j � t| j ¡ ƒ}| j ¡  W 5 Q R X | jrD|D ]}|  |¡ q4d S r   )r1   Úlistr-   ÚvaluesÚclearr0   )r   rH   r;   r   r   r   rI   �   s    zRecentlyUsedContainer.clearzset[_KT]c              
   C  s,   | j � t| j ¡ ƒW  5 Q R £ S Q R X d S r   )r1   Úsetr-   r   r   r   r   r   r   ™   s    zRecentlyUsedContainer.keys)r2   N)r   r   r   Ú__doc__Ú__annotations__r6   r   rB   rC   rD   rF   rI   r   Ú__classcell__r   r   r7   r   r   A   s   
  ý
c                   @  sV   e Zd ZU dZded< dddœdd„Zdd	œd
d„Zdd	œdd„Zdddœdd„ZdS )ÚHTTPHeaderDictItemViewa�  
    HTTPHeaderDict is unusual for a Mapping[str, str] in that it has two modes of
    address.

    If we directly try to get an item with a particular name, we will get a string
    back that is the concatenated version of all the values:

    >>> d['X-Header-Name']
    'Value1, Value2, Value3'

    However, if we iterate over an HTTPHeaderDict's items, we will optionally combine
    these values based on whether combine=True was called when building up the dictionary

    >>> d = HTTPHeaderDict({"A": "1", "B": "foo"})
    >>> d.add("A", "2", combine=True)
    >>> d.add("B", "bar")
    >>> list(d.items())
    [
        ('A', '1, 2'),
        ('B', 'foo'),
        ('B', 'bar'),
    ]

    This class conforms to the interface required by the MutableMapping ABC while
    also giving us the nonstandard iteration behavior we want; items with duplicate
    keys, ordered by time of first insertion.
    r   Ú_headersr3   )Úheadersr   c                 C  s
   || _ d S r   )rO   )r   rP   r   r   r   r6   ½   s    zHTTPHeaderDictItemView.__init__r.   r   c                 C  s   t t| j ¡ ƒƒS r   )r>   rG   rO   Ú	iteritemsr   r   r   r   rD   À   s    zHTTPHeaderDictItemView.__len__ú typing.Iterator[tuple[str, str]]c                 C  s
   | j  ¡ S r   )rO   rQ   r   r   r   r   rF   Ã   s    zHTTPHeaderDictItemView.__iter__r"   Úbool)r:   r   c                 C  sD   t |tƒr@t|ƒdkr@|\}}t |tƒr@t |tƒr@| j ||¡S dS )Né   F)r%   Útupler>   r   rO   Ú_has_value_for_header)r   r:   Z
passed_keyZ
passed_valr   r   r   Ú__contains__Æ   s
    z#HTTPHeaderDictItemView.__contains__N)	r   r   r   rK   rL   r6   rD   rF   rW   r   r   r   r   rN   ž   s   
rN   c                      s  e Zd ZU dZded< dTdddœ‡ fdd	„Zddd
dœdd„Zdddœdd„Zdd
dœdd„Zdddœdd„Z	dUddddœ‡ fdd„Z
dddœdd„Zdddœdd„Zd d!œd"d#„Zd$d!œd%d&„Zdd
dœd'd(„Zd)d*œdddd
d+œd,d-„Zd.dd
d/œd0d1„Zejdd2dœd3d4„ƒZejdd5d6dœd7d4„ƒZejfdd8d6dœd9d4„Zd:d!œd;d<„ZeZeZeZeZdd!œd=d>„Zd d
dœd?d@„Zd:d!œdAdB„ZdCd!œdDdE„ZdCd!œdFdG„Z dHd!œdIdJ„Z!ddddKœdLdM„Z"dd dœdNdO„Z#dd:dœdPdQ„Z$dd:dœdRdS„Z%‡  Z&S )Vr   ap  
    :param headers:
        An iterable of field-value pairs. Must not contain multiple field names
        when compared case-insensitively.

    :param kwargs:
        Additional field-value pairs to pass in to ``dict.update``.

    A ``dict`` like container for storing HTTP Headers.

    Field names are stored and compared case-insensitively in compliance with
    RFC 7230. Iteration provides the first case-sensitive key seen for each
    case-insensitive pair.

    Using ``__setitem__`` syntax overwrites fields that compare equal
    case-insensitively in order to maintain ``dict``'s api. For fields that
    compare equal, instead create a new ``HTTPHeaderDict`` and use ``.add``
    in a loop.

    If multiple fields that are equal case-insensitively are passed to the
    constructor or ``.update``, the behavior is undefined and some will be
    lost.

    >>> headers = HTTPHeaderDict()
    >>> headers.add('Set-Cookie', 'foo=bar')
    >>> headers.add('set-cookie', 'baz=quxx')
    >>> headers['content-length'] = '7'
    >>> headers['SET-cookie']
    'foo=bar, baz=quxx'
    >>> headers['Content-Length']
    '7'
    z%typing.MutableMapping[str, list[str]]r-   Nr#   r   )rP   Úkwargsc                   sJ   t ƒ  ¡  i | _|d k	r8t|tƒr.|  |¡ n
|  |¡ |rF|  |¡ d S r   )r5   r6   r-   r%   r   Ú
_copy_fromÚextend)r   rP   rX   r7   r   r   r6   ò   s    


zHTTPHeaderDict.__init__r3   )r   Úvalr   c                 C  s*   t |tƒr| d¡}||g| j| ¡ < d S )Núlatin-1)r%   ÚbytesÚdecoder-   Úlower©r   r   r[   r   r   r   rB   ý   s    

zHTTPHeaderDict.__setitem__r   c                 C  s    | j | ¡  }d |dd … ¡S )Nú, é   ©r-   r_   Újoinr`   r   r   r   r     s    zHTTPHeaderDict.__getitem__c                 C  s   | j | ¡ = d S r   ©r-   r_   r   r   r   r   rC     s    zHTTPHeaderDict.__delitem__r"   rS   c                 C  s   t |tƒr| ¡ | jkS dS )NF)r%   r   r_   r-   r   r   r   r   rW   
  s    
zHTTPHeaderDict.__contains__Ú )r   Údefaultr   c                   s   t ƒ  ||¡S r   )r5   Ú
setdefault©r   r   rg   r7   r   r   rh     s    zHTTPHeaderDict.setdefault)Úotherr   c                 C  sD   t |ƒ}|d krdS t| ƒ|ƒ}dd„ |  ¡ D ƒdd„ | ¡ D ƒkS )NFc                 S  s   i | ]\}}|  ¡ |“qS r   )r_   )Ú.0ÚkÚvr   r   r   Ú
<dictcomp>  s      z)HTTPHeaderDict.__eq__.<locals>.<dictcomp>)r,   ÚtypeÚ
itermerged)r   rj   Úmaybe_constructableZother_as_http_header_dictr   r   r   Ú__eq__  s    ÿzHTTPHeaderDict.__eq__c                 C  s   |   |¡ S r   )rr   )r   rj   r   r   r   Ú__ne__  s    zHTTPHeaderDict.__ne__r.   r   c                 C  s
   t | jƒS r   )r>   r-   r   r   r   r   rD      s    zHTTPHeaderDict.__len__r
   c                 c  s   | j  ¡ D ]}|d V  q
d S )Nr   )r-   rH   )r   Úvalsr   r   r   rF   #  s    zHTTPHeaderDict.__iter__c                 C  s$   z
| |= W n t k
r   Y nX d S r   )r=   r   r   r   r   Údiscard(  s    
zHTTPHeaderDict.discardF)Úcombine)r   r[   rv   r   c                C  sr   t |tƒr| d¡}| ¡ }||g}| j ||¡}||k	rnt|ƒdksJt‚|rd|d d | |d< n
| |¡ dS )aù  Adds a (name, value) pair, doesn't overwrite the value if it already
        exists.

        If this is called with combine=True, instead of adding a new header value
        as a distinct item during iteration, this will instead append the value to
        any existing header value with a comma. If no existing header value exists
        for the key, then the value will simply be added, ignoring the combine parameter.

        >>> headers = HTTPHeaderDict(foo='bar')
        >>> headers.add('Foo', 'baz')
        >>> headers['foo']
        'bar, baz'
        >>> list(headers.items())
        [('foo', 'bar'), ('foo', 'baz')]
        >>> headers.add('foo', 'quz', combine=True)
        >>> list(headers.items())
        [('foo', 'bar, baz, quz')]
        r\   rT   éÿÿÿÿra   N)	r%   r]   r^   r_   r-   rh   r>   ÚAssertionErrorÚappend)r   r   r[   rv   Ú	key_lowerÚnew_valsrt   r   r   r   Úadd.  s    

zHTTPHeaderDict.addÚValidHTTPHeaderSource)ÚargsrX   r   c                 O  s,  t |ƒdkr tdt |ƒ› d�ƒ‚t |ƒdkr4|d nd}t|tƒrb| ¡ D ]\}}|  ||¡ qJn¦t|tjƒrŽ| ¡ D ]\}}|  ||¡ qvnzt|tj	ƒrÒt 
tj	tjttf  |¡}|D ]\}}|  ||¡ qºn6t|dƒ�rt|dƒ�r| ¡ D ]}|  ||| ¡ qò| ¡ D ]\}}|  ||¡ �qdS )	zÀGeneric import function for any type of header-like object.
        Adapted version of MutableMapping.update in order to insert items
        with self.add instead of self.__setitem__
        rb   z/extend() takes at most 1 positional arguments (z given)r   r   r   r   N)r>   Ú	TypeErrorr%   r   rQ   r|   r&   r'   Úitemsr)   r(   r*   r   r+   r   )r   r~   rX   rj   r   r[   r;   r   r   r   rZ   Q  s(    ÿ
zHTTPHeaderDict.extendz	list[str]c                 C  s   d S r   r   r   r   r   r   Úgetlistr  s    zHTTPHeaderDict.getlistr   zlist[str] | _DTc                 C  s   d S r   r   ri   r   r   r   r�   v  s    z_Sentinel | _DTc                 C  sN   z| j | ¡  }W n* tk
r<   |tjkr4g  Y S | Y S X |dd… S dS )zmReturns a list of all the values for the named field. Returns an
        empty list if the key doesn't exist.rb   N)r-   r_   r=   r    r!   )r   r   rg   rt   r   r   r   r�   z  s    

r   c                 C  s*   dddddddg}|D ]}|   |¡ q| S )z–
        Remove content-specific header fields before changing the request
        method to GET or HEAD according to RFC 9110, Section 15.4.
        zContent-EncodingzContent-LanguagezContent-LocationzContent-TypezContent-LengthÚDigestzLast-Modified)ru   )r   Úcontent_specific_headersÚheaderr   r   r   Ú_prepare_for_method_changeŒ  s    ù	z)HTTPHeaderDict._prepare_for_method_changec                 C  s   t | ƒj› dt|  ¡ ƒ› d�S )Nú(ú))ro   r   Údictrp   r   r   r   r   Ú__repr__¦  s    zHTTPHeaderDict.__repr__c                 C  s,   |D ]"}|  |¡}|f|•| j| ¡ < qd S r   )r�   r-   r_   )r   rj   r   r[   r   r   r   rY   ©  s    
zHTTPHeaderDict._copy_fromc                 C  s   t | ƒƒ }| | ¡ |S r   )ro   rY   )r   Úcloner   r   r   Úcopy®  s    

zHTTPHeaderDict.copyrR   c                 c  s<   | D ]2}| j | ¡  }|dd… D ]}|d |fV  q"qdS )z8Iterate over all header lines, including duplicate ones.rb   Nr   re   )r   r   rt   r[   r   r   r   rQ   ³  s    zHTTPHeaderDict.iteritemsc                 c  s8   | D ].}| j | ¡  }|d d |dd… ¡fV  qdS )z:Iterate over all headers, merging duplicate ones together.r   ra   rb   Nrc   r`   r   r   r   rp   º  s    zHTTPHeaderDict.itermergedrN   c                 C  s   t | ƒS r   )rN   r   r   r   r   r€   À  s    zHTTPHeaderDict.items)Úheader_nameÚpotential_valuer   c                 C  s&   || kr"|| j | ¡  dd … kS dS )Nrb   Fre   )r   rŒ   r�   r   r   r   rV   Ã  s    z$HTTPHeaderDict._has_value_for_headerc                 C  s"   t |ƒ}|d krtS |  |¡ | S r   )r,   ÚNotImplementedrZ   )r   rj   rq   r   r   r   Ú__ior__È  s
    
zHTTPHeaderDict.__ior__c                 C  s*   t |ƒ}|d krtS |  ¡ }| |¡ |S r   )r,   rŽ   r‹   rZ   ©r   rj   rq   Úresultr   r   r   Ú__or__Ñ  s    
zHTTPHeaderDict.__or__c                 C  s.   t |ƒ}|d krtS t| ƒ|ƒ}| | ¡ |S r   )r,   rŽ   ro   rZ   r�   r   r   r   Ú__ror__Û  s    
zHTTPHeaderDict.__ror__)N)rf   )'r   r   r   rK   rL   r6   rB   r   rC   rW   rh   rr   rs   rD   rF   ru   r|   rZ   r&   Úoverloadr�   r    r!   r…   Ú
getheadersÚgetallmatchingheadersÚigetÚget_allr‰   rY   r‹   rQ   rp   r€   rV   r�   r’   r“   rM   r   r   r7   r   r   Î   sH   
!#!ÿ	
)"Ú
__future__r   r&   Úcollectionsr   Úenumr   r   Ú	threadingr   ÚTYPE_CHECKINGr   Ztyping_extensionsr   r	   Ú__all__ÚTypeVarr   r   r   ÚUnionr'   r   r)   r*   r}   r    r,   ÚGenericÚMutableMappingr   ÚSetrN   r   r   r   r   r   Ú<module>   s0   


ýÿ&] 0