o
    ߥi                     @   s   d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	 d dl
mZmZmZmZmZmZ d dl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 d d	lmZ d
dl m!Z!m"Z" G dd de#Z$G dd dee%ef eZ&G dd de&Z'dS )    N)AnyAsyncIteratorDictIteratorMapping)	parse_qsl)URLAddressFormDataHeadersQueryParams
UploadFile)MalformedJSONMalformedMultipartUnsupportedMediaType)parse_async_stream)MoreInfoFromHeaderMixin)ReceiveScopeSend)cached_property   )empty_receive
empty_sendc                   @   s   e Zd ZdZdS )ClientDisconnectz'
    HTTP connection disconnected.
    N)__name__
__module____qualname____doc__ r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/baize/asgi/requests.pyr      s    r   c                	   @   s   e Zd ZdZeefdedededdfddZ	d	e
defd
dZdee
 fddZdefddZdedefddZedefddZedefddZedee
ef fddZedefddZedefddZdS )HTTPConnectionz
    A base class for incoming HTTP connections.

    It is a valid Mapping type that allows you to directly
    access the values in any ASGI `scope` dictionary.
    scopereceivesendreturnNc                 C   s   || _ || _|| _d S N)_scope_send_receiveselfr"   r#   r$   r   r   r    __init__%   s   
zHTTPConnection.__init__keyc                 C   s
   | j | S r&   )r'   )r+   r-   r   r   r    __getitem__,      
zHTTPConnection.__getitem__c                 C   
   t | jS r&   )iterr'   r+   r   r   r    __iter__/   r/   zHTTPConnection.__iter__c                 C   r0   r&   )lenr'   r2   r   r   r    __len__2   r/   zHTTPConnection.__len__otherc                 C   s4   t || jstS | j|jko| j|jko| j|jkS r&   )
isinstance	__class__NotImplementedr'   r(   r)   )r+   r6   r   r   r    __eq__5   s   

zHTTPConnection.__eq__c                 C   s   |  dpd\}}t||dS )z
        Client's IP and Port.

        Note that this depends on the "client" value given by
        the ASGI Server, and is not necessarily accurate.
        client)NN)hostport)getr	   )r+   r<   r=   r   r   r    r;   >   s   zHTTPConnection.clientc                 C   s   t | jdS )z/
        The full URL of this request.
        )r"   )r   r'   r2   r   r   r    urlI      zHTTPConnection.urlc                 C   s   |  di S )z>
        The path parameters parsed by the framework.
        path_params)r>   r2   r   r   r    rA   P   r@   zHTTPConnection.path_paramsc                 C   s   t | d S )z?
        Query parameter. It is a multi-value mapping.
        query_string)r   r2   r   r   r    query_paramsW   r@   zHTTPConnection.query_paramsc                 C   s   t dd | jd D S )z
        A read-only case-independent mapping.

        Note that in its internal storage, all keys are in lower case.
        c                 s   s(    | ]\}}| d | d fV  qdS )latin-1N)decode).0r-   valuer   r   r    	<genexpr>e   s
    
z)HTTPConnection.headers.<locals>.<genexpr>headers)r   r'   r2   r   r   r    rI   ^   s   zHTTPConnection.headers)r   r   r   r   r   r   r   r   r   r,   strr   r.   r   r3   intr5   boolr:   r   r	   r;   r   r?   r   rA   r   rC   r   rI   r   r   r   r    r!      s4    
	
r!   c                	       s   e Zd Zeefdedededdf fddZe	de
fdd	Zdee fd
dZedefddZedefddZdede
defddZedefddZdddZdefddZ  ZS )Requestr"   r#   r$   r%   Nc                    s0   |d dksJ t  ||| d| _d| _d S )NtypehttpF)superr,   _stream_consumed_is_disconnectedr*   r8   r   r    r,   l   s   
zRequest.__init__c                 C   s   | d S )z0
        HTTP method. Uppercase string.
        methodr   r2   r   r   r    rT   t   s   zRequest.methodc                 C  s   d| j v r| j d  r| jI dH V  dV  dS | jr tdd| _	 |  I dH }|d dkrD|dd}|r<|V  |dd	sCnn|d d
