Vc:csHdZdgZddlZddlZddlZejdZejdZGddeZ 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]cseZdZdZdS) WSGIWarningz: Raised in response to WSGI-spec-related warnings N)__name__ __module__ __qualname____doc__)/usr/lib64/python3.11/wsgiref/validate.pyrrysrrcs|s t|dSN)AssertionError)Zcondargs r assert_r~s $d##$$rcst|tur|Std|t |)Nz!{0} must be of type str (got {1}))typestrr ZformatZrepr)valueZtitler r check_string_typersH U||s +225$u++FF H HHrcsfd}|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). cstt|dkdt| d|\}t|gfd}t|d|d<t |d|d<||}t|duo|dkdt |t |S) NzTwo arguments requiredNo keyword arguments allowedcstt|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_infoZappend WriteWrapper)r kwstatusheadersexc_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)r renvironr#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)?@@@rr)r-r.s` r rrs)'A'A'A'A'AR Orcs2eZdZdZdZdZdZdZdZdS)r'c||_dSr )input)self wsgi_inputr r __init__zInputWrapper.__init__s  rcstt|dk|jj|}tt |t u|SNr)rrr0readrbytesr1r v r r5zInputWrapper.readsHD Q DJOT "Q5 !!!rcstt|dk|jj|}tt |t u|Sr4)rrr0readlinerr6r7r9r r:zInputWrapper.readlinesID Q DJ  &Q5 !!!rcstt|dk|jj|}tt |t u|D]%}tt |t u&|Sr4)rrr0 readlinesrlistr6)r1r Zlinesline r r;zInputWrapper.readlinesstD Q$ $d+U t#$$$ ) )D DJJ%' ( ( ( ( rc#sBK |}|sdS|Vr )r:)r1r=r r __iter__zInputWrapper.__iter__s3 ==??D JJJ  rc&tdddS)Nrz input.close() must not be calledrr1 r closezInputWrapper.closes566666rN) rrrr3r5r:r;r?rDrrr r'r'sn     77777rr'c,eZdZdZdZdZdZdZdS)r(cr/r )errors)r1 wsgi_errorsr r r3zErrorWrapper.__init__ ! rcstt|tu|j|dSr )rrrrFwriter1Zsr r rIzErrorWrapper.writes6Q3 !rcs8|jdSr )rFflushrBrCr rKzErrorWrapper.flushs rcs:|D]}||dSr )rI)r1Zseqr=r9r writelineszErrorWrapper.writeliness0  D JJt      rcr@)Nrz!errors.close() must not be calledrArBrCr rDzErrorWrapper.closes677777rN)rrrr3rIrKrLrDrrr r(r(s_"""88888rr(ceZdZdZdZdS)rcr/r )writer)r1Z wsgi_writerr r r3zWriteWrapper.__init__rHrcsvtt|tu||dSr )rrr6rNrJr r __call__zWriteWrapper.__call__s.Q5 !!! ArN)rrrr3rOrrr rrs2"""rrcrM)PartialIteratorWrappercr/r r,)r1 wsgi_iteratorr r r3zPartialIteratorWrapper.__init__s % rcs,t|jdSr )r*r,rBrCr r?zPartialIteratorWrapper.__iter__st}d333rN)rrrr3r?rrr rPrPs2&&&44444rrPcrE)r*csX||_t||_d|_||_dS)NF)original_iteratorZiterr,closedcheck_start_response)r1rRrUr9r r3zIteratorWrapper.__init__ s.!.]++  $8!!!rcs|Sr rrBrCr r?zIteratorWrapper.__iter__s rcst|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)rrTZnextr,rr6rU)r1r8r r __next__zIteratorWrapper.__next__sDK ( * * *    77%  J EEQQQH I I I  $ - D-| ~ ~ ~(,D %rcstd|_t|jdr|jdSdS)NTrD)rThasattrrSrDrBrCr rDzIteratorWrapper.closesC 4)7 3 3 +  " ( ( * * * * * + +rcs||jstjdt |jddS)Nz/Iterator garbage collected without being closed)rTsysZstderrrIrrBrCr __del__zIteratorWrapper.__del__#sP{ C J  A C C C = ? ? ? ? ?rN)rrrr3r?rWrDrZrrr r*r*s_999    +++ ?????rr*c stt|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: rV) REQUEST_METHODZ SERVER_NAMEZ SERVER_PORT wsgi.versionr$r%zwsgi.multithreadzwsgi.multiprocessz wsgi.run_oncez"Environment missing required key: )ZHTTP_CONTENT_TYPEZHTTP_CONTENT_LENGTHz%Environment should not have the key: z (use iz instead)Z 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 likelyz.zEnvironmental variable z is not a string: z (value: r\z wsgi.version should be a tuple (zwsgi.url_scheme)ZhttpZhttpszwsgi.url_scheme unknown: %rr$r%r[)ZGETZHEADZPOSTZOPTIONSZPATCHZPUTZDELETEZTRACEzUnknown REQUEST_METHOD: %rZ SCRIPT_NAMEz/z$SCRIPT_NAME doesn't start with /: %rZ PATH_INFOz"PATH_INFO doesn't start with /: %rZCONTENT_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 '/')rrZdictwarningswarnrZkeysrtuple check_input check_errorsZgetZ startswithint)r+Zkeyr r r&r&*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 & &# - "#####rcXdD]&}tt||d|d|'dS)N)r5r:r;r?z wsgi.input () doesn't have the attribute rrX)r2attrr r r`r`ksQ="" D)))zz44 ! " " " """rcrc)N)rKrIrLz wsgi.errors (rdre)rGrfr r raraqsQ0## T***{{DD " # # # ###rcsnt|d}|ddd}tt|dkd|zt |}t|dkd|zt|dks |dd krt jd |ztdSdS) NStatusrrrz)Status codes must be three characters: %ridzStatus code is invalid: %riz zjThe status string (%r) should be a three-digit integer followed by a single space and a status explanation)rsplitrrrbr]r^r)rZ status_codeZ status_intr9r rrws vx 0 0F,,tQ''*K C   !3kACCC[!!J J# ;jHIII 6{{Q#&)s*#  B ! # # # # ###rc stt|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 valuerzyThe Status header cannot be used; it conflicts with CGI script, and HTTP status is not given through headers (value: %r).z z:z,Header names may not contain ':' or '\n': %rzBad header name: %rz-Z_z#Names may not end in '-' or '_': %rrzBad header value: z (bad char: rV) rrr<r_rrlower header_reZsearchZendswithbad_header_value_reZgroup)rZitemnamerr>r rrs 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%CCrcsBt|d}t|ddd}d}|D]G\}}t|d}|dkr||vrdSt dd|zH||vrt dd|zdSdS) Nrgrr)ii0riz content-typezJContent-Type header found in a %s response, which must not return content.z,No Content-Type header found in headers (%s))rrbrhrjr)rrZcodeZNO_MESSAGE_BODYrmrs r rrs vx 0 0F v||D!$$Q' ( (D!OBB e }55 ::<<> ) B?*  A9<@A B B B ?"MAGKLLLLLMMrc s|t|dupt|tud|dt|dS)Nz exc_info (z) is not a tuple: )rrr_)r rCr rrsO H  7X% 7.6hhXGIIIIIrcs\tt|ttf ddS)NzwYou should not return a string as your application iterator, instead return a single-item list containing a bytestring.)rZ isinstancerr6rQrCr r)r)s> 8c5\22 2 EFFFFFr)rZ__all__ZrerYr]ZcompilerkrlZWarningrrrrr'r(rrPr*r&r`rarrrrr)rrr rns iiT -  BJ3 4 4  bj00' $$$HHH 555n!7!7!7!7!7!7!7!7F88888888&44444444!?!?!?!?!?!?!?!?F?#?#?#B""" ### # # #CCC2MMM III FFFFFr