o
    Qœ_  ã                   @   sÆ   d Z ddlZddlmZ ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZ ddlmZ ddl	Z	e	jrMdd	l	mZ dd
lmZ dedefdd„ZG dd„ deƒZejZdS )a  WSGI support for the Tornado web framework.

WSGI is the Python standard for web servers, and allows for interoperability
between Tornado and other Python web frameworks and servers.

This module provides WSGI support via the `WSGIContainer` class, which
makes it possible to run applications using other WSGI frameworks on
the Tornado HTTP server. The reverse is not supported; the Tornado
`.Application` and `.RequestHandler` classes are designed for use with
the Tornado `.HTTPServer` and cannot be used in a generic WSGI
container.

é    N)ÚBytesIO)Úescape)Úhttputil)Ú
access_log)ÚListÚTupleÚOptionalÚCallableÚAnyÚDictÚText)ÚTracebackType)ÚType)ÚWSGIApplicationÚsÚreturnc                 C   s   t | tƒsJ ‚|  d¡S )NÚlatin1)Ú
isinstanceÚbytesÚdecode)r   © r   ú./usr/lib/python3/dist-packages/tornado/wsgi.pyÚto_wsgi_str3   s   
r   c                   @   sf   e Zd ZdZddd„Zdejddfd	d
„Zedejde	e
ef fdd„ƒZdedejddfdd„ZdS )ÚWSGIContainera¢  Makes a WSGI-compatible function runnable on Tornado's HTTP server.

    .. warning::

       WSGI is a *synchronous* interface, while Tornado's concurrency model
       is based on single-threaded asynchronous execution.  This means that
       running a WSGI app with Tornado's `WSGIContainer` is *less scalable*
       than running the same app in a multi-threaded WSGI server like
       ``gunicorn`` or ``uwsgi``.  Use `WSGIContainer` only when there are
       benefits to combining Tornado and WSGI in the same process that
       outweigh the reduced scalability.

    Wrap a WSGI function in a `WSGIContainer` and pass it to `.HTTPServer` to
    run it. For example::

        def simple_app(environ, start_response):
            status = "200 OK"
            response_headers = [("Content-type", "text/plain")]
            start_response(status, response_headers)
            return ["Hello world!\n"]

        container = tornado.wsgi.WSGIContainer(simple_app)
        http_server = tornado.httpserver.HTTPServer(container)
        http_server.listen(8888)
        tornado.ioloop.IOLoop.current().start()

    This class is intended to let other frameworks (Django, web.py, etc)
    run on the Tornado HTTP server and I/O loop.

    The `tornado.web.FallbackHandler` class is often useful for mixing
    Tornado and WSGI apps in the same server.  See
    https://github.com/bdarnell/django-tornado-demo for a complete example.
    Úwsgi_applicationÚWSGIAppTyper   Nc                 C   s
   || _ d S ©N)r   )Úselfr   r   r   r   Ú__init__[   s   
zWSGIContainer.__init__Úrequestc                    s¶  i ‰ g ‰	 ddt dttt t f  dttdtt tt f  dttgtf f‡ ‡fdd„}|  	t
 |¡|¡}zˆ |¡ d ˆ¡}W t|d	ƒrJ| ¡  nt|d	ƒrU| ¡  w w ˆ s\td
ƒ‚ˆ d  dd¡\}}t|ƒ}ˆ d }tdd„ |D ƒƒ}	t |¡}|dkr˜d|	vr�| dt t|ƒƒf¡ d|	vr˜| d¡ d|	vr¦| ddtj f¡ t d||¡}
t ¡ }|D ]
\}}| ||¡ q³|jd usÅJ ‚|jj|
||d� |j  ¡  |  !||¡ d S )NÚstatusÚheadersÚexc_infozOptional[Type[BaseException]]r   c                    s   | ˆ d< |ˆ d< ˆj S )Nr    r!   )Úappend)r    r!   r"   ©ÚdataÚresponser   r   Ústart_responseb   s   z.WSGIContainer.__call__.<locals>.start_responseó    Úclosez$WSGI app did not call start_responseú é   c                 s   s   � | ]	\}}|  ¡ V  qd S r   )Úlower)Ú.0ÚkÚvr   r   r   Ú	<genexpr>€   s   € z)WSGIContainer.__call__.<locals>.<genexpr>i0  zcontent-lengthúContent-Lengthzcontent-type)úContent-Typeztext/html; charset=UTF-8ÚserverÚServerzTornadoServer/%szHTTP/1.1)Úchunkr   )"Ústrr   r   r   ÚBaseExceptionr   r	   r   r
   r   r   ÚenvironÚextendÚjoinÚhasattrr)   Ú	ExceptionÚsplitÚintÚsetr   Úutf8r#   ÚlenÚtornadoÚversionr   ÚResponseStartLineÚHTTPHeadersÚaddÚ
connectionÚwrite_headersÚfinishÚ_log)r   r   r'   Úapp_responseÚbodyÚstatus_code_strÚreasonÚstatus_coder!   Ú
header_setÚ
start_lineÚ
header_objÚkeyÚvaluer   r$   r   Ú__call__^   sj   ÷ÿþþÿÿý
ö
ÿ

€
ÿ
ÿ


zWSGIContainer.__call__c                 C   sü   | j  d¡}t|ƒdkr|d }t|d ƒ}n| j }| jdkr!dnd}| jdttj| j	d	d
d�ƒ| j
| j|t|ƒ| jd| jtt | j¡ƒtjd
dd
dœ}d| jv rY| j d¡|d< d| jv rf| j d¡|d< | j ¡ D ]\}}||d| dd¡ ¡  < qk|S )zOConverts a `tornado.httputil.HTTPServerRequest` to a WSGI environment.
        ú:é   r   r+   Úhttpsi»  éP   Ú NF)ÚencodingÚplus)r+   r   T)ÚREQUEST_METHODÚSCRIPT_NAMEÚ	PATH_INFOÚQUERY_STRINGÚREMOTE_ADDRÚSERVER_NAMEÚSERVER_PORTÚSERVER_PROTOCOLzwsgi.versionzwsgi.url_schemez
wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocesszwsgi.run_oncer2   ÚCONTENT_TYPEr1   ÚCONTENT_LENGTHÚHTTP_ú-Ú_)Úhostr=   rA   r>   ÚprotocolÚmethodr   r   Úurl_unescapeÚpathÚqueryÚ	remote_ipr6   rC   r   r@   rL   ÚsysÚstderrr!   ÚpopÚitemsÚreplaceÚupper)r   Úhostportrj   Úportr8   rS   rT   r   r   r   r8   “   s>   ÿï

zWSGIContainer.environrO   c                 C   s~   |dk rt j}n|dk rt j}nt j}d| ¡  }|jd us J ‚|jd us'J ‚|jd |j d |j d }|d|||ƒ d S )Ni�  iô  g     @�@r*   z (ú)z%d %s %.2fms)r   ÚinfoÚwarningÚerrorÚrequest_timerl   Úurirp   )r   rO   r   Ú
log_methodr}   Úsummaryr   r   r   rJ   ¹   s   zWSGIContainer._log)r   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚHTTPServerRequestrU   Ústaticmethodr   r   r
   r8   r>   rJ   r   r   r   r   r   8   s    
"5%r   )r„   rq   Úior   rB   r   r   Útornado.logr   Útypingr   r   r   r	   r
   r   r   Útypesr   ÚTYPE_CHECKINGr   Úwsgiref.typesr   r   r   r6   r   Úobjectr   r…   ÚHTTPRequestr   r   r   r   Ú<module>   s"   $ 
