\d.TdZddlmZmZmZddlmZddlZddlZddl Z gdZ gdZ gdZ d Z hd jZd Zd ZGd dZGddeZGddeZGddeZGddeZdS)z/Base classes for server/gateway implementations) FileWrapper guess_scheme is_hop_by_hop)HeadersN) BaseHandler SimpleHandlerBaseCGIHandler CGIHandler IISCGIHandler read_environ)MonTueWedThuFriSatSun) NJanFebMarAprMayJunJulAugSepOctNovDecc tj|\ }}}}}}}}} dt||t|||||fzS)Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)timegmtime _weekdayname _monthname) timestampyearmonthdayhhmmsswdyzs !..\python\lib\wsgiref\handlers.pyformat_date_timer1sO-1[-C-C*D%b"b"a 0R#z%0$B4 > HTTPS AUTH_TYPE PATH_INFO REMOTE_USER SCRIPT_NAME CONTENT_TYPE QUERY_STRING REMOTE_IDENTCONTENT_LENGTHREQUEST_METHODct|pU|dp@|dp+|dot|ddS)NHTTP_SSL_ REDIRECT_ ) _is_request startswith_needs_transcode)ks r0rDrDsd q>> CQ\\'22 Call66J6J C LL % % A*:1QRR5*A*ACr2cTtj}d} dd|n#t$rd}YnwxYwi}tjD]F\}}t|r+tjdkrtj dd }| dr)|d d}n| d rn| d r-d |vr)|d d}nS||d d}n)||| d}|||<H|S) z'Read environment, fixing HTTP variablessurrogateescapezutf-8replacewin32SERVER_SOFTWAREzmicrosoft-iis/ iso-8859-1zapache/z simplehttp/zpython/3) sysgetfilesystemencodingencode LookupErrorosenvironitemsrDplatformgetlowerrCdecode)encescrRrEvsoftwares r0r r "s # % %C C '3 G    ""++1 A  ( <|w&&:>>*;R@@FFHH&&'788F))00>>AA((33F '' 66 F"h..))00>>AA i0077 EEAA HHS#&&--l;; Ns . ==ceZdZdZdZdZdZdZdZdZ dZ e Z e ZeZdZdZdgZd ZdxZZdZdZd Zd Zd Zd ZdZdZdZd#dZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.d Z/d!Z0d"Z1dS)$rz+Manage the invocation of a WSGI application)rrTFz1.0Nz500 Internal Server Error)z Content-Typez text/plains;A server error occurred. Please contact the administrator.rc. |||j|j|_|dS#t t tf$rYdS |YdS#| xYwxYw)zInvoke the applicationN) setup_environrRstart_responseresultfinish_responseConnectionAbortedErrorBrokenPipeErrorConnectionResetError handle_errorclose)self applications r0runzBaseHandler.runs     %+dlD4GHHDK  " " " " "&9MN    FF  !!######  s$AAB!B#A::BBc|jx}|_|||d<||d<|j|d<|j|d<||d<|j |d<|j |d<|j |j |d <|j r$|j r|d |j dSdSdS) z&Set up the environment for one requestz wsgi.inputz wsgi.errorsz wsgi.versionz wsgi.run_oncezwsgi.url_schemezwsgi.multithreadzwsgi.multiprocessNzwsgi.file_wrapperrK) os_environcopyrR add_cgi_vars get_stdin get_stderr wsgi_version wsgi_run_once get_schemewsgi_multithreadwsgi_multiprocesswsgi_file_wrapper origin_serverserver_software setdefault)rgenvs r0r^zBaseHandler.setup_environs"_11333dl #'>>#3#3L#'??#4#4M#'#4N#'#5O#'??#4#4 #'#8 #'#9   ! -'+'=C# $   C$"6 C NN,T-A B B B B B C C C Cr2cN |r|s3|jD]}||||dS#t |jdr|jxYw)a>Send any iterable data, then close self and the iterable Subclasses intended for use in asynchronous servers will want to redefine this method, such that it sets up callbacks in the event loop to iterate over the data, and to call 'self.close()' once the response is finished. rfN)result_is_filesendfiler`writefinish_contentrfhasattrrgdatas r0razBaseHandler.finish_responses &&(( &  & K%%DJJt$$$$##%%% JJLLLLL t{G,, $ !!### s AA331B$c*t|jS)z Return the URL scheme being used)rrRrgs r0rrzBaseHandler.get_schemesDL)))r2c t|j}|dkrt|j|jd<dSdS#t t tf$rYdSwxYw)z@Compute Content-Length or switch to chunked encoding if possiblerContent-LengthN)lenr`str bytes_sentheaders TypeErrorAttributeErrorNotImplementedError)rgblockss r0set_content_lengthzBaseHandler.set_content_lengthsp %%Fqyy14T_1E1E -.y.)<=    DD s<AAcDd|jvr|dSdS)zqMake any necessary header changes or defaults Subclasses can extend this to add other defaults. rN)rrrs r0cleanup_headerszBaseHandler.cleanup_headerss1 4< / /  # # % % % % % 0 /r2c2|r |jr d}n#d}wxYw|jtd||_|||_||d}t |dks Jd|dds Jd|ddks Jd  |D]Q\}}||d }||d }t|rJd |d|dR|j S)z4'start_response()' callable as specified by PEP 3333NzHeaders already set!Statusz$Status must be at least 4 charactersz(Status message must begin w/3-digit code z+Status message must have a space after codeTz Header namez Header valuezHop-by-hop header, 'z: z', not allowed) headers_sentrAssertionErrorstatus headers_class_convert_string_typerisdigitrr})rgrrexc_infonamevals r0r_zBaseHandler.start_responsesq  9 $ 4 \ % !788 8 ))'22 **68<<6{{A~~~D~~~bqbz!!##OO%OOOOay#~~~L~~~ J$ J J c00}EE//^DD(..JJIdIIcIIIJJJJzsct|tur|Std|t |)zConvert/check value type.z!{0} must be of type str (got {1}))typerrformatrepr)rgvaluetitles r0rz BaseHandler._convert_string_typesC ;;#  L / 6 6ud5kk J J   r2cB|jr|r|d|jd|jddd|jvrI|dttjzd|j r=d|jvr6|d|j zdd Sd Sd Sd S|d |jzdd S) z6Transmit version/status/date/server, via self._write()zHTTP/rz rLDatez Date: %s Serverz Server: %s z Status: %s N) rvclient_is_modern_write http_versionrrOrr1r"rwrs r0 send_preamblezBaseHandler.send_preambles;   O$$&& ` 1B1B1B4;;;OWWXdeefff--KK'*:49;;*G*GGOOP\]]'`HDL,H,HKK!1D4H!H P PQ] ^ ^_____ ` ` ``,H,H KK)DK7?? MM N N N N Nr2c^t|tus Jd|jstd|js)t ||_|n|xjt |z c_||| dS)z+'write()' callable as specified by PEP 3333z)write() argument must be a bytes instancezwrite() before start_response()N) rbytesrrrrr send_headersr_flushrs r0r}zBaseHandler.writesDzzU""" 7#""{ ) !BCC C" )!$iiDO        OOs4yy (OO D r2cdS)aPlatform-specific file transmission Override this method in subclasses to support platform-specific file transmission. It is only called if the application's return iterable ('self.result') is an instance of 'self.wsgi_file_wrapper'. This method should return a true value if it was able to actually transmit the wrapped file-like object using a platform-specific approach. It should return a false value if normal iteration should be used instead. An exception can be raised to indicate that transmission was attempted, but failed. NOTE: this method should call 'self.send_headers()' if 'self.headers_sent' is false and it is going to attempt direct transmission of the file. Frs r0r|zBaseHandler.sendfile(s $ur2cv|js1|jdd|dSdS)z.Ensure headers and content have both been sentr0N)rrrxrrs r0r~zBaseHandler.finish_content=sE   L # #$4c : : :         Dr2c t|jdr|jdx|_x|_x|_|_d|_d|_dS#dx|_x|_x|_|_d|_d|_wxYw)zClose the iterable (if needed) and reset all instance vars Subclasses may want to also drop the client connection. rfNrF)rr`rfrrrRrrrs r0rfzBaseHandler.closeGs  ;t{7++ $ !!###FJ JDK J$, Jt|DOU!2!2!2GK JDK J$, Jt|DOU!2!:!:!:!:s .A,Bc|d|_|jr|r=||t |jdSdS)z1Transmit headers to the client, via self._write()TN)rrrvrrrrrrs r0rzBaseHandler.send_headersTsx  ! -T%:%:%<%< -    KKdl++ , , , , , - -r2cB|j}|duot|j|S)z@True if 'self.result' is an instance of 'self.wsgi_file_wrapper'N)ru isinstancer`)rgwrappers r0r{zBaseHandler.result_is_file]s&(d"Fz$+g'F'FFr2cH|jddkS)z,True if client can accept status and headersSERVER_PROTOCOLzHTTP/0.9)rRupperrs r0rzBaseHandler.client_is_moderncs!|-.4466*DDr2c ddlm}|}||d|d|d|j||d}dS#d}wxYw)zLog the 'exc_info' tuple in the server log Subclasses may override to retarget the output or change its format. r)print_exceptionrN) tracebackrrotraceback_limitflush)rgrrstderrs r0 log_exceptionzBaseHandler.log_exceptionhs|  1 1 1 1 1 1__&&F O Xa[(1+$f    LLNNNHHHtHOOOOs AAAc|tj|js;||j|j|_|dSdS)z>Log current error, and send error output to client if possibleN) rrMrr error_outputrRr_r`rars r0rezBaseHandler.handle_errorxse 3<>>***  #++DL$:MNNDK  " " " " " # #r2cr||j|jddtj|jgS)aZWSGI mini-app to create error output By default, this just uses the 'error_status', 'error_headers', and 'error_body' attributes to generate an output page. It can be overridden in a subclass to dynamically generate diagnostics, choose an appropriate message for the user's preferred language, etc. Note, however, that it's not recommended from a security perspective to spit out diagnostics to any old user; ideally, you should have to do something special to enable diagnostic output, which is why we don't include any here! N) error_status error_headersrMr error_body)rgrRr_s r0rzBaseHandler.error_outputs8 t();AAA)>s|~~NNN  r2ct)aOverride in subclass to buffer data for send to client It's okay if this method actually transmits the data; BaseHandler just separates write and flush operations for greater efficiency when the underlying system actually has such a distinction. rrs r0rzBaseHandler._writes "!r2ct)zOverride in subclass to force sending of recent '_write()' calls It's okay if this method is a no-op (i.e., if '_write()' actually sends the data. rrs r0rzBaseHandler._flushs "!r2ct)z4Override in subclass to return suitable 'wsgi.input'rrs r0rnzBaseHandler.get_stdin!!r2ct)z5Override in subclass to return suitable 'wsgi.errors'rrs r0rozBaseHandler.get_stderrrr2ct)z>Override in subclass to insert CGI variables in 'self.environ'rrs r0rmzBaseHandler.add_cgi_varsrr2N)2__name__ __module__ __qualname____doc__rprsrtrqrvrrwr rkrrurrrrrrrr`rrrrir^rarrrrr_rrr}r|r~rfrr{rrrerrrrnrormrr2r0rr^s55LMMLO  J$MO.L23MOJFVLGJ0CCC*4***   &&&8    O O O** ; ; ;---GGG EEE  ###!!!&"""""""""""""""""r2rc:eZdZdZ d dZdZdZdZdZd Z d S) r aqHandler that's just initialized with streams, environment, etc. This handler subclass is intended for synchronous HTTP/1.0 origin servers, and handles sending the entire response output, given the correct inputs. Usage:: handler = SimpleHandler( inp,out,err,env, multithread=False, multiprocess=True ) handler.run(app)TFcZ||_||_||_||_||_||_dSr)stdinstdoutrbase_envrsrt)rgrrrrR multithread multiprocesss r0__init__zSimpleHandler.__init__s6    +!-r2c|jSr)rrs r0rnzSimpleHandler.get_stdins zr2c|jSr)rrs r0rozSimpleHandler.get_stderrs {r2cD|j|jdSr)rRupdaterrs r0rmzSimpleHandler.add_cgi_varss  DM*****r2c|j|}||t|krdSddlm}|dt  ||d}|sdS|j|}))Nr)warnz9SimpleHandler.stdout.write() should not do partial writes)rr}rwarningsrDeprecationWarning)rgrr`rs r0rzSimpleHandler._writes""4(( >Vs4yy00 F!!!!!!  H     -=D [&&t,,F  -r2cZ|j|jj|_dSr)rrrrs r0rzSimpleHandler._flushs& k' r2N)TF) rrrrrrnrormrrrr2r0r r s  (-....+++ - - -(((((r2r ceZdZdZdZdS)r aCGI-like systems using input/output/error streams and environ mapping Usage:: handler = BaseCGIHandler(inp,out,err,env) handler.run(app) This handler class is useful for gateway protocols like ReadyExec and FastCGI, that have usable input/output/error streams and an environment mapping. It's also the base class for CGIHandler, which just uses sys.stdin, os.environ, and so on. The constructor also takes keyword arguments 'multithread' and 'multiprocess' (defaulting to 'True' and 'False' respectively) to control the configuration sent to the application. It sets 'origin_server' to False (to enable CGI-like output), and assumes that 'wsgi.run_once' is False. FN)rrrrrvrr2r0r r s&MMMr2r c eZdZdZdZiZdZdS)r aCGI-based invocation via sys.stdin/stdout/stderr and os.environ Usage:: CGIHandler().run(app) The difference between this class and BaseCGIHandler is that it always uses 'wsgi.run_once' of 'True', 'wsgi.multithread' of 'False', and 'wsgi.multiprocess' of 'True'. It does not take any initialization parameters, but always uses 'sys.stdin', 'os.environ', and friends. If you need to override any of these parameters, use BaseCGIHandler instead. Tc t|tjjtjjtjtdddS)NFTrr)r rrMrbufferrrr rs r0rzCGIHandler.__init__sI #)"CJ$5sz NND      r2Nrrrrrqrkrrr2r0r r s;  MJ     r2r c eZdZdZdZiZdZdS)r aCGI-based invocation with workaround for IIS path bug This handler should be used in preference to CGIHandler when deploying on Microsoft IIS without having set the config allowPathInfo option (IIS>=7) or metabase allowPathInfoForScriptMappings (IIS<7). Tc vt}|dd}|dd}|dz|dzr|t|d|d<t|t jjt j jt j |dddS)Nr5rHr7/FTr) r rUrCrr rrMrrrr)rgrRpathscripts r0rzIISCGIHandler.__init__1s{{;++]B// H  , , 6#'F #5GK  #)"CJ$5sz T      r2Nrrr2r0r r s9 MJ      r2r )rutilrrrrrrMrQr"__all__r$r%r1 __contains__rBrDr rr r r r rr2r0rs55::::::::::    A@@ 888   CCC999xP"P"P"P"P"P"P"P"f /(/(/(/(/(K/(/(/(d]0