Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/webtest/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/webtest/__pycache__/lint3.cpython-37.opt-1.pyc

B

���O�@�@sdZddlmZddlZddlZddlZddlmZe�d�Z	e�d�Z
eefZ
Gdd�de�Zd'd	d
�ZGdd�de�ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zee_d
d&gZ dS)(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 zero or 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).
�)�unicode_literalsN)�	to_stringz^[a-zA-Z][a-zA-Z0-9\-_]*$z[\000-\037]c@seZdZdZdS)�WSGIWarningz:
    Raised in response to WSGI-spec-related warnings
    N)�__name__�
__module__�__qualname__�__doc__�r	r	�>/opt/alt/python37/lib/python3.7/site-packages/webtest/lint3.pyr~srcs�fdd�}|S)a�
    When applied between a WSGI server and a WSGI application, this
    middleware will check for WSGI compliancy on a number of levels.
    This middleware does not modify the request or response in any
    way, but will throw 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 throw an exception
    at that point).
    cs�t|�dkstd��|r td��|\}�t|�g���fdd�}t|d�|d<t|d�|d<�||�}|dk	r||dks�td	��t|�t|��S)
N�zTwo arguments requiredzNo keyword arguments allowedcs�t|�dks$t|�dks$td|��|r0td��|d}|d}t|�dkrV|d}nd}t|�t|�t||�t|���d�t�|��S)Nr�zInvalid number of arguments: %szNo keyword arguments allowedr�)�len�AssertionError�check_status�
check_headers�check_content_type�check_exc_info�append�WriteWrapper)�args�kw�status�headers�exc_info)�start_response�start_response_startedr	r
�start_response_wrapper�s



z<middleware.<locals>.lint_app.<locals>.start_response_wrapperz
wsgi.inputzwsgi.errorsFz>The application must return an iterator, if only an empty list)rr�
check_environ�InputWrapper�ErrorWrapper�check_iterator�IteratorWrapper)rr�environr�iterator)�application)rrr
�lint_app�s
zmiddleware.<locals>.lint_appr	)r%�global_confr&r	)r%r
�
middleware�s)r(c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)rcCs
||_dS)N)�input)�self�
wsgi_inputr	r	r
�__init__�szInputWrapper.__init__cGs0t|�dkst�|jj|�}t|�tks,t�|S)Nr
)rrr)�read�type�bytes)r*r�vr	r	r
r-�szInputWrapper.readcGs |jj|�}t|�tkst�|S)N)r)�readliner.r/r)r*rr0r	r	r
r1�szInputWrapper.readlinecGsRt|�dkst�|jj|�}t|�tg�ks0t�x|D]}t|�tks6t�q6W|S)Nr
)rrr)�	readlinesr.r/)r*r�lines�liner	r	r
r2�s
zInputWrapper.readlinesccs x|��}|sdS|VqWdS)N)r1)r*r4r	r	r
�__iter__�s
zInputWrapper.__iter__cCsdstd��dS)Nrz input.close() must not be called)r)r*r	r	r
�close�szInputWrapper.closeN)	rrrr,r-r1r2r5r6r	r	r	r
r�src@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)r cCs
||_dS)N)�errors)r*�wsgi_errorsr	r	r
r,�szErrorWrapper.__init__cCs t|�tkst�|j�|�dS)N)r.r/rr7�write)r*�sr	r	r
r9�szErrorWrapper.writecCs|j��dS)N)r7�flush)r*r	r	r
r;�szErrorWrapper.flushcCsx|D]}|�|�qWdS)N)r9)r*�seqr4r	r	r
�
writelines�s
zErrorWrapper.writelinescCsdstd��dS)Nrz!errors.close() must not be called)r)r*r	r	r
r6�szErrorWrapper.closeN)rrrr,r9r;r=r6r	r	r	r
r �s
r c@seZdZdd�Zdd�ZdS)rcCs
||_dS)N)�writer)r*Zwsgi_writerr	r	r
r,�szWriteWrapper.__init__cCst|�tkst�|�|�dS)N)r.r/rr>)r*r:r	r	r
�__call__�szWriteWrapper.__call__N)rrrr,r?r	r	r	r
r�src@seZdZdd�Zdd�ZdS)�PartialIteratorWrappercCs
||_dS)N)r$)r*�
wsgi_iteratorr	r	r
r,�szPartialIteratorWrapper.__init__cCs
t|j�S)N)r"r$)r*r	r	r
r5szPartialIteratorWrapper.__iter__N)rrrr,r5r	r	r	r
r@�sr@c@s8eZdZdd�Zdd�Zdd�ZeZdd�Zd	d
�ZdS)r"cCs ||_t|�|_d|_||_dS)NF)�original_iterator�iterr$�closed�check_start_response)r*rArEr	r	r
r,	s
zIteratorWrapper.__init__cCs|S)Nr	)r*r	r	r
r5szIteratorWrapper.__iter__cCsX|jrtd��t|j�}|jdk	r6|js0td��d|_t|t�sTtd|jt|f��|S)NzIterator read after closedzjThe application returns and we started iterating over its body, but start_response has not yet been calledz(Iterator %r returned a non-%r object: %r)rDr�nextr$rE�
isinstancer/)r*r0r	r	r
rFs

