ž ¬ÿf/;c@sNdZdgZddlZddlZddlZejdƒZejdƒZGdd„deƒZ dd „Z d d „Z d d„Z Gd d„dƒZ Gdd„dƒZGdd„dƒZGdd„dƒZGdd„dƒZdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„ZdS('u& Middleware to check for obedience to the WSGI specification. Some of the things this checks: * Signature of the application and start_response (including that keyword arguments are not used). * Environment checks: - Environment is a dictionary (and not a subclass). - That all the required keys are in the environment: REQUEST_METHOD, SERVER_NAME, SERVER_PORT, wsgi.version, wsgi.input, wsgi.errors, wsgi.multithread, wsgi.multiprocess, wsgi.run_once - That HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH are not in the environment (these headers should appear as CONTENT_LENGTH and CONTENT_TYPE). - Warns if QUERY_STRING is missing, as the cgi module acts unpredictably in that case. - That CGI-style variables (that don't contain a .) have (non-unicode) string values - That wsgi.version is a tuple - That wsgi.url_scheme is 'http' or 'https' (@@: is this too restrictive?) - Warns if the REQUEST_METHOD is not known (@@: probably too restrictive). - That SCRIPT_NAME and PATH_INFO are empty or start with / - That at least one of SCRIPT_NAME or PATH_INFO are set. - That CONTENT_LENGTH is a positive integer. - That SCRIPT_NAME is not '/' (it should be '', and PATH_INFO should be '/'). - That wsgi.input has the methods read, readline, readlines, and __iter__ - That wsgi.errors has the methods flush, write, writelines * The status is a string, contains a space, starts with an integer, and that integer is in range (> 100). * That the headers is a list (not a subclass, not another kind of sequence). * That the items of the headers are tuples of strings. * That there is no 'status' header (that is used in CGI, but not in WSGI). * That the headers don't contain newlines or colons, end in _ or -, or contain characters codes below 037. * That Content-Type is given if there is content (CGI often has a default content type, but WSGI does not). * That no Content-Type is given when there is no content (@@: is this too restrictive?) * That the exc_info argument to start_response is a tuple or None. * That all calls to the writer are with strings, and no other methods on the writer are accessed. * That wsgi.input is used properly: - .read() is called with zero or one argument - That it returns a string - That readline, readlines, and __iter__ return strings - That .close() is not called - No other methods are provided * That wsgi.errors is used properly: - .write() and .writelines() is called with a string - That .close() is not called, and no other methods are provided. * The response iterator: - That it is not a string (it should be a list of a single string; a string will work, but perform horribly). - That .__next__() returns a string - That the iterator is not iterated over until start_response has been called (that can signal either a server or application error). - That .close() is called (doesn't raise exception, only prints to sys.stderr, because we only know it isn't called when the object is garbage collected). u validatoriNu^[a-zA-Z][a-zA-Z0-9\-_]*$u [\000-\037]cBs|EeZdZdZdS(u WSGIWarningu: Raised in response to WSGI-spec-related warnings N(u__name__u __module__u __qualname__u__doc__(u __locals__((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu WSGIWarningysu WSGIWarningcGs|st|Œ‚ndS(N(uAssertionError(uconduargs((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyuassert_~suassert_cCs8t|ƒtkr|Stdj|t|ƒƒƒ‚dS(Nu!{0} must be of type str (got {1})(utypeustruAssertionErroruformaturepr(uvalueutitle((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyucheck_string_type‚sucheck_string_typecs‡fdd†}|S(u® When applied between a WSGI server and a WSGI application, this middleware will check for WSGI compliancy on a number of levels. This middleware does not modify the request or response in any way, but will raise an AssertionError if anything seems off (except for a failure to close the application iterator, which will be printed to stderr -- there's no way to raise an exception at that point). csÅtt|ƒdkdƒt| dƒ|\}‰t|ƒg‰‡‡fdd†}t|dƒ|d.lint_app..start_response_wrapperu wsgi.inputu wsgi.errorsu>The application must return an iterator, if only an empty listF( uassert_ulenu check_environu InputWrapperu ErrorWrapperuNoneuFalseucheck_iteratoruIteratorWrapper(uargsukwuenvironustart_response_wrapperuiterator(u application(ustart_responseustart_response_startedu5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyulint_app”s   uvalidator..lint_app((u applicationulint_app((u applicationu5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu validatorˆs )cBs\|EeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd S(u InputWrappercCs ||_dS(N(uinput(uselfu wsgi_input((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__init__ÁsuInputWrapper.__init__cGsBtt|ƒdkƒ|jj|Œ}tt|ƒtkƒ|S(Ni(uassert_ulenuinputureadutypeubytes(uselfuargsuv((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyureadÄsuInputWrapper.readcGsBtt|ƒdkƒ|jj|Œ}tt|ƒtkƒ|S(Ni(uassert_ulenuinputureadlineutypeubytes(uselfuargsuv((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyureadlineÊsuInputWrapper.readlinecGsitt|ƒdkƒ|jj|Œ}tt|ƒtkƒx$|D]}tt|ƒtkƒqEW|S(Ni(uassert_ulenuinputu readlinesutypeulistubytes(uselfuargsulinesuline((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu readlinesÐs  uInputWrapper.readlinesccs%x|jƒ}|sdS|VqdS(N(ureadline(uselfuline((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__iter__Øs  uInputWrapper.__iter__cCstddƒdS(Niu input.close() must not be called(uassert_(uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyucloseßsuInputWrapper.closeN( u__name__u __module__u __qualname__u__init__ureadureadlineu readlinesu__iter__uclose(u __locals__((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu InputWrapper¿s      u InputWrappercBsP|EeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd S( u ErrorWrappercCs ||_dS(N(uerrors(uselfu wsgi_errors((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__init__äsuErrorWrapper.__init__cCs*tt|ƒtkƒ|jj|ƒdS(N(uassert_utypeustruerrorsuwrite(uselfus((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyuwriteçsuErrorWrapper.writecCs|jjƒdS(N(uerrorsuflush(uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyuflushësuErrorWrapper.flushcCs"x|D]}|j|ƒqWdS(N(uwrite(uselfusequline((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu writelinesîs uErrorWrapper.writelinescCstddƒdS(Niu!errors.close() must not be called(uassert_(uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyucloseòsuErrorWrapper.closeN(u__name__u __module__u __qualname__u__init__uwriteuflushu writelinesuclose(u __locals__((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu ErrorWrapperâs     u ErrorWrappercBs,|EeZdZdd„Zdd„ZdS(u WriteWrappercCs ||_dS(N(uwriter(uselfu wsgi_writer((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__init__÷suWriteWrapper.__init__cCs'tt|ƒtkƒ|j|ƒdS(N(uassert_utypeubytesuwriter(uselfus((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__call__úsuWriteWrapper.__call__N(u__name__u __module__u __qualname__u__init__u__call__(u __locals__((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu WriteWrapperõs u WriteWrappercBs,|EeZdZdd„Zdd„ZdS(uPartialIteratorWrappercCs ||_dS(N(uiterator(uselfu wsgi_iterator((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__init__suPartialIteratorWrapper.__init__cCst|jdƒS(N(uIteratorWrapperuiteratoruNone(uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__iter__suPartialIteratorWrapper.__iter__N(u__name__u __module__u __qualname__u__init__u__iter__(u __locals__((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyuPartialIteratorWrapperþs uPartialIteratorWrappercBsP|EeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd S( uIteratorWrappercCs.||_t|ƒ|_d|_||_dS(NF(uoriginal_iteratoruiteruiteratoruFalseucloseducheck_start_response(uselfu wsgi_iteratorucheck_start_response((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__init__ s  uIteratorWrapper.__init__cCs|S(N((uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__iter__suIteratorWrapper.__iter__cCsxt|j dƒt|jƒ}t|ƒtk rItdd|fƒn|jdk rtt|jdƒd|_n|S(NuIterator read after closedu$Iterator yielded non-bytestring (%r)ujThe application returns and we started iterating over its body, but start_response has not yet been calledF( uassert_uclosedunextuiteratorutypeubytesuFalseucheck_start_responseuNone(uselfuv((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__next__s   uIteratorWrapper.__next__cCs/d|_t|jdƒr+|jjƒndS(NucloseT(uTrueucloseduhasattruoriginal_iteratoruclose(uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyucloses uIteratorWrapper.closecCs0|jstjjdƒnt|jdƒdS(Nu/Iterator garbage collected without being closed(uclosedusysustderruwriteuassert_(uself((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu__del__#s     uIteratorWrapper.__del__N(u__name__u __module__u __qualname__u__init__u__iter__u__next__ucloseu__del__(u __locals__((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyuIteratorWrappers    uIteratorWrapperc Cs›tt|ƒtkdt|ƒ|fƒxCdddddddd d g D] }t||kd |fƒqKWx;d d gD]-}t||kd||dd…fƒq|Wd|krÌtjdtƒnx^|jƒD]P}d|krñqÙntt||ƒtkd|t||ƒ||fƒqÙWtt|dƒtkd|dfƒt|dd+kd|dƒt |dƒt |dƒ|dd,kr»tjd |dtƒnt|j d!ƒ pÞ|d!j d"ƒd#|d!ƒt|j d$ƒ p|d$j d"ƒd%|d$ƒ|j d&ƒrVtt |d&ƒd'kd(|d&ƒn|j d!ƒs{td$|kd)ƒnt|j d!ƒd"kd*ƒdS(-Nu:Environment is not of the right type: %r (environment: %r)uREQUEST_METHODu SERVER_NAMEu SERVER_PORTu wsgi.versionu wsgi.inputu wsgi.errorsuwsgi.multithreaduwsgi.multiprocessu wsgi.run_onceu$Environment missing required key: %ruHTTP_CONTENT_TYPEuHTTP_CONTENT_LENGTHu8Environment should not have the key: %s (use %s instead)iu QUERY_STRINGu’QUERY_STRING is not in the WSGI environment; the cgi module will use sys.argv when this variable is missing, so application errors are more likelyu.u9Environmental variable %s is not a string: %r (value: %r)u#wsgi.version should be a tuple (%r)uwsgi.url_schemeuhttpuhttpsuwsgi.url_scheme unknown: %ruGETuHEADuPOSTuOPTIONSuPUTuDELETEuTRACEuUnknown REQUEST_METHOD: %ru SCRIPT_NAMEu/u$SCRIPT_NAME doesn't start with /: %ru PATH_INFOu"PATH_INFO doesn't start with /: %ruCONTENT_LENGTHiuInvalid CONTENT_LENGTH: %rugOne of SCRIPT_NAME or PATH_INFO are required (PATH_INFO should at least be '/' if SCRIPT_NAME is empty)uOSCRIPT_NAME cannot be '/'; it should instead be '', and PATH_INFO should be '/'(uhttpuhttps(uGETuHEADuPOSTuOPTIONSuPUTuDELETEuTRACE(uassert_utypeudictuwarningsuwarnu WSGIWarningukeysustrutupleu check_inputu check_errorsugetu startswithuint(uenvironukey((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu check_environ*s`        #     u check_environcCsAx:ddddgD]&}tt||ƒd||fƒqWdS(Nureadureadlineu readlinesu__iter__u-wsgi.input (%r) doesn't have the attribute %s(uassert_uhasattr(u wsgi_inputuattr((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu check_inputksu check_inputcCs>x7dddgD]&}tt||ƒd||fƒqWdS(Nuflushuwriteu writelinesu.wsgi.errors (%r) doesn't have the attribute %s(uassert_uhasattr(u wsgi_errorsuattr((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu check_errorsqsu check_errorscCs¢t|dƒ}|jddƒd}tt|ƒdkd|ƒt|ƒ}t|dkd|ƒt|ƒdks‡|dd kržtjd |tƒndS( NuStatusiiiu)Status codes must be three characters: %riduStatus code is invalid: %riu ujThe status string (%r) should be a three-digit integer followed by a single space and a status explanation( ucheck_string_typeusplituNoneuassert_ulenuintuwarningsuwarnu WSGIWarning(ustatusu status_codeu status_int((u5/opt/alt/python33/lib64/python3.3/wsgiref/validate.pyu check_statusws  "u check_statusc Cs}tt|ƒtkd|t|ƒfƒi}xG|D]?}tt|ƒtkd|t|ƒfƒtt|ƒdkƒ|\}}t|dƒ}t|dƒ}t|jƒdkd|ƒd||jƒns.       7#  # A