U
    CŠ]H  ã                   @   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ejeeejee	f e
ddœdd„Zdeeeeed	œ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   s0   || _ || _|| _|| _|d kr&tƒ n|| _d S ©N)r   r   r   r   r   r   )Úselfr   r   r   r   r   © r   ús/var/www/html/staging.mfahmagazine.net/magazine_api/magazine_env/lib/python3.8/site-packages/httpx/dispatch/asgi.pyÚ__init__1   s
    zASGIDispatch.__init__)ÚrequestÚverifyÚcertÚtimeoutr   c                 ƒ   s\  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ƒ‰ˆ ¡ ‰tdœ‡fd	d
„‰td dœ‡‡‡‡‡fdd„‰d dœ‡ ‡‡‡‡‡	‡fdd„}ˆ
j |¡ ¡ I d H ‰ˆ ¡ I d H  ˆd k	�rˆ
j�rˆ ˆ¡I d H  ˆ‚ˆd k	�stdƒ‚ˆd k	�s,t‚d 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   )Úlogger©r   c                  “   s@   zˆ   ¡ I d H } W n  tk
r2   ddddœ Y S X 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)Úmessager   c                 “   s‚   | d dkr*| d ‰|   dg ¡‰ ˆ ¡  nT| d dkr~|   dd¡}|   dd	¡}|rlˆjd
krlˆ |¡I d H  |s~ˆ ¡ I d H  d S )Nr$   zhttp.response.startÚstatusr'   zhttp.response.bodyr.   r-   r/   FZHEAD)ÚgetÚsetr&   ÚputÚmark_as_done)r4   r.   r/   )r'   r   Úresponse_bodyÚresponse_started_or_failedÚstatus_coder   r   Úsendc   s    
zASGIDispatch.send.<locals>.sendc               
   “   s`   zBzˆ ˆˆˆƒI d H  W n& tk
r> }  z| ‰W 5 d } ~ X Y nX W 5 ˆ  ¡ I d H  ˆ ¡  X d S r   )r9   r7   Ú	Exception)Úexc)r   Úapp_excr3   r:   r;   Úscoper=   r   r   Úrun_appu   s    z"ASGIDispatch.send.<locals>.run_appz&application did not return a response.c                   “   s4   ˆ  ¡ I d H  ˆ ˆ ¡I d H  ˆ d k	r0ˆjr0ˆ ‚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&   r'   ÚrawÚurlr(   r)   ÚqueryÚencodeÚhostr   r   r
   r   r+   r   Zcreate_eventÚBodyIteratorÚstreamÚdictZbackground_managerÚ
__aenter__Úwaitr   rD   ÚAssertionErrorr	   Úiterate)r   r   r   r   r    rB   rF   r   )r   r@   rE   r'   r3   r   r2   r:   r;   rA   r   r=   r<   r   r=   ?   sL    	õ

úzASGIDispatch.send)Tr   r   N)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtypingÚCallableÚboolÚstrZTupleÚintr   r   r   r   r   r   r	   r=   r   r   r   r   r      s.   &    úù   ûúr   c                   @   s`   e Zd ZdZeddœdd„Zeje dœdd„Z	ddœd	d
„Z
eddœdd„Zddœdd„ZdS )rM   zm
    Provides a byte-iterator interface that the client can use to
    ingest the response content from.
    N)r   r   c                 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__r,   c                 C  s6   | j  ¡ I dH }|| jkrq2t|tƒs*t‚|V  q dS )zS
        A byte-iterator, used by the client to consume the response body.
        N)r^   r6   r`   Ú
isinstanceÚbytesrR   ©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.drain)rd   r   c                 Ã   s   | j  |¡I dH  dS )zF
        Used by the server to add data to the response body.
        N)r^   r8   rc   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   r`   )r   r   r   r   r9   Â   s    zBodyIterator.mark_as_done)rT   rU   rV   rW   r   r   rX   ÚAsyncIteratorrb   rS   rC   r8   r9   r   r   r   r   rM   Ÿ   s   rM   )rX   Zconcurrency.asyncior   Zconcurrency.baser   Úconfigr   r   r   Zmodelsr   r	   Úutilsr
   r   Úbaser   rT   r+   r   rM   r   r   r   r   Ú<module>   s    