U
    
W['   ã                   @   sN   d Z ddlmZmZ ddlmZmZmZ dd„ ZeG dd„ de	ƒƒZ
dgZdS )	z2
An API for storing HTTP header names and values.
é    )ÚdivisionÚabsolute_import)Ú
comparableÚcmpÚunicodec                 C   s   d  dd„ |  d¡D ƒ¡S )a  
    Return a byte string which is capitalized using '-' as a word separator.

    @param name: The name of the header to capitalize.
    @type name: L{bytes}

    @return: The given header capitalized using '-' as a word separator.
    @rtype: L{bytes}
    ó   -c                 S   s   g | ]}|  ¡ ‘qS © )Ú
capitalize)Ú.0Zwordr   r   ú:/usr/lib/python3/dist-packages/twisted/web/http_headers.pyÚ
<listcomp>   s     z#_dashCapitalize.<locals>.<listcomp>)ÚjoinÚsplit)Únamer   r   r   Ú_dashCapitalize   s    
r   c                   @   s    e Zd ZdZdddddddd	œZd)dd„Zdd„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd*d#d$„Zd%d&„ Zd'd(„ Zd
S )+ÚHeadersaŸ  
    Stores HTTP headers in a key and multiple value format.

    Most methods accept L{bytes} and L{unicode}, with an internal L{bytes}
    representation. When passed L{unicode}, header names (e.g. 'Content-Type')
    are encoded using ISO-8859-1 and header values (e.g.
    'text/html;charset=utf-8') are encoded using UTF-8. Some methods that return
    values will return them in the same type as the name given.

    If the header keys or values cannot be encoded or decoded using the rules
    above, using just L{bytes} arguments to the methods of this class will
    ensure no decoding or encoding is done, and L{Headers} will treat the keys
    and values as opaque byte strings.

    @cvar _caseMappings: A L{dict} that maps lowercase header names
        to their canonicalized representation.

    @ivar _rawHeaders: A L{dict} mapping header names as L{bytes} to L{list}s of
        header values as L{bytes}.
    s   Content-MD5s   DNTs   ETags   P3Ps   TEs   WWW-Authenticates   X-XSS-Protection)s   content-md5s   dnts   etags   p3ps   tes   www-authenticates   x-xss-protectionNc                 C   s0   i | _ |d k	r,| ¡ D ]\}}|  ||¡ qd S )N)Ú_rawHeadersÚitemsÚsetRawHeaders)ÚselfZ
rawHeadersr   Úvaluesr   r   r   Ú__init__;   s    zHeaders.__init__c                 C   s   d| j j| jf S )zR
        Return a string fully describing the headers set on this object.
        z%s(%r))Ú	__class__Ú__name__r   ©r   r   r   r   Ú__repr__B   s    zHeaders.__repr__c                 C   s,   t |tƒr(tt| j ¡ ƒt|j ¡ ƒƒS tS )zu
        Define L{Headers} instances as being equal to each other if they have
        the same raw headers.
        )Ú
isinstancer   r   Úsortedr   r   ÚNotImplemented)r   Úotherr   r   r   Ú__cmp__I   s    
þzHeaders.__cmp__c                 C   s    t |tƒr| ¡  d¡S | ¡ S )a"  
        Encode the name of a header (eg 'Content-Type') to an ISO-8859-1 encoded
        bytestring if required.

        @param name: A HTTP header name
        @type name: L{unicode} or L{bytes}

        @return: C{name}, encoded if required, lowercased
        @rtype: L{bytes}
        z
iso-8859-1)r   r   ÚlowerÚencode©r   r   r   r   r   Ú_encodeNameU   s    
zHeaders._encodeNamec                 C   s   t |tƒr| d¡S |S )a  
        Encode a single header value to a UTF-8 encoded bytestring if required.

        @param value: A single HTTP header value.
        @type value: L{bytes} or L{unicode}

        @return: C{value}, encoded if required
        @rtype: L{bytes}
        Úutf8)r   r   r"   )r   Úvaluer   r   r   Ú_encodeValuee   s    