krPd| _t q$dV  dS )a(  
        Streaming read request body. e.g. `async for chunk in request.stream(): ...`

        If you access `.stream()` then the byte chunks are provided
        without storing the entire body to memory. Any subsequent
        calls to `.body`, `.form`, or `.json` will raise an error.
        bodyN    zStream consumedTrN   zhttp.request	more_bodyFhttp.disconnect)	__dict__donerU   rQ   RuntimeErrorr)   r>   rR   r   )r+   messagerU   r   r   r    stream{   s,   
zRequest.streamc                    s    d dd |  2 I dH S )zZ
        Read all the contents of the request body into the memory and return it.
        rV   c                    s   g | z3 d H W }|q6 S r&   r   )rF   chunkr   r   r    
<listcomp>   s    z Request.body.<locals>.<listcomp>N)joinr]   r2   r   r   r    rU      s   zRequest.bodyc              
      sh   | j dkr0| jI dH }zt|| j jddW S  tjy/ } ztt	|dd}~ww t
d)z
        Call `await self.body` and use `json.loads` parse it.

        If `content_type` is not equal to `application/json`,
        an HTTPExcption exception will be thrown.
        zapplication/jsonNcharsetutf8)content_typerU   jsonloadsrE   optionsr>   JSONDecodeErrorr   rJ   r   )r+   dataexcr   r   r    rd      s   
zRequest.jsonboundaryra   c                    s    t t|  ||tdI d H S )N)file_factory)r
   parse_multipartr]   r   )r+   rj   ra   r   r   r    _parse_multipart   s   
zRequest._parse_multipartc                    s   | j dkr*| j jdd}d| j jvrtd| j jd d}| ||I dH S | j dkrG| jI dH j| j jddd	}tt	|d
dS t
d)a  
        Parse the data in the form format and return it as a multi-value mapping.

        If `content_type` is equal to `multipart/form-data`, it will directly
        perform streaming analysis, and subsequent calls to `self.body`
        or `self.json` will raise errors.

        If `content_type` is not equal to `multipart/form-data` or
        `application/x-www-form-urlencoded`, an HTTPExcption exception will be thrown.
        zmultipart/form-datara   rb   rj   z'Missing boundary in header content-typerD   Nz!application/x-www-form-urlencoded)encodingT)keep_blank_valuesz6multipart/form-data, application/x-www-form-urlencoded)rc   rf   r>   r   encoderm   rU   rE   r
   r   r   )r+   ra   rj   rU   r   r   r    form   s   

zRequest.formc                    s<   d| j v r| j d  r| jI dH  I dH  dS dS dS )z
        Close all temporary files in the `self.form`.

        This can always be called, regardless of whether you use form or not.
        rq   N)rY   rZ   rq   acloser2   r   r   r    close   s   zRequest.closec                    sX   | j s)ztj|  ddI dH }|ddk| _ W | j S  tjy(   Y | j S w | j S )zU
        The method used to determine whether the connection is interrupted.
        gHz>)timeoutNrN   rX   )rR   asynciowait_forr)   r>   TimeoutError)r+   r\   r   r   r    is_disconnected   s   zRequest.is_disconnected)r%   N)r   r   r   r   r   r   r   r   r,   propertyrJ   rT   r   bytesr]   r   rU   r   rd   r
   rm   rq   rs   rL   rx   __classcell__r   r   rS   r    rM   k   s.    
	rM   )(ru   rd   typingr   r   r   r   r   urllib.parser   baize.datastructuresr   r	   r
   r   r   r   baize.exceptionsr   r   r   baize.multipart_helperr   rl   baize.requestsr   baize.typingr   r   r   baize.utilsr   helperr   r   	Exceptionr   rJ   r!   rM   r   r   r   r    <module>   s     N