
    F\h:                     *   S r S/rSSKrSSKrSSKr\R
                  " S5      r\R
                  " S5      r " S S\5      r	S r
S	 rS
 r " S S5      r " S S5      r " S S5      r " S S5      r " S S5      rS rS rS rS rS rS rS rS rg)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).
	validator    Nz^[a-zA-Z][a-zA-Z0-9\-_]*$z[\000-\037]c                       \ rS rSrSrSrg)WSGIWarningy   z2
Raised in response to WSGI-spec-related warnings
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       '/usr/lib/python3.13/wsgiref/validate.pyr   r   y   s    r   r   c                 "    U (       d  [        U6 eg N)AssertionError)condargss     r   assert_r   ~   s    d## r   c                 r    [        U 5      [        L a  U $ [        SR                  U[	        U 5      5      5      e)Nz!{0} must be of type str (got {1}))typestrr   formatrepr)valuetitles     r   check_string_typer      s6    U|s
+225$u+FH Hr   c                    ^  U 4S jnU$ )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).
c                  B  >^^ [        [        U 5      S:H  S5        [        U(       + S5        U u  nm[        U5        / mUU4S jn[        US   5      US'   [	        US   5      US'   T" X#5      n[        US L=(       a    US:g  S5        [        U5        [        UT5      $ )	N   zTwo arguments requiredNo keyword arguments allowedc                  `  > [        [        U 5      S:H  =(       d    [        U 5      S:H  SU < 35        [        U(       + S5        U S   nU S   n[        U 5      S:X  a  U S   nOS n[        U5        [        U5        [	        X#5        [        U5        TR                  S 5        [        T" U 6 5      $ )Nr       zInvalid number of arguments: r!   r      )r   lencheck_statuscheck_headerscheck_content_typecheck_exc_infoappendWriteWrapper)r   kwstatusheadersexc_infostart_responsestart_response_starteds        r   start_response_wrapper;validator.<locals>.lint_app.<locals>.start_response_wrapper   s    CIN4c$i1n59;>F:;!WF1gG4yA~7 '"v/8$"))$/ 566r   
wsgi.inputwsgi.errorsFz>The application must return an iterator, if only an empty list)r   r%   check_environInputWrapperErrorWrappercheck_iteratorIteratorWrapper)r   r,   environr2   iteratorr0   r1   applications        @@r   lint_appvalidator.<locals>.lint_app   s    D	Q 89B67"&g "$	7& !-W\-B C!-gm.D!Ew?$:U):L	N 	x x)?@@r   r   )r=   r>   s   ` r   r   r      s    'AR Or   c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
r7      c                     Xl         g r   input)self
wsgi_inputs     r   __init__InputWrapper.__init__   s    
r   c                     [        [        U5      S:H  5        U R                  R                  " U6 n[        [	        U5      [
        L 5        U$ Nr$   )r   r%   rD   readr   bytesrE   r   vs      r   rK   InputWrapper.read   s:    D	QJJOOT"Q5 !r   c                     [        [        U5      S:*  5        U R                  R                  " U6 n[        [	        U5      [
        L 5        U$ rJ   )r   r%   rD   readliner   rL   rM   s      r   rQ   InputWrapper.readline   s<    D	QJJ&Q5 !r   c                     [        [        U5      S:*  5        U R                  R                  " U6 n[        [	        U5      [
        L 5        U H  n[        [	        U5      [        L 5        M     U$ rJ   )r   r%   rD   	readlinesr   listrL   )rE   r   lineslines       r   rT   InputWrapper.readlines   sW    D	Q

$$d+Ut#$DDJ%'( r   c              #   v   #    U R                  5       =n(       a  Uv   U R                  5       =n(       a  M  g g 7fr   )rQ   )rE   rW   s     r   __iter__InputWrapper.__iter__   s1     mmo%d%J mmo%d%%s   399c                     [        SS5        g )Nr   z input.close() must not be calledr   rE   s    r   closeInputWrapper.close   s    56r   rC   N)r   r	   r
   r   rG   rK   rQ   rT   rZ   r_   r   r   r   r   r7   r7      s      7r   r7   c                   2    \ rS rSrS rS rS rS rS rSr	g)	r8      c                     Xl         g r   errors)rE   wsgi_errorss     r   rG   ErrorWrapper.__init__       !r   c                 n    [        [        U5      [        L 5        U R                  R	                  U5        g r   )r   r   r   re   writerE   ss     r   rj   ErrorWrapper.write   s$    Q3!r   c                 8    U R                   R                  5         g r   )re   flushr^   s    r   ro   ErrorWrapper.flush   s    r   c                 8    U H  nU R                  U5        M     g r   )rj   )rE   seqrW   s      r   
writelinesErrorWrapper.writelines   s    DJJt r   c                     [        SS5        g )Nr   z!errors.close() must not be calledr]   r^   s    r   r_   ErrorWrapper.close   s    67r   rd   N)
r   r	   r
   r   rG   rj   ro   rs   r_   r   r   r   r   r8   r8      s    "8r   r8   c                        \ rS rSrS rS rSrg)r+      c                     Xl         g r   writer)rE   wsgi_writers     r   rG   WriteWrapper.__init__   rh   r   c                 Z    [        [        U5      [        L 5        U R                  U5        g r   )r   r   rL   r{   rk   s     r   __call__WriteWrapper.__call__   s    Q5 !Ar   rz   N)r   r	   r
   r   rG   r   r   r   r   r   r+   r+      s    "r   r+   c                        \ rS rSrS rS rSrg)PartialIteratorWrapper   c                     Xl         g r   r<   )rE   wsgi_iterators     r   rG   PartialIteratorWrapper.__init__   s    %r   c                 .    [        U R                  S 5      $ r   )r:   r<   r^   s    r   rZ   PartialIteratorWrapper.__iter__   s    t}}d33r   r   N)r   r	   r
   r   rG   rZ   r   r   r   r   r   r      s    &4r   r   c                   2    \ rS rSrS rS rS rS rS rSr	g)	r:   i  c                 J    Xl         [        U5      U l        SU l        X l        g )NF)original_iteratoriterr<   closedcheck_start_response)rE   r   r   s      r   rG   IteratorWrapper.__init__  s!    !.]+$8!r   c                     U $ r   r   r^   s    r   rZ   IteratorWrapper.__iter__  s    r   c                     [        U R                  (       + S5        [        U R                  5      n[	        U5      [
        La  [        SSU< S35        U R                  b  [        U R                  S5        S U l        U$ )NzIterator 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)r   r   nextr<   r   rL   r   )rE   rN   s     r   __next__IteratorWrapper.__next__  si    DKK(	*7%EQHI$$0D--|~(,D%r   c                 ~    SU l         [        U R                  S5      (       a  U R                  R                  5         g g )NTr_   )r   hasattrr   r_   r^   s    r   r_   IteratorWrapper.close  s3    4))733""((* 4r   c                     U R                   (       d  [        R                  R                  S5        [	        U R                   S5        g )Nz/Iterator garbage collected without being closed)r   sysstderrrj   r   r^   s    r   __del__IteratorWrapper.__del__   s0    {{JJAC=	?r   )r   r   r<   r   N)
r   r	   r
   r   rG   rZ   r   r_   r   r   r   r   r   r:   r:     s    9
+
?r   r:   c                    [        [        U 5      [        L S[        U 5      < SU < S35        S H  n[        X;   SU< 35        M     S H  n[        X;  SU< SUS	S  < S
35        M     SU ;  a  [        R                  " S[
        5        U R                  5        HB  nSU;   a  M  [        [        X   5      [        L SU< S[        X   5      < SX   < S35        MD     [        [        U S   5      [        L SU S   < S35        [        U S   S;   SU S   -  5        [        U S   5        [        U S   5        U S   S;  a!  [        R                  " SU S   -  [
        5        [        U R                  S5      (       + =(       d    U S   R                  S5      SU S   -  5        [        U R                  S5      (       + =(       d    U S   R                  S5      SU S   -  5        U R                  S 5      (       a!  [        [        U S    5      S!:  S"U S    -  5        U R                  S5      (       d  [        SU ;   S#5        [        U R                  S5      S:g  S$5        g )%Nz&Environment is not of the right type: z (environment: r   )	REQUEST_METHODSERVER_NAMESERVER_PORTwsgi.versionr4   r5   zwsgi.multithreadzwsgi.multiprocesszwsgi.run_oncez"Environment missing required key: )HTTP_CONTENT_TYPEHTTP_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.zEnvironmental variable z is not a string: z	 (value: r   z wsgi.version should be a tuple (zwsgi.url_scheme)httphttpszwsgi.url_scheme unknown: %rr4   r5   r   )GETHEADPOSTOPTIONSPATCHPUTDELETETRACEzUnknown REQUEST_METHOD: %rSCRIPT_NAME/z$SCRIPT_NAME doesn't start with /: %r	PATH_INFOz"PATH_INFO doesn't start with /: %rCONTENT_LENGTHr   zInvalid 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 '/')r   r   dictwarningswarnr   keysr   tuplecheck_inputcheck_errorsget
startswithint)r;   keys     r   r6   r6   '  sj   DMT!='	#$! 	69;	=	! <""%s12w0	1 <
 W$4 		 ||~#:W\"c)D&6	7	  D()U2181HJLG%&*;;%0A(BBD %&'(   )M M(73C+DD	 M** 6}%005.1GGI K(( 4{#..s3,w{/CCE {{#$$G,-.!3(73C+DD	F ;;}%%w&>	? GKK&#-	"#r   c                 P    S H   n[        [        X5      SU < SU< 35        M"     g )N)rK   rQ   rT   rZ   zwsgi.input () doesn't have the attribute r   r   )rF   attrs     r   r   r   h  s%    =
)4!	" >r   c                 P    S H   n[        [        X5      SU < SU< 35        M"     g )N)ro   rj   rs   zwsgi.errors (r   r   )rf   r   s     r   r   r   n  s%    0*D"	# 1r   c                 $   [        U S5      n U R                  S S5      S   n[        [        U5      S:H  SU-  5        [	        U5      n[        US:  SU-  5        [        U 5      S:  d	  U S   S	:w  a  [
        R                  " S
U -  [        5        g g )NStatusr$   r   r#   z)Status codes must be three characters: %rd   zStatus code is invalid: %r    zjThe status string (%r) should be a three-digit integer followed by a single space and a status explanation)r   splitr   r%   r   r   r   r   )r-   status_code
status_ints      r   r&   r&   t  s    vx0F,,tQ'*KC!3kAC[!JJ#;jHI
6{Q&)s*B!	# +r   c           
         [        [        U 5      [        L SU < S[        U 5      < 35        U  GHW  n[        [        U5      [        L SU< S[        U5      < 35        [        [	        U5      S:H  5        Uu  p#[        US5      n[        US5      n[        UR                  5       S:g  S	U-  5        [        S
U;  =(       a    SU;  SU-  5        [        [        R                  U5      SU-  5        [        UR                  S5      (       + =(       a    UR                  S5      (       + SU-  5        [        R                  U5      (       d  GM   [        SSU< S[        R                  U5      R                  S5      < S35        GMZ     g )Nz	Headers (z) must be of type list: zIndividual 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': %rzBad header name: %r-_z#Names may not end in '-' or '_': %rr   zBad header value: z (bad char: r   )r   r   rU   r   r%   r   lower	header_researchendswithbad_header_value_regroup)r.   itemnamer   s       r   r'   r'     sN   DMT!DM	#$ T
e#T$Z!	" 	D	Q }5!%8

("#	$ 	D 4S_;dB	D	  &(=(DEDMM#&&At}}S/A+A1D8	:%%e,,A)007==a@B C% r   c                    [        U S5      n [        U R                  S S5      S   5      nSnU H=  u  pE[        US5      nUR                  5       S:X  d  M'  X#;  a    g [	        SSU-  5        M?     X#;  a  [	        SSU-  5        g g )	Nr   r$   r   )   i0  r   zcontent-typezJContent-Type header found in a %s response, which must not return content.z,No Content-Type header found in headers (%s))r   r   r   r   r   )r-   r.   codeNO_MESSAGE_BODYr   r   s         r   r(   r(     s    vx0Fv||D!$Q'(D !O }5::<>)*A 9<@A B  "AGKL #r   c           	      r    [        U S L =(       d    [        U 5      [        L SU < S[        U 5      < 35        g )Nz
exc_info (z) is not a tuple: )r   r   r   )r/   s    r   r)   r)     s-    H7X% 7.6XGIr   c                 N    [        [        U [        [        45      (       + S5        g )NzwYou should not return a string as your application iterator, instead return a single-item list containing a bytestring.)r   
isinstancer   rL   r   s    r   r9   r9     s#     
8c5\22	EFr   )r   __all__rer   r   compiler   r   Warningr   r   r   r   r7   r8   r+   r   r:   r6   r   r   r&   r'   r(   r)   r9   r   r   r   <module>r      s   
iT - 
 
 JJ34	jj0 ' 
$H5n7 7@8 8& 4 4!? !?F?#B"##C2M I
Fr   