Fa R @spdZddlmZmZmZddlmZddlZddlZddl 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 CsOtj|\ }}}}}}}}} dt||t|||||fS)Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)timeZgmtime _weekdayname _monthname) Z timestampZyearZmonthZdayZhhZmmZssZwdyzr&/usr/lib/python3.5/wsgiref/handlers.pyformat_date_times*r SCRIPT_NAME PATH_INFOZ QUERY_STRINGZREQUEST_METHODZ AUTH_TYPEZ CONTENT_TYPEZCONTENT_LENGTHZHTTPSZ REMOTE_USERZ REMOTE_IDENTcCsMt|pL|jdpL|jdpL|jdoLt|ddS)NZHTTP_ZSSL_Z REDIRECT_ ) _is_request startswith_needs_transcode)krrrrs*rc CsVtj}d}ydjd|Wntk r@d}YnXi}xtjjD]\}}t|rDtjdkr)tjj ddj }|j dr|jdj d}qD|j d rqD|j d r d |kr |jdj d}qD|j|dj d}n|j||j d}|||rz+Manage the invocation of a WSGI applicationrrTFz1.0Nz500 Internal Server Error Content-Type text/plains;A server error occurred. Please contact the administrator.cCsey0|j||j|j|_|jWn.y|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 r|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 Cs[zI|j s|j rHx|jD]}|j|q'W|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 Cs[yt|j}Wntttfk r0Yn'X|dkrWt|j|jdLog current error, and send error output to client if possibleN) ryr r`rY error_outputr%r1r2r3)r6rrrr4ks zBaseHandler.handle_errorcCs0||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%r1rrrrzss &zBaseHandler.error_outputcCs tdS)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)r6rKrrrriszBaseHandler._writecCs tdS)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)r6rrrrnszBaseHandler._flushcCs tdS)z4Override in subclass to return suitable 'wsgi.input'N)rO)r6rrrr;szBaseHandler.get_stdincCs tdS)z5Override in subclass to return suitable 'wsgi.errors'N)rO)r6rrrr<szBaseHandler.get_stderrcCs tdS)z>Override in subclass to insert CGI variables in 'self.environ'N)rO)r6rrrr:szBaseHandler.add_cgi_vars)rr)r.r/)2__name__ __module__ __qualname____doc__r=r@rAr>rCrjrDr r8rrBrr]rvr{r|r}r\r2rYrRrQr7r0r3r?rSrTr1r^rkrIrHrJr5rmrGrhryr4rzrirnr;r<r:rrrrr^sV                     c@sdeZdZdZddddZddZdd 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)stdinstdoutrxbase_envr@rA)r6rrrxr% multithread multiprocessrrr__init__s      zSimpleHandler.__init__cCs|jS)N)r)r6rrrr;szSimpleHandler.get_stdincCs|jS)N)rx)r6rrrr<szSimpleHandler.get_stderrcCs|jj|jdS)N)r%updater)r6rrrr:szSimpleHandler.add_cgi_varscCs|jj|}|dks0|t|kr4dSddlm}|dtx-||d}|skP|jj|}qTWdS)Nr)warnz9SimpleHandler.stdout.write() should not do partial writes)rrIrLwarningsrDeprecationWarning)r6rKr2rrrrriszSimpleHandler._writecCs |jj|jj|_dS)N)rrwrn)r6rrrrns zSimpleHandler._flushN) r~rrrrr;r<r:rirnrrrrrs     c@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)r~rrrrCrrrrr s c@s.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|tjjtjjtjtdddddS)NrFrT)r rr rbufferrrxr )r6rrrrszCGIHandler.__init__N)r~rrrr>r8rrrrrr s c@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 Cst}|jdd}|jdd}|dj|dr^|t|d|dr8rrrrrr s )rutilrrrrRrr r$r __all__rrr __contains__rrr rrr r r rrrrs($     <G2