
    [i                     $    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                   D    e Zd ZdZddZd Zd Zd ZddZd Z	d	 Z
d
 Zy)AsyncUnreadera8  Async 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.

    Performance optimization: Reuses BytesIO buffer with truncate/seek
    instead of creating new objects to reduce GC pressure.
    c                 `    || _         t        j                         | _        || _        d| _        y)zInitialize the async unreader.

        Args:
            reader: asyncio.StreamReader instance
            max_chunk: Maximum bytes to read at once
        r   N)readerioBytesIObuf	max_chunk
_buf_start)selfr   r
   s      g/var/www/html/userprofiledev.eatanceapp.com/venv/lib/python3.12/site-packages/gunicorn/asgi/unreader.py__init__zAsyncUnreader.__init__   s&     ::<"    c                 ~    | j                   j                  d       | j                   j                  d       d| _        y)z7Reset buffer for reuse instead of creating new BytesIO.r   N)r	   seektruncater   r   s    r   _reset_bufferzAsyncUnreader._reset_buffer$   s+    a!r   c                     | j                   j                  | j                         | j                   j                         }| j	                          |S )z'Get all buffered data and reset buffer.)r	   r   r   readr   )r   datas     r   _get_buffered_dataz AsyncUnreader._get_buffered_data*   s6    doo&xx}}r   c                 t    | j                   j                  dt        j                        }|| j                  z
  S )zGet size of buffered data.r   )r	   r   r   SEEK_ENDr   )r   ends     r   _buffer_sizezAsyncUnreader._buffer_size1   s)    hhmmAr{{+T__$$r   Nc                 ^  K   |t        |t              st        d      ||dk(  ry|dk  rd}| j                         }||dkD  r| j	                         S || j                          d{   }|S ||k  r| j                          d{   }|s| j	                         S | j                  j                  dt        j                         | j                  j                  |       |t        |      z  }||k  r| j                  j                  | j                         | j                  j                  |      }| xj                  |z  c_        | j                  dkD  rG| j                  j                         }| j                          |r| j                  j                  |       |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   r   _read_chunkr	   r   r   r   writelenr   r   r   )r   sizebuf_sizechunkr   	remainings         r   r   zAsyncUnreader.read6   sq     JtS$9DEEqyax$$& <HqL**,,<**,,EL o**,,E..00HHMM!R[[)HHNN5!E
"H o 	doo&xx}}T" 	4 ??T!I y)5 -
 -s,   A)F-+F',F-
F*A.F-:B.F-*F-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
   	Exceptionr   s    r   r"   zAsyncUnreader._read_chunkk   s;     	))$..9999 		s,   A(3 13 A3 	?A?Ac                    |r| j                   j                  | j                         | j                   j                         }| j	                          | j                   j                  |       |r| j                   j                  |       yyy)zPush data back into the buffer for re-reading.

        Args:
            data: bytes to push back

        Note: This prepends data to the buffer so it will be read first.
        N)r	   r   r   r   r   r#   )r   r   existings      r   unreadzAsyncUnreader.unreadr   se     HHMM$//*xx}}H  HHNN4 x(  r   c                 (    | j                         dkD  S )z-Check if there's data in the pushback buffer.r   )r   r   s    r   has_buffered_datazAsyncUnreader.has_buffered_data   s      "Q&&r   )r   )N)__name__
__module____qualname____doc__r   r   r   r   r   r"   r-   r/    r   r   r   r      s0    
%
3j)&'r   r   )r3   r   r   r4   r   r   <module>r5      s   
 
y' y'r   