Ë
    êmxi?  ã                   óv  — d Z ddlZddlZddlmZ ddlmZ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 ddlZdd
lmZmZmZmZmZmZmZmZmZ ej4                  rddlmZ  G d„ deee	j8                  «      Z G d„ de	j<                  «      Z G d„ d«      Z  G d„ de	j<                  «      Z!e	jD                  Z#y)a¼  A non-blocking, single-threaded HTTP server.

Typical applications have little direct interaction with the `HTTPServer`
class except to start a server at the beginning of the process
(and even that is often done indirectly via `tornado.web.Application.listen`).

.. versionchanged:: 4.0

   The ``HTTPRequest`` class that used to live in this module has been moved
   to `tornado.httputil.HTTPServerRequest`.  The old name remains as an alias.
é    N)Ú
native_str)ÚHTTP1ServerConnectionÚHTTP1ConnectionParameters)Úhttputil)Úiostream)Únetutil)Ú	TCPServer)ÚConfigurable)	ÚUnionÚAnyÚDictÚCallableÚListÚTypeÚTupleÚOptionalÚ	Awaitable)ÚSetc                   óÔ  — e Zd ZdZdededdfd„Z	 	 	 	 	 	 	 	 	 	 	 	 ddeej                  e	ej                  gdf   f   ded	ed
eeeeef   ej                   f      dee   dedee   dee   dee   dee   dee   dee   deee      ddfd„Zedee   fd„«       Zedee   fd„«       Zd d„Zdej8                  deddfd„Zdedej@                  dejB                  fd„Z"deddfd„Z#y)!Ú
HTTPServeraã  A non-blocking, single-threaded HTTP server.

    A server is defined by a subclass of `.HTTPServerConnectionDelegate`,
    or, for backwards compatibility, a callback that takes an
    `.HTTPServerRequest` as an argument. The delegate is usually a
    `tornado.web.Application`.

    `HTTPServer` supports keep-alive connections by default
    (automatically for HTTP/1.1, or for HTTP/1.0 when the client
    requests ``Connection: keep-alive``).

    If ``xheaders`` is ``True``, we support the
    ``X-Real-Ip``/``X-Forwarded-For`` and
    ``X-Scheme``/``X-Forwarded-Proto`` headers, which override the
    remote IP and URI scheme/protocol for all requests.  These headers
    are useful when running Tornado behind a reverse proxy or load
    balancer.  The ``protocol`` argument can also be set to ``https``
    if Tornado is run behind an SSL-decoding proxy that does not set one of
    the supported ``xheaders``.

    By default, when parsing the ``X-Forwarded-For`` header, Tornado will
    select the last (i.e., the closest) address on the list of hosts as the
    remote host IP address.  To select the next server in the chain, a list of
    trusted downstream hosts may be passed as the ``trusted_downstream``
    argument.  These hosts will be skipped when parsing the ``X-Forwarded-For``
    header.

    To make this server serve SSL traffic, send the ``ssl_options`` keyword
    argument with an `ssl.SSLContext` object. For compatibility with older
    versions of Python ``ssl_options`` may also be a dictionary of keyword
    arguments for the `ssl.SSLContext.wrap_socket` method.::

       ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
       ssl_ctx.load_cert_chain(os.path.join(data_dir, "mydomain.crt"),
                               os.path.join(data_dir, "mydomain.key"))
       HTTPServer(application, ssl_options=ssl_ctx)

    `HTTPServer` initialization follows one of three patterns (the
    initialization methods are defined on `tornado.tcpserver.TCPServer`):

    1. `~tornado.tcpserver.TCPServer.listen`: single-process::

            async def main():
                server = HTTPServer()
                server.listen(8888)
                await asyncio.Event().wait()

            asyncio.run(main())

       In many cases, `tornado.web.Application.listen` can be used to avoid
       the need to explicitly create the `HTTPServer`.

       While this example does not create multiple processes on its own, when
       the ``reuse_port=True`` argument is passed to ``listen()`` you can run
       the program multiple times to create a multi-process service.

    2. `~tornado.tcpserver.TCPServer.add_sockets`: multi-process::

            sockets = bind_sockets(8888)
            tornado.process.fork_processes(0)
            async def post_fork_main():
                server = HTTPServer()
                server.add_sockets(sockets)
                await asyncio.Event().wait()
            asyncio.run(post_fork_main())

       The ``add_sockets`` interface is more complicated, but it can be used with
       `tornado.process.fork_processes` to run a multi-process service with all
       worker processes forked from a single parent.  ``add_sockets`` can also be
       used in single-process servers if you want to create your listening
       sockets in some way other than `~tornado.netutil.bind_sockets`.

       Note that when using this pattern, nothing that touches the event loop
       can be run before ``fork_processes``.

    3. `~tornado.tcpserver.TCPServer.bind`/`~tornado.tcpserver.TCPServer.start`:
       simple **deprecated** multi-process::

            server = HTTPServer()
            server.bind(8888)
            server.start(0)  # Forks multiple sub-processes
            IOLoop.current().start()

       This pattern is deprecated because it requires interfaces in the
       `asyncio` module that have been deprecated since Python 3.10. Support for
       creating multiple processes in the ``start`` method will be removed in a
       future version of Tornado.

    .. versionchanged:: 4.0
       Added ``decompress_request``, ``chunk_size``, ``max_header_size``,
       ``idle_connection_timeout``, ``body_timeout``, ``max_body_size``
       arguments.  Added support for `.HTTPServerConnectionDelegate`
       instances as ``request_callback``.

    .. versionchanged:: 4.1
       `.HTTPServerConnectionDelegate.start_request` is now called with
       two arguments ``(server_conn, request_conn)`` (in accordance with the
       documentation) instead of one ``(request_conn)``.

    .. versionchanged:: 4.2
       `HTTPServer` is now a subclass of `tornado.util.Configurable`.

    .. versionchanged:: 4.5
       Added the ``trusted_downstream`` argument.

    .. versionchanged:: 5.0
       The ``io_loop`` argument has been removed.
    ÚargsÚkwargsÚreturnNc                  ó   — y ©N© )Úselfr   r   s      úI/home/htdocs/ttos/venv/lib/python3.12/site-packages/tornado/httpserver.pyÚ__init__zHTTPServer.__init__œ   s   € ð 	ó    Úrequest_callbackÚno_keep_aliveÚxheadersÚssl_optionsÚprotocolÚdecompress_requestÚ
