o
    rN)j                     @   sl   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZ G dd dejZG dd dZdS )	    N)BytesIO)UnexpectedMessage)ASGIMessageResponseScopec                   @   s(   e Zd ZdZe Ze Ze ZdS )HTTPCycleStatea  
    The state of the ASGI `http` connection.
    * **REQUEST** - Initial state. The ASGI application instance will be run with the
    connection scope containing the `http` type.
    * **RESPONSE** - The `http.response.start` event has been sent by the application.
    The next expected message is the `http.response.body` event, containing the body
    content. An application may pass the `more_body` argument to send content in chunks,
    however content will always be returned in a single response, never streamed.
    * **COMPLETE** - The body content from the ASGI application has been completely
    read. A disconnect event will be sent to the application, and the response will
    be returned.
    N)	__name__
__module____qualname____doc__enumautoREQUESTRESPONSECOMPLETE r   r   v/var/www/html/finance.cargoinsureonline.com/_shared/backend-venv/lib/python3.10/site-packages/mangum/protocols/http.pyr   
   s
    r   c                   @   sf   e Zd ZdededdfddZdedefdd	Zdeddfd
dZ	de
fddZde
ddfddZdS )	HTTPCyclescopebodyreturnNc                 C   sD   || _ t | _tj| _td| _t	
 | _| jd|dd d S )Nzmangum.httpzhttp.requestFtyper   	more_body)r   r   bufferr   r   statelogging	getLoggerloggerasyncioQueue	app_queue
put_nowait)selfr   r   r   r   r   __init__   s   
zHTTPCycle.__init__appc                 C   s8   |  |}t }||}|| | j| j| jdS )N)statusheadersr   )runr    get_event_loopcreate_taskrun_until_completer'   r(   r   )r$   r&   asgi_instanceloop	asgi_taskr   r   r   __call__,   s   


zHTTPCycle.__call__c              	      s   z|| j | j| jI d H  W d S  tyY   | jd | jtju rA| ddddggdI d H  | ddd	d
I d H  Y d S | jtj	urVd| _
d| _ddgg| _Y d S Y d S w )Nz*An error occurred running the application.http.response.starti  s   content-types   text/plain; charset=utf-8)r   r'   r(   http.response.bodys   Internal Server ErrorFr   )r   receivesendBaseExceptionr   	exceptionr   r   r   r   r'   r   r(   )r$   r&   r   r   r   r)   8   s0   
zHTTPCycle.runc                    s   | j  I d H S )N)r"   get)r$   r   r   r   r3   Q   s   zHTTPCycle.receivemessagec                    s   | j tju r|d dkr|d | _|dg | _tj| _ d S | j tju rm|d dkrm|dd}|dd	}| j| |sk| j	 | _
| j  tj| _ | jdd
iI d H  | jd| jd | jd | j d S d S td|d  )Nr   r1   r'   r(   r2   r       r   Fzhttp.disconnectz%s %s %smethodpathzUnexpected )r   r   r   r'   r7   r(   r   r   writegetvaluer   closer   r"   putr   infor   r   )r$   r8   r   r   r   r   r   r4   T   s,   

zHTTPCycle.send)r	   r
   r   r   bytesr%   r   r   r0   r)   r   r3   r4   r   r   r   r   r      s    r   )r    r   r   ior   mangum.exceptionsr   mangum.typesr   r   r   r   Enumr   r   r   r   r   r   <module>   s    