
    Vh<Q                     &   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ deeef   defdZdedefdZ G d d      Z G d d      Z G d d      Z ee       G d d             ZdgZ y)zo
An implementation of
U{Python Web Server Gateway Interface v1.0.1<http://www.python.org/dev/peps/pep-3333/>}.
    )Sequence)exc_info)ListUnion)warn)implementer)blockingCallFromThread)Logger)Failure)INTERNAL_SERVER_ERROR)	IResource)NOT_DONE_YETstringreturnc                     t        | t              r | j                  d      j                  d      S | j                  d      S )aW  
    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.
    
iso-8859-1)
isinstancestrencodedecoder   s    @/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/wsgi.py_wsgiStringr   %   s7     &#}}\*11,??}}\**    c                 $    | j                  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.
    r   )r   r   s    r   _wsgiStringToBytesr   7   s     ==&&r   c                   J    e Zd ZdZ e       ZdeddfdZdee   ddfdZ	d Z
y)	_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.
    datar   Nc                     t        |t              s%t        d|dt        |      j                  d      | j
                  j                  |dd|f       y)a  
        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: if C{data} is not a native string.
        z"write() argument must be str, not  ()wsgiT)systemisErrormessageN)r   r   	TypeErrortype__name___logerror)selfr   s     r   writez_ErrorStream.writeR   sK     $$d,,.  			VTD7Kr   iovecc                 D    | j                  dj                  |             y)aZ  
        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: if C{iovec} contains any non-native strings.
         N)r-   join)r,   r.   s     r   
writelinesz_ErrorStream.writelinesg   s     	

2775>"r   c                      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.
        N r,   s    r   flushz_ErrorStream.flushu   s    r   )r)   
__module____qualname____doc__r
   r*   r   r-   r   r2   r6   r4   r   r   r   r   D   sB    	 8DL# L$ L*#S	 #d #r   r   c                   4    e Zd ZdZd ZddZddZddZd Zy)	_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     r   Nc                 p    || j                   j                         S | j                   j                  |      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     r   rA   z_InputStream.read   s1     <==%%''}}!!$''r   c                 z    |dk(  s|| j                   j                         S | j                   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.
        )r=   readlinerB   s     r   rF   z_InputStream.readline   s7     2:==))++}}%%d++r   c                 p    || j                   j                         S | j                   j                  |      S )z
        Pass through to the underlying C{readlines}.

        This is called in a WSGI application thread, not the I/O thread.
        )r=   	readlinesrB   s     r   rH   z_InputStream.readlines   s1     <==**,,}}&&t,,r   c                 ,    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=   r5   s    r   __iter__z_InputStream.__iter__   s     DMM""r   N)	r)   r7   r8   r9   r?   rA   rF   rH   rK   r4   r   r   r;   r;   ~   s     	(,	-#r   r;   c                   N    e Zd ZdZdZ e       Zd Zd ZddZ	d Z
d Zd	 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                    d| _         || _        || _        || _        || _        | j                  j                         j                  | j                         |j                  rddj                  |j                        z   }nd}|j                  rddj                  |j                        z   }nd}|j                  j                  dd      }t        |      dk(  rd}n|d   }|j                         }	t        |j                         t        |	j"                        t        t%        |	j&                              t        |      t        |      t        |      t        |j)                  d      xs d      t        |j)                  d      xs d      t        |j+                               t        t%        |j-                         j&                              t        |j.                        d	| _        d | j                  _        |j4                  j7                         D ]f  \  }
}d
t        |
      j9                         j;                  dd      z   }
dj                  d |D              j;                  dd      | j0                  |
<   h | j0                  j=                  d|j?                         xr dxs ddddtA               tC        |jD                        d       y )NF   /r      ?   s   content-typer0   s   content-length)REQUEST_METHODREMOTE_ADDRREMOTE_PORTSCRIPT_NAME	PATH_INFOQUERY_STRINGCONTENT_TYPECONTENT_LENGTHSERVER_NAMESERVER_PORTSERVER_PROTOCOLHTTP_-_,c              3   2   K   | ]  }t        |        y wrL   )r   ).0vs     r   	<genexpr>z)_WSGIResponse.__init__.<locals>.<genexpr>  s     )IQ+a.)Is   
 )rR   r   httpshttpT)zwsgi.versionzwsgi.url_schemezwsgi.run_oncezwsgi.multithreadzwsgi.multiprocesszwsgi.errorsz
wsgi.input)#startedreactor
threadpoolapplicationrequestnotifyFinishaddBoth	_finishedprepathr1   postpathurisplitlengetClientAddressr   methodhostr   port	getHeadergetRequestHostnamegetHostclientprotoenvirondefaultContentTyperequestHeadersgetAllRawHeadersupperreplaceupdateisSecurer   r;   content)r,   rk   rl   rm   rn   
scriptNamepathInfopartsqueryString
remotePeernamevaluess               r   r?   z_WSGIResponse.__init__   sU   $&!!#++DNN;??		'// ::JJdii(8(899HH!!$*u:?K(K --/
)'..9&z7&s:??';<&z2$X.'4'(9(9/(J(PbQ)'*;*;<M*N*TRTU&w'A'A'CD&s7??+<+A+A'BC*7+>+>?
" +/'#22CCE 	LD&[.446>>sCHHD "%)I&)I!I!Q!Qc"DLL		 	 &#*#3#3#5#A'#KV!&$(%*+~ +7??;+	
r   c                     d| _         y)zc
        Record the end of the response generation for the request being
        serviced.
        TN)_requestFinished)r,   ignoreds     r   rq   z_WSGIResponse._finished2  s    
 !%r   Nc           	      T   | j                   r||d   j                  |d         t        |t              s.t	        dj                  |t        |      j                              t        |t              rnat        |t              r,t        d|dt        |      j                  dt               n%t	        d|dt        |      j                  d      |D ]  }t        |t              rnat        |t              r,t        d	|dt        |      j                  dt               n%t	        d