chunk_sizeÚmax_header_sizeÚidle_connection_timeoutÚbody_timeoutÚmax_body_sizeÚmax_buffer_sizeÚtrusted_downstreamc           	      óÂ   — || _         || _        || _        t        ||||	xs d||
|¬«      | _        t        j                  | |||¬«       t        «       | _        || _	        y )Ni  )Ú
decompressr'   r(   Úheader_timeoutr+   r*   r"   )r$   r,   Úread_chunk_size)
r!   r#   r%   r   Úconn_paramsr	   r   ÚsetÚ_connectionsr-   )r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   s                 r   Ú
initializezHTTPServer.initialize¤   sq   € ð. !1ˆÔØ ˆŒØ ˆŒÜ4Ø)Ø!Ø+Ø2Ò:°dØ'Ø%Ø'ô
ˆÔô 	×ÑØØ#Ø+Ø&õ		
ô  ›EˆÔØ"4ˆÕr    c                 ó   — t         S r   ©r   ©Úclss    r   Úconfigurable_basezHTTPServer.configurable_baseÐ   ó   € äÐr    c                 ó   — t         S r   r7   r8   s    r   Úconfigurable_defaultzHTTPServer.configurable_defaultÔ   r;   r    c              ƒ   ó°   K  — | j                   rDt        t        | j                   «      «      }|j                  «       ƒ d{  –—†  | j                   rŒCyy7 Œ­w)a&  Close all open connections and asynchronously wait for them to finish.

        This method is used in combination with `~.TCPServer.stop` to
        support clean shutdowns (especially for unittests). Typical
        usage would call ``stop()`` first to stop accepting new
        connections, then ``await close_all_connections()`` to wait for
        existing connections to finish.

        This method does not currently close open websocket connections.

        Note that this method is a coroutine and must be called with ``await``.

        N)r4   ÚnextÚiterÚclose)r   Úconns     r   Úclose_all_connectionsz HTTPServer.close_all_connectionsØ   sE   è ø€ ð ×Òäœ˜T×.Ñ.Ó/Ó0ˆDØ—*‘*“,×Ðð ×Õð ús   ‚>AÁ AÁAÁAÚstreamÚaddressc                 óÎ   — t        ||| j                  | j                  «      }t        || j                  |«      }| j
                  j                  |«       |j                  | «       y r   )Ú_HTTPRequestContextr%   r-   r   r2   r4   ÚaddÚstart_serving)r   rD   rE   ÚcontextrB   s        r   Úhandle_streamzHTTPServer.handle_streamë   sW   € Ü%Ø�G˜TŸ]™]¨D×,CÑ,Có
