
    d:                     H   d Z dgZddlZddlZddlZ ej        d          Z ej        d          Z G d de          Z	d Z
d	 Zd
 Z G d d          Z G d d          Z G d d          Z G d d          Z G d d          Zd Zd Zd Zd Zd Zd Zd Zd ZdS )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                       e Zd ZdZdS )WSGIWarningz:
    Raised in response to WSGI-spec-related warnings
    N)__name__
__module____qualname____doc__     "  /croot/python-split_1694437901252/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/lib/python3.11/wsgiref/validate.pyr   r   y   s           r   r   c                     | s	t          | d S N)AssertionError)condargss     r   assert_r   ~   s     $d##$ $r   c                     t          |           t          u r| S t          d                    |t	          |                               )Nz!{0} must be of type str (got {1}))typestrr   formatrepr)valuetitles     r   check_string_typer      sF    U||s
+225$u++FFH H Hr   c                       fd}|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).
    c                     t          t          |           dk    d           t          | d           | \  }t          |           g fd}t          |d                   |d<   t	          |d                   |d<    ||          }t          |d uo|dk    d           t          |           t          |          S )	N   zTwo arguments requiredNo keyword arguments allowedc                     t          t          |           dk    pt          |           dk    d|            t          | d           | d         }| d         }t          |           dk    r	| d         }nd }t          |           t          |           t	          ||           t          |                               d            t           |            S )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_wrapperz;validator.<locals>.lint_app.<locals>.start_response_wrapper   s    CIIN4c$ii1n59T;> > >F:;;;!WF1gG4yyA~~7   '"""vw///8$$$"))$/// 56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)   environr/   iteratorr-   r.   applications        @@r   lint_appzvalidator.<locals>.lint_app   s    D		Q 8999B6777"&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
   )r9   r:   s   ` r   r   r      s)    'A 'A 'A 'A 'AR Or   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )r3   c                     || _         d S r   )input)self
wsgi_inputs     r   __init__zInputWrapper.__init__   s    


r   c                     t          t          |          dk                | j        j        | }t          t	          |          t
          u            |S Nr!   )r   r"   r=   readr   bytesr>   r   vs      r   rC   zInputWrapper.read   sH    D		QDJOT"Q5 !!!r   c                     t          t          |          dk                | j        j        | }t          t	          |          t
          u            |S rB   )r   r"   r=   readliner   rD   rE   s      r   rH   zInputWrapper.readline   sI    D		QDJ&Q5 !!!r   c                     t          t          |          dk                | j        j        | }t          t	          |          t
          u            |D ]%}t          t	          |          t          u            &|S rB   )r   r"   r=   	readlinesr   listrD   )r>   r   lineslines       r   rJ   zInputWrapper.readlines   st    D		Q$
$d+Ut#$$$ 	) 	)DDJJ%'((((r   c              #   B   K   	 |                                  }|sd S |V  r   )rH   )r>   rM   s     r   __iter__zInputWrapper.__iter__   s3      	==??D JJJ		r   c                 &    t          dd           d S )Nr   z input.close() must not be calledr   r>   s    r   closezInputWrapper.close   s    566666r   N)	r   r   r   r@   rC   rH   rJ   rO   rS   r
   r   r   r3   r3      sn                     7 7 7 7 7r   r3   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )r4   c                     || _         d S r   )errors)r>   wsgi_errorss     r   r@   zErrorWrapper.__init__       !r   c                     t          t          |          t          u            | j                            |           d S r   )r   r   r   rV   writer>   ss     r   rZ   zErrorWrapper.write   s6    Q3!r   c                 8    | j                                          d S r   )rV   flushrR   s    r   r^   zErrorWrapper.flush   s    r   c                 :    |D ]}|                      |           d S r   )rZ   )r>   seqrM   s      r   
writelineszErrorWrapper.writelines   s0     	 	DJJt	 	r   c                 &    t          dd           d S )Nr   z!errors.close() must not be calledrQ   rR   s    r   rS   zErrorWrapper.close   s    677777r   N)r   r   r   r@   rZ   r^   ra   rS   r
   r   r   r4   r4      s_        " " "      8 8 8 8 8r   r4   c                       e Zd Zd Zd ZdS )r(   c                     || _         d S r   )writer)r>   wsgi_writers     r   r@   zWriteWrapper.__init__   rX   r   c                 v    t          t          |          t          u            |                     |           d S r   )r   r   rD   re   r[   s     r   __call__zWriteWrapper.__call__   s.    Q5 !!!Ar   N)r   r   r   r@   rh   r
   r   r   r(   r(      s2        " " "    r   r(   c                       e Zd Zd Zd ZdS )PartialIteratorWrapperc                     || _         d S r   r8   )r>   wsgi_iterators     r   r@   zPartialIteratorWrapper.__init__   s    %r   c                 ,    t          | j        d           S r   )r6   r8   rR   s    r   rO   zPartialIteratorWrapper.__iter__  s    t}d333r   N)r   r   r   r@   rO   r
   r   r   rj   rj      s2        & & &4 4 4 4 4r   rj   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )r6   c                 X    || _         t          |          | _        d| _        || _        d S )NF)original_iteratoriterr8   closedcheck_start_response)r>   rm   rt   s      r   r@   zIteratorWrapper.__init__	  s.    !.]++$8!!!r   c                     | S r   r
   rR   s    r   rO   zIteratorWrapper.__iter__  s    r   c                     t          | j         d           t          | j                  }t	          |          t
          urt          dd|d           | j        t          | j        d           d | _        |S )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   rs   nextr8   r   rD   rt   )r>   rF   s     r   __next__zIteratorWrapper.__next__  s    DK(	* 	* 	*77%EEQQQHIII$0D-|~ ~ ~(,D%r   c                 t    d| _         t          | j        d          r| j                                         d S d S )NTrS   )rs   hasattrrq   rS   rR   s    r   rS   zIteratorWrapper.close  sC    4)733 	+"((*****	+ 	+r   c                 |    | j         st          j                            d           t	          | j         d           d S )Nz/Iterator garbage collected without being closed)rs   sysstderrrZ   r   rR   s    r   __del__zIteratorWrapper.__del__#  sP    { 	CJAC C C=	? 	? 	? 	? 	?r   N)r   r   r   r@   rO   ry   rS   r   r
   r   r   r6   r6     s_        9 9 9  
 
 
+ + +
? ? ? ? ?r   r6   c                    t          t          |           t          u dt          |           d| d           dD ]}t          || v d|           dD ]#}t          || vd|d|d	d          d
           $d| vrt          j        dt
                     |                                 D ]T}d|v rt          t          | |                   t          u d|dt          | |                   d| |         d           Ut          t          | d                   t          u d| d         d           t          | d         dv d| d         z             t          | d                    t          | d                    | d         dvr#t          j        d| d         z  t
                     t          |                     d           p| d                             d          d| d         z             t          |                     d           p| d                             d          d| d         z             |                     d           r0t          t          | d                    d!k    d"| d          z             |                     d          st          d| v d#           t          |                     d          dk    d$           d S )%Nz&Environment is not of the right type: z (environment: rw   )	REQUEST_METHODSERVER_NAMESERVER_PORTwsgi.versionr0   r1   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: %rr0   r1   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)r7   keys     r   r2   r2   *  s   DMMT!!===='''	#$ $ $! = = 	w69c;	= 	= 	= 	= < 1 17"""%##s122www0	1 	1 	1 	1 W$$4 		 	 	 ||~~ 7 7#::WS\""c))ssD&&&&6	7 	7 	7 	7 D())U22181H1H1HJL L LG%&*;;%0A(BBD D D %&&&'(((   )M M M(73C+DD	 	 	 M*** 6}%0055.1GGI I I K((( 4{#..s33,w{/CCE E E {{#$$ FG,-..!3(73C+DD	F 	F 	F ;;}%% ?w&>	? 	? 	? GKK&&#-	"# # # # #r   c                 X    dD ]&}t          t          | |          d| d|           'd S )N)rC   rH   rJ   rO   zwsgi.input () doesn't have the attribute r   r{   )r?   attrs     r   r   r   k  sQ    = " "
D)))zz44!	" 	" 	" 	"" "r   c                 X    dD ]&}t          t          | |          d| d|           'd S )N)r^   rZ   ra   zwsgi.errors (r   r   )rW   r   s     r   r   r   q  sQ    0 # #T***{{DD"	# 	# 	# 	## #r   c                 n   t          | d          } |                     d d          d         }t          t          |          dk    d|z             t	          |          }t          |dk    d|z             t          |           dk     s| d         d	k    rt          j        d
| z  t                     d S d S )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#   w  s    vx00F,,tQ''*KC!3kAC C C[!!JJ#;jHIII
6{{Q&)s**B!	# 	# 	# 	# 	# +*r   c           
         t          t          |           t          u d| dt          |                      | D ]}t          t          |          t          u d|dt          |                     t          t	          |          dk               |\  }}t          |d          }t          |d          }t          |                                dk    d	|z             t          d
|vod|vd|z             t          t                              |          d|z             t          |	                    d           o|	                    d           d|z             t                              |          rBt          dd|dt                              |                              d          d           d S )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: rw   )r   r   rK   r   r"   r   lower	header_researchendswithbad_header_value_regroup)r+   itemnamer   s       r   r$   r$     s   DMMT!!77DMMM	#$ $ $  C CT

e##ttT$ZZZ!	" 	" 	" 	D		Qe }55!%88

("#	$ 	$ 	$ 	D 4S_;dB	D 	D 	D	  &&(=(DEEEDMM#&&&At}}S/A/A+A1D8	: 	: 	:%%e,, 	CAAuu)0077==a@@@@B C C C%C Cr   c                 B   t          | d          } t          |                     d d          d                   }d}|D ]G\  }}t          |d          }|                                dk    r||vr d S t	          dd|z             H||vrt	          dd|z             d S d S )	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00Fv||D!$$Q'((D !O B Be }55::<<>))?**A 9<@A B B B?""AGKLLLLL #"r   c           	      |    t          | d u pt          |           t          u d| dt          |                      d S )Nz
exc_info (z) is not a tuple: )r   r   r   )r,   s    r   r&   r&     sO    H7X% 7.6hhXGI I I I Ir   c                 \    t          t          | t          t          f           d           d S )NzwYou should not return a string as your application iterator, instead return a single-item list containing a bytestring.)r   
isinstancer   rD   rl   s    r   r5   r5     s>     
8c5\222	EF F F F Fr   )r	   __all__rer}   r   compiler   r   Warningr   r   r   r   r3   r4   r(   rj   r6   r2   r   r   r#   r$   r%   r&   r5   r
   r   r   <module>r      s  
i iT - 
			 



 BJ344	 bj00     '   
$ $ $H H H5 5 5n!7 !7 !7 !7 !7 !7 !7 !7F8 8 8 8 8 8 8 8&       4 4 4 4 4 4 4 4!? !? !? !? !? !? !? !?F?# ?# ?#B" " "# # ## # #C C C2M M M I I I
F F F F Fr   