Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/wsgi.cpython-37.opt-1.pyc

B

;��]wU�@s�dZeZddlmZddlmZddlmZddl	m
Z
ddlmZm
Z
ddlmZddlmZdd	lmZdd
lmZddlmZeekr�dd
�Zdd�Zndd
�Zdd�ZGdd�d�ZGdd�d�ZGdd�d�Zee�Gdd�d��ZdgZ dS)zo
An implementation of
U{Python Web Server Gateway Interface v1.0.1<http://www.python.org/dev/peps/pep-3333/>}.
�)�exc_info)�warn)�implementer)�blockingCallFromThread)�reraise�Sequence)�Failure)�	IResource)�NOT_DONE_YET)�INTERNAL_SERVER_ERROR)�LoggercCst|t�r|S|�d�SdS)z�
        Convert C{string} to an ISO-8859-1 byte string, if it is not already.

        @type string: C{str}/C{bytes} or C{unicode}
        @rtype: C{str}/C{bytes}

        @raise UnicodeEncodeError: If C{string} contains non-ISO-8859-1 chars.
        z
iso-8859-1N)�
isinstance�str�encode)�string�r�C/opt/alt/python37/lib64/python3.7/site-packages/twisted/web/wsgi.py�_wsgiString-s	
rcCs|S)z�
        Return C{string} as is; a WSGI string is a byte string in Python 2.

        @type string: C{str}/C{bytes}
        @rtype: C{str}/C{bytes}
        r)rrrr�_wsgiStringToBytes;srcCs(t|t�r|�d��d�S|�d�SdS)as
        Convert C{string} to a WSGI "bytes-as-unicode" string.

        If it's a byte string, decode as ISO-8859-1. If it's a Unicode string,
        round-trip it to bytes and back using ISO-8859-1 as the encoding.

        @type string: C{str} or C{bytes}
        @rtype: C{str}

        @raise UnicodeEncodeError: If C{string} contains non-ISO-8859-1 chars.
        z
iso-8859-1N)r
rr�decode)rrrrrEs
cCs
|�d�S)z�
        Convert C{string} from a WSGI "bytes-as-unicode" string to an
        ISO-8859-1 byte string.

        @type string: C{str}
        @rtype: C{bytes}

        @raise UnicodeEncodeError: If C{string} contains non-ISO-8859-1 chars.
        z
iso-8859-1)r)rrrrrVs
c@s.eZdZdZe�Zdd�Zdd�Zdd�ZdS)	�_ErrorStreama�
    File-like object instances of which are used as the value for the
    C{'wsgi.errors'} key in the C{environ} dictionary passed to the application
    object.

    This simply passes writes on to L{logging<twisted.logger>} system as
    error events from the C{'wsgi'} system.  In the future, it may be desirable
    to expose more information in the events it logs, such as the application
    object which generated the message.
    cCs^t|t�sDttkr.td|t|�jftd�ntd|t|�jf��|jj	|dd|fd�dS)aG
        Generate an event for the logging system with the given bytes as the
        message.

        This is called in a WSGI application thread, not the I/O thread.

        @type data: str

        @raise TypeError: On Python 3, if C{data} is not a native string. On
            Python 2 a warning will be issued.
        z+write() argument should be str, not %r (%s))�categoryz)write() argument must be str, not %r (%s)ZwsgiT)�systemZisError�messageN)
r
r�bytesr�type�__name__�UnicodeWarning�	TypeError�_log�error)�self�datarrr�writeqs
z_ErrorStream.writecCs|�d�|��dS)a�
        Join the given lines and pass them to C{write} to be handled in the
        usual way.

        This is called in a WSGI application thread, not the I/O thread.

        @param iovec: A C{list} of C{'\n'}-terminated C{str} which will be
            logged.

        @raise TypeError: On Python 3, if C{iovec} contains any non-native
            strings. On Python 2 a warning will be issued.
        �N)r#�join)r!Ziovecrrr�
writelines�s
z_ErrorStream.writelinescCsdS)z�
        Nothing is buffered, so flushing does nothing.  This method is required
        to exist by PEP 333, though.

        This is called in a WSGI application thread, not the I/O thread.
        Nr)r!rrr�flush�sz_ErrorStream.flushN)	r�
__module__�__qualname__�__doc__rrr#r&r'rrrrrds

rc@s>eZdZdZdd�Zd
dd�Zddd�Zdd	d
�Zdd�ZdS)�_InputStreama�
    File-like object instances of which are used as the value for the
    C{'wsgi.input'} key in the C{environ} dictionary passed to the application
    object.

    This only exists to make the handling of C{readline(-1)} consistent across
    different possible underlying file-like object implementations.  The other
    supported methods pass through directly to the wrapped object.
    cCs
||_dS)zt
        Initialize the instance.

        This is called in the I/O thread, not a WSGI application thread.
        N)�_wrapped)r!�inputrrr�__init__�sz_InputStream.__init__NcCs|dkr|j��S|j�|�S)z�
        Pass through to the underlying C{read}.

        This is called in a WSGI application thread, not the I/O thread.
        N)r,�read)r!�sizerrrr/�s