ˆô % V¨T×-=Ñ-=¸wÓGˆØ×Ñ×Ñ˜dÔ#Ø×Ñ˜4Õ r    Úserver_connÚrequest_connc                 óä   — t        | j                  t        j                  «      r| j                  j	                  ||«      }nt        | j                  |«      }| j                  rt        ||«      }|S r   )Ú
isinstancer!   r   ÚHTTPServerConnectionDelegateÚstart_requestÚ_CallableAdapterr#   Ú_ProxyAdapter)r   rL   rM   Údelegates       r   rQ   zHTTPServer.start_requestó   s\   € ô �d×+Ñ+¬X×-RÑ-RÔSØ×,Ñ,×:Ñ:¸;ÈÓU‰Hä'¨×(=Ñ(=¸|ÓLˆHà�=Š=Ü$ X¨|Ó<ˆHàˆr    c                 ój   — | j                   j                  t        j                  t        |«      «       y r   )r4   ÚremoveÚtypingÚcastr   )r   rL   s     r   Úon_closezHTTPServer.on_close   s"   € Ø×Ñ× Ñ ¤§¡Ô-BÀKÓ!PÕQr    )FFNNFNNNNNNN©r   N)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   rP   r   ÚHTTPServerRequestÚboolr   r   ÚstrÚsslÚ
SSLContextÚintÚfloatr   r5   Úclassmethodr   r
   r:   r=   rC   r   ÚIOStreamr   rK   ÚobjectÚHTTPConnectionÚHTTPMessageDelegaterQ   rY   r   r    r   r   r   .   sæ  „ ñkðZ˜cð ¨Sð °Tó ð $ØØGKØ"&Ø#(Ø$(Ø)-Ø37Ø(,Ø'+Ø)-Ø26ñ#*5àØ×1Ñ1Ø�h×0Ñ0Ð1°4Ð7Ñ8ð:ñ
ð*5ð ð*5ð ð*5ð ˜e D¨¨c¨¡N°C·N±NÐ$BÑCÑDð*5ð ˜3‘-ð*5ð !ð*5ð ˜S‘Mð*5ð " #™ð*5ð "*¨%¡ð*5ð ˜u‘oð*5ð   ‘}ð*5ð  " #™ð!*5ð" % T¨#¡YÑ/ð#*5ð$ 
ó%*5ðX ð $ |Ñ"4ò ó ðð ð T¨,Ñ%7ò ó ðóð&! H×$5Ñ$5ð !Àð !È$ó !ðØ!ðØ19×1HÑ1Hðà	×	%Ñ	%óðR Fð R¨tô Rr    r   c                   óæ   — e Zd Zdeej
                  gdf   dej                  ddfd„Zdeej                  ej                  f   dej                  deed      fd„Zd	edeed      fd
