Ϫf.2dZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl mZmZmZmZGd d ej$e j&eZGd d ej$ZGd deZGddej.e j0Zy)zH I hold resource classes and helper classes that deal with CGI scripts. N)AnyStr)protocol)Logger)filepath)pb)httpresourceserverstaticceZdZdZdZdZy) CGIDirectoryctjj|tjj||yN)r Resource__init__rFilePath)selfpathnames 3/usr/lib/python3/dist-packages/twisted/web/twcgi.pyrzCGIDirectory.__init__s,""4(""42c|j|}|jstjjS|j rt |jSt|jSr) childexistsr File childNotFoundisdirr path CGIScript)rrrequestfnps rgetChildzCGIDirectory.getChildsPjjzz|;;,, , YY[) )SXX& &rcNtjd}|j|S)Nz1CGI directories do not support directory listing.)r NoResourcerender)rrnotFounds rr$zCGIDirectory.render%s&&& ? w''rN)__name__ __module__ __qualname__rr!r$rrr r s3'(rr c,eZdZdZdZddZdZgfdZy)rz L{CGIScript} is a resource which runs child processes according to the CGI specification. The implementation is complex due to the fact that it requires asynchronous IPC with an external process with an unpleasant protocol. Nc0||_|ddlm}||_y)zA Initialize, with the name of a CGI script file. Nr)reactor)filenametwisted.internetr-_reactor)rr.registryr-s rrzCGIScript.__init__7s! ? 1 rc 8ddj|jz}|jjdd}tj |d|j t|jj|j||j|jd }|jj}|||d<|j}|rddj|z|d <t!|d r`|j"j%dd |j"j'}|j"j%ddt||d < |jj)d }|j|dzdx} |d<d| vrg} nJ| jdD cgc]/} t*j,j/| j11} } |j5j7D]3\} } | j9ddj;}| dvrd|z}| ||<5t<j>j7D]\}}||vs |||<|jA||| tjBScc} w#t2$r d|d<g} YwxYw)a Do various things to conform to the CGI specification. I will set up the usual slew of environment variables, then spin off a process. @type request: L{twisted.web.http.Request} @param request: An HTTP request. /:rzCGI/1.1) SERVER_SOFTWARE SERVER_NAMEGATEWAY_INTERFACESERVER_PROTOCOL SERVER_PORTREQUEST_METHOD SCRIPT_NAMESCRIPT_FILENAME REQUEST_URIN REMOTE_ADDR/ PATH_INFOcontentCONTENT_LENGTH?r+ QUERY_STRING=+-_)s content-typescontent-lengthsproxysHTTP_)"joinprepathgetRequestHostnamesplitr version clientprotostrgetHostportmethodr.urigetClientAddresshostpostpathhasattrrAseektellindexurllibparseunquotedecode ValueError getAllHeadersitemsreplaceupperosenviron runProcess NOT_DONE_YET)rr scriptName serverNameenvippplengthqindexqsqargsxtitleheaderenvnamekeyvalues rr$zCGIScript.renderCsjDIIgoo66 //177=a@ %~~%!*&22w0556%nn%#}}";;   % % ' , , >!#C     "SXXb\1C  7I & OO A &__))+F OO A &$'KC ! S[[&&t,F (/{{6A:<'@ @B^$rzCE88D>Ra--ahhj9RR%224::< "ME6mmD$/557GJJ"W,!CL  " ****, !JC#~ C ! We,"""S "$C E sJ#4JJJc t|}|jj||j|jg|z|tj j |jy)aI Run the cgi script. @type env: A L{dict} of L{str}, or L{None} @param env: The environment variables to pass to the process that will get spawned. See L{twisted.internet.interfaces.IReactorProcess.spawnProcess} for more information about environments and process creation. @type request: L{twisted.web.http.Request} @param request: An HTTP request. @type qargs: A L{list} of L{str} @param qargs: The command line arguments to pass to the process that will get spawned. N)CGIProcessProtocolr0 spawnProcessr.rfrdirnamerrlrrrps rrhzCGIScript.runProcesssO" w ' "" MM ]]Oe #  GGOODMM *  r)NN)r&r'r(__doc__isLeafrr$rhr)rrrr,s#F  ?#B.0 rrceZdZdZdZgfdZy)FilteredScripta I am a special version of a CGI script, that uses a specific executable. This is useful for interfacing with other scripting languages that adhere to the CGI standard. My C{filter} attribute specifies what executable to run, and my C{filename} init parameter describes which script to pass to the first argument of that script. To customize me for a particular location of a CGI interpreter, override C{filter}. @type filter: L{str} @ivar filter: The absolute path to the executable. z /usr/bin/catc t|}|jj||j|j|jg|z|t j j|jy)ad Run a script through the C{filter} executable. @type env: A L{dict} of L{str}, or L{None} @param env: The environment variables to pass to the process that will get spawned. See L{twisted.internet.interfaces.IReactorProcess.spawnProcess} for more information about environments and process creation. @type request: L{twisted.web.http.Request} @param request: An HTTP request. @type qargs: A L{list} of L{str} @param qargs: The command line arguments to pass to the process that will get spawned. N)rzr0r{filterr.rfrr|r}s rrhzFilteredScript.runProcesssV" w ' "" KK [[$-- (5 0  GGOODMM *  rN)r&r'r(rrrhr)rrrrs F-/ rrcveZdZdZdZdZdZeZdZ dZ dZ dZ dZ d Zd Zd Zd Zd ZdZdZdZy)rzr+rrFc$|jyr)resumeProducingrissuers rview_resumeProducingz'CGIProcessProtocol.view_resumeProducings rc$|jyr)pauseProducingrs rview_pauseProducingz&CGIProcessProtocol.view_pauseProducings rc$|jyr) stopProducingrs rview_stopProducingz%CGIProcessProtocol.view_stopProducings rc8|jjyr) transportrrs rrz"CGIProcessProtocol.resumeProducings &&(rc8|jjyr)rrrs rrz!CGIProcessProtocol.pauseProducing %%'rc8|jjyr)rloseConnectionrs rrz CGIProcessProtocol.stopProducingrrcx||_|jjj|jyr)r notifyFinishaddBoth _finished)rrs rrzCGIProcessProtocol.__init__s(  !!#++DNN;rc>|jj|d|jjjdd|jjj }|r|j j ||j jy)Nr+r)rregisterProducerrArZreadrwrite closeStdin)rrAs rconnectionMadez!CGIProcessProtocol.connectionMadesm %%dA. !!!Q',,&&++-  NN  ) !!#rc,|j|z|_yr) errortext)rerrors r errReceivedzCGIProcessProtocol.errReceiveds%/rc<|jr|j|z}g}dD],}|j|}|dk7s|j||f.|rdd|j_|j |d\}}|d||_|dt|dz}|jj|}|D]}|jd} | dk(r|jjd|7|d| j} || dzd} | d k(r)|jjtj| d k(r+ t| dd } |jj| | jd vs|jj"j%| | ||t|zd}d|_|jr||_|js|jj'|yy#t $r|jjd YfwxYw)z) Handle a chunk of input )s s s s NrrBs: z)ignoring malformed CGI header: {header!r})ruslocationsstatuszmalformed status header)sserversdate)handling_headers headertextfindappendrdefaultContentTypesortlenrN_logrlowersetResponseCoderFOUNDint BaseExceptionresponseHeaders addRawHeaderr) routputtext headerEnds delimiter headerend linebreakheadersrubr headerName headerText statusNums r outReceivedzCGIProcessProtocol.outReceiveds  ??V+DJE >  IIi0 ?%%y)&<= >37 /!'1!}$ 9"&z "2%&;I!(;< //// :%"FU+BRx GPV(&,CR[%6%6%8 %+BFH%5 %4 LL88D%2H,/ 2A,? !% < > IIOO4LL$$..    IIOOALL$$??   (( ""&&22*:fT\\* $$ LL + + - LL   !%rcd|_y)zc Record the end of the response generation for the request being serviced. TN)r)rignoreds rrzCGIProcessProtocol._finishedRs !%rN)r&r'r(rheaders_writtenrrrrrrrrrrrrrrrrrr)rrrzrzsbOJI 8D)((<$08't"B%rrz)rrfr]typingrr/rtwisted.loggerrtwisted.pythonrtwisted.spreadr twisted.webrr r r rrr rrProcessProtocolViewablerzr)rrrs   &!#66(8$$h&7&7&?(*p !!p f* Y* ZK%112;;K%r