
    Vhy"                        d 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mZmZmZmZ ddlmZmZ ddlmZ  G d de      Z ed      Zdd	Ze G d
 d             Z G d d      Z e       Z	 dgZy)z2
An API for storing HTTP header names and values.
    )annotations)AnyStrClassVarDictIteratorListMappingOptionalSequenceTupleTypeVarUnionoverload)cmp
comparable)_istokenc                      e Zd ZdZy)InvalidHeaderNamezE
    HTTP header names must be tokens, per RFC 9110 section 5.1.
    N)__name__
__module____qualname____doc__     H/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/http_headers.pyr   r      s    r   r   _Tc                @    dj                  | j                               S )z
    Replace linear whitespace (C{\n}, C{\r\n}, C{\r}) in a header
    value with a single space.

    @param headerComponent: The header value to sanitize.

    @return: The sanitized header value.
        )join
splitlines)headerComponents    r   _sanitizeLinearWhitespacer"   &   s     99_//122r   c                      e Zd ZdZdgZ	 d	 	 	 ddZddZd ZddZddZ	dd	Z
	 	 	 	 	 	 dd
ZddZedd       Zedd       Z	 d	 	 	 	 	 ddZddZy)Headersa  
    Stores HTTP headers in a key and multiple value format.

    When passed L{str}, 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.

    @ivar _rawHeaders: A L{dict} mapping header names as L{bytes} to L{list}s of
        header values as L{bytes}.
    _rawHeadersNc                l    i | _         |+|j                         D ]  \  }}| j                  ||        y y N)r%   itemssetRawHeaders)self
rawHeadersnamevaluess       r   __init__zHeaders.__init__G   sC     68! * 0 0 2 1f""401 "r   c                b    dj                  | j                  j                  | j                        S )zR
        Return a string fully describing the headers set on this object.
        z{}({!r}))format	__class__r   r%   r*   s    r   __repr__zHeaders.__repr__P   s-       NN##
 	
r   c                    t        |t              rNt        t        | j                  j                               t        |j                  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*   others     r   __cmp__zHeaders.__cmp__Y   sP    
 eW%t''--/0&9J9J9P9P9R2S  r   c                8    | j                  | j                        S )zg
        Return a copy of itself with the same headers set.

        @return: A new L{Headers}
        )r1   r%   r2   s    r   copyzHeaders.copyd   s     ~~d..//r   c                D    t         j                  |      | j                  v S )z
        Check for the existence of a given header.

        @param name: The name of the HTTP header to check for.

        @return: C{True} if the header exists, otherwise C{False}.
        )_nameEncoderencoder%   r*   r,   s     r   	hasHeaderzHeaders.hasHeadern   s      ""4(D,<,<<<r   c                b    | j                   j                  t        j                  |      d       y)z
        Remove the named header from this header object.

        @param name: The name of the HTTP header to remove.

        @return: L{None}
        N)r%   popr=   r>   r?   s     r   removeHeaderzHeaders.removeHeaderx   s%     	\006=r   c                    t         j                  |      }g }|D ]@  }t        |t              r|j                  d      }n|}|j	                  t        |             B || j                  |<   y)a  
        Sets the raw representation of the given header.

        @param name: The name of the HTTP header to set the values for.

        @param values: A list of strings each one being a header value of
            the given name.

        @raise TypeError: Raised if C{values} is not a sequence of L{bytes}
            or L{str}, or if C{name} is not L{bytes} or L{str}.

        @return: L{None}
        utf8N)r=   r>   r5   strappendr"   r%   )r*   r,   r-   _nameencodedValuesv_vs          r   r)   zHeaders.setRawHeaders   sm      ##D)%' 	@A!S!XXf%  !:2!>?	@ #0r   c                    | j                   j                  t        j                  |      g       j	                  t        t        |t              r|j                  d                   y|             y)z
        Add a new raw value for the given header.

        @param name: The name of the header for which to set the value.

        @param value: The value to set for the named header.
        rE   N)r%   
