K i?vdZddlZddlZddlmZddlmZmZddlm Z ddlm Z ddlm Z ddl m Z dd lmZddlZdd lmZmZmZmZmZmZmZmZmZej4rdd lmZGd d e ee j8ZGdde j<ZGddZ Gdde j<Z!e jDZ#y)aA 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)HTTP1ServerConnectionHTTP1ConnectionParameters)httputil)iostream)netutil) TCPServer) Configurable) UnionAnyDictCallableListTypeTupleOptional Awaitable)SetceZdZdZdededdfdZ ddeeje ejgdffde d e d e ee eefej fd e ed e d e ede ede ede ede ede ede eeddfdZedeefdZedeefdZd dZdej8deddfdZdedej@dejBfdZ"deddfdZ#y)! HTTPServeraA 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. argskwargsreturnNcyN)selfrrs X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/httpserver.py__init__zHTTPServer.__init__s request_callback no_keep_alivexheaders ssl_optionsprotocoldecompress_request chunk_sizemax_header_sizeidle_connection_timeout body_timeout max_body_sizemax_buffer_sizetrusted_downstreamc ||_||_||_t|||| xsd| | ||_t j ||| |t|_| |_ y)Ni) decompressr'r(header_timeoutr+r*r")r$r,read_chunk_size) r!r#r%r conn_paramsr rset _connectionsr-)rr!r"r#r$r%r&r'r(r)r*r+r,r-s r initializezHTTPServer.initializesq.!1    4)!+2:d'%'   #+&   E"4r ctSrrclss rconfigurable_basezHTTPServer.configurable_baser ctSrr7r8s rconfigurable_defaultzHTTPServer.configurable_defaultr;r cK|jrDtt|j}|jd{|jrCyy7w)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)r4nextiterclose)rconns rclose_all_connectionsz HTTPServer.close_all_connectionssET../0D**,   s>AAAAstreamaddressct|||j|j}t||j|}|j j ||j|yr)_HTTPRequestContextr%r-rr2r4add start_serving)rrDrEcontextrBs r handle_streamzHTTPServer.handle_streamsW% GT]]D,C,C %VT-=-=wG d# 4 r server_conn request_connct|jtjr|jj ||}nt |j|}|j r t||}|Sr) isinstancer!rHTTPServerConnectionDelegate start_request_CallableAdapterr# _ProxyAdapter)rrLrMdelegates rrQzHTTPServer.start_requests\ d++X-R-R S,,::; UH'(=(=|LH ==$X|$>#?#EF --    (33X5O5OOP %%  )D/ " 5Xio-F, r rRc eZdZ d dejdedeedeeeddf dZ defdZ d e jddfd Z d d Zy)rGNrDrEr%r-rc||_|j|jj|_nd|_|jtjtj fvr | |d|_nd|_|r||_n)t|tjrd|_nd|_|j |_ |j|_ t|xsg|_y)Nrz0.0.0.0httpshttp)rEsocketfamilyaddress_familyAF_INETAF_INET6 remote_ipr%rOr SSLIOStream_orig_remote_ip_orig_protocolr3r-)rrDrEr%r-s rrz_HTTPRequestContext.__init__+s  == $"(--"6"6D "&D    FNNFOO#D D#$QZDN'DN $DM  4 4 5#DM"DM#~~"mm"%&8&>B"?r c|jtjtjfvr |jSt |j trt|j St|j Sr) rrrrrrOrErrrars r__str__z_HTTPRequestContext.__str__MsU   6>>6??"C C>> !  e ,dll+ +t||$ $r rqc|jd|j}dt|jdDD]}||jvsn|jd|}t j |r||_|jd|jd|j}|r"|jddj}|dvr||_y y ) z2Rewrite the ``remote_ip`` and ``protocol`` fields.zX-Forwarded-Forc3<K|]}|jywr)strip).0cands r z6_HTTPRequestContext._apply_xheaders..]sDD4::<Ds,z X-Real-IpzX-SchemezX-Forwarded-Proto)rrN) getrreversedsplitr-r is_valid_ipr%r)rrqip proto_headers r_apply_xheadersz#_HTTPRequestContext._apply_xheadersXs[[*DNN ;DHRXXc],CD B000 [[b )   r "DN{{  $7G  (--c226<<>L , ,(DM -r cH|j|_|j|_y)zUndo changes from `_apply_xheaders`. Xheaders are per-request so they should not leak to the next request on the same connection. N)rrrr%rs r_unapply_xheadersz%_HTTPRequestContext._unapply_xheadersns --++ r rrZ)r[r\r]rrgrrrarrrrrrrrr rrGrG*s 37 @!! @ @3- @ %T#Y/ @  @D % %)x';';)),,r rGceZdZdejdej ddfdZdeejejfdejde e dfdZ d ede e dfd Zdd Zdd Zdd Zy)rSrTrMrNc ||_||_yr)rmrT)rrTrMs rrz_ProxyAdapter.__init__ys '  r rprqc|jjj||jj ||Sr)rmrJrrTrurts rruz_ProxyAdapter.headers_receiveds3 //8}}--j'BBr rvc8|jj|Sr)rTrzrys rrzz_ProxyAdapter.data_receiveds}}**511r cX|jj|jyr)rTr_cleanuprs rrz_ProxyAdapter.finishs  r cX|jj|jyr)rTrrrs rrz!_ProxyAdapter.on_connection_closes ))+ r cL|jjjyr)rmrJrrs rrz_ProxyAdapter._cleanups 113r rZ)r[r\r]rrjrirr rsrrrrrurrzrrrrr rrSrSxs!..!--!  !C(33X5O5OOPC%%C )D/ " C252Xio-F24r rS)$r^rrbtornado.escapertornado.http1connectionrrtornadorrrtornado.tcpserverr tornado.utilr rWr r r rrrrrr TYPE_CHECKINGrrPrrjrRrGrSr_ HTTPRequestrr rrs  %T'% UUU SRL(*O*OSRl#x33#LK,K,\4H004D(( r