Ë
    êmxi/*  ã                   ó  — d Z ddlZddlmZ ddl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 ddlmZ dd	lmZmZmZmZmZmZ dd
lmZ ddlZej0                  rddlmZ ddlmZ dedefd„Z G d„ d«      Z e
jB                  Z"y)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)Údummy_executor)Úescape)Úhttputil)ÚIOLoop)Ú
access_log)ÚListÚTupleÚOptionalÚCallableÚAnyÚDict)ÚTracebackType)ÚType)ÚWSGIApplicationÚsÚreturnc                 óH   — t        | t        «      sJ ‚| j                  d«      S )NÚlatin1)Ú
isinstanceÚbytesÚdecode)r   s    úC/home/htdocs/ttos/venv/lib/python3.12/site-packages/tornado/wsgi.pyÚto_wsgi_strr   6   s!   € Ü�aœÔÐÐØ�8‰8�HÓÐó    c                   óô   — e Zd ZdZ	 ddddeej                  j                     ddfd„Zde	j                  ddfd	„Zde	j                  ddfd
„Zde	j                  deeef   fd„Zdede	j                  ddfd„Zy)ÚWSGIContainera‡  Makes a WSGI-compatible application runnable on Tornado's HTTP server.

    .. warning::

       WSGI is a *synchronous* interface, while Tornado's concurrency model
       is based on single-threaded *asynchronous* execution.  Many of Tornado's
       distinguishing features are not available in WSGI mode, including efficient
       long-polling and websockets. The primary purpose of `WSGIContainer` is
       to support both WSGI applications and native Tornado ``RequestHandlers`` in
       a single process. WSGI-only applications are likely to be better off
       with a dedicated WSGI server such as ``gunicorn`` or ``uwsgi``.

    Wrap a WSGI application in a `WSGIContainer` to make it implement the Tornado
    `.HTTPServer` ``request_callback`` interface.  The `WSGIContainer` object can
    then be passed to classes from the `tornado.routing` module,
    `tornado.web.FallbackHandler`, or to `.HTTPServer` directly.

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

    Realistic usage will be more complicated, but the simplest possible example uses a
    hand-written WSGI application with `.HTTPServer`::

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

        async def main():
            container = tornado.wsgi.WSGIContainer(simple_app)
            http_server = tornado.httpserver.HTTPServer(container)
            http_server.listen(8888)
            await asyncio.Event().wait()

        asyncio.run(main())

    The recommended pattern is to use the `tornado.routing` module to set up routing
    rules between your WSGI application and, typically, a `tornado.web.Application`.
    Alternatively, `tornado.web.Application` can be used as the top-level router
    and `tornado.web.FallbackHandler` can embed a `WSGIContainer` within it.

    If the ``executor`` argument is provided, the WSGI application will be executed
    on that executor. This must be an instance of `concurrent.futures.Executor`,
    typically a ``ThreadPoolExecutor`` (``ProcessPoolExecutor`` is not supported).
    If no ``executor`` is given, the application will run on the event loop thread in
    Tornado 6.3; this will change to use an internal thread pool by default in
    Tornado 7.0.

    .. warning::
       By default, the WSGI application is executed on the event loop's thread. This
       limits the server to one request at a time (per process), making it less scalable
       than most other WSGI servers. It is therefore highly recommended that you pass
       a ``ThreadPoolExecutor`` when constructing the `WSGIContainer`, after verifying
       that your application is thread-safe. The default will change to use a
       ``ThreadPoolExecutor`` in Tornado 7.0.

    .. versionadded:: 6.3
       The ``executor`` parameter.

    .. deprecated:: 6.3
       The default behavior of running the WSGI application on the event loop thread
       is deprecated and will change in Tornado 7.0 to use a thread pool by default.
    NÚwsgi_applicationÚWSGIAppTypeÚexecutorr   c                 ó<   — || _         |€t        | _        y || _        y ©N)r   r   r    )Úselfr   r    s      r   Ú__init__zWSGIContainer.__init__}   s   € ð
 !1ˆÔØ*2Ð*:œˆ�Àˆ�r   Úrequestc                 ó`   — t        j                  «       j                  | j                  |«       y r"   )r   ÚcurrentÚspawn_callbackÚhandle_request)r#   r%   s     r   Ú__call__zWSGIContainer.__call__…   s   € Ü�‰Ó×'Ñ'¨×(;Ñ(;¸WÕEr   c              ƒ   óä  ‡‡‡K  — 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„}t        j                  «       }|j                  | j                  | j                  | j                  |«      |«      ƒ d {  –—† }	 t        |«      Šdt        t           fˆfd„}	 |j                  | j                  |«      ƒ d {  –—† }|€n‰j!                  |«       Œ9	 t#        |d«      r|j%                  «        	 d	j'                  ‰«      }‰st)        d
«      ‚‰d   j+                  dd«      \  }}	t-        |«      }
‰d   }|D ��ch c]  \  }}|j/                  «       ’Œ }}}t1        j2                  |«      }|
dk7  r>d|vr%|j!                  dt        t5        |«      «      f«       d|vr|j!                  d«       d|vr$|j!                  ddt6        j8                  z  f«       t;        j<                  d|
|	«      }t;        j>                  «       }|D ]  \  }}|jA                  ||«       Œ |jB                  €J ‚|jB                  jE                  |||¬«       |jB                  jG                  «        | jI                  |
|«       y 7 �Œ7 �ŒÃ# t#        |d«      r|j%                  «        w w xY wc c}}w ­w)NÚstatusÚheadersÚexc_infozOptional[Type[BaseException]]r   c                 ó0   •— | ‰d<   |‰d<   ‰j                   S )Nr,   r-   )Úappend)r,   r-   r.   ÚdataÚresponses      €€r   Ústart_responsez4WSGIContainer.handle_request.<locals>.start_responseŒ   s!   ø€ ð $ˆD�‰NØ%ˆD�‰OØ—?‘?Ð"r   c                  ó:   •— 	 t        ‰ «      S # t        $ r Y y w xY wr"   )ÚnextÚStopIteration)Úapp_response_iters   €r   Ú
next_chunkz0WSGIContainer.handle_request.<locals>.next_chunk¥   s(   ø€ ð ÜÐ 1Ó2Ð2øÜ$ò  ñ  ð ús   ƒ
 Ž	™Úcloser   z$WSGI app did not call start_responseú é   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'   Úrun_in_executorr    r   ÚenvironÚiterr0   Úhasattrr9   ÚjoinÚ	ExceptionÚsplitÚintÚlowerr   Úutf8ÚlenÚtornadoÚversionr   ÚResponseStartLineÚHTTPHeadersÚaddÚ
connectionÚwrite_headersÚfinishÚ_log)r#   r%   r3   ÚloopÚapp_responser8   r@   ÚbodyÚstatus_code_strÚreasonÚstatus_coder-   ÚkÚvÚ
header_setÚ
start_lineÚ
header_objÚkeyÚvaluer7   r1   r2   s                      @@@r   r)   zWSGIContainer.handle_requestˆ   sÊ  úè ø€ ØˆØˆð ñ	#Üð	#äœ%¤¤S ™/Ñ*ð	#ô ÜØ3Üœ]Ñ+Üœ]Ñ+ð-ññð	#ô ”u�gœs�lÑ#ö	#ô �~‰~ÓˆØ!×1Ñ1Ø�M‰MØ×!Ñ!Ø�L‰L˜Ó!Øó	
÷ 
ˆð	%Ü $ \Ó 2Ðð ¤¬¡õ  ð Ø"×2Ñ2°4·=±=À*ÓM×M�Ø�=ØØ—‘ Ô&ð	 ð ô �| WÔ-Ø×"Ñ"Õ$Ø�x‰x˜Ó!ˆÙÜÐBÓCÐCà"& x¡.×"6Ñ"6°s¸AÓ">Ñˆ˜Ü˜/Ó*ˆØ�y‘/ˆØ.5×6¡F Q¨�a—g‘g•iÐ6ˆ
Ñ6Ü�{‰{˜4Ó ˆØ˜#ÒØ zÑ1Ø—‘Ð 0´#´c¸$³i³.ÐAÔBØ ZÑ/Ø—‘ÐKÔLØ˜:Ñ%Ø�N‰N˜HÐ&8¼7¿?¹?Ñ&JÐKÔLä×/Ñ/°
¸KÈÓPˆ
Ü×)Ñ)Ó+ˆ
Ø!ò 	'‰JˆC�Ø�N‰N˜3 Õ&ð	'à×!Ñ!Ð-Ð-Ð-Ø×Ñ×(Ñ(¨°ZÀtÐ(ÔLØ×Ñ×!Ñ!Ô#Ø�	‰	�+˜wÕ'ðc
ùð$ Núô
 �| WÔ-Ø×"Ñ"Õ$ð .üó 7ùsJ   …B9K0Â>KÂ?K0Ã>K ÄKÄK ÄA(K0ÆK*ÆD$K0ËK ËK'Ë'	K0c                 ór  — |j                   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
¬«      «      |j                  |j                  |t        |«      |j                  d|j                  t        t        j                  |j                   «      «      t"        j$                  | j&                  t(        udd
dœ}d|j*                  v r|j*                  j-                  d«      |d<   d|j*                  v r|j*                  j-                  d«      |d<   |j*                  j/                  «       D ]+  \  }}||d|j1                  dd«      j3                  «       z   <   Œ- |S )z–Converts a `tornado.httputil.HTTPServerRequest` to a WSGI environment.

        .. versionchanged:: 6.3
           No longer a static method.
        ú:é   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_oncer=   ÚCONTENT_TYPEr<   ÚCONTENT_LENGTHÚHTTP_ú-Ú_)ÚhostrI   rM   rJ   ÚprotocolÚmethodr   r   Úurl_unescapeÚpathÚqueryÚ	remote_iprA   rO   r   rL   rY   ÚsysÚstderrr    r   r-   ÚpopÚitemsÚreplaceÚupper)r#   r%   Úhostportry   ÚportrD   rb   rc   s           r   rD   zWSGIContainer.environÏ   s|  € ð —<‘<×%Ñ% cÓ*ˆÜˆx‹=˜AÒØ˜A‘;ˆDÜ�x ‘{Ó#‰Dà—<‘<ˆDØ!×*Ñ*¨gÒ5‘3¸2ˆDà%Ÿn™nØÜ$Ü×#Ñ# G§L¡L¸4ÀeÔLóð $ŸM™MØ"×,Ñ,ØÜ˜t›9Ø&Ÿ™Ø"Ø&×/Ñ/Ü!¤&§+¡+¨g¯l©lÓ";Ó<ÜŸ:™:Ø $§¡´^Ð CØ!%Ø"ñ#
ˆð& ˜WŸ_™_Ñ,Ø&-§o¡o×&9Ñ&9¸.Ó&IˆG�NÑ#Ø˜wŸ™Ñ.Ø(/¯©×(;Ñ(;Ð<LÓ(MˆGÐ$Ñ%Ø!Ÿ/™/×/Ñ/Ó1ò 	E‰JˆC�Ø?DˆG�G˜cŸk™k¨#¨sÓ3×9Ñ9Ó;Ñ;Ò<ð	Eàˆr   r\   c                 óN  — |dk  rt         j                  }n&|dk  rt         j                  }nt         j                  }d|j	                  «       z  }|j
                  €J ‚|j                  €J ‚|j
                  dz   |j                  z   dz   |j                  z   dz   } |d|||«       y )Ni�  iô  g     @�@r:   z (ú)z%d %s %.2fms)r   ÚinfoÚwarningÚerrorÚrequest_timer{   Úurir   )r#   r\   r%   Ú
log_methodr�   Úsummarys         r   rV   zWSGIContainer._log÷   s¸   € Ø˜ÒÜ#Ÿ™‰JØ˜3ÒÜ#×+Ñ+‰Jä#×)Ñ)ˆJØ × 4Ñ 4Ó 6Ñ6ˆØ�~‰~Ð)Ð)Ð)Ø�{‰{Ð&Ð&Ð&à�N‰NØñà�k‰kñð ñð ×Ññ	 ð
 ñð 	ñ 	�> ;°¸ÕFr   r"   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú
concurrentÚfuturesÚExecutorr$   r   ÚHTTPServerRequestr*   r)   r   rA   r   rD   rJ   rV   © r   r   r   r   ;   sÂ   „ ñ?ðH ;?ñIà'ðIð ˜:×-Ñ-×6Ñ6Ñ7ðIð 
ó	IðF × :Ñ :ð F¸tó FðE(¨H×,FÑ,Fð E(È4ó E(ðN&˜x×9Ñ9ð &¸dÀ3ÈÀ8¹nó &ðPG ð G¨h×.HÑ.Hð GÈTô Gr   r   )#r”   Úconcurrent.futuresr•   Úior   rN   r€   Útornado.concurrentr   r   r   Útornado.ioloopr   Útornado.logr   Útypingr	   r
   r   r   r   r   Útypesr   ÚTYPE_CHECKINGr   Ú_typeshed.wsgir   r   r   rA   r   r   r˜   ÚHTTPRequestr™   r   r   ú<module>r¤      sq   ðñ ó Ý Û Û 
å -Ý Ý Ý !Ý "ç =× =Ý Û à	×ÒÝÝ=ð�5ð ˜Só ÷
NGñ NGðb ×(Ñ(�r   