3 \ R @sdZddlmZmZmZddlmZddlZddlZddl Z dddd d d gZ d d dddddgZ dddddddddddddg Z dd Z d!d"d#d$d%d&d'd(d)d*h jZd+d,Zd-d ZGd.ddZGd/ddeZGd0ddeZGd1d d eZGd2d d eZdS)3z/Base classes for server/gateway implementations) FileWrapper guess_scheme is_hop_by_hop)HeadersN BaseHandler SimpleHandlerBaseCGIHandler CGIHandler IISCGIHandler read_environZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc Cs:tj|\ }}}}}}}}} dt||t|||||fS)Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)timeZgmtime _weekdayname _monthname) Z timestampZyearZmonthZdayZhhZmmZssZwdyzr /usr/lib64/python3.6/handlers.pyformat_date_timesr SCRIPT_NAME PATH_INFOZ QUERY_STRINGZREQUEST_METHODZ AUTH_TYPEZ CONTENT_TYPEZCONTENT_LENGTHZHTTPSZ REMOTE_USERZ REMOTE_IDENTcCs6t|p4|jdp4|jdp4|jdo4t|ddS)NZHTTP_ZSSL_Z REDIRECT_ ) _is_request startswith_needs_transcode)krrrrsrc Cstj}d}ydjd|Wntk r4d}YnXi}xtjjD]\}}t|rtjdkrtjj ddj }|j dr|jdj d}q|j d rq|j d rd |kr|jdj d}q|j|dj d}n|j||j d}|||<qFW|S) z'Read environment, fixing HTTP variablessurrogateescapezutf-8replaceZwin32SERVER_SOFTWAREzmicrosoft-iis/z iso-8859-1zapache/z simplehttp/zpython/3) sysgetfilesystemencodingencode LookupErrorosenvironitemsrplatformgetlowerrdecode)encescr%rvZsoftwarerrrr "s,      c@s"eZdZdZdddZ ddZ!ddZ"ddZ#d d!Z$d"d#Z%d$d%Z&d&d'Z'd(d)Z(d*d+Z)d,d-Z*d.d/Z+d0d1Z,d2d3Z-d4d5Z.d6d7Z/d8d9Z0d:d;Z1dS)?rz+Manage the invocation of a WSGI applicationrrTFz1.0Nz500 Internal Server Error Content-Type text/plains;A server error occurred. Please contact the administrator.cCsZy$|j||j|j|_|jWn0y |jWn|jYnXYnXdS)zInvoke the applicationN) setup_environr%start_responseresultfinish_response handle_errorclose)selfZ applicationrrrruns  zBaseHandler.runcCs|jj}|_|j|j|d<|j|d<|j|d<|j|d<|j|d<|j |d<|j |d<|j dk rx|j |d <|j r|j r|jd |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_environcopyr% add_cgi_vars get_stdin get_stderr wsgi_version wsgi_run_once get_schemewsgi_multithreadwsgi_multiprocesswsgi_file_wrapper origin_serverserver_software setdefault)r6envrrrr0s          zBaseHandler.setup_environc CsJz:|j s|j r8x|jD]}|j|qW|jWd|jXdS)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. N)result_is_filesendfiler2writefinish_contentr5)r6datarrrr3s   zBaseHandler.finish_responsecCs t|jS)z Return the URL scheme being used)rr%)r6rrrr?szBaseHandler.get_schemec CsJyt|j}Wntttfk r(YnX|dkrFt|j|jd<dSdS)z@Compute Content-Length or switch to chunked encoding if possiblerzContent-LengthN)lenr2 TypeErrorAttributeErrorNotImplementedErrorstr bytes_sentheaders)r6Zblocksrrrset_content_lengthszBaseHandler.set_content_lengthcCsd|jkr|jdS)zqMake any necessary header changes or defaults Subclasses can extend this to add other defaults. zContent-LengthN)rRrS)r6rrrcleanup_headerss zBaseHandler.cleanup_headersc Csh|r2z$|jr&|d|dj|dWdd}Xn|jdk rDtd||_|j||_|j|d}|jS)z4'start_response()' callable as specified by PEP 3333rrNzHeaders already set!ZStatus) headers_sentwith_tracebackrRAssertionErrorstatus headers_class_convert_string_typerI)r6rYrRexc_inforrrr1s   zBaseHandler.start_responsecCs(t|tkr|Stdj|t|dS)zConvert/check value type.z!{0} must be of type str (got {1})N)typerPrXformatrepr)r6valuetitlerrrr[s z BaseHandler._convert_string_typecCs|jrx|jr|jd|j|jfjdd|jkrP|jdttjjd|j rd|jkr|jd|j jdn|jd|jjddS) z6Transmit version/status/date/server, via self._write()z HTTP/%s %s z iso-8859-1ZDatez Date: %s ZServerz Server: %s z Status: %s N) rCclient_is_modern_write http_versionrYr"rRrr rD)r6rrr send_preambles zBaseHandler.send_preamblecCsR|jstdn,|js*t||_|jn|jt|7_|j||jdS)z+'write()' callable as specified by PEP 3333zwrite() before start_response()N)rYrXrVrLrQ send_headersrc_flush)r6rKrrrrIs    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. Fr)r6rrrrHszBaseHandler.sendfilecCs"|js|jjdd|jndS)z.Ensure headers and content have both been sentzContent-Length0N)rVrRrErf)r6rrrrJ0s zBaseHandler.finish_contentc CsFzt|jdr|jjWdd|_|_|_|_d|_d|_XdS)zClose the iterable (if needed) and reset all instance vars Subclasses may want to also drop the client connection. r5NrF)hasattrr2r5rRrYr%rQrV)r6rrrr5:s  zBaseHandler.closecCs:|jd|_|j s|jr6|j|jt|jdS)z1Transmit headers to the client, via self._write()TN)rTrVrCrbrercbytesrR)r6rrrrfGs zBaseHandler.send_headerscCs|j}|dk ot|j|S)z@True if 'self.result' is an instance of 'self.wsgi_file_wrapper'N)rB isinstancer2)r6wrapperrrrrGPszBaseHandler.result_is_filecCs|jdjdkS)z,True if client can accept status and headersZSERVER_PROTOCOLzHTTP/0.9)r%upper)r6rrrrbVszBaseHandler.client_is_modernc CsJz>ddlm}|j}||d|d|d|j||jWdd}XdS)zLog the 'exc_info' tuple in the server log Subclasses may override to retarget the output or change its format. r)print_exceptionrrUN) tracebackrnr<traceback_limitflush)r6r\rnstderrrrr log_exception[s   zBaseHandler.log_exceptioncCs2|jtj|js.|j|j|j|_|jdS)z>Log current error, and send error output to client if possibleN) rsr r\rV error_outputr%r1r2r3)r6rrrr4kszBaseHandler.handle_errorcCs$||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_headersr r\ error_body)r6r%r1rrrrtss zBaseHandler.error_outputcCstdS)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)rO)r6rKrrrrcszBaseHandler._writecCstdS)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. N)rO)r6rrrrgszBaseHandler._flushcCstdS)z4Override in subclass to return suitable 'wsgi.input'N)rO)r6rrrr;szBaseHandler.get_stdincCstdS)z5Override in subclass to return suitable 'wsgi.errors'N)rO)r6rrrr<szBaseHandler.get_stderrcCstdS)z>Override in subclass to insert CGI variables in 'self.environ'N)rO)r6rrrr:szBaseHandler.add_cgi_vars)rr)r.r/)N)2__name__ __module__ __qualname____doc__r=r@rAr>rCrdrDr r8rrBrrZrprurvrwrYr2rVrRrQr7r0r3r?rSrTr1r[rerIrHrJr5rfrGrbrsr4rtrcrgr;r<r:rrrrr^sV      c@sBeZdZdZdddZddZdd 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(||_||_||_||_||_||_dS)N)stdinstdoutrrbase_envr@rA)r6r|r}rrr% multithread multiprocessrrr__init__s zSimpleHandler.__init__cCs|jS)N)r|)r6rrrr;szSimpleHandler.get_stdincCs|jS)N)rr)r6rrrr<szSimpleHandler.get_stderrcCs|jj|jdS)N)r%updater~)r6rrrr:szSimpleHandler.add_cgi_varscCsb|jj|}|dks |t|kr$dSddlm}|dtx"||d}|sNP|jj|}qr8rrrrrr sc@s eZdZdZdZiZddZdS)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}|jdd}|jdd}|dj|drD|t|d|d<tj|tjjtj jtj |ddddS)Nrrr/FT)rr) r r(rrLr rr r|rr}rr)r6r%pathZscriptrrrr$s  zIISCGIHandler.__init__N)rxryrzr{r>r8rrrrrr s)r{utilrrrrRrr r$r __all__rrr __contains__rrr rrr r r rrrrs*   <H2