K idZddlZddlZddlZddlZddlmZmZmZddl m Z m Z ddl m Z ddl mZddl mZddlmZmZdd lmZdd lmZmZmZmZmZmZmZej:d ZGd d eZ GddZ!GddZ"GddejFZ$GddejJZ&GddZ'ej:dZ(ej:dZ)de*de+fdZ,de*de+fdZ-dej\de/fdZ0y) zFClient and server implementations of HTTP/1.x. .. versionadded:: 4.0 N)Futurefuture_add_done_callback"future_set_result_unless_cancelled) native_strutf8)gen)httputil)iostream)gen_logapp_log)GzipDecompressor)castOptionalType AwaitableCallableUnionTuples | ceZdZddZy)_QuietExceptionNcyNselfs ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/http1connection.py__init__z_QuietException.__init__- returnN)__name__ __module__ __qualname__rrrrrr,s rrcneZdZdZdej ddfdZd dZddd ee d e jddfd Z y) _ExceptionLoggingContextzUsed with the ``with`` statement when calling delegate methods to log any exceptions with the given logger. Any exceptions caught are converted to _QuietException loggerr!Nc||_yr)r')rr's rrz!_ExceptionLoggingContext.__init__7s  rcyrrrs r __enter__z"_ExceptionLoggingContext.__enter__:rrtypzOptional[Type[BaseException]]valuetbc|E|Jt|tjry|jj d|||ft y)NUncaught exceptionexc_info) isinstancer HTTPInputErrorr'errorr)rr+r,r-s r__exit__z!_ExceptionLoggingContext.__exit__=sQ  ? "?%!8!89 KK  2c5"=M  N! ! rr ) r"r#r$__doc__loggingLoggerrr*r BaseExceptiontypes TracebackTyper5rrrr&r&1sY w~~$  " , " & "    "  "rr&cfeZdZdZ d dedeedeedeedeedeed ed dfd Zy) HTTP1ConnectionParametersz?Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.N no_keep_alive chunk_sizemax_header_sizeheader_timeout max_body_size body_timeout decompressr!cv||_|xsd|_|xsd|_||_||_||_||_y)a; :arg bool no_keep_alive: If true, always close the connection after one request. :arg int chunk_size: how much data to read into memory at once :arg int max_header_size: maximum amount of data for HTTP headers :arg float header_timeout: how long to wait for all headers (seconds) :arg int max_body_size: maximum amount of data for body :arg float body_timeout: how long to wait while reading body (seconds) :arg bool decompress: if true, decode incoming ``Content-Encoding: gzip`` iN)r>r?r@rArBrCrD)rr>r?r@rArBrCrDs rrz"HTTP1ConnectionParameters.__init__OsD*+$-.7%,*($r)FNNNNNF) r"r#r$r6boolrintfloatrrrrr=r=LsI$$()-*.'+(, %%SM%"# % ! %  } %uo%% %rr=c eZdZdZ d)dej dedeedee ddf dZ d e jde efd Zd e jdefd Zd*d Zd eegdfddfdZd*dZd*dZdej fdZdeddfdZdeddfdZ d+dee j6e j8fde j:deeddfdZdedefdZ deddfdZ!d*dZ"d,dZ#de j6de j:defdZ$d-d Z%d!ede&e'e j:ffd"Z(d#ede j:d e jdee dfd$Z)d%ed e jddfd&Z*d e jddfd'Z+d e jddfd(Z,y).HTTP1ConnectionzImplements the HTTP/1.x protocol. This class can be on its own for clients, or via `HTTP1ServerConnection` for servers. Nstream is_clientparamscontextr!c||_||_| t}||_||_|j |_|jj |jj n|jj|_|jj|_ d|_ d|_ t|_d|_|j!d|_d|_d|_d|_d|_d|_y)a :arg stream: an `.IOStream` :arg bool is_client: client or server :arg params: a `.HTTP1ConnectionParameters` instance or ``None`` :arg context: an opaque application-defined object that can be accessed as ``connection.context``. NF)rLrKr=rMrNr>rBmax_buffer_size_max_body_sizerC _body_timeout_write_finished_read_finishedr_finish_future_disconnect_on_finish_clear_callbacks_request_start_line_response_start_line_request_headers_chunking_output_expected_content_remaining_pending_write)rrKrLrMrNs rrzHTTP1Connection.__init__ts# >.0F  #11 {{((4 KK % %,,  "[[55 %#%h&+" $( $(! $ %,0("rdelegatec|jjr t||jj}|j |S)azRead a single HTTP response. Typical client-mode usage is to write a request using `write_headers`, `write`, and `finish`, and then call ``read_response``. :arg delegate: a `.HTTPMessageDelegate` Returns a `.Future` that resolves to a bool after the full response has been read. The result is true if the stream is still open. )rMrD_GzipMessageDelegater? _read_message)rr^s r read_responsezHTTP1Connection.read_responses8 ;; ! !+Hdkk6L6LMH!!(++rcKd} |jjd|jj}|jj |d{}nh t j |jjj|jjz|tjd{}|j#|\}}|j$r&t'j(|}||_|}d|_n=t'j.|} | |_||_| }|j5| | |_d}tt5|j7||} | | d{ddd|j@d} |r(tt5|jdddd}|j!yd} |j$rt9|t&j:sJ|j0|j0j<dk(rd} |j>} | dk(rd} d| cxkrd krxnnud |vsd |vrt'j@d | z|jC|d{n;|jEd dk(r'|jFs|jjId| s|jK|j$r j>nd||} | u|jL | d{n^ t j |jjj|jLz| tjd{d|_*|jFr |j$r*d}tt5|jWddd|jXj[s_|jS|jj]s9|jj_|j`|jXd{|j$r|j,r|j|j> |r(tt5|jdddd}|j!y |r(tt5|jdddd}|j!y7r7 #t j$r\|jY|r5tt5|jdddn #1swYnxYwd}|j!ywxYw7#1swYxYw#1swYxYw77n7#t j$rtOjPd|jR|jjY|r5tt5|jdddn #1swYnxYwd}|j!ywxYw#1swYmxYw7#1swYxYw#t&j@$r}tOjPd|jR||j$s$|jjIdd{7|jYd}~|r5tt5|jdddn #1swYnxYwd}|j!yd}~wwxYw#1swY>xYw#|r5tt5|jdddn #1swYnxYwd}|j!wxYww)NFs ? ?  max_bytes)quiet_exceptionsTHEAD0dContent-LengthTransfer-Encodingz!Response code %d cannot have bodyExpectz 100-continuesHTTP/1.1 100 (Continue) rzTimeout reading body from %sz"Malformed HTTP message from %s: %ssHTTP/1.1 400 Bad Request )1rKread_until_regexrMr@rAr with_timeoutio_looptimer StreamClosedError TimeoutErrorcloser&r on_connection_closerW_parse_headersrLr parse_response_start_linerYrVparse_request_start_linerXrZ_can_keep_aliveheaders_receivedr2ResponseStartLinemethodcoder3ragetrSwrite _read_bodyrRr inforNrTfinishrUdoneclosedset_close_callback_on_connection_close)rr^need_delegate_close header_future header_datastart_line_strheadersresp_start_line start_linereq_start_lineheader_recv_future skip_bodyr} body_futurees rrazHTTP1Connection._read_messagesU#u $ KK88)D)D9M{{))1$11 !(+(8(8 ++002T[[5O5OO%)1)C)C)#K'+&9&9+&F #NG~~"*"D"D^"T,;)#.3*!)!B!B>!R+9((/%+ 151E1E"G2.*#' )'2 -%-%>%>z7%S"%1,,, -{{"&+#P#-g630023 M  ! ! #WI~~!*h.H.HIII,,800776A $I!3;!%I$$$(726IW6T&55?$F ,,X666;;x(N:4CWCWKK%%&HI"oo,0NNO((7H *))1))) )"%"2"2 $ 3 3 8 8 :T=O=O O +191K1K##'D ''4>>&+#-g6&OO%& '',,.KK+ **, ..t/H/HI))))~~$"<"< {{"#-g630023 M  ! ! ###-g630023 M  ! ! #c2# ''!JJL D#-g63002333 M  ! ! #Q!0- - -`33c7* #//)#LL)GV KK--/#(6#-g63002333 M  ! ! #E)&&*33&&  LL=t||Q O>>kk''(KLLL JJL"-g63002333 M  ! ! # 33#-g63002333 M  ! ! #s ]A XSXA"S ;S<S BXT?.T</T?3X ]U ,]B&X-U.BX.U/X5AU" UU"0XW>AX XX][".A [9Y<:[[2]$Z>5 ]>[][""[2%[/*]2\=\ \=\' #\==]c~d|_d|_d|_|j|jj dyy)zClears the callback attributes. This allows the request handler to be garbage collected more quickly in CPython by breaking up reference cycles. N)_write_callback _write_future_close_callbackrKrrs rrWz HTTP1Connection._clear_callbacks6s= $!# ;; " KK * *4 0 #rcallbackc||_y)a$Sets a callback that will be run when the connection is closed. Note that this callback is slightly different from `.HTTPMessageDelegate.on_connection_close`: The `.HTTPMessageDelegate` method is called when the connection is closed while receiving a message. This callback is used when there is not an active delegate (for example, on the server side this callback is used if the client closes the connection after sending its request but before receiving all the response. N)rrrs rrz"HTTP1Connection.set_close_callbackBs  (rc|j|j}d|_||jjst|jd|j yr)rrUrrrWrs rrz$HTTP1Connection._on_connection_closePsV    +++H#'D J""'') .t/B/BD I rc|j|jj|j|jj st |jdyyr)rKrtrWrUrrrs rrtzHTTP1Connection.close\sO ;; " KK    ""'') .t/B/BD I*rc|j|j}d|_|jjst |jd|S)a>Take control of the underlying stream. Returns the underlying `.IOStream` object and stops all further HTTP processing. May only be called during `.HTTPMessageDelegate.headers_received`. Intended for implementing protocols like websockets that tunnel over an HTTP handshake. N)rWrKrUrr)rrKs rdetachzHTTP1Connection.detachcsG  ""'') .t/B/BD I rtimeoutc||_y)ztSets the body timeout for a single request. Overrides the value from `.HTTP1ConnectionParameters`. N)rR)rrs rset_body_timeoutz HTTP1Connection.set_body_timeoutrs %rrBc||_y)zwSets the body size limit for a single request. Overrides the value from `.HTTP1ConnectionParameters`. N)rQ)rrBs rset_max_body_sizez!HTTP1Connection.set_max_body_sizeys ,rrrchunk Future[None]cg}|jrdt|tjsJ||_|j t |dd|dd|jdvxrd|v|_nJt|tjsJ|jJ|jJ||_ |j t d|d|d fz|jjd k(xrQ|jjd k7xr6|jd vxr&|jd kxs|jdk\xrd|v|_|jjd k(r|jrd|d<|jjdk(r2|jjddj!dk(rd|d<|jrd|d<|jsH|jjd k(s't#tj|jdk(rd|_nd|vrt'|d|_nd|_d|j)D}|j+d|D|D]%}t,j/|st1d|zd}|j2j5rFt7x}|_|j;t=j>|jA|St7x}|_djC|dz}|r||jE|z }|j2jG||_$tK|jH|jL|S)z+Implements `.HTTPConnection.write_headers`.r z HTTP/1.1)POSTPUTPATCHrkNzHTTP/1.1 %d %sHTTP/1.1rg)rhrirjrt ConnectionzHTTP/1.0 keep-alivez Keep-Alivechunkedrlrhc3VK|]!\}}t|dzt|z#yw)z: N)r).0nvs r z0HTTP1Connection.write_headers..s+ 59QJqMD :a= 0 s')c3>K|]}|jdyw)latin1N)encode)rlines rrz0HTTP1Connection.write_headers..sDtT[[*Dsz+Illegal characters (CR or LF) in header: %r s )'rLr2r RequestStartLinerXappendrr|r[r{rZrYversionr}rVr~lowerrr\ parse_intget_allextend CR_OR_LF_REsearch ValueErrorrKrrr set_exceptionr rr exceptionjoin _format_chunkrr]r_on_write_complete) rrrrlines header_linesrfuturedatas r write_headerszHTTP1Connection.write_headerssW >>j(*C*CD DD'1D $ LLAqAyIJ K !!%==4$G3  ! j(*D*DE EE++7 77((4 44(2D % LL.*Q-A1OOP Q ((00J> 4,,33v= 4 OO:5 4 __s*Djoo.D 4%G3  !"((00J>..(/ %((00J>))--lB?EEG<W(4 %  +4G' (~~  $ $ + +v 5H.. ;@@CG/0D ,  (/8AQ9R/SD ,/3D , =D__=N   D|DD WD!!$' !NQU!UVV W ;;   *0( 2FT'  !;!;!= >     +1( 2FT'<<&4D**511"&++"3"3D"9D  $T%8%8$:Q:Q R rc2|j\|xjt|zc_|jdkr/|jjt j d|j r"|r tdt|zdz|zdzS|S)Nrz,Tried to write more data than Content-Lengthz%xr)r\lenrKrtr HTTPOutputErrorr[r)rrs rrzHTTP1Connection._format_chunks  + + 7  , ,E : ,//!3 !!#..B  Us5z)*W4ur~rris_transfer_encoding_chunkedgetattr)rrrconnection_headers rryzHTTP1Connection._can_keep_alive&s ;; $ $#KK 5  ( 1 7 7 9     +$/ /  '+G4z8T2oE% 4 4rc|j|js|jr|jy|jj d|j jst|j dyy)NF) rWrLrVrtrKrrUrr)rrs rrzHTTP1Connection._finish_request:sd ~~$"<"< JJL  &""'') .t/B/BD I*rrct|jdjd}|jd}|d|j d}t j j||d}||fS)Nrz   )rdecodelstripfindrstripr HTTPHeadersparse)rrdata_streolrrs rrvzHTTP1Connection._parse_headersEsm dkk(34;;FCmmD!ds^**40 &&,,Xcd^<7""rr}czd|vrd|dvrPtjd|dtfdDrtjd|dzd|d< t |d}tt||jkDrtjdd}t|}|d k(r |s|d vrtjd |zd}|r|j|S||j||S|jr|j|Sy#t $rtjd|dzwxYw) Nrk,z,\s*c3.K|] }|dk7yw)rNr)ripiecess rrz-HTTP1Connection._read_body..]s6!qF1I~6sz$Multiple unequal Content-Lengths: %rrz*Only integer Content-Length is allowed: %szContent-Length too longr)Nrz*Response with code %d should not have body)resplitanyr r3rrrrGrQr_read_chunked_body_read_fixed_bodyrL_read_body_until_close)rr}rr^content_length is_chunkedrs @rrzHTTP1Connection._read_bodyQsu w &g.//'73C+DE6v66"11>!"234-31I() 09'BR:S0TC(4+>+>>--.GHH!N1': 3;^9<--@4GN **84 4  %((B B >>..x8 8= --@./0 s D%D:rcK|dkDr|jjt|jj|dd{}|t |z}|j r |jr5tt5|j|}| |d{ddd|dkDryy7g7#1swYxYww)NrTpartial) rK read_bytesminrMr?rrSrLr&r data_received)rrr^bodyrets rrz HTTP1Connection._read_fixed_bodysq //DKK**N;T0D c$i 'N''4>>-g6""006C! " q """sBAB>B.9B>B2B0B2 B>,B>0B22B;7B>cxKd} |jjddd{} tt|dd}|dk(r>|jjdd{}|dk7rt j d y||z }||jkDrt j d |}|r|jjt||jjd d{}|t|z}|jr |jr5tt 5|j#|}| |d{ddd|r|jjdd{}|dk(sJz7Z#t$rt j dwxYw7?77e#1swYdxYw7Ew) NrTr@rdzinvalid chunk sizerz%improperly terminated chunked requestzchunked body too larger)rK read_until parse_hex_intrrr r3rrQrrMr?rrSrLr&r r) rr^ total_size chunk_len_str chunk_lencrlf bytes_to_readrrs rrz"HTTP1Connection._read_chunked_bodys "&++"8"8B"8"OOM D)*]3B5G*HI A~![[33A667?"11? ) #JD///--.FGG%M"kk44 t{{'='=>5U+ ++t~~1':&&44U;?"%II&  //22D7? "?;O D--.BCC D7&&& 3s$F:FF:F#F:&F%'B F:1F(29F:+F,F*F, F:F:2F83F:F""F:(F:*F,,F51F:c K|jjd{}|jr |jr6t t 5|j |}| |d{dddyy7S7#1swYyxYwwr)rKread_until_closerSrLr&r r)rr^rrs rrz&HTTP1Connection._read_body_until_closesz[[1133##t~~)'2 ,,T2?II  (64   s9BA4+B A8%A6&A8* B6A88B=BNNr r)rrr!N)rzOptional[Future[None]]r!N)-r"r#r$r6r IOStreamrFrr=objectrr HTTPMessageDelegaterrbrarWrrrrtrrHrrGrrrr{rbytesrrrrrryrrstrrvrrrrrrrrJrJmsn7;$( 7#!!7#7#23 7# &! 7#  7#r ,h&B&B ,yQU ,xH,H,HxTxt 1 (8HRX4F+G (D (  J )) %%$%,s,t,"& U(33X5O5OOPU%%U U  Un5U 5^$P@ ="33>F>R>R ( J #5 #U38L8L3L-M #33%%3.. 3 )D/ " 3j "! "-5-I-I "  " #1M1M #RV #D 44 rrJceZdZdZdej deddfdZdeejejfdejde e dfd Zd eddfd Zdd Zdd Zy)r`zDWraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.r^r?r!Nc.||_||_d|_yr) _delegate _chunk_size _decompressor)rr^r?s rrz_GzipMessageDelegate.__init__s!%!rrrc|jddjdk(r't|_|j d|d|d=|j j ||S)NzContent-EncodingrgzipzX-Consumed-Content-Encoding)r~rr raddrrz)rrrs rrzz%_GzipMessageDelegate.headers_receivedsc ;;)2 . 4 4 6& @!1!3D  KK5w?Q7R S*+~~..z7CCrrcK|jr|}|r|jj||j}|r'|jj |}| |d{|jj }|r|st jd|ryy|jj |}| |d{yy7a7w)Nz8encountered unconsumed gzip data without making progress)rrDrrrunconsumed_tailr r3)rrcompressed_data decompressedrs rrz"_GzipMessageDelegate.data_receiveds   #O!#11<<#T%5%5   ..66|DC! "&"4"4"D"D"<"11R"....u5C "s*ACB?5C$C8C9CCc|j'|jj}|r td|jj S)Nz:decompressor.flush returned data; possible truncated input)rflushrrr)rtails rrz_GzipMessageDelegate.finishsL    )%%++-D!P~~$$&&rc6|jjSr)rrurs rruz(_GzipMessageDelegate.on_connection_closes~~1133rr )r"r#r$r6r rrGrrrr{rrrrzrrrrurrrr`r`sN"!=!="3"SW" D(33X5O5OOP D%% D )D/ " D4* '4rr`c eZdZdZ d dej deedeeddfdZ d dZ d e jddfd Z d e jddfd Zy)HTTP1ServerConnectionzAn HTTP/1.x server.NrKrMrNr!cT||_| t}||_||_d|_y)z :arg stream: an `.IOStream` :arg params: a `.HTTP1ConnectionParameters` or None :arg context: an opaque application-defined object that is accessible as ``connection.context`` N)rKr=rMrN_serving_future)rrKrMrNs rrzHTTP1ServerConnection.__init__s. >.0F  #rcK|jj|jJ |jd{y7#t$rYywxYww)zmCloses the connection. Returns a `.Future` that resolves after the serving loop has exited. N)rKrtr' Exceptionrs rrtzHTTP1ServerConnection.closesQ ##/// && & &   s8)AAAAAA A AAAr^ct|tjsJtj|j |}||_|jjj|dy)znStarts serving requests on this connection. :arg delegate: a `.HTTPServerConnectionDelegate` c"|jSr)r)fs rz5HTTP1ServerConnection.start_serving..,s ahhjrN) r2r HTTPServerConnectionDelegaterconvert_yielded_server_request_loopr'rKrp add_future)rr^futs r start_servingz#HTTP1ServerConnection.start_serving#sX (H$I$IJJJ!!$";";H"EF" &&s,@ArcK t|jd|j|j}|j ||} |j |d{}|s |j|ytj"dd{77#t jt jtjf$rY|j|yt$r$|jY|j|yt$r;tj dd|jY|j|ywxYw7#|j|wxYww)NTFr/r0r)rJrKrMrN start_requestrbr rrUnsatisfiableReadErrorasyncioCancelledErroron_closerrtr)r r4sleep)rr^connrequest_delegaters rr0z*HTTP1ServerConnection._server_request_loop.s= $&t{{E4;; U#+#9#9$#E  $ 2 23C DDC   d #mmA&&&+E..33**    d #'JJL   d #!MM"6FJJL   d # '   d #sE ?E BBBE E 2E E E B2EE E E/E 0E /E1E 2E EE EE rr )r"r#r$r6r rrr=rrrtr r.r3r0rrrr%r%s 7;$( $!!$23$&! $  $&  Bh&K&K BPT B$ ==$ $rr%z[0-9]+z [0-9a-fA-F]+sr!c^tj|td|zt|S)z+Parse a non-negative integer from a string.znot an integer: %r)DIGITS fullmatchrrGr=s rrrPs- "-122 q6Mrc`tj|td|zt|dS)z7Parse a non-negative hexadecimal integer from a string.znot a hexadecimal integer: %r) HEXDIGITSr@rrGrAs rrrWs01%81<== q":rrcd|vryd|vrtjd|djdk(rytjd|dz)zReturns true if the headers specify Transfer-Encoding: chunked. Raise httputil.HTTPInputError if any other transfer encoding is used. rlFrkz6Message with both Transfer-Encoding and Content-LengthrTz Unsupported Transfer-Encoding %s)r r3r)rs rrr^sn')7"%% D  "#))+y8  ! !*W5H-II r)1r6r7r7rr:tornado.concurrentrrrtornado.escaperrtornadorr r tornado.logr r tornado.utilr typingrrrrrrrcompilerr)rr&r=HTTPConnectionrJrr`r%r?rDrrGrrrrFrrrrrNs     ,()KJJbjj"  i ""6%%BQ h--Q h;4877;4|J$J$Z I  BJJ ' SS(*>*>4r