
    AҐiA                     $    d Z ddlZ G d d      Zy)zt
Async version of gunicorn/http/unreader.py for ASGI workers.

Provides async reading with pushback buffer support.
    Nc                   2    e Zd ZdZddZd	dZd Zd Zd Zy)
AsyncUnreaderzAsync socket reader with pushback buffer support.

    This class wraps an asyncio StreamReader and provides the ability
    to "unread" data back into a buffer for re-parsing.
    c                 R    || _         t        j                         | _        || _        y)zInitialize the async unreader.

        Args:
            reader: asyncio.StreamReader instance
            max_chunk: Maximum bytes to read at once
        N)readerioBytesIObuf	max_chunk)selfr   r
   s      O/var/www/descvideos/venv/lib/python3.12/site-packages/gunicorn/asgi/unreader.py__init__zAsyncUnreader.__init__   s     ::<"    Nc                   K   |t        |t              st        d      ||dk(  ry|dk  rd}| j                  j	                  dt
        j                         |O| j                  j                         r5| j                  j                         }t        j                         | _        |S || j                          d{   }|S | j                  j                         |k  r| j                          d{   }|s5| j                  j                         }t        j                         | _        |S | j                  j                  |       | j                  j                         |k  r| j                  j                         }t        j                         | _        | j                  j                  ||d        |d| S 7 7 ͭw)a  Read data from the stream, using buffered data first.

        Args:
            size: Number of bytes to read. If None, returns all buffered
                  data or reads a single chunk.

        Returns:
            bytes: Data read from buffer or stream
        Nz&size parameter must be an int or long.r   r   )
isinstanceint	TypeErrorr	   seekr   SEEK_ENDtellgetvaluer   _read_chunkwrite)r   sizeretchunkdatas        r   readzAsyncUnreader.read    sg     JtS$9DEEqyax 	a% <DHHMMO((##%Czz|DHJ<**,,EL hhmmo$**,,Ehh'')::<
HHNN5! hhmmo$ xx  "::<tDE{#ET{ -
 -s,   B=G?G 6G6G7A3G+AGGc                    K   	 | j                   j                  | j                         d{   S 7 # t        $ r Y yw xY ww)z0Read a chunk of data from the underlying stream.Nr   )r   r   r
   	Exception)r   s    r   r   zAsyncUnreader._read_chunkM   s;     	))$..9999 		s,   A(3 13 A3 	?A?Ac                     |rF| j                   j                  dt        j                         | j                   j	                  |       yy)zkPush data back into the buffer for re-reading.

        Args:
            data: bytes to push back
        r   N)r	   r   r   r   r   )r   r   s     r   unreadzAsyncUnreader.unreadT   s2     HHMM!R[[)HHNN4  r   c                     | j                   j                         }| j                   j                  dt        j                         | j                   j                         dkD  }| j                   j                  |       |S )z-Check if there's data in the pushback buffer.r   )r	   r   r   r   r   )r   poshas_datas      r   has_buffered_datazAsyncUnreader.has_buffered_data^   sP    hhmmoa%88==?Q&cr   )i    )N)	__name__
__module____qualname____doc__r   r   r   r!   r%    r   r   r   r      s!    	#+Z!r   r   )r)   r   r   r*   r   r   <module>r+      s   
 
V Vr   