|dt        |      j                  d      t        |      dk7  rt	        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.
        rR      z!status must be str, not {!r} ({})zheaders should be a list, not r!   r"   )categoryzheaders 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 )rj   with_tracebackr   r   r'   formatr(   r)   listr   r   RuntimeWarningtuplerv   statusheadersr-   )r,   r   r   excInfoheaderelems         r   startResponsez_WSGIResponse.startResponse9  s    <<G/!*++GAJ77 &#&3::DL11  gt$*DM224' DM224   	XF&%(FH-tF|446+  tF|446  6{a"I& TUU
  X!$,#&KF:$VWWX-	X4 zzr   c                 |      fd}	 t         j                  | j                        d _        S # d _        w xY w)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                          j                  j                         y rL   )_sendResponseHeadersrn   r-   )rj   r   r,   s    r   	wsgiWritez&_WSGIResponse.write.<locals>.wsgiWrite  s$    ))+LLt$r   T)r	   rk   rj   )r,   r   r   s   `` r   r-   z_WSGIResponse.write~  s1    B	%
	 )$,,	4<<PDL4DLs    2 	;c                 `   | j                   j                  dd      \  }}t        |      }| j                  j	                  |t        |             | j                  D ]P  \  }}|j                         dvs| j                  j                  j                  t        |      t        |             R y)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.
        NrR   )serverdate)
r   ru   intrn   setResponseCoder   r   lowerresponseHeadersaddRawHeader)r,   coder&   r   values        r   r   z"_WSGIResponse._sendResponseHeaders  s     ))$2g4y$$T+=g+FG<< 	KD%zz|#55,,99&t,.@.G	r   c                 N    | j                   j                  | j                         y)zo
        Start the WSGI application in the threadpool.

        This must be called in the I/O thread.
        N)rl   callInThreadrunr5   s    r   startz_WSGIResponse.start  s     	$$TXX.r   c                     	  j                   j                   j                        }|D ]#  }|r j                  |        j                  s# n t        |dd      }| |         fd} j                  j                  | j                         d _        y# t        $ r?  fd}  j                  j                  | j                  gt                 Y d _        yw xY w)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    j                   s-| sj                          j                  j                          y y rL   )r   r   rn   finish)rj   r,   s    r   
wsgiFinishz%_WSGIResponse.run.<locals>.wsgiFinish  s0    ,,"113LL'') -r   c                    j                   j                  dt        |||             | rj                  j	                          y j                  j                  t               j                  j                          y )NzWSGI application error)failure)r*   r   r   rn   loseConnectionr   r   r   )rj   r(   r   	tracebackr,   s       r   	wsgiErrorz$_WSGIResponse.run.<locals>.wsgiError  s`    		!!,geT96U "  LL//1LL001FGLL'')r   T)rm   r   r   r-   r   getattrrk   callFromThreadrj   BaseExceptionr   )r,   appIteratorr   r   r   r   s   `     r   r   z_WSGIResponse.run  s    	B**4<<9K9KLK# JJt$((	
 K$7E  * LL''
DLLA-  	N* (DLL''	4<<M(*M -	Ns   A
B B >C"!C"rL   )r)   r7   r8   r9   r   r
   r*   r?   rq   r   r-   r   r   r   r4   r   r   rN   rN      s?    B 8DN
`%CJ) V(/'r   rN   c                   ,    e Zd ZdZdZd Zd Zd Zd Zy)WSGIResourceaW  
    An L{IResource} implementation which delegates responsibility for all
    resources hierarchically inferior to it to a WSGI application.

    The C{environ} argument passed to the application, includes the
    C{REMOTE_PORT} key to complement the C{REMOTE_ADDR} key.

    @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                 .    || _         || _        || _        y rL   )_reactor_threadpool_application)r,   rk   rl   rm   s       r   r?   zWSGIResource.__init__  s    %'r   c                     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.
        )rN   r   r   r   r   r   )r,   rn   responses      r   renderzWSGIResource.render
  s9     !MM4++T->->
 	r   c                     t        d      )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   rn   s      r   getChildWithDefaultz WSGIResource.getChildWithDefault  s     LMMr   c                     t        d      )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   putChildzWSGIResource.putChild"  s     MNNr   N)	r)   r7   r8   r9   isLeafr?   r   r   r   r4   r   r   r   r     s$    $ F(
 NOr   r   N)!r9   collections.abcr   sysr   typingr   r   warningsr   zope.interfacer   twisted.internet.threadsr	   twisted.loggerr
   twisted.python.failurer   twisted.web.httpr   twisted.web.resourcer   twisted.web.serverr   r   bytesr   r   r   r;   rN   r   __all__r4   r   r   <module>r      s   
 %    & ; ! * 2 * + +c5j) +c +$
's 
'u 
'7 7t<# <#~o od	 Y8O 8O 8Ov 
r   