o w[eT@sdZddlmZmZmZddlmZddlZddlZddl Z gdZ gdZ gdZ d d Z hd jZd d ZddZGdddZGdddeZGdddeZGdddeZGdddeZdS)z/Base classes for server/gateway implementations) FileWrapper guess_scheme is_hop_by_hop)HeadersN) BaseHandler SimpleHandlerBaseCGIHandler CGIHandler IISCGIHandler read_environ)MonTueWedThuFriSatSun) NJanFebMarAprMayJunJulAugSepOctNovDecc Cs:t|\ }}}}}}}}} dt||t|||||fS)Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)timegmtime _weekdayname _monthname) timestampyearmonthdayhhmmsswdyzr.'/usr/lib/python3.10/wsgiref/handlers.pyformat_date_timesr0> HTTPS AUTH_TYPE PATH_INFO REMOTE_USER SCRIPT_NAME CONTENT_TYPE QUERY_STRING REMOTE_IDENTCONTENT_LENGTHREQUEST_METHODcCs6t|p|dp|dp|dot|ddS)NHTTP_SSL_ REDIRECT_ ) _is_request startswith_needs_transcode)kr.r.r/rAsrAcCst}d}zdd|Wn tyd}Ynwi}tjD]S\}}t|rptjdkrgtj dd }| drE|d d}n+| d rKn%| d r]d |vr]|d d}n||d d}n ||| d}|||<q!|S) z'Read environment, fixing HTTP variablessurrogateescapezutf-8replacewin32SERVER_SOFTWAREzmicrosoft-iis/ iso-8859-1zapache/z simplehttp/zpython/3) sysgetfilesystemencodingencode LookupErrorosenvironitemsrAplatformgetlowerr@decode)encescrNrBvsoftwarer.r.r/r "s0     r c@s"eZdZdZdZdZdZdZdZdZ dZ e Z e ZeZdZdZdgZd ZdZZdZdZd Zd d Zd dZddZddZddZddZd;ddZ ddZ!ddZ"ddZ#dd Z$d!d"Z%d#d$Z&d%d&Z'd'd(Z(d)d*Z)d+d,Z*d-d.Z+d/d0Z,d1d2Z-d3d4Z.d5d6Z/d7d8Z0d9d:Z1dS)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. r`N)result_is_filesendfilerZwritefinish_contenthasattrr`radatar.r.r/r[s     zBaseHandler.finish_responsecCs t|jS)z Return the URL scheme being used)rrNrar.r.r/rks zBaseHandler.get_schemec CsJzt|j}Wn tttfyYdSw|dkr#t|j|jd<dSdS)z@Compute Content-Length or switch to chunked encoding if possiblerContent-LengthN)lenrZ TypeErrorAttributeErrorNotImplementedErrorstr bytes_sentheaders)rablocksr.r.r/set_content_lengthszBaseHandler.set_content_lengthcCsd|jvr |dSdS)zqMake any necessary header changes or defaults Subclasses can extend this to add other defaults. r{N)rrrzr.r.r/cleanup_headerss  zBaseHandler.cleanup_headerscCs|rz|jr|d|d|dWd}n d}w|jdur#td||_|||_||d}t|dkss zBaseHandler.finish_contentc Csz5t|jdr!|jWd|_|_|_|_d|_d|_dSWd|_|_|_|_d|_d|_dSd|_|_|_|_d|_d|_w)zClose the iterable (if needed) and reset all instance vars Subclasses may want to also drop the client connection. r`NrF)rwrZr`rrrNrrrzr.r.r/r`Hs  zBaseHandler.closecCs<|d|_|jr|r||t|jdSdS)z1Transmit headers to the client, via self._write()TN)rrrorrrrrrzr.r.r/rUs zBaseHandler.send_headerscCs|j}|duo t|j|S)z@True if 'self.result' is an instance of 'self.wsgi_file_wrapper'N)rn isinstancerZ)rawrapperr.r.r/rs^szBaseHandler.result_is_filecCs|jddkS)z,True if client can accept status and headersSERVER_PROTOCOLzHTTP/0.9)rNupperrzr.r.r/rdszBaseHandler.client_is_moderncCsLz"ddlm}|}||d|d|d|j||Wd}dSd}w)zLog the 'exc_info' tuple in the server log Subclasses may override to retarget the output or change its format. r)print_exceptionrrN) tracebackrrhtraceback_limitflush)rarrstderrr.r.r/ log_exceptionis  zBaseHandler.log_exceptioncCs6|t|js||j|j|_|dSdS)z>Log current error, and send error output to client if possibleN) rrIrr error_outputrNrYrZr[rzr.r.r/r_ys  zBaseHandler.handle_errorcCs$||j|jddt|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_headersrIr error_body)rarNrYr.r.r/rs zBaseHandler.error_outputcCt)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. rrxr.r.r/rszBaseHandler._writecCr)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. rrzr.r.r/rszBaseHandler._flushcCr)z4Override in subclass to return suitable 'wsgi.input'rrzr.r.r/rgzBaseHandler.get_stdincCr)z5Override in subclass to return suitable 'wsgi.errors'rrzr.r.r/rhrzBaseHandler.get_stderrcCr)z>Override in subclass to insert CGI variables in 'self.environ'rrzr.r.r/rfrzBaseHandler.add_cgi_varsN)2__name__ __module__ __qualname____doc__rirlrmrjrorrpr rdrrnrrrrrrrrZrrrrcrXr[rkrrrYrrrurtrvr`rrsrrr_rrrrgrhrfr.r.r.r/r^sX      rc@sDeZdZdZ dddZddZdd Zd d Zd d ZddZ dS)raqHandler 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)TFcCs(||_||_||_||_||_||_dSr)stdinstdoutrbase_envrlrm)rarrrrN multithread multiprocessr.r.r/__init__s  zSimpleHandler.__init__cC|jSr)rrzr.r.r/rgzSimpleHandler.get_stdincCrr)rrzr.r.r/rhrzSimpleHandler.get_stderrcCs|j|jdSr)rNupdaterrzr.r.r/rfszSimpleHandler.add_cgi_varscCs^|j|}|dus|t|krdSddlm}|dt ||d}|s(dS|j|}q)Nr)warnz9SimpleHandler.stdout.write() should not do partial writes)rrur|warningsrDeprecationWarning)raryrZrr.r.r/rs    zSimpleHandler._writecCs|j|jj|_dSr)rrrrzr.r.r/rs zSimpleHandler._flushN)TF) rrrrrrgrhrfrrr.r.r.r/rs   rc@seZdZdZdZdS)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)rrrrror.r.r.r/r sr c@ eZdZdZdZiZddZdS)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 Cs(tj|tjjtjjtjtddddS)NFTrr)r rrIrbufferrrr rzr.r.r/rs zCGIHandler.__init__Nrrrrrjrdrr.r.r.r/r s  r c@r)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 Csjt}|dd}|dd}|d|dr"|t|d|d<tj|tjjtj jtj |ddddS)Nr3rDr5/FTr) r rQr@r|r rrIrrrr)rarNpathscriptr.r.r/r2s   zIISCGIHandler.__init__Nrr.r.r.r/r s  r )rutilrrrrrrIrMr __all__r"r#r0 __contains__r?rAr rrr r r r.r.r.r/s( <V2