zHeaders._encodeValuec                 C   s"   g }|D ]}|  |  |¡¡ q|S )aZ  
        Encode a L{list} of header values to a L{list} of UTF-8 encoded
        bytestrings if required.

        @param values: A list of HTTP header values.
        @type values: L{list} of L{bytes} or L{unicode} (mixed types allowed)

        @return: C{values}, with each item encoded if required
        @rtype: L{list} of L{bytes}
        )Úappendr'   ©r   r   Z	newValuesr&   r   r   r   Ú_encodeValuest   s    zHeaders._encodeValuesc                 C   s"   g }|D ]}|  | d¡¡ q|S )a  
        Decode a L{list} of header values into a L{list} of Unicode strings.

        @param values: A list of HTTP header values.
        @type values: L{list} of UTF-8 encoded L{bytes}

        @return: C{values}, with each item decoded
        @rtype: L{list} of L{unicode}
        r%   )r(   Údecoder)   r   r   r   Ú_decodeValues†   s    
zHeaders._decodeValuesc                 C   s   |   | j¡S )zg
        Return a copy of itself with the same headers set.

        @return: A new L{Headers}
        )r   r   r   r   r   r   Úcopy—   s    zHeaders.copyc                 C   s   |   |¡| jkS )a  
        Check for the existence of a given header.

        @type name: L{bytes} or L{unicode}
        @param name: The name of the HTTP header to check for.

        @rtype: L{bool}
        @return: C{True} if the header exists, otherwise C{False}.
        )r$   r   r#   r   r   r   Ú	hasHeader    s    
zHeaders.hasHeaderc                 C   s   | j  |  |¡d¡ dS )zÄ
        Remove the named header from this header object.

        @type name: L{bytes} or L{unicode}
        @param name: The name of the HTTP header to remove.

        @return: L{None}
        N)r   Úpopr$   r#   r   r   r   ÚremoveHeader­   s    	zHeaders.removeHeaderc                 C   s<   t |tƒstd|t|ƒf ƒ‚|  |¡}|  |¡| j|< dS )aw  
        Sets the raw representation of the given header.

        @type name: L{bytes} or L{unicode}
        @param name: The name of the HTTP header to set the values for.

        @type values: L{list} of L{bytes} or L{unicode} strings
        @param values: A list of strings each one being a header value of
            the given name.

        @return: L{None}
        z?Header entry %r should be list but found instance of %r insteadN)r   ÚlistÚ	TypeErrorÚtyper$   r*   r   )r   r   r   r   r   r   r   ¹   s    

ÿ
zHeaders.setRawHeadersc                 C   s4   |   |¡}|dk	r| |¡ n|g}|  ||¡ dS )a  
        Add a new raw value for the given header.

        @type name: L{bytes} or L{unicode}
        @param name: The name of the header for which to set the value.

        @type value: L{bytes} or L{unicode}
        @param value: The value to set for the named header.
        N)ÚgetRawHeadersr(   r   )r   r   r&   r   r   r   r   ÚaddRawHeaderÎ   s
    

zHeaders.addRawHeaderc                 C   s8   |   |¡}| j ||¡}t|tƒr4||k	r4|  |¡S |S )a  
        Returns a list of headers matching the given name as the raw string
        given.

        @type name: L{bytes} or L{unicode}
        @param name: The name of the HTTP header to get the values of.

        @param default: The value to return if no header with the given C{name}
            exists.

        @rtype: L{list} of strings, same type as C{name} (except when
            C{default} is returned).
        @return: If the named header is present, a L{list} of its
            values.  Otherwise, C{default}.
        )r$   r   Úgetr   r   r,   )r   r   ÚdefaultZencodedNamer   r   r   r   r4   â   s
    

zHeaders.getRawHeadersc                 c   s(   | j  ¡ D ]\}}|  |¡|fV  q
dS )zµ
        Return an iterator of key, value pairs of all headers contained in this
        object, as L{bytes}.  The keys are capitalized in canonical
        capitalization.
        N)r   r   Ú_canonicalNameCaps)r   ÚkÚvr   r   r   ÚgetAllRawHeadersú   s    zHeaders.getAllRawHeadersc                 C   s   | j  |t|ƒ¡S )a  
        Return the canonical name for the given header.

        @type name: L{bytes}
        @param name: The all-lowercase header name to capitalize in its
            canonical form.

        @rtype: L{bytes}
        @return: The canonical name of the header.
        )Ú_caseMappingsr6   r   r#   r   r   r   r8     s    zHeaders._canonicalNameCaps)N)N)r   Ú
__module__Ú__qualname__Ú__doc__r<   r   r   r    r$   r'   r*   r,   r-   r.   r0   r   r5   r4   r;   r8   r   r   r   r   r      s0   ù	
	

r   N)r?   Z
__future__r   r   Ztwisted.python.compatr   r   r   r   Úobjectr   Ú__all__r   r   r   r   Ú<module>   s    w