o
    Zh                     @  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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bodytyping.Iterable[_T]returnc                 C  s,   t | } | D ]}|rt|g|   S qg S N)iter	itertoolschain)r   chunk r   M/var/www/html/lang_env/lib/python3.10/site-packages/httpx/_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 D ]}|V  qd S r   )r   )r   partr   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 )WSGITransporta  
    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 = httpx.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 = httpx.WSGITransport(
        app=app,
        script_name="/submount",
        remote_addr="1.2.3.4"
    )
    client = httpx.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                   sT  |   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jD ]\}}|d d
d}|dvrXd| }|d||< qAd d d  	 dd fdd}| ||}	t|	}
d usJ d usJ  r d r| jr d t d }dd D }t|||
dS )NP   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_METHODZSCRIPT_NAMEZ	PATH_INFOZQUERY_STRINGZSERVER_NAMEZSERVER_PORTZSERVER_PROTOCOLZREMOTE_ADDR-_)ZCONTENT_TYPEZCONTENT_LENGTHZ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   )r9   r   r   r   <lambda>   s    zFWSGITransport.handle_request.<locals>.start_response.<locals>.<lambda>r   )r:   r;   r=   Zseen_exc_infoZseen_response_headersZseen_statusr   r   start_responsex   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   )r:   r.   r;   r<   r=   r>   r   r?   )readioBytesIOcontenturlportschemer0   sysstderrmethodr-   pathquerydecodehostr.   r/   rH   rawupperreplacer*   r   r+   intsplitr   )r   r2   Z
wsgi_inputrO   environZ
header_keyheader_valuerE   rB   r   rI   status_coderH   r   rA   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    r`   r   r   r   r   r'   )   s    #r'   )r   r   r   r   )
__future__r   rK   r   rQ   typingZ_modelsr   r   _typesr   baser   TYPE_CHECKINGZ	_typeshedr	   Z_typeshed.wsgir
   TypeVarr   r   r   r'   r   r   r   r   <module>   s    

