f:*dZdgZddlZddlZddlZej dZej dZGddeZ dZ d Z d Z Gd d Z Gd dZGddZGddZGddZdZdZdZdZdZdZdZdZy)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]ceZdZdZy) WSGIWarningz: Raised in response to WSGI-spec-related warnings N)__name__ __module__ __qualname____doc__'/usr/lib/python3.12/wsgiref/validate.pyrrysr rc|st|yN)AssertionError)condargss r assert_r~s d## r cpt|tur|Stdj|t |)Nz!{0} must be of type str (got {1}))typestrrformatrepr)valuetitles r check_string_typers6 U|s +225$u+F 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). c0tt|dk(dt| d|\}t|gfd}t|d|d<t |d|d<||}t|duxr|dk7dt |t |S) NzTwo arguments requiredNo keyword arguments allowedcLtt|dk(xst|dk(d|t| d|d}|d}t|dk(r|d}nd}t|t|t ||t |j 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 CIN4c$i1n59; > F: ;!WF1gG4yA~7  ' " vw / 8 $ " ) )$ / 56 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 89B67"&g"$ 7&!-W\-B C !-gm.D!E w(>?$:U): L N x x)?@@r r )r9r:s` r rrs'AR Or c0eZdZdZdZdZdZdZdZy)r3c||_yr)input)self wsgi_inputs r __init__zInputWrapper.__init__s  r ctt|dk(|jj|}tt |t u|SNr!)rr"r=readrbytesr>rvs r rCzInputWrapper.reads<D Q DJJOOT "Q5 !r ctt|dk|jj|}tt |t u|SrB)rr"r=readlinerrDrEs r rHzInputWrapper.readlines>D Q DJJ   &Q5 !r ctt|dk|jj|}tt |t u|D]}tt |t u|SrB)rr"r= readlinesrlistrD)r>rlineslines r rJzInputWrapper.readliness^D Q$ $$d+U t#$ )D DJ%' ( ) r c#`K|jx}r||jx}ryywr)rH)r>rMs r __iter__zInputWrapper.__iter__s/mmo%d%Jmmo%d%s)..ctddy)Nrz input.close() must not be calledrr>s r closezInputWrapper.closes 56r N) rrrr@rCrHrJrOrSr r r r3r3s    7r r3c*eZdZdZdZdZdZdZy)r4c||_yr)errors)r> wsgi_errorss r r@zErrorWrapper.__init__ ! r cntt|tu|jj |yr)rrrrVwriter>ss r rZzErrorWrapper.writes$Q3 !r c8|jjyr)rVflushrRs r r^zErrorWrapper.flushs r c4|D]}|j|yr)rZ)r>seqrMs r writelineszErrorWrapper.writeliness D JJt  r ctddy)Nrz!errors.close() must not be calledrQrRs r rSzErrorWrapper.closes 67r N)rrrr@rZr^rarSr r r r4r4s"8r r4ceZdZdZdZy)r(c||_yr)writer)r> wsgi_writers r r@zWriteWrapper.__init__rXr cZtt|tu|j|yr)rrrDrer[s r __call__zWriteWrapper.__call__sQ5 ! Ar N)rrrr@rhr r r r(r(s "r r(ceZdZdZdZy)PartialIteratorWrapperc||_yrr8)r> wsgi_iterators r r@zPartialIteratorWrapper.__init__s % r c.t|jdSr)r6r8rRs r rOzPartialIteratorWrapper.__iter__st}}d33r N)rrrr@rOr r r rjrjs &4r rjc*eZdZdZdZdZdZdZy)r6cN||_t||_d|_||_y)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|j t|j dd|_|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__siDKK ( *   7%  EQH I  $ $ 0 D--| ~(,D %r ctd|_t|jdr|jjyy)NTrS)rshasattrrqrSrRs r rSzIteratorWrapper.closes0 4))7 3  " " ( ( * 4r c|jstjjdt |jdy)Nz/Iterator garbage collected without being closed)rssysstderrrZrrRs r __del__zIteratorWrapper.__del__ s0{{ JJ  A C = ?r N)rrrr@rOryrSrr r r r6r6s9  + ?r r6c btt|tudt|d|ddD]}t||vd|dD]}t||vd|d|d dd d |vrtjd t |j D]>}d |vrtt||tud|dt||d||d@tt|dtud|ddt|ddvd|dzt|dt|d|ddvr tjd|dzt t|jd xs|djdd|dzt|jd xs|djdd|dz|jd r!tt|d d!k\d"|d z|jdstd|vd#t|jddk7d$y)%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} DMT ! =' #$!= w69 ; = =<17""%s12w 0 11 W$  4    ||~7 #: WS\"c)D&  6 7 7 D( )U 2181HJL G% &*; ;%0A(BBD %&'( )MM (73C+D D    M* * 6}%005.1GGI  K( ( 4{#..s3,w{/CCE{{#$G,-.!3 (73C+D D F ;;} % w& > ? GKK &# - "#r cJdD]}tt||d|d| y)N)rCrHrJrOz wsgi.input () doesn't have the attribute rr{)r?attrs r rrhs,=" D)4 ! ""r cJdD]}tt||d|d| y)N)r^rZraz wsgi.errors (rr)rWrs r rrns,0# T*D " ##r ct|d}|jddd}tt|dk(d|zt |}t|dk\d|zt|dks|dd k7rt j d |ztyy) 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#ts vx 0F,,tQ'*K C  !3kAC[!J J# ;jHI 6{Q&)s*  B ! #+r c tt|tud|dt||D]9}tt|tud|dt|tt |dk(|\}}t |d}t |d}t|j dk7d |ztd |vxrd |vd |zttj|d |zt|jd xr|jd d|ztj|stdd|dtj|jdd<y)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$sT DMT ! DM #$CT e#T$Z ! " D Q e }5!%8 ( " # $ D 4S_ ;d B D   &(=(DEDMM#&&At}}S/A+A 1D 8 :  % %e , A)007==a@B C%Cr ct|d}t|jddd}d}|D]:\}}t|d}|jdk(s&||vryt dd|z<||vrt dd|zyy) 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 0F v||D!$Q' (D!OB e }5 ::<> )?* A9<@A B B ?"AGKL#r c dt|duxst|tud|dt|y)Nz exc_info (z) is not a tuple: )rrr)r,s r r&r&s- H  7X% 7.6XGIr cFtt|ttf dy)NzwYou should not return a string as your application iterator, instead return a single-item list containing a bytestring.)r isinstancerrDrls r r5r5s# 8c5\2 2 EFr )r __all__rer}rcompilerrWarningrrrrr3r4r(rjr6r2rrr#r$r%r&r5r r r rs iT -  BJJ3 4  bjj0' $H 5n77@88&44!?!?F?#B" # #C2M I Fr