U
    9Š]T  ã                   @   sf   d dl Z d dlZddlmZmZmZ ddlmZmZ ddl	m
Z
 G dd„ de
ƒZG d	d
„ d
e jƒZdS )é    Né   )Ú	CertTypesÚTimeoutTypesÚVerifyTypes)ÚRequestÚResponseé   )Ú
Dispatcherc                   @   sB   e Zd ZdZdejeeeddœdd„Zde	e
eeed	œd
d„ZdS )ÚWSGIDispatchav  
    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 WSGIDispatch class:

    ```
    dispatch = httpx.WSGIDispatch(
        app=app,
        script_name="/submount",
        remote_addr="1.2.3.4"
    )
    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.
    * `script_name` - The root path on which the ASGI application should be mounted.
    * `remote_addr` - A string indicating the client IP of incoming requests.
    ```
    TÚ ú	127.0.0.1N)ÚappÚraise_app_exceptionsÚscript_nameÚremote_addrÚreturnc                 C   s   || _ || _|| _|| _d S ©N)r   r   r   r   )Úselfr   r   r   r   © r   ús/var/www/html/staging.mfahmagazine.net/magazine_api/magazine_env/lib/python3.8/site-packages/httpx/dispatch/wsgi.pyÚ__init__-   s    zWSGIDispatch.__init__)ÚrequestÚverifyÚcertÚtimeoutr   c           
         s  d|j jt| ¡ ƒt ¡ ddd|j| j|j j|j j	|j j
t|j jƒ| jdœ}|j ¡ D ]0\}}| ¡  dd¡}|dkr~d| }|||< qVd ‰d ‰d ‰ dtttjd d	œ‡ ‡‡fd
d„}|  ||¡}	ˆd k	sÌt‚ˆd k	sØt‚ˆ rê| jrêˆ d ‚ttˆ ¡ d ƒdˆdd„ |	D ƒt|	dd ƒd�S )N)r   r   TF)zwsgi.versionzwsgi.url_schemez
wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocesszwsgi.run_onceZREQUEST_METHODZSCRIPT_NAMEZ	PATH_INFOZQUERY_STRINGZSERVER_NAMEZSERVER_PORTZREMOTE_ADDRú-Ú_)ZCONTENT_TYPEZCONTENT_LENGTHZHTTP_)ÚstatusÚresponse_headersÚexc_infor   c                    s   | ‰|‰|‰ d S r   r   )r   r   r   ©Zseen_exc_infoZseen_response_headersZseen_statusr   r   Ústart_responseZ   s    z)WSGIDispatch.send.<locals>.start_responser   r   zHTTP/1.1c                 s   s   | ]
}|V  qd S r   r   )Ú.0Úchunkr   r   r   Ú	<genexpr>m   s     z$WSGIDispatch.send.<locals>.<genexpr>Úclose)Zstatus_codeZhttp_versionÚheadersÚcontentZon_close)N)ÚurlÚschemeÚ
BodyStreamÚstreamÚioÚBytesIOÚmethodr   ÚpathÚqueryÚhostÚstrÚportr   r&   ÚitemsÚupperÚreplaceÚlistÚtypingZAnyr   ÚAssertionErrorr   r   ÚintÚsplitÚgetattr)
r   r   r   r   r   ÚenvironÚkeyÚvaluer!   Úresultr   r    r   Úsend9   sT    

ò
 ÿ  þ

ûzWSGIDispatch.send)Tr   r   )NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   ÚCallableÚboolr2   r   r   r   r   r   r   rA   r   r   r   r   r
   	   s*   &   ûú   ûúr
   c                   @   sœ   e Zd Zeje ddœdd„Zdeedœdd„Zed	œd
d„Z	e
eje dœdd„Zeedœdd„Zed	œdd„Zdeeedœdd„Zdeedœdd„ZdS )r*   N)Úiteratorr   c                 C   s   || _ d| _d| _d S )Nó    F)Ú	_iteratorÚ_bufferÚ_closed)r   rH   r   r   r   r   s   s    zBodyStream.__init__éÿÿÿÿ)Úsizer   c                 C   s†   | j r
dS |dkr|  ¡ S z(t| jƒ|k r@|  jt| jƒ7  _qW n  tk
rb   d| _ | j Y S X | jd |… }| j|d … | _|S )NrI   rM   T)rL   ÚreadallÚlenrK   ÚnextrJ   ÚStopIteration)r   rN   Úoutputr   r   r   Úreadx   s    zBodyStream.read)r   c                 C   s4   | j rtdƒ‚| jD ]}|  j|7  _qd| _ | jS )NzStream closedT)rL   ÚOSErrorrJ   rK   )r   r#   r   r   r   rO   Š   s    
zBodyStream.readall)Úbr   c                 C   s&   |   t|ƒ¡}t|ƒ}||d |…< |S r   )rT   rP   )r   rV   rS   Úcountr   r   r   Úreadinto”   s    zBodyStream.readintoc                 C   s   t dƒ‚d S ©NzOperation not supported©rU   )r   rV   r   r   r   Úwriteš   s    zBodyStream.writec                 C   s   t dƒ‚d S rY   rZ   )r   r   r   r   Úfileno�   s    zBodyStream.filenor   )ÚoffsetÚwhencer   c                 C   s   t dƒ‚d S rY   rZ   )r   r]   r^   r   r   r   Úseek    s    zBodyStream.seekc                 C   s   t dƒ‚d S rY   rZ   )r   rN   r   r   r   Útruncate£   s    zBodyStream.truncate)rM   )r   )N)rB   rC   rD   r8   ÚIteratorÚbytesr   r:   rT   rO   Ú	bytearrayÚOptionalrX   r[   r\   r_   r`   r   r   r   r   r*   r   s   
r*   )r,   r8   Úconfigr   r   r   Zmodelsr   r   Úbaser	   r
   Ú	RawIOBaser*   r   r   r   r   Ú<module>   s   i