„Zdd„Zdd„Zy)rR   r!   NrM   r   c                 óJ   — || _         || _        d | _        d | _        g | _        y r   )Ú
connectionr!   ÚrequestrT   Ú_chunks)r   r!   rM   s      r   r   z_CallableAdapter.__init__  s(   € ð
 'ˆŒØ 0ˆÔØˆŒØˆŒØˆ�r    Ú
start_lineÚheadersc                 ó–   — t        j                  | j                  t        j                  t         j
                  |«      |¬«      | _        y )N)rm   rp   rq   )r   r_   rm   rW   rX   ÚRequestStartLinern   ©r   rp   rq   s      r   Úheaders_receivedz!_CallableAdapter.headers_received  s9   € ô
  ×1Ñ1Ø—‘Ü—{‘{¤8×#<Ñ#<¸jÓIØô
ˆŒð
 r    Úchunkc                 ó:   — | j                   j                  |«       y r   )ro   Úappend©r   rv   s     r   Údata_receivedz_CallableAdapter.data_received  s   € Ø�‰×Ñ˜EÔ"Ør    c                 óÞ   — | j                   €J ‚dj                  | j                  «      | j                   _        | j                   j	                  «        | j                  | j                   «       y )Nr    )rn   Újoinro   ÚbodyÚ_parse_bodyr!   ©r   s    r   Úfinishz_CallableAdapter.finish   sM   € Ø�|‰|Ð'Ð'Ð'ØŸH™H T§\¡\Ó2ˆ�‰ÔØ�‰× Ñ Ô"Ø×Ñ˜dŸl™lÕ+r    c                 ó   — | ` y r   )ro   r   s    r   Úon_connection_closez$_CallableAdapter.on_connection_close&  s   € Ø‰Lr    rZ   )r[   r\   r]   r   r   r_   ri   r   r   rs   ÚResponseStartLineÚHTTPHeadersr   r   ru   Úbytesrz   r€   r‚   r   r    r   rR   rR     s¨   „ ð	à" H×$>Ñ$>Ð#?ÀÐ#EÑFð	ð ×-Ñ-ð	ð 
ó		ð
à˜(×3Ñ3°X×5OÑ5OÐOÑPð
ð ×%Ñ%ð
ð 
�)˜D‘/Ñ	"ó	
ð 5ð ¨X°iÀ±oÑ-Fó ó,ôr    rR   c                   óŠ   — e Zd Z	 ddej                  dedee   deee      ddf
d„Z	defd„Z
d	ej                  ddfd
„Zdd„Zy)rG   NrD   rE   r%   r-   r   c                 óÂ  — || _         |j                  �|j                  j                  | _        nd | _        | j                  t        j                  t        j
                  fv r|�|d   | _        nd| _        |r|| _        n)t        |t        j                  «      rd| _        nd| _        | j                  | _        | j                  | _        t        |xs g «      | _        y )Nr   z0.0.0.0ÚhttpsÚhttp)rE   ÚsocketÚfamilyÚaddress_familyÚAF_INETÚAF_INET6Ú	remote_ipr%   rO   r   ÚSSLIOStreamÚ_orig_remote_ipÚ_orig_protocolr3   r-   )r   rD   rE   r%   r-   s        r   r   z_HTTPRequestContext.__init__+  s»   € ð ˆŒð �=‰=Ð$Ø"(§-¡-×"6Ñ"6ˆDÕà"&ˆDÔð ×Ñ¤F§N¡N´F·O±OÐ#DÑDØÐ#à$ Q™ZˆD�Nð 'ˆDŒNÙØ$ˆD�MÜ˜¤× 4Ñ 4Ô5Ø#ˆD�Mà"ˆDŒMØ#Ÿ~™~ˆÔØ"Ÿm™mˆÔÜ"%Ð&8Ò&>¸BÓ"?ˆÕr    c                 óú   — | j                   t        j                  t        j                  fv r| j                  S t        | j                  t        «      rt        | j                  «      S t        | j                  «      S r   )
rŒ   rŠ   r�   rŽ   r�   rO   rE   r…   r   ra   r   s    r   Ú__str__z_HTTPRequestContext.__str__M  sU   € Ø×Ñ¤6§>¡>´6·?±?Ð"CÑCØ—>‘>Ð!Ü˜Ÿ™¤eÔ,ô ˜dŸl™lÓ+Ð+ä�t—|‘|Ó$Ð$r    rq   c                 ó¼  — |j                  d| j                  «      }d„ t        |j                  d«      «      D «       D ]  }|| j                  vsŒ n |j                  d|«      }t        j                  |«      r|| _        |j                  d|j                  d| j                  «      «      }|r"|j                  d«      d   j                  «       }|dv r|| _        y	y	)
z2Rewrite the ``remote_ip`` and ``protocol`` fields.zX-Forwarded-Forc              3   ó<   K  — | ]  }|j                  «       –— Œ y ­wr   )Ústrip)Ú.0Úcands     r   ú	<genexpr>z6_HTTPRequestContext._apply_xheaders.<locals>.<genexpr>]  s   è ø€ ÒD D�4—:‘:—<ÑDùs   ‚ú,z	X-Real-IpzX-SchemezX-Forwarded-Protoéÿÿÿÿ)r‰   rˆ   N)	Úgetr�   ÚreversedÚsplitr-   r   Úis_valid_ipr%   r—   )r   rq   ÚipÚproto_headers       r   Ú_apply_xheadersz#_HTTPRequestContext._apply_xheadersX  sÏ   € ð �[‰[Ð*¨D¯N©NÓ;ˆáD¬H°R·X±X¸c³]Ó,CÔDò 	ˆBØ˜×0Ñ0Ò0Ùð	ð �[‰[˜ bÓ)ˆÜ×Ñ˜rÔ"ØˆDŒNà—{‘{Ø˜Ÿ™Ð$7¸¿¹ÓGó
ˆñ ð (×-Ñ-¨cÓ2°2Ñ6×<Ñ<Ó>ˆLØÐ,Ñ,Ø(ˆD�Mð -r    c                 óH   — | j                   | _        | j                  | _        y)z›Undo changes from `_apply_xheaders`.

        Xheaders are per-request so they should not leak to the next
        request on the same connection.
        N)r‘   r�   r’   r%   r   s    r   Ú_unapply_xheadersz%_HTTPRequestContext._unapply_xheadersn  s   € ð ×-Ñ-ˆŒØ×+Ñ+ˆ�r    r   rZ   )r[   r\   r]   r   rg   r   r   ra   r   r   r”   r   r„   r£   r¥   r   r    r   rG   rG   *  s€   „ ð 37ñ @à×!Ñ!ð @ð ð @ð ˜3‘-ð	 @ð
 % T¨#¡YÑ/ð @ð 
ó @ðD	%˜ó 	%ð) x×';Ñ';ð )Àó )ô,,r    rG   c                   óâ   — e Zd Zdej                  dej
                  ddfd„Zdeej                  ej                  f   dej                  deed      fd„Zd	edeed      fd
„Zdd„Zdd„Zdd„Zy)rS   rT   rM   r   Nc                 ó    — || _         || _        y r   )rm   rT   )r   rT   rM   s      r   r   z_ProxyAdapter.__init__y  s   € ð
 'ˆŒØ ˆ�r    rp   rq   c                 ó„   — | j                   j                  j                  |«       | j                  j	                  ||«      S r   )rm   rJ   r£   rT   ru   rt   s      r   ru   z_ProxyAdapter.headers_received�  s3   € ð 	�‰×Ñ×/Ñ/°Ô8Ø�}‰}×-Ñ-¨j¸'ÓBÐBr    rv   c                 ó8   — | j                   j                  |«      S r   )rT   rz   ry   s     r   rz   z_ProxyAdapter.data_received‹  s   € Ø�}‰}×*Ñ*¨5Ó1Ð1r    c                 óX   — | j                   j                  «        | j                  «        y r   )rT   r€   Ú_cleanupr   s    r   r€   z_ProxyAdapter.finishŽ  s   € Ø�‰×ÑÔØ�‰�r    c                 óX   — | j                   j                  «        | j                  «        y r   )rT   r‚   r«   r   s    r   r‚   z!_ProxyAdapter.on_connection_close’  s   € Ø�‰×)Ñ)Ô+Ø�‰�r    c                 óL   — | j                   j                  j                  «        y r   )rm   rJ   r¥   r   s    r   r«   z_ProxyAdapter._cleanup–  s   € Ø�‰×Ñ×1Ñ1Õ3r    rZ   )r[   r\   r]   r   rj   ri   r   r   rs   rƒ   r„   r   r   ru   r…   rz   r€   r‚   r«   r   r    r   rS   rS   x  s¤   „ ð!à×.Ñ.ð!ð ×-Ñ-ð!ð 
ó	!ðCà˜(×3Ñ3°X×5OÑ5OÐOÑPðCð ×%Ñ%ðCð 
�)˜D‘/Ñ	"ó	Cð2 5ð 2¨X°iÀ±oÑ-Fó 2óóô4r    rS   )$r^   rŠ   rb   Útornado.escaper   Útornado.http1connectionr   r   Útornador   r   r   Útornado.tcpserverr	   Útornado.utilr
   rW   r   r   r   r   r   r   r   r   r   ÚTYPE_CHECKINGr   rP   r   rj   rR   rG   rS   r_   ÚHTTPRequestr   r    r   ú<module>rµ      sš   ðñ 
ó Û 
å %ß TÝ Ý Ý Ý 'Ý %ã ß U× UÕ Uà	×ÒÝôSR�˜L¨(×*OÑ*Oô SRôl#�x×3Ñ3ô #÷LK,ñ K,ô\4�H×0Ñ0ô 4ðD ×(Ñ(�r    