zIteratorWrapper.nextcCs d|_t|jd�r|j��dS)NTr6)rD�hasattrrBr6)r*r	r	r
r6"szIteratorWrapper.closecCs$|jstj�d�|js td��dS)Nz/Iterator garbage collected without being closed)rD�sys�stderrr9r)r*r	r	r
�__del__'s
zIteratorWrapper.__del__N)	rrrr,r5rF�__next__r6rKr	r	r	r
r"sr"cCst|�tks tdt|�|f��x dD]}||ks&td|��q&Wx,dD]$}||ksHtd||dd�f��qHWd|kr�t�dt�xD|D]<}d	|kr�q�t||�tks�td
|t||�||f��q�Wt|d�tks�td|d��|d
dk�std|d
��t|d�t	|d�|ddk�rBt�d|dt�|�
d��rn|d�d��sntd|d��|�
d��r�|d�d��s�td|d��|�
d��r�t|d�dk�s�td|d��|�
d��s�d|k�s�td��|�
d�dk�s�td��dS)Nz:Environment is not of the right type: %r (environment: %r))	�REQUEST_METHODZSERVER_NAMEZSERVER_PORTzwsgi.versionz
wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocessz
wsgi.run_oncez$Environment missing required key: %r)ZHTTP_CONTENT_TYPEZHTTP_CONTENT_LENGTHz8Environment should not have the key: %s (use %s instead)�ZQUERY_STRINGz�QUERY_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�.z9Environmental variable %s is not a string: %r (value: %r)zwsgi.versionz#wsgi.version should be a tuple (%r)zwsgi.url_scheme)ZhttpZhttpszwsgi.url_scheme unknown: %rz
wsgi.inputzwsgi.errorsrM)ZGETZHEADZPOSTZOPTIONSZPUTZDELETEZTRACEzUnknown REQUEST_METHOD: %rZSCRIPT_NAME�/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 '/')
r.�dictr�warnings�warnr�
METADATA_TYPE�tuple�check_input�check_errors�get�
startswith�int)r#�keyr	r	r
r/sZ





rcCs,x&dD]}t||�std||f��qWdS)N)r-r1r2r5z-wsgi.input (%r) doesn't have the attribute %s)rHr)r+�attrr	r	r
rVqs
rVcCs,x&dD]}t||�std||f��qWdS)N)r;r9r=z.wsgi.errors (%r) doesn't have the attribute %s)rHr)r8r\r	r	r
rWxs
rWcCs�t|�tkstdt|f��|�dd�d}t|�dksDtd|��t|�}|dks`td|��t|�dksx|dd	kr�t�d
|t�dS)NzStatus must be a %s (not %r)r
rrz)Status codes must be three characters: %r�dzStatus code is invalid: %r�� zjThe status string (%r) should be a three-digit integer followed by a single space and a status explanation)	r.rTr�splitrrZrRrSr)rZstatus_codeZ
status_intr	r	r
rs
rc	Cs�t|�tks td|t|�f���xj|D�]`}t|�tksNtd|t|�f��t|�dks^t�|\}}t|�tkr�y|�d�Wn"tk
r�td|f��YnXt|�}|�	�dks�td|��d|kr�d	|ks�td
|��t
�|�s�td|��|�d��s|�d
��rtd|��t|�tk�r\y|�d�Wn$tk
�rZtd|f��YnXt|�}t
�|�r(td|t
�|��d�f��q(WdS)Nz%Headers (%r) must be of type list: %rz1Individual headers (%r) must be of type tuple: %rr�latin1zKHeaders name must be latin1 string or bytes.%r is not a valid latin1 stringrzyThe 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 '_': %rzMHeaders values must be latin1 string or bytes.%r is not a valid latin1 stringz#Bad header value: %r (bad char: %r)r)r.�listrrUr�str�encode�UnicodeEncodeErrorr�lower�	header_re�search�endswith�bad_header_value_re�group)r�item�name�value�str_nameZ	str_valuer	r	r
r�sH

rc	Cs�t|�dd�d�}d}d}d}x4|D],\}}t|�}|��dkr&|��r&t|�}q&Wx\|D]T\}}t|�}|��dkr\||kr�dS|dkr�t�d|t�dSds\td|��q\W||kr�|dk	r�|dkr�ds�td	|��dS)
Nr
r)����i0)rui0zcontent-lengthzcontent-typezEContent-Type header found in a %s response, which not return content.zJContent-Type header found in a %s response, which must not return content.z,No Content-Type header found in headers (%s))	rZr`rrj�isdigitrRrSrr)	rr�codeZNO_MESSAGE_BODYZNO_MESSAGE_TYPE�lengthrqrrrsr	r	r
r�s,rcCs,|dks(t|�tks(td|t|�f��dS)Nz exc_info (%r) is not a tuple: %r)r.rUr)rr	r	r
r�srcCs(t|t�rtd��t|t�r$td��dS)NzuYou should not return a bytes as your application iterator, instead return a single-item list containing that string.zpYou should not return a string as your application iterator, instead return a single-item list containing bytes.)rGr/rrg)r$r	r	r
r!�sr!cCst|�S)N)r()r%r'r	r	r
�make_middleware�sry)N)!rZ
__future__r�rerIrRZwebtest.compatr�compilerkrnrgr/rT�Warningrr(�objectrr rr@r"rrVrWrrrrr!ry�__all__r	r	r	r
�<module>ps4


8#

(B(