Vc:HdZdgZddlZddlZddlZejdZejdZGddeZ dZ d Z d Z Gd d Z Gd dZGddZGddZGddZdZdZdZdZdZdZdZdZdS)a& 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 exactly 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). validatorNz^[a-zA-Z][a-zA-Z0-9\-_]*$z [\000-\037]ceZdZdZdS) WSGIWarningz: Raised in response to WSGI-spec-related warnings N)__name__ __module__ __qualname____doc__)/usr/lib64/python3.11/wsgiref/validate.pyrrysr rc|s t|dSN)AssertionError)condargss r assert_r~s $d##$$r ct|tur|Std|t |)Nz!{0} must be of type str (got {1}))typestrrformatrepr)valuetitles r check_string_typersH U||s +225$u++FF H HHr cfd}|S)a When applied between a WSGI server and a WSGI application, this middleware will check for WSGI compliance 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). ctt|dkdt| d|\}t|gfd}t|d|d<t |d|d<||}t|duo|dkdt |t |S) NzTwo arguments requiredNo keyword arguments allowedctt|dkpt|dkd|t| d|d}|d}t|dkr |d}nd}t|t|t ||t |dt|S)NrzInvalid number of arguments: rr)rlen check_status check_headerscheck_content_typecheck_exc_infoappend WriteWrapper)rkwstatusheadersexc_infostart_responsestart_response_starteds r start_response_wrapperz;validator..lint_app..start_response_wrappers CIIN4c$ii1n59T; > > > F: ; ; ;!WF1gG4yyA~ 7  ' " " " vw / / / 8 $ $ $ " ) )$ / / / 566 6r wsgi.input wsgi.errorsFz>The application must return an iterator, if only an empty list)rr" check_environ InputWrapper ErrorWrappercheck_iteratorIteratorWrapper)rr)environr/iteratorr-r. applications @@r lint_appzvalidator..lint_appsD Q 8999B6777"&g"$ 7 7 7 7 7 7&!-W\-B C C !-gm.D!E!E ;w(>??$:U): L N N N x   x)?@@@r r )r9r:s` r rrs)'A'A'A'A'AR Or c2eZdZdZdZdZdZdZdZdS)r3c||_dSr)input)self wsgi_inputs r __init__zInputWrapper.__init__s  r ctt|dk|jj|}tt |t u|SNr!)rr"r=readrbytesr>rvs r rCzInputWrapper.readsHD Q DJOT "Q5 !!!r ctt|dk|jj|}tt |t u|SrB)rr"r=readlinerrDrEs r rHzInputWrapper.readlinesID Q DJ  &Q5 !!!r ctt|dk|jj|}tt |t u|D]%}tt |t u&|SrB)rr"r= readlinesrlistrD)r>rlineslines r rJzInputWrapper.readlinesstD Q$ $d+U t#$$$ ) )D DJJ%' ( ( ( ( r c#BK |}|sdS|Vr)rH)r>rMs r __iter__zInputWrapper.__iter__s3 ==??D JJJ  r c&tdddS)Nrz input.close() must not be calledrr>s r closezInputWrapper.closes566666r N) rrrr@rCrHrJrOrSr r r r3r3sn     77777r r3c,eZdZdZdZdZdZdZdS)r4c||_dSr)errors)r> wsgi_errorss r r@zErrorWrapper.__init__ ! r ctt|tu|j|dSr)rrrrVwriter>ss r rZzErrorWrapper.writes6Q3 !r c8|jdSr)rVflushrRs r r^zErrorWrapper.flushs r c:|D]}||dSr)rZ)r>seqrMs r writelineszErrorWrapper.writeliness0  D JJt      r c&tdddS)Nrz!errors.close() must not be calledrQrRs r rSzErrorWrapper.closes677777r N)rrrr@rZr^rarSr r r r4r4s_"""88888r r4ceZdZdZdZdS)r(c||_dSr)writer)r> wsgi_writers r r@zWriteWrapper.__init__rXr cvtt|tu||dSr)rrrDrer[s r __call__zWriteWrapper.__call__s.Q5 !!! Ar N)rrrr@rhr r r r(r(s2"""r r(ceZdZdZdZdS)PartialIteratorWrapperc||_dSrr8)r> wsgi_iterators r r@zPartialIteratorWrapper.__init__s % r c,t|jdSr)r6r8rRs r rOzPartialIteratorWrapper.__iter__st}d333r N)rrrr@rOr r r rjrjs2&&&44444r rjc,eZdZdZdZdZdZdZdS)r6cX||_t||_d|_||_dS)NF)original_iteratoriterr8closedcheck_start_response)r>rmrts r r@zIteratorWrapper.__init__ s.!.]++  $8!!!r c|Srr rRs r rOzIteratorWrapper.__iter__s r ct|j dt|j}t |t urtdd|d|jt|jdd|_|S)NzIterator read after closedFz!Iterator yielded non-bytestring ()zjThe application returns and we started iterating over its body, but start_response has not yet been called)rrsnextr8rrDrt)r>rFs r __next__zIteratorWrapper.__next__sDK ( * * *    77%  J EEQQQH I I I  $ - D-| ~ ~ ~(,D %r ctd|_t|jdr|jdSdS)NTrS)rshasattrrqrSrRs r rSzIteratorWrapper.closesC 4)7 3 3 +  " ( ( * * * * * + +r c||jstjdt |jddS)Nz/Iterator garbage collected without being closed)rssysstderrrZrrRs r __del__zIteratorWrapper.__del__#sP{ C J  A C C C = ? ? ? ? ?r N)rrrr@rOryrSrr r r r6r6s_999    +++ ?????r r6c tt|tudt|d|ddD]}t||vd|dD]#}t||vd|d|d dd $d |vrtjd t |D]T}d |vrtt||tud|dt||d||dUtt|dtud|ddt|ddvd|dzt|dt|d|ddvr#tjd|dzt t| d p|d dd|dzt| d p|d dd|dz| d r0tt|d d!kd"|d z| dstd|vd#t| ddkd$dS)%Nz&Environment is not of the right type: z (environment: rw) REQUEST_METHOD SERVER_NAME SERVER_PORT wsgi.versionr0r1zwsgi.multithreadzwsgi.multiprocessz wsgi.run_oncez"Environment missing required key: )HTTP_CONTENT_TYPEHTTP_CONTENT_LENGTHz%Environment should not have the key: z (use z instead) QUERY_STRINGzQUERY_STRING is not in the WSGI environment; the cgi module will use sys.argv when this variable is missing, so application errors are more likely.zEnvironmental variable z is not a string: z (value: rz wsgi.version should be a tuple (zwsgi.url_scheme)httphttpszwsgi.url_scheme unknown: %rr0r1r)GETHEADPOSTOPTIONSPATCHPUTDELETETRACEzUnknown REQUEST_METHOD: %r SCRIPT_NAME/z$SCRIPT_NAME doesn't start with /: %r PATH_INFOz"PATH_INFO doesn't start with /: %rCONTENT_LENGTHrzInvalid CONTENT_LENGTH: %rzgOne of SCRIPT_NAME or PATH_INFO are required (PATH_INFO should at least be '/' if SCRIPT_NAME is empty)zOSCRIPT_NAME cannot be '/'; it should instead be '', and PATH_INFO should be '/')rrdictwarningswarnrkeysrtuple check_input check_errorsget startswithint)r7keys r r2r2*s DMMT ! ! ====''' #$$$!== w69c ; = = = =<117"""%##s122www 0 1 1 1 1W$  4      ||~~77 #:  WS\""c))ssD&&&&  6 7 7 7 7 D( ) )U 2 2181H1H1HJLLL G% &*; ;%0A(BBDDD %&&&'((( )MM (73C+D D      M** * 6}%0055.1GGIII  K(( ( 4{#..s33,w{/CCEEE{{#$$FG,-..!3 (73C+D D F F F ;;} % %? w& > ? ? ? GKK & &# - "#####r cXdD]&}tt||d|d|'dS)N)rCrHrJrOz wsgi.input () doesn't have the attribute rr{)r?attrs r rrksQ="" D)))zz44 ! " " " """r cXdD]&}tt||d|d|'dS)N)r^rZraz wsgi.errors (rr)rWrs r rrqsQ0## T***{{DD " # # # ###r cnt|d}|ddd}tt|dkd|zt |}t|dkd|zt|dks |dd krt jd |ztdSdS) NStatusr!rr z)Status codes must be three characters: %rdzStatus code is invalid: %r zjThe status string (%r) should be a three-digit integer followed by a single space and a status explanation)rsplitrr"rrrr)r* status_code status_ints r r#r#ws vx 0 0F,,tQ''*K C   !3kACCC[!!J J# ;jHIII 6{{Q#&)s*#  B ! # # # # ###r c tt|tud|dt||D]}tt|tud|dt|tt |dk|\}}t |d}t |d}t|dkd |ztd |vod |vd |ztt|d |zt| d o| d d|zt|rBtdd|dt| dddS)Nz Headers (z) must be of type list: zIndividual headers (z) must be of type tuple: r Header namez Header valuer*zyThe Status header cannot be used; it conflicts with CGI script, and HTTP status is not given through headers (value: %r). :z,Header names may not contain ':' or '\n': %rzBad header name: %r-_z#Names may not end in '-' or '_': %rrzBad header value: z (bad char: rw) rrrKrr"rlower header_researchendswithbad_header_value_regroup)r+itemnamers r r$r$s DMMT ! ! 77DMMM #$$$CCT e##ttT$ZZZ ! " " " D Q e }55!%88 ( " # $ $ $ D 4S_ ;d B D D D   &&(=(DEEEDMM#&&&At}}S/A/A+A 1D 8 : : :  % %e , , C AAuu)0077==a@@@@B C C C%CCr cBt|d}t|ddd}d}|D]G\}}t|d}|dkr||vrdSt dd|zH||vrt dd|zdSdS) Nrr!r)i0rz content-typezJContent-Type header found in a %s response, which must not return content.z,No Content-Type header found in headers (%s))rrrrr)r*r+codeNO_MESSAGE_BODYrrs r r%r%s vx 0 0F v||D!$$Q' ( (D!OBB e }55 ::<<> ) B?*  A9<@A B B B ?"MAGKLLLLLMMr c |t|dupt|tud|dt|dS)Nz exc_info (z) is not a tuple: )rrr)r,s r r&r&sO H  7X% 7.6hhXGIIIIIr c\tt|ttf ddS)NzwYou should not return a string as your application iterator, instead return a single-item list containing a bytestring.)r isinstancerrDrls r r5r5s> 8c5\22 2 EFFFFFr )r __all__rer}rcompilerrWarningrrrrr3r4r(rjr6r2rrr#r$r%r&r5r r r rs iiT -  BJ3 4 4  bj00' $$$HHH 555n!7!7!7!7!7!7!7!7F88888888&44444444!?!?!?!?!?!?!?!?F?#?#?#B""" ### # # #CCC2MMM III FFFFFr