z_InputStream.readcCs&|dks|dkr|j��S|j�|�S)z�
        Pass through to the underlying C{readline}, with a size of C{-1} replaced
        with a size of L{None}.

        This is called in a WSGI application thread, not the I/O thread.
        ���N)r,�readline)r!r0rrrr2�s	
z_InputStream.readlinecCs|dkr|j��S|j�|�S)z�
        Pass through to the underlying C{readlines}.

        This is called in a WSGI application thread, not the I/O thread.
        N)r,�	readlines)r!r0rrrr3�s
z_InputStream.readlinescCs
t|j�S)z�
        Pass through to the underlying C{__iter__}.

        This is called in a WSGI application thread, not the I/O thread.
        )�iterr,)r!rrr�__iter__�sz_InputStream.__iter__)N)N)N)	rr(r)r*r.r/r2r3r5rrrrr+�s		


r+c@sTeZdZdZdZe�Zdd�Zdd�Zddd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dS)�
_WSGIResponsea$
    Helper for L{WSGIResource} which drives the WSGI application using a
    threadpool and hooks it up to the L{http.Request}.

    @ivar started: A L{bool} indicating whether or not the response status and
        headers have been written to the request yet.  This may only be read or
        written in the WSGI application thread.

    @ivar reactor: An L{IReactorThreads} provider which is used to call methods
        on the request in the I/O thread.

    @ivar threadpool: A L{ThreadPool} which is used to call the WSGI
        application object in a non-I/O thread.

    @ivar application: The WSGI application object.

    @ivar request: The L{http.Request} upon which the WSGI environment is
        based and to which the application's output will be sent.

    @ivar environ: The WSGI environment L{dict}.

    @ivar status: The HTTP response status L{str} supplied to the WSGI
        I{start_response} callable by the application.

    @ivar headers: A list of HTTP response headers supplied to the WSGI
        I{start_response} callable by the application.

    @ivar _requestFinished: A flag which indicates whether it is possible to
        generate more response data or not.  This is L{False} until
        L{http.Request.notifyFinish} tells us the request is done,
        then L{True}.
    FcCs�d|_||_||_||_||_|j���|j�|jrHdd�	|j�}nd}|j
rddd�	|j
�}nd}|j�dd�}t
|�dkr�d}n|d}t|j�t|��j�t|�t|�t|�t|�d�p�d�t|�d�p�d�t|���tt|��j��t|j�d	�
|_d|j_xR|j��D]D\}	}
d
t|	����dd�}	d
�	dd�|
D���dd�|j|	<�qW|j�d|���rtd�pvddddt �t!|j"�d��dS)NF�/��?�scontent-typer$scontent-length)
ZREQUEST_METHODZREMOTE_ADDRZSCRIPT_NAMEZ	PATH_INFOZQUERY_STRINGZCONTENT_TYPEZCONTENT_LENGTHZSERVER_NAMEZSERVER_PORTZSERVER_PROTOCOLZHTTP_�-�_�,css|]}t|�VqdS)N)r)�.0�vrrr�	<genexpr>Dsz)_WSGIResponse.__init__.<locals>.<genexpr>�
� )r:rZhttpsZhttpT)zwsgi.versionzwsgi.url_schemez
wsgi.run_oncezwsgi.multithreadzwsgi.multiprocesszwsgi.errorsz
wsgi.input)#�started�reactor�
threadpool�application�requestZnotifyFinishZaddBoth�	_finishedZprepathr%ZpostpathZuri�split�lenr�methodZgetClientAddress�hostZ	getHeaderZgetRequestHostnamerZgetHostZportZclientproto�environZdefaultContentTypeZrequestHeadersZgetAllRawHeaders�upper�replace�updateZisSecurerr+Zcontent)r!rDrErFrGZ
scriptNameZpathInfo�partsZqueryString�name�valuesrrrr.sR
$z_WSGIResponse.__init__cCs
d|_dS)zc
        Record the end of the response generation for the request being
        serviced.
        TN)�_requestFinished)r!ZignoredrrrrH^sz_WSGIResponse._finishedNcCs0|jr |dk	r t|d|d�t|t�s@td|t|�jf��t|t�rLn<t|t�rrt	d|t|�jft
d�ntd|t|�jf��x�|D]�}t|t�r�n<t|t�r�t	d|t|�jft
d�ntd	|t|�jf��t|�dkr�td
|f��x$|D]}t|t�s�td|f��q�Wq�W||_
||_|jS)z�
        The WSGI I{start_response} callable.  The given values are saved until
        they are needed to generate the response.

        This will be called in a non-I/O thread.
        Nr:�zstatus must be str, not %r (%s)z%headers should be a list, not %r (%s))rz#headers must be a list, not %r (%s)z0header should be a (str, str) tuple, not %r (%s)z.header must be a (str, str) tuple, not %r (%s)z)header must be a (str, str) tuple, not %rz'header must be (str, str) tuple, not %r)rCrr
