HOME


Mini Shell 1.0
DIR: /usr/lib/python3/dist-packages/twisted/web/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/twisted/web/__pycache__/wsgi.cpython-312.pyc
�

Ϫ�f�U���dZddlmZddlmZddlmZddlmZddl	m
Z
ddlmZddl
mZdd	lmZdd
lmZddlmZeeurd�Zd
�Znd�Zd�ZGd�d�ZGd�d�ZGd�d�Zee�Gd�d��ZdgZy)zo
An implementation of
U{Python Web Server Gateway Interface v1.0.1<http://www.python.org/dev/peps/pep-3333/>}.
�)�Sequence)�exc_info)�warn)�implementer)�blockingCallFromThread)�Logger)�Failure)�INTERNAL_SERVER_ERROR)�	IResource)�NOT_DONE_YETc�H�t|t�r|S|jd�S)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.
        �
iso-8859-1)�
isinstance�str�encode��strings �2/usr/lib/python3/dist-packages/twisted/web/wsgi.py�_wsgiStringr*s"���f�c�"��M��=�=��.�.�c��|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}
        �rs r�_wsgiStringToBytesr8s	���
rc��t|t�r |jd�jd�S|jd�S)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.
        r)rrr�decoders rrrCs7���f�c�"��=�=��.�5�5�l�C�C��=�=��.�.rc�$�|jd�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.
        r)rrs rrrTs���}�}�\�*�*rc�0�eZdZdZe�Zd�Zd�Zd�Zy)�_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.
    c��t|t�s]ttur,td|�dt	|�j
�d�t��n%td|�dt	|�j
�d���|jj|dd|f��y	)
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 � (�)��categoryz"write() argument must be str, not �wsgiT)�system�isError�messageN)
rr�bytesr�type�__name__�UnicodeWarning�	TypeError�_log�error)�self�datas  r�writez_ErrorStream.writeoss���$��$��e�|���T�$�Z�0�0�2�+�� ��T�$�Z�0�0�2���	
�	�	����V�T�D�7��Krc�D�|jdj|��y)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)r1�join)r/�iovecs  r�
writelinesz_ErrorStream.writelines�s��	
�
�
�2�7�7�5�>�"rc��y)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/s r�flushz_ErrorStream.flush�s�rN)	r*�
__module__�__qualname__�__doc__rr-r1r6r9rrrrras ��	��8�D�L�:
#�rrc�4�eZdZdZd�Zdd�Zdd�Zdd�Zd�Zy)	�_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.
    c��||_y)zt
        Initialize the instance.

        This is called in the I/O thread, not a WSGI application thread.
        N)�_wrapped)r/�inputs  r�__init__z_InputStream.__init__�s����
rNc�p�|�|jj�S|jj|�S)z�
        Pass through to the underlying C{read}.

        This is called in a WSGI application thread, not the I/O thread.
        )r@�read�r/�sizes  rrDz_InputStream.read�s1���<��=�=�%�%�'�'��}�}�!�!�$�'�'rc�z�|dk(s|�|jj�S|jj|�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.
        ���)r@�readlinerEs  rrIz_InputStream.readline�s7���2�:����=�=�)�)�+�+��}�}�%�%�d�+�+rc�p�|�|jj�S|jj|�S)z�
        Pass through to the underlying C{readlines}.

        This is called in a WSGI application thread, not the I/O thread.
        )r@�	readlinesrEs  rrKz_InputStream.readlines�s1���<��=�=�*�*�,�,��}�}�&�&�t�,�,rc�,�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@r8s r�__iter__z_InputStream.__iter__�s���D�M�M�"�"r�N)	r*r:r;r<rBrDrIrKrNrrrr>r>�s ����	(�,�	-�#rr>c�N�eZdZdZdZe�Zd�Zd�Zdd�Z	d�Z
d�Zd	�Zd
�Z
y)�
_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�r�d|_||_||_||_||_|jj�j
|j�|jrddj|j�z}nd}|jrddj|j�z}nd}|jjdd�}t|�dk(rd}n|d}t|j�t|j!�j"�t|�t|�t|�t|j%d�xsd�t|j%d�xsd�t|j'��tt)|j+�j,��t|j.�d	�
|_d|j_|j4j7�D]f\}	}
d
t|	�j9�j;dd�z}	d
jd�|
D��j;dd�|j0|	<�h|j0j=d|j?�xrdxsddddtA�tC|jD�d��y)NF�/r�?�scontent-typer3scontent-length)
�REQUEST_METHOD�REMOTE_ADDR�SCRIPT_NAME�	PATH_INFO�QUERY_STRING�CONTENT_TYPE�CONTENT_LENGTH�SERVER_NAME�SERVER_PORT�SERVER_PROTOCOL�HTTP_�-�_�,c3�2K�|]}t|����y�wrO)r)�.0�vs  r�	<genexpr>z)_WSGIResponse.__init__.<locals>.<genexpr>8s����)I�Q�+�a�.�)I�s��
� )rUr�https�httpT)zwsgi.versionzwsgi.url_schemez
wsgi.run_oncezwsgi.multithreadzwsgi.multiprocesszwsgi.errorsz
wsgi.input)#�started�reactor�
threadpool�application�request�notifyFinish�addBoth�	_finished�prepathr4�postpath�uri�split�lenr�method�getClientAddress�host�	getHeader�getRequestHostnamer�getHost�port�clientproto�environ�defaultContentType�requestHeaders�getAllRawHeaders�upper�replace�update�isSecurerr>�content)r/rmrnrorp�
scriptName�pathInfo�parts�queryString�name�valuess           rrBz_WSGIResponse.__init__sA��������$���&���������!�!�#�+�+�D�N�N�;��?�?���	�	�'�/�/� :�:�J��J�����d�i�i��(8�(8�9�9�H��H����!�!�$��*���u�:��?��K���(�K�
*�'�.�.�9�&�w�'?�'?�'A�'F�'F�G�&�z�2�$�X�.�'��4�'��(9�(9�/�(J�(P�b�Q�)�'�*;�*;�<M�*N�*T�RT�U�&�w�'A�'A�'C�D�&�s�7�?�?�+<�+A�+A�'B�C�*�7�+>�+>�?�
��� +/����'�#�2�2�C�C�E�	�L�D�&��[��.�4�4�6�>�>�s�C�H�H�D�"%���)I�&�)I�!I�!Q�!Q��c�"�D�L�L���		�	
����� &�#*�#3�#3�#5�#A�'�#K�V�!&�$(�%*�+�~�+�7�?�?�;�+
�	
rc��d|_y)zc
        Record the end of the response generation for the request being
        serviced.
        TN)�_requestFinished)r/�ignoreds  rrsz_WSGIResponse._finishedVs��
!%��rNc	�T�|jr|�|dj|d��t|t�s.t	dj|t
|�j���t|t�rnat|t�r,td|�dt
|�j�d�t��n%t	d|�dt
|�j�d���|D]�}t|t�rnat|t�r,td	|�dt
|�j�d�t��n%t	d
|�dt
|�j�d���t|�dk7rt	d
|����|D] }t|t�r�t	d|������||_||_|j S)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.
        rU�z!status must be str, not {!r} ({})zheaders should be a list, not r r!r"zheaders must be a list, not z)header should be a (str, str) tuple, not z'header must be a (str, str) tuple, not z%header must be (str, str) tuple, not )rl�with_tracebackrrr,�formatr)r*�listrr�RuntimeWarning�tuplerx�status�headersr1)r/r�r��excInfo�header�elems      r�
startResponsez_WSGIResponse.startResponse]s����<�<�G�/��!�*�+�+�G�A�J�7�7�
�&�#�&��3�:�:��D��L�1�1���
��g�t�$��
���
*���D��M�2�2�4�'�
���D��M�2�2�4��
��	X�F��&�%�(���F�H�-���t�F�|�4�4�6�+�� ��t�F�|�4�4�6����6�{�a���"I�&�� T�U�U�
�
X��!�$��,�#�&K�F�:�$V�W�W�
X�-	X�4�������z�z�rc�|�����fd�}	t�j|�j�d�_S#d�_wxYw)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.
        c�`��|s�j��jj��yrO)�_sendResponseHeadersrpr1)rlr0r/s ��r�	wsgiWritez&_WSGIResponse.write.<locals>.wsgiWrite�s$�����)�)�+��L�L���t�$rT)rrmrl)r/r0r�s`` rr1z_WSGIResponse.write�s1���B	%�
	 �)�$�,�,�	�4�<�<�P��D�L��4�D�L�s� 2�	;c�`�|jjdd�\}}t|�}|jj	|t|��|jD]P\}}|j�dvs�|jjjt|�t|���Ry)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.
        NrU)�server�date)
