a
    xd                     @   s   d dl 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 e
ee	eee f f Zeeee f Zee ee dddZee ee ddd	ZdeeeedddZdS )    )	b64encode)AnyListMutableMappingOptionalAnyStrSequenceUnionMapping)to_bytes
to_unicode)headers_rawreturnc                 C   s|   | du rdS |   }dd |D }i }|D ]L}t|dks<q*|d  }|d  }||v rl|| | q*|g||< q*|S )a  
    Convert raw headers (single multi-line bytestring)
    to a dictionary.

    For example:

    >>> import w3lib.http
    >>> w3lib.http.headers_raw_to_dict(b"Content-type: text/html\n\rAccept: gzip\n\n")   # doctest: +SKIP
    {'Content-type': ['text/html'], 'Accept': ['gzip']}

    Incorrect input:

    >>> w3lib.http.headers_raw_to_dict(b"Content-typt gzip\n\n")
    {}
    >>>

    Argument is ``None`` (return ``None``):

    >>> w3lib.http.headers_raw_to_dict(None)
    >>>

    Nc                 S   s   g | ]}| d dqS )   :   )split).0header r   B/var/www/html/Ranjet/env/lib/python3.9/site-packages/w3lib/http.py
<listcomp>$       z'headers_raw_to_dict.<locals>.<listcomp>   r   r   )
splitlineslenstripappend)r   headersZheaders_tuplesZresult_dictZheader_itemZitem_keyZ
item_valuer   r   r   headers_raw_to_dict	   s    r   )headers_dictr   c                 C   sx   | du rdS g }|   D ]T\}}t|tr@|d||g qt|ttfr|D ]}|d||g qRqd|S )a  
    Returns a raw HTTP headers representation of headers

    For example:

    >>> import w3lib.http
    >>> w3lib.http.headers_dict_to_raw({b'Content-type': b'text/html', b'Accept': b'gzip'}) # doctest: +SKIP
    'Content-type: text/html\\r\\nAccept: gzip'
    >>>

    Note that keys and values must be bytes.

    Argument is ``None`` (returns ``None``):

    >>> w3lib.http.headers_dict_to_raw(None)
    >>>

    Ns   : s   
)items
isinstancebytesr   joinlisttuple)r   Z	raw_lineskeyvaluevr   r   r   headers_dict_to_raw6   s    
r)   
ISO-8859-1)usernamepasswordencodingr   c                 C   s*   t |  dt | }dtt||d S )aA  
    Return an `Authorization` header field value for `HTTP Basic Access Authentication (RFC 2617)`_

    >>> import w3lib.http
    >>> w3lib.http.basic_auth_header('someuser', 'somepass')
    'Basic c29tZXVzZXI6c29tZXBhc3M='

    .. _HTTP Basic Access Authentication (RFC 2617): http://www.ietf.org/rfc/rfc2617.txt

    :s   Basic )r-   )r   r   r   )r+   r,   r-   authr   r   r   basic_auth_headerV   s    r0   N)r*   )base64r   typingr   r   r   r   r   r   r	   r
   Z
w3lib.utilr   r   r"   ZHeadersDictInputZHeadersDictOutputr   r)   strr0   r   r   r   r   <module>   s   (-! 