setdefaultr=   r>   rG   r"   r5   rF   )r*   r,   values      r   addRawHeaderzHeaders.addRawHeader   sZ     	##L$7$7$=rBII%(25#(>V$	
DI	
r   c                     y r'   r   r?   s     r   getRawHeaderszHeaders.getRawHeaders       r   c                     y r'   r   )r*   r,   defaults      r   rQ   zHeaders.getRawHeaders   rR   r   c                    t         j                  |      }| j                  j                  |g       }|s|S t	        |t
              r|D cg c]  }|j                  d       c}S |S c c}w )a  
        Returns a sequence of headers matching the given name as the raw string
        given.

        @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.

        @return: If the named header is present, a sequence of its
            values.  Otherwise, C{default}.
        rE   )r=   r>   r%   getr5   rF   decode)r*   r,   rT   encodedNamer-   rJ   s         r   rQ   zHeaders.getRawHeaders   sb     #))$/!!%%k26NdC .45AHHV$55 6s   
A'c                H    t        | j                  j                               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.
        )iterr%   r(   r2   s    r   getAllRawHeaderszHeaders.getAllRawHeaders   s     D$$**,--r   r'   )r+   z+Optional[Mapping[AnyStr, Sequence[AnyStr]]]returnNone)r\   rF   )r\   r$   )r,   r   r\   bool)r,   r   r\   r]   )r,   Union[str, bytes]r-   zSequence[Union[str, bytes]]r\   r]   )r,   r_   rN   r_   r\   r]   )r,   r   r\   zOptional[Sequence[AnyStr]])r,   r   rT   r   r\   zUnion[Sequence[AnyStr], _T])r,   r   rT   zOptional[_T]r\   z%Union[Sequence[AnyStr], Optional[_T]])r\   z'Iterator[Tuple[bytes, Sequence[bytes]]])r   r   r   r   	__slots__r.   r3   r9   r;   r@   rC   r)   rO   r   rQ   r[   r   r   r   r$   r$   2   s    " I CG1?1 
1
	0=>0%0/J0	06
     59%1	.0.r   r$   c                  Z    e Zd ZU dZdZded<   ddddd	d
ddZded<   dZded<   d ZddZ	y)_NameEncodera  
    C{_NameEncoder} converts HTTP header names to L{bytes} and canonicalizies
    their capitalization.

    @cvar _caseMappings: A L{dict} that maps conventionally-capitalized
        header names to their canonicalized representation, for headers with
        unconventional capitalization.

    @cvar _canonicalHeaderCache: A L{dict} that maps header names to their
        canonicalized representation.
    _canonicalHeaderCachezDict[Union[bytes, str], bytes]rd   s   Content-MD5s   DNTs   ETags   P3Ps   TEs   WWW-Authenticates   X-XSS-Protection)s   Content-Md5s   Dnts   Etags   P3ps   Tes   Www-Authenticates   X-Xss-ProtectionzClassVar[Dict[bytes, bytes]]_caseMappingsi'  zClassVar[int]_MAX_CACHED_HEADERSc                    i | _         y r'   rc   r2   s    r   r.   z_NameEncoder.__init__   s
    %'"r   c                   | j                   j                  |      x}r|S t        |t              r|j	                  d      n|}t        |      st        |      dj                  |j                  d      D cg c]  }|j                          c}      }|| j                  v r| j                  |   }t        | j                         | j                  k  r|| j                   |<   |S c c}w )a  
        Encode the name of a header (eg 'Content-Type') to an ISO-8859-1
        bytestring if required. It will be canonicalized to Http-Header-Case.

        @raises InvalidHeaderName:
            If the header name contains invalid characters like whitespace
            or NUL.

        @param name: An HTTP header name

        @return: C{name}, encoded if required, in Header-Case
        z
iso-8859-1   -)rd   rV   r5   rF   r>   r   r   r   split
capitalizere   lenrf   )r*   r,   canonicalName
bytes_namewordresults         r   r>   z_NameEncoder.encode   s     !66::4@@=@  2<T32GT[[.T

##J//*:J:J4:PQ$DOO-QR T'''''/F t))*T-E-EE/5D&&t, Rs   7C%N)r,   r_   r\   bytes)
r   r   r   r   r`   __annotations__re   rf   r.   r>   r   r   r   rb   rb      sQ    
 +I99 '003M/  *0/(#r   rb   N)r!   rq   r\   rq   )r   
__future__r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   twisted.python.compatr   r   twisted.web._abnfr   
ValueErrorr   r   r"   r$   rb   r=   __all__r   r   r   <module>ry      s   
 #    2 &
  T]	3 ^. ^. ^.BB BJ ~
 +r   