f ž _¥Tc@sÈdZddlmZmZmZddlmZddlZddlZddl Z gd¢Z gd¢Z gd¢Z d d „Z hd £jZd d „Zdd„ZGdd„dƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZdS)z/Base classes for server/gateway implementationsé)Ú FileWrapperÚ guess_schemeÚ is_hop_by_hop)ÚHeaderséN)Ú BaseHandlerÚ SimpleHandlerÚBaseCGIHandlerÚ CGIHandlerÚ IISCGIHandlerÚ read_environ)ZMonZTueZWedZThuZFriZSatZSun) NZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc Cs:t |¡\ }}}}}}}}} dt||t|||||fS)Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)ÚtimeZgmtimeÚ _weekdaynameÚ _monthname) Z timestampZyearZmonthZdayZhhZmmZssZwdZyZz©rú)/usr/lib64/python3.10/wsgiref/handlers.pyÚformat_date_timesÿr> Ú SCRIPT_NAMEZ REMOTE_IDENTZ QUERY_STRINGZ AUTH_TYPEÚ PATH_INFOZCONTENT_LENGTHZ CONTENT_TYPEZREQUEST_METHODZHTTPSZ REMOTE_USERcCs6t|ƒp4| d¡p4| d¡p4| d¡o4t|dd…ƒS)NZHTTP_ZSSL_Z REDIRECT_i )Ú _is_requestÚ startswithÚ_needs_transcode)ÚkrrrrsÿrcCsît ¡}d}zd d|¡Wnty2d}Yn0i}tj ¡D]¦\}}t|ƒràtjdkrÎtj  dd¡  ¡}|  d¡rŠ| d¡  d¡}nV|  d ¡r–nJ|  d ¡rºd |vrº| d¡  d¡}n&| |d¡  d¡}n| ||¡  d¡}|||<qB|S) z'Read environment, fixing HTTP variablesZsurrogateescapeÚzutf-8ZreplaceZwin32ÚSERVER_SOFTWAREzmicrosoft-iis/ú iso-8859-1zapache/z simplehttp/zpython/3) ÚsysZgetfilesystemencodingÚencodeZ LookupErrorÚosÚenvironZitemsrZplatformÚgetZlowerrZdecode)ZencZescrrZvZsoftwarerrrr "s0     ÿþ r c@s"eZdZdZdZdZdZdZdZdZ dZ e ƒZ e ZeZdZdZdgZd ZdZZdZdZd Zd d „Zd d„Zdd„Zdd„Zdd„Zdd„Zd;dd„Z dd„Z!dd„Z"dd„Z#dd „Z$d!d"„Z%d#d$„Z&d%d&„Z'd'd(„Z(d)d*„Z)d+d,„Z*d-d.„Z+d/d0„Z,d1d2„Z-d3d4„Z.d5d6„Z/d7d8„Z0d9d:„Z1dS)YdSz | ¡Wn|  ¡‚Yn0Yn0dS)zInvoke the applicationN) Ú setup_environrÚstart_responseÚresultÚfinish_responseZConnectionAbortedErrorZBrokenPipeErrorZConnectionResetErrorÚ handle_errorÚclose)ÚselfZ applicationrrrÚrun€s  zBaseHandler.runcCs–|j ¡}|_| ¡| ¡|d<| ¡|d<|j|d<|j|d<| ¡|d<|j |d<|j |d<|j durx|j |d <|j r’|j r’| d |j ¡dS) z&Set up the environment for one requestz wsgi.inputz wsgi.errorsz wsgi.versionz wsgi.run_oncezwsgi.url_schemezwsgi.multithreadzwsgi.multiprocessNzwsgi.file_wrapperr)Ú os_environZcopyrÚ add_cgi_varsÚ get_stdinÚ get_stderrÚ wsgi_versionÚ wsgi_run_onceÚ get_schemeÚwsgi_multithreadÚwsgi_multiprocessÚwsgi_file_wrapperÚ origin_serverÚserver_softwareÚ setdefault)r&Zenvrrrr ˜s          zBaseHandler.setup_environcCsdz2| ¡r| ¡s0|jD]}| |¡q| ¡Wn$t|jdƒrP|j ¡‚Yn 0| ¡dS)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. r%N)Úresult_is_fileÚsendfiler"ÚwriteÚfinish_contentÚhasattrr%©r&Údatarrrr#­s     zBaseHandler.finish_responsecCs t|jƒS)z Return the URL scheme being used)rr©r&rrrr.ÇózBaseHandler.get_schemec CsHzt|jƒ}Wntttfy&Yn0|dkrDt|jƒ|jd<dSdS)z@Compute Content-Length or switch to chunked encoding if possiblerúContent-LengthN)Úlenr"Z TypeErrorZAttributeErrorÚNotImplementedErrorÚstrÚ bytes_sentÚheaders)r&ZblocksrrrÚset_content_lengthÌszBaseHandler.set_content_lengthcCsd|jvr| ¡dS)zqMake any necessary header changes or defaults Subclasses can extend this to add other defaults. r>N)rCrDr<rrrÚcleanup_headersÙs zBaseHandler.cleanup_headerscCsî|r4z(|jr&|d|dƒ |d¡‚Wd}nd}0|jdurFtdƒ‚||_| |¡|_| |d¡}t|ƒdksxJdƒ‚|dd … ¡sJd ƒ‚|d d ks¤Jd ƒ‚|D]>\}}| |d ¡}| |d¡}t |ƒr¨Jd|›d|›dƒ‚q¨|j S)z4'start_response()' callable as specified by PEP 3333rréNzHeaders already set!ZStatusiz$Status must be at least 4 charactersiz(Status message must begin w/3-digit codez z+Status message must have a space after codez Header namez Header valuezHop-by-hop header, 'z: z', not allowed) Ú headers_sentZwith_tracebackrCÚAssertionErrorÚstatusÚ headers_classÚ_convert_string_typer?Zisdigitrr7)r&rIrCÚexc_infoZnameZvalrrrr!ás(        ÿzBaseHandler.start_responsecCs(t|ƒtur|Std |t|ƒ¡ƒ‚dS)zConvert/check value type.z!{0} must be of type str (got {1})N)ÚtyperArHZformatZrepr)r&ZvalueZtitlerrrrKþs  ÿz BaseHandler._convert_string_typecCs’|jrx| ¡rŽ| d|j|jf d¡¡d|jvrP| dtt ¡ƒ d¡¡|j rŽd|jvrŽ| d|j  d¡¡n| d|j d¡¡dS) z6Transmit version/status/date/server, via self._write()z HTTP/%s %s rZDatez Date: %s ZServerz Server: %s z Status: %s N) r2Úclient_is_modernÚ_writeÚ http_versionrIrrCrr r3r<rrrÚ send_preambles ÿzBaseHandler.send_preamblecCsft|ƒtusJdƒ‚|js$tdƒ‚n,|js>t|ƒ|_| ¡n|jt|ƒ7_| |¡|  ¡dS)z+'write()' callable as specified by PEP 3333z)write() argument must be a bytes instancezwrite() before start_response()N) rMÚbytesrIrHrGr?rBÚ send_headersrOÚ_flushr:rrrr7sÿ    zBaseHandler.writecCsdS)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. Frr<rrrr6)szBaseHandler.sendfilecCs"|js|j dd¡| ¡ndS)z.Ensure headers and content have both been sentr>Z0N)rGrCr4rSr<rrrr8>s zBaseHandler.finish_contentc Csjz>t|jdƒr|j ¡Wd|_|_|_|_d|_d|_n&d|_|_|_|_d|_d|_0dS)z„Close the iterable (if needed) and reset all instance vars Subclasses may want to also drop the client connection. r%NrF)r9r"r%rCrIrrBrGr<rrrr%Hs  ÿzBaseHandler.closecCs8| ¡d|_|jr| ¡r4| ¡| t|jƒ¡dS)z1Transmit headers to the client, via self._write()TN)rErGr2rNrQrOrRrCr<rrrrSUs zBaseHandler.send_headerscCs|j}|duot|j|ƒS)z@True if 'self.result' is an instance of 'self.wsgi_file_wrapper'N)r1Z isinstancer")r&Zwrapperrrrr5^szBaseHandler.result_is_filecCs|jd ¡dkS)z,True if client can accept status and headersZSERVER_PROTOCOLzHTTP/0.9)rZupperr<rrrrNdr=zBaseHandler.client_is_moderncCsNzBddlm}| ¡}||d|d|d|j|ƒ| ¡Wd}nd}0dS)zLog the 'exc_info' tuple in the server log Subclasses may override to retarget the output or change its format. r)Úprint_exceptionrrFN)Z tracebackrUr+Útraceback_limitÚflush)r&rLrUÚstderrrrrÚ log_exceptionis þ zBaseHandler.log_exceptioncCs2| t ¡¡|js.| |j|j¡|_| ¡dS)z>Log current error, and send error output to client if possibleN) rYrrLrGÚ error_outputrr!r"r#r<rrrr$yszBaseHandler.handle_errorcCs$||j|jdd…t ¡ƒ|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_headersrrLÚ error_body)r&rr!rrrrZs zBaseHandler.error_outputcCót‚dS)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. N©r@r:rrrrO”szBaseHandler._writecCr^)z¬Override 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. Nr_r<rrrrTszBaseHandler._flushcCr^)z4Override in subclass to return suitable 'wsgi.input'Nr_r<rrrr*¥r=zBaseHandler.get_stdincCr^)z5Override in subclass to return suitable 'wsgi.errors'Nr_r<rrrr+©r=zBaseHandler.get_stderrcCr^)z>Override in subclass to insert CGI variables in 'self.environ'Nr_r<rrrr)­r=zBaseHandler.add_cgi_vars©N)2Ú__name__Ú __module__Ú __qualname__Ú__doc__r,r/r0r-r2rPr3r r(rr1rrJrVr[r\r]rIr"rGrCrBr'r r#r.rDrEr!rKrQr7r6r8r%rSr5rNrYr$rZrOrTr*r+r)rrrrr^sV      rc@sBeZdZdZddd„Zdd„Zdd „Zd d „Zd d „Zdd„Z 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`)ÚstdinÚstdoutrXÚbase_envr/r0)r&rerfrXrÚ multithreadÚ multiprocessrrrÚ__init__¿s zSimpleHandler.__init__cCó|jSr`)rer<rrrr*ÉózSimpleHandler.get_stdincCrkr`)rXr<rrrr+ÌrlzSimpleHandler.get_stderrcCs|j |j¡dSr`)rZupdatergr<rrrr)ÏrlzSimpleHandler.add_cgi_varscCs^|j |¡}|dus |t|ƒkr$dSddlm}|dtƒ||d…}|sLqZ|j |¡}q:dS)Nr)Úwarnz9SimpleHandler.stdout.write() should not do partial writes)rfr7r?ZwarningsrmZDeprecationWarning)r&r;r"rmrrrrOÒs  ÿ zSimpleHandler._writecCs|j ¡|jj|_dSr`)rfrWrTr<rrrrTßs zSimpleHandler._flushN)TF) rarbrcrdrjr*r+r)rOrTrrrrr²s  rc@seZdZdZdZdS)raÚCGI-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)rarbrcrdr2rrrrräsrc@ó eZdZdZdZiZdd„ZdS)r aéCGI-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|tjjtjjtjtƒddddS)NFT©rhri)rrjrreÚbufferrfrXr r<rrrrjsþzCGIHandler.__init__N©rarbrcrdr-r(rjrrrrr üsr c@rn)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 |d¡rD|t|ƒd…|d<tj|tjjtj jtj |ddddS)Nrrrz/FTro) r rrr?rrjrrerprfrX)r&rZpathZscriptrrrrj2s  þzIISCGIHandler.__init__Nrqrrrrr sr )rdZutilrrrrCrrrr Z__all__r rrZ __contains__rrr rrrr r rrrrZs"  <V2