rrrr�listrr�RuntimeWarning�tuplerJ�status�headersr#)r!rYrZZexcInfo�header�elemrrr�
startResponsefsF








z_WSGIResponse.startResponsecs,��fdd�}zt�j|�j�Sd�_XdS)a
        The WSGI I{write} callable returned by the I{start_response} callable.
        The given bytes will be written to the response body, possibly flushing
        the status and headers first.

        This will be called in a non-I/O thread.
        cs|s����j���dS)N)�_sendResponseHeadersrGr#)rC)r"r!rr�	wsgiWrite�sz&_WSGIResponse.write.<locals>.wsgiWriteNT)rrDrC)r!r"r_r)r"r!rr#�s
!z_WSGIResponse.writecCsh|j�dd�\}}t|�}|j�|t|��x6|jD],\}}|��dkr4|jj�	t|�t|��q4WdS)a,
        Set the response code and response headers on the request object, but
        do not flush them.  The caller is responsible for doing a write in
        order for anything to actually be written out in response to the
        request.

        This must be called in the I/O thread.
        Nr:)ZserverZdate)
rYrI�intrG�setResponseCoderrZ�lowerZresponseHeadersZaddRawHeader)r!�coderrR�valuerrrr^�s	z"_WSGIResponse._sendResponseHeaderscCs|j�|j�dS)zo
        Start the WSGI application in the threadpool.

        This must be called in the I/O thread.
        N)rEZcallInThread�run)r!rrr�start�sz_WSGIResponse.startcs�yR���j�j�}x"|D]}|r*��|��jrPqWt|dd�}|dk	rP|�Wn0�fdd�}�jj|�jft	���YnX�fdd�}�j�|�j�d�_dS)z�
        Call the WSGI application object, iterate it, and handle its output.

        This must be called in a non-I/O thread (ie, a WSGI application
        thread).
        �closeNcsB�jjdt|||�d�|r(�j��n�j�t��j��dS)NzWSGI application error)�failure)rrhrrGZloseConnectionrar�finish)rCrrd�	traceback)r!rr�	wsgiErrorsz$_WSGIResponse.run.<locals>.wsgiErrorcs �js|s����j��dS)N)rTr^rGri)rC)r!rr�
wsgiFinishsz%_WSGIResponse.run.<locals>.wsgiFinishT)
rFrMr]r#rT�getattrrDZcallFromThreadrCr)r!ZappIteratorr\rgrkrlr)r!rre�s 



z_WSGIResponse.run)N)rr(r)r*rTrrr.rHr]r#r^rfrerrrrr6�s L
A-	r6c@s4eZdZdZdZdd�Zdd�Zdd�Zd	d
�ZdS)�WSGIResourcea�
    An L{IResource} implementation which delegates responsibility for all
    resources hierarchically inferior to it to a WSGI application.

    @ivar _reactor: An L{IReactorThreads} provider which will be passed on to
        L{_WSGIResponse} to schedule calls in the I/O thread.

    @ivar _threadpool: A L{ThreadPool} which will be passed on to
        L{_WSGIResponse} to run the WSGI application object.

    @ivar _application: The WSGI application object.
    TcCs||_||_||_dS)N)�_reactor�_threadpool�_application)r!rDrErFrrrr.,szWSGIResource.__init__cCs t|j|j|j|�}|��tS)a�
        Turn the request into the appropriate C{environ} C{dict} suitable to be
        passed to the WSGI application object and then pass it on.

        The WSGI application object is given almost complete control of the
        rendering process.  C{NOT_DONE_YET} will always be returned in order
        and response completion will be dictated by the application object, as
        will the status, headers, and the response body.
        )r6rorprqrfr
)r!rGZresponserrr�render2s
zWSGIResource.rendercCstd��dS)z�
        Reject attempts to retrieve a child resource.  All path segments beyond
        the one which refers to this resource are handled by the WSGI
        application object.
        z/Cannot get IResource children from WSGIResourceN)�RuntimeError)r!rRrGrrr�getChildWithDefaultBsz WSGIResource.getChildWithDefaultcCstd��dS)z�
        Reject attempts to add a child resource to this resource.  The WSGI
        application object handles all path segments beneath this resource, so
        L{IResource} children can never be found.
        z0Cannot put IResource children under WSGIResourceN)rs)r!�pathZchildrrr�putChildKszWSGIResource.putChildN)	rr(r)r*ZisLeafr.rrrtrvrrrrrns
	rnN)!r*rZ
__metaclass__�sysr�warningsrZzope.interfacerZtwisted.internet.threadsrZtwisted.python.compatrrZtwisted.python.failurerZtwisted.web.resourcer	Ztwisted.web.serverr
Ztwisted.web.httprZtwisted.loggerrrrrrrr+r6rn�__all__rrrr�<module>s.

FC.;