o
    ùT·j(  ã                   @  s°   d dl mZ d dlZd dlZd dlZd dlZddlmZmZ ddl	m
Z
 ddlmZ ejr9d dlmZ d d	lmZ e d
¡ZdgZddd„ZG dd„ de
ƒZG dd„ deƒZdS )é    )ÚannotationsNé   )ÚRequestÚResponse)ÚSyncByteStreamé   )ÚBaseTransport)Ú
OptExcInfo)ÚWSGIApplicationÚ_TÚWSGITransportÚbodyútyping.Iterable[_T]Úreturnc                 C  s,   t | ƒ} | D ]}|rt |g| ¡  S qg S ©N)ÚiterÚ	itertoolsÚchain)r   Úchunk© r   úZ/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/httpx2/_transports/wsgi.pyÚ_skip_leading_empty_chunks   s   ÿr   c                   @  s*   e Zd Zddd„Zddd	„Zdd
d„ZdS )ÚWSGIByteStreamÚresultútyping.Iterable[bytes]r   ÚNonec                 C  s   t |dd ƒ| _t|ƒ| _d S )NÚclose)ÚgetattrÚ_closer   Ú_result)Úselfr   r   r   r   Ú__init__   s   zWSGIByteStream.__init__útyping.Iterator[bytes]c                 c  s   � | j E d H  d S r   )r   ©r    r   r   r   Ú__iter__#   s   €zWSGIByteStream.__iter__c                 C  s   | j d ur|   ¡  d S d S r   )r   r#   r   r   r   r   &   s   
ÿzWSGIByteStream.closeN)r   r   r   r   )r   r"   )r   r   )Ú__name__Ú
__module__Ú__qualname__r!   r$   r   r   r   r   r   r      s    

r   c                   @  s.   e Zd ZdZ				dddd„Zddd„ZdS )r   a  
    A custom transport that handles sending requests directly to an WSGI app.
    The simplest way to use this functionality is to use the `app` argument.

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

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

    ```
    transport = httpx2.WSGITransport(
        app=app,
        script_name="/submount",
        remote_addr="1.2.3.4"
    )
    client = httpx2.Client(transport=transport)
    ```

    Arguments:
        app: The WSGI 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 WSGI application should be mounted.
        remote_addr: A string indicating the client IP of incoming requests.
    TÚ ú	127.0.0.1NÚappr
   Úraise_app_exceptionsÚboolÚscript_nameÚstrÚremote_addrÚwsgi_errorsútyping.TextIO | Noner   r   c                 C  s"   || _ || _|| _|| _|| _d S r   )r*   r+   r-   r/   r0   )r    r*   r+   r-   r/   r0   r   r   r   r!   J   s
   
zWSGITransport.__init__Úrequestr   r   c                   sv  |  ¡  t |j¡}|jjpdddœ|jj }d|jj|| jp!tj	ddd|j
| j|jj|jj d¡|jjt|ƒd| jd	œ}|jrNd
|jvrNtt|jƒƒ|d< |jjD ]\}}| d¡ ¡  dd¡}|dvrid| }| d¡||< qRd ‰d ‰d ‰ 	 dd ‡ ‡‡fdd„}|  ||¡}	t|	ƒ}
ˆd us’J ‚ˆd us˜J ‚ˆ r¥ˆ d r¥| jr¥ˆ d ‚tˆ ¡ d ƒ}dd„ ˆD ƒ}t|||
d�S )!NéP   i»  )ÚhttpÚhttps)r   r   TFÚasciizHTTP/1.1)zwsgi.versionzwsgi.url_schemez
wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocesszwsgi.run_onceÚREQUEST_METHODÚSCRIPT_NAMEÚ	PATH_INFOÚQUERY_STRINGÚSERVER_NAMEÚSERVER_PORTÚSERVER_PROTOCOLÚREMOTE_ADDRzContent-LengthÚCONTENT_LENGTHú-Ú_)ÚCONTENT_TYPEr?   ÚHTTP_Ústatusr.   Úresponse_headersúlist[tuple[str, str]]Úexc_infoúOptExcInfo | Noner   ú$typing.Callable[[bytes], typing.Any]c                   s   | ‰|‰|‰ dd„ S )Nc                 S  s   d S r   r   )rA   r   r   r   Ú<lambda>ƒ   s    zFWSGITransport.handle_request.<locals>.start_response.<locals>.<lambda>r   )rD   rE   rG   ©Úseen_exc_infoÚseen_response_headersÚseen_statusr   r   Ústart_responsez   s   z4WSGITransport.handle_request.<locals>.start_responser   r   c                 S  s$   g | ]\}}|  d ¡|  d ¡f‘qS )r6   )Úencode)Ú.0ÚkeyÚvaluer   r   r   Ú
<listcomp>�   s   $ z0WSGITransport.handle_request.<locals>.<listcomp>)ÚheadersÚstreamr   )rD   r.   rE   rF   rG   rH   r   rI   )ÚreadÚioÚBytesIOÚcontentÚurlÚportÚschemer0   ÚsysÚstderrÚmethodr-   ÚpathÚqueryÚdecodeÚhostr.   r/   rU   ÚlenÚrawÚupperÚreplacer*   r   r+   ÚintÚsplitr   )r    r2   Ú
wsgi_inputr\   ÚenvironÚ
header_keyÚheader_valuerR   rO   r   rV   Ústatus_coderU   r   rK   r   Úhandle_requestX   sP   
ñýzWSGITransport.handle_request)Tr(   r)   N)r*   r
   r+   r,   r-   r.   r/   r.   r0   r1   r   r   )r2   r   r   r   )r%   r&   r'   Ú__doc__r!   rp   r   r   r   r   r   +   s    !ú)r   r   r   r   )Ú
__future__r   rX   r   r^   ÚtypingÚ_modelsr   r   Ú_typesr   Úbaser   ÚTYPE_CHECKINGÚ	_typeshedr	   Ú_typeshed.wsgir
   ÚTypeVarr   Ú__all__r   r   r   r   r   r   r   Ú<module>   s     

