o
    hH                     @   s   d dl 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
mZ ddlmZmZ dd	lmZ eeZG d
d deZG dd dZdS )    N   )AsyncioBackend)ConcurrencyBackend)	CertTypesTimeoutTypesVerifyTypes)AsyncRequestAsyncResponse)MessageLoggerASGIMiddleware
get_logger   )AsyncDispatcherc                   @   sn   e Zd ZdZ				ddejdeded	ejee	f d
e
ddfddZ			ddededededef
ddZdS )ASGIDispatcha  
    A custom dispatcher that handles sending requests directly to an ASGI app.

    The simplest way to use this functionality is to use the `app` argument.
    This will automatically infer if 'app' is a WSGI or an ASGI application,
    and will setup an appropriate dispatch class:

    ```
    client = httpx.Client(app=app)
    ```

    Alternatively, you can setup the dispatch instance explicitly.
    This allows you to include any additional configuration arguments specific
    to the ASGIDispatch class:

    ```
    dispatch = httpx.ASGIDispatch(
        app=app,
        root_path="/submount",
        client=("1.2.3.4", 123)
    )
    client = httpx.Client(dispatch=dispatch)
    ```

    Arguments:

    * `app` - The ASGI application.
    * `raise_app_exceptions` - Boolean indicating if exceptions in the application
       should be raised. Default to `True`. Can be set to `False` for use cases
       such as testing the content of a client 500 response.
    * `root_path` - The root path on which the ASGI application should be mounted.
    * `client` - A two-tuple indicating the client IP and port of incoming requests.
    ```
    T z	127.0.0.1{   Nappraise_app_exceptions	root_pathclientbackendreturnc                 C   s6   || _ || _|| _|| _|d u rt | _d S || _d S N)r   r   r   r   r   r   )selfr   r   r   r   r    r   [/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/httpx/dispatch/asgi.py__init__1   s
   zASGIDispatch.__init__requestverifycerttimeoutc                    sP  dddidj jjjjjjjjdjj
j	
j
d	t
jtd d d d 
j t
j dtffd	d
dtdd ffddd 	fdd}
j| I d H  I d H  d ur
jrI d H  d usJ dd usJ d
fdd}td |dS )Nhttpversionz3.0z1.1ascii)typeZasgihttp_versionmethodheadersschemepathZquery_stringserverr   r   )loggerr   c                     s@   z	   I d H } W n ty   dddd Y S w d| ddS )Nzhttp.request    F)r$   body	more_bodyT)	__anext__StopAsyncIteration)r-   )request_streamr   r   receive\   s   z"ASGIDispatch.send.<locals>.receivemessagec                    s   | d dkr| d |  dg    d S | d dkrC|  dd}|  dd	}|r8jd
kr8|I d H  |sE I d H  d S d S d S )Nr$   zhttp.response.startstatusr'   zhttp.response.bodyr-   r,   r.   FHEAD)getsetr&   putmark_as_done)r3   r-   r.   )r'   r   response_bodyresponse_started_or_failedstatus_coder   r   sendc   s   zASGIDispatch.send.<locals>.sendc               
      s   z<z I d H  W n t y! }  z| W Y d } ~ nd } ~ ww W  I d H    d S W  I d H    d S  I d H    w r   )	Exceptionr9   r7   )exc)r   app_excr2   r:   r;   scoper=   r   r   run_appu   s   
z"ASGIDispatch.send.<locals>.run_appz&application did not return a response.c                      s:     I d H   I d H   d urjr d S d S r   )draincloser   r   )r@   
backgroundr:   r   r   r   on_close   s   z#ASGIDispatch.send.<locals>.on_closezHTTP/1.1)r<   r%   r'   contentrF   r   r   N)r&   r'   rawurlr(   r)   queryencodehostr   r   r
   r   r+   r   Zcreate_eventBodyIteratorstreamdictZbackground_manager
__aenter__waitr   rD   r	   iterate)r   r   r   r   r    rB   rF   r   )r   r@   rE   r'   r2   r   r1   r:   r;   rA   r   r=   r<   r   r=   ?   sN   	

zASGIDispatch.send)Tr   r   N)NNN)__name__
__module____qualname____doc__typingCallableboolstrTupleintr   r   r   r   r   r   r	   r=   r   r   r   r   r      sB    &
r   c                   @   s\   e Zd ZdZdeddfddZdeje fddZ	dd	d
Z
deddfddZdddZdS )rN   zm
    Provides a byte-iterator interface that the client can use to
    ingest the response content from.
    r   r   Nc                 C   s   |j dd| _t | _d S )Nr   )max_size)Zcreate_queue_queueobject_done)r   r   r   r   r   r      s   zBodyIterator.__init__c                 C  s8   	 | j  I dH }|| ju rdS t|tsJ |V  q)zS
        A byte-iterator, used by the client to consume the response body.
        TN)r_   r6   ra   
isinstancebytesr   datar   r   r   rS      s   
zBodyIterator.iteratec                    s   |   2 z3 dH W }q6 dS )zl
        Drain any remaining body, in order to allow any blocked `put()` calls
        to complete.
        N)rS   )r   chunkr   r   r   rC      s   zBodyIterator.drainre   c                    s   | j |I dH  dS )zF
        Used by the server to add data to the response body.
        N)r_   r8   rd   r   r   r   r8      s   zBodyIterator.putc                    s   | j | jI dH  dS )zL
        Used by the server to signal the end of the response body.
        N)r_   r8   ra   )r   r   r   r   r9      s   zBodyIterator.mark_as_donerH   )rT   rU   rV   rW   r   r   rX   AsyncIteratorrc   rS   rC   r8   r9   r   r   r   r   rN      s    
rN   )rX   Zconcurrency.asyncior   Zconcurrency.baser   configr   r   r   modelsr   r	   utilsr
   r   baser   rT   r+   r   rN   r   r   r   r   <module>   s     