r�rw�intrp�setResponseCoderr��lower�responseHeaders�addRawHeader)r/�coder'r��values     rr�z"_WSGIResponse._sendResponseHeaders�s������)�)�$��2�
��g��4�y�����$�$�T�+=�g�+F�G��<�<�	�K�D�%��z�z�|�#5�5����,�,�9�9�&�t�,�.@��.G��	rc�N�|jj|j�y)zo
        Start the WSGI application in the threadpool.

        This must be called in the I/O thread.
        N)rn�callInThread�runr8s r�startz_WSGIResponse.start�s��	
���$�$�T�X�X�.rc����	�j�j�j�}|D]#}|r�j|��js�#nt|dd�}|�|��fd�}�jj|�j�d�_y#t$r?�fd�}�jj|�jgt����Yd�_ywxYw)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).
        �closeNc�x���js-|s�j��jj�yyrO)r�r�rp�finish)rlr/s �r�
wsgiFinishz%_WSGIResponse.run.<locals>.wsgiFinish
s0����,�,�"��1�1�3��L�L�'�'�)�-rc����jjdt|||���|r�jj	�y�jjt��jj�y)NzWSGI application error)�failure)r-r�r	rp�loseConnectionr�r
r�)rlr)r��	tracebackr/s    �r�	wsgiErrorz$_WSGIResponse.run.<locals>.wsgiError�s`����	�	�!�!�,�g�e�T�9�6U�"����L�L�/�/�1��L�L�0�0�1F�G��L�L�'�'�)rT)ror�r�r1r��getattrrm�callFromThreadrl�
BaseExceptionr)r/�appIteratorr�r�r�r�s`     rr�z_WSGIResponse.run�s����	B��*�*�4�<�<��9K�9K�L�K�#�
����J�J�t�$��(�(��	
�
�K��$�7�E�� ��� 
*�
�L�L�'�'�
�D�L�L�A�����-�	N�
*�
(�D�L�L�'�'�	�4�<�<�M�(�*�M����-	N�s�A
B�B�>C"�!C"rO)r*r:r;r<r�rr-rBrsr�r1r�r�r�rrrrQrQ�s?���B���8�D�L
�\%�D�L) �V�(/�'rrQc�,�eZdZdZdZd�Zd�Zd�Zd�Zy)�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.
    Tc�.�||_||_||_yrO)�_reactor�_threadpool�_application)r/rmrnros    rrBzWSGIResource.__init__'s����
�%���'��rc��t|j|j|j|�}|j	�t
S)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.
        )rQr�r�r�r�r)r/rp�responses   r�renderzWSGIResource.render,s9��!��M�M�4�+�+�T�->�->��
��	�����rc��td��)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 WSGIResource��RuntimeError)r/r�rps   r�getChildWithDefaultz WSGIResource.getChildWithDefault<s���L�M�Mrc��td��)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 WSGIResourcer�)r/�path�childs   r�putChildzWSGIResource.putChildDs���M�N�NrN)	r*r:r;r<�isLeafrBr�r�r�rrrr�r�s$����F�(�
� N�Orr�N)r<�collections.abcr�sysr�warningsr�zope.interfacer�twisted.internet.threadsr�twisted.loggerr�twisted.python.failurer	�twisted.web.httpr
�twisted.web.resourcer�twisted.web.serverrrr(rrrr>rQr��__all__rrr�<module>r�s����
%���&�;�!�*�2�*�+�(�%�<�/��/�"
+�@�@�F<#�<#�~n�n�b	
�Y��5O�5O��5O�p�
�r