ϪfUdZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZdd lmZeeurd Zd ZndZdZGddZGddZGddZeeGddZdgZy)zo An implementation of U{Python Web Server Gateway Interface v1.0.1}. )Sequence)exc_info)warn) implementer)blockingCallFromThread)Logger)Failure)INTERNAL_SERVER_ERROR) IResource) NOT_DONE_YETcHt|tr|S|jdS)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) isinstancestrencodestrings 2/usr/lib/python3/dist-packages/twisted/web/wsgi.py _wsgiStringr*s" fc "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  rct|tr |jdjdS|jdS)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)rrrdecoders rrrCs7 fc "==.55lC C==. .rc$|jdS)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}}\**rc0eZdZdZeZdZdZdZy) _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} 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. ct|ts]ttur,td|dt |j dt n%td|dt |j d|jj|dd|fy ) 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)systemisErrormessageN) rrbytesrtype__name__UnicodeWarning TypeError_logerror)selfdatas rwritez_ErrorStream.writeoss$$e|T$Z002+  T$Z002 VTD7KrcD|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)r1join)r/iovecs r writelinesz_ErrorStream.writeliness 2775>"rcy)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. Nrr/s rflushz_ErrorStream.flushsrN) r* __module__ __qualname____doc__rr-r1r6r9rrrrras   8DL: # rrc4eZdZdZdZddZddZddZdZy) _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  rNcp||jjS|jj|S)z Pass through to the underlying C{read}. This is called in a WSGI application thread, not the I/O thread. )r@readr/sizes rrDz_InputStream.reads1 <==%%' '}}!!$''rcz|dk(s||jjS|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.readlines7 2:==))+ +}}%%d++rcp||jjS|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.readliness1 <==**, ,}}&&t,,rc,t|jS)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 DMM""rN) r*r:r;r<rBrDrIrKrNrrrr>r>s  ( , -#rr>cNeZdZdZdZeZdZdZd dZ dZ dZ 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}. Fcrd|_||_||_||_||_|jj j |j|jrddj|jz}nd}|jrddj|jz}nd}|jjdd}t|dk(rd}n|d}t|jt|j!j"t|t|t|t|j%dxsdt|j%dxsdt|j'tt)|j+j,t|j.d |_d|j_|j4j7D]f\} } d t| j9j;d d z} d jd| Dj;dd|j0| <h|j0j=d|j?xrdxsddddtAtC|jDdy)NF/r?s content-typer3scontent-length) REQUEST_METHOD REMOTE_ADDR SCRIPT_NAME PATH_INFO QUERY_STRING CONTENT_TYPECONTENT_LENGTH SERVER_NAME SERVER_PORTSERVER_PROTOCOLHTTP_-_,c32K|]}t|ywrO)r).0vs r z)_WSGIResponse.__init__..8s)IQ+a.)Is  )rUrhttpshttpT)z wsgi.versionzwsgi.url_schemez wsgi.run_oncezwsgi.multithreadzwsgi.multiprocessz wsgi.errorsz wsgi.input)#startedreactor threadpool applicationrequest notifyFinishaddBoth _finishedprepathr4postpathurisplitlenrmethodgetClientAddresshost getHeadergetRequestHostnamergetHostport clientprotoenvirondefaultContentTyperequestHeadersgetAllRawHeadersupperreplaceupdateisSecurerr>content) r/rmrnrorp scriptNamepathInfoparts queryStringnamevaluess rrBz_WSGIResponse.__init__sA  $&  !!#++DNN; ?? '// ::JJ   dii(8(899HH !!$* u:?K(K *'..9&w'?'?'A'F'FG&z2$X.' 4'(9(9/(J(PbQ)'*;*;+>?   +/ '#22CCE LD&[.446>>sCHHD"%)I&)I!I!Q!Qc"DLL     &#*#3#3#5#A'#KV!&$(%*+~+7??;+  rcd|_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|dt|ts.t dj |t |jt|trnat|tr,td|dt |jdtn%t d|dt |jd|D]}t|trnat|tr,td |dt |jdtn%t d |dt |jdt|dk7rt d ||D] }t|trt 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. rUz!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 )rlwith_tracebackrrr,formatr)r*listrrRuntimeWarningtuplerxstatusheadersr1)r/rrexcInfoheaderelems r startResponsez_WSGIResponse.startResponse]s <.wsgiWrites$))+ LL  t $rT)rrmrl)r/r0rs`` rr1z_WSGIResponse.writes1B %  )$,, 4<<PDL4DLs 2 ;c`|jjdd\}}t|}|jj |t ||j D]P\}}|jdvs|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)serverdate) rrwintrpsetResponseCoderrlowerresponseHeaders addRawHeader)r/coder'rvalues rrz"_WSGIResponse._sendResponseHeaderss ))$2 g4y $$T+=g+FG<< KD%zz|#55 ,,99&t,.@.G rcN|jj|jy)zo Start the WSGI application in the threadpool. This must be called in the I/O thread. N)rn callInThreadrunr8s rstartz_WSGIResponse.starts $$TXX.rc jjj}|D]#}|rj|js#nt |dd}||fd}j j|jd_y#t$r?fd}j j|jgtYd_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). closeNcxjs-|sjjjyyrO)rrrpfinish)rlr/s r wsgiFinishz%_WSGIResponse.run..wsgiFinish s0,,"113LL'')-rcjjdt||||rjj yjj t jjy)NzWSGI application error)failure)r-rr rploseConnectionrr r)rlr)r tracebackr/s r wsgiErrorz$_WSGIResponse.run..wsgiErrors` !!,geT96U"LL//1LL001FGLL'')rT) rorrr1rgetattrrmcallFromThreadrl BaseExceptionr)r/ appIteratorrrrrs` rrz_WSGIResponse.runs B**4<<9K9KLK# JJt$((   K$7E   * LL ' ' DLL A - N * (DLL ' ' 4<< M(* M - NsA BB>C"!C"rO)r*r:r;r<rrr-rBrsrr1rrrrrrrQrQs?B 8DL \%DL) V(/'rrQc,eZdZdZdZdZdZdZdZy) 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 %'rct|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. )rQrrrrr )r/rpresponses rrenderzWSGIResource.render,s9! MM4++T->->  rctd)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/rrps rgetChildWithDefaultz WSGIResource.getChildWithDefault<s LMMrctd)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/pathchilds rputChildzWSGIResource.putChildDs MNNrN) r*r:r;r<isLeafrBrrrrrrrrs$ F(  NOrrN)r<collections.abcrsysrwarningsrzope.interfacertwisted.internet.threadsrtwisted.loggerrtwisted.python.failurer twisted.web.httpr twisted.web.resourcer twisted.web.serverr rr(rrrr>rQr__all__rrrrs %&;!*2*+(%< //" +@ @ F<#<#~nnb  Y5O5O5Op  r