ó
    ×›‘h×.  ã                   ó2  • S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJrJrJrJr   " S	 S
\R$                  \
R&                  \   5      r " S S\R$                  5      r " S S\5      r " S S\R.                  \R0                  5      rg)zH
I hold resource classes and helper classes that deal with CGI scripts.
é    N)ÚAnyStr)Úprotocol)ÚLogger)Úfilepath)Úpb)ÚhttpÚresourceÚserverÚstaticc                   ó&   • \ rS rSrS rS rS rSrg)ÚCGIDirectoryé   c                 ó€   • [         R                  R                  U 5        [        R                  R                  X5        g ©N)r	   ÚResourceÚ__init__r   ÚFilePath)ÚselfÚpathnames     ÚUC:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/web/twcgi.pyr   ÚCGIDirectory.__init__   s*   € Ü×Ñ×"Ñ" 4Ô(Ü×Ñ×"Ñ" 4Õ2ó    c                 ó   • U R                  U5      nUR                  5       (       d  [        R                  R                  $ UR                  5       (       a  [        UR                  5      $ [        UR                  5      $ r   )	ÚchildÚexistsr   ÚFileÚchildNotFoundÚisdirr   ÚpathÚ	CGIScript)r   r   ÚrequestÚfnps       r   ÚgetChildÚCGIDirectory.getChild   sT   € Ø�j‰j˜ÓˆØ�z‰z�|‰|Ü—;‘;×,Ñ,Ð,Ø�Y‰Y�[‰[Ü §¡Ó)Ð)ä˜SŸX™XÓ&Ð&r   c                 óP   • [         R                  " S5      nUR                  U5      $ )Nz1CGI directories do not support directory listing.)r	   Ú
NoResourceÚrender)r   r!   ÚnotFounds      r   r'   ÚCGIDirectory.render%   s&   € Ü×&Ò&Ø?ó
ˆð �‰˜wÓ'Ð'r   © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   r#   r'   Ú__static_attributes__r*   r   r   r   r      s   † ò3ò'õ(r   r   c                   ó8   • \ rS rSrSrSrS	S jrS r/ 4S jrSr	g)
r    é,   zâ
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.
é   Nc                 ó.   • Xl         Uc  SSKJn  X0l        g)z1
Initialize, with the name of a CGI script file.
Nr   )Úreactor)ÚfilenameÚtwisted.internetr4   Ú_reactor)r   r5   Úregistryr4   s       r   r   ÚCGIScript.__init__7   s   € ð !ŒØ‰?õ 1Ø�r   c           
      ó^  • SSR                  UR                  5      -   nUR                  5       R                  S5      S   n[        R
                  USUR                  [        UR                  5       R                  5      UR                  UU R                  UR                  S.	nUR                  5       R                  nUb  XTS'   UR                  nU(       a  SSR                  U5      -   US	'   [!        US
5      (       a`  UR"                  R%                  SS5        UR"                  R'                  5       nUR"                  R%                  SS5        [        U5      US'    UR                  R)                  S5      nUR                  US-   S =o”S'   SU	;   a  / n
OMU	R                  S5       Vs/ s H0  n[*        R,                  R/                  UR1                  5       5      PM2     n
n UR5                  5       R7                  5        H4  u  pÍUR9                  SS5      R;                  5       nUS;  a  SU-   nXÔU'   M6     [<        R>                  R7                  5        H  u  nnXô;  d  M  UXO'   M     U RA                  XAU
5        [        RB                  $ s  snf ! [2         a
    SUS'   / n
 NÃf = f)zÖ
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.
ó   /ó   :r   zCGI/1.1)	ÚSERVER_SOFTWAREÚSERVER_NAMEÚGATEWAY_INTERFACEÚSERVER_PROTOCOLÚSERVER_PORTÚREQUEST_METHODÚSCRIPT_NAMEÚSCRIPT_FILENAMEÚREQUEST_URINÚREMOTE_ADDRÚ/Ú	PATH_INFOÚcontenté   ÚCONTENT_LENGTHó   ?r2   ÚQUERY_STRINGó   =ó   +Ú ó   -ó   _)s   content-types   content-lengths   proxys   HTTP_)"ÚjoinÚprepathÚgetRequestHostnameÚsplitr
   ÚversionÚclientprotoÚstrÚgetHostÚportÚmethodr5   ÚuriÚgetClientAddressÚhostÚpostpathÚhasattrrI   ÚseekÚtellÚindexÚurllibÚparseÚunquoteÚdecodeÚ
ValueErrorÚgetAllHeadersÚitemsÚreplaceÚupperÚosÚenvironÚ
runProcessÚNOT_DONE_YET)r   r!   Ú
scriptNameÚ
serverNameÚenvÚipÚppÚlengthÚqindexÚqsÚqargsÚxÚtitleÚheaderÚenvnameÚkeyÚvalues                    r   r'   ÚCGIScript.renderC   sX  € ð ˜DŸI™I g§o¡oÓ6Ñ6ˆ
Ø×/Ñ/Ó1×7Ñ7¸Ó=¸aÑ@ˆ
ä%Ÿ~™~Ø%Ø!*Ø&×2Ñ2Ü˜wŸ™Ó0×5Ñ5Ó6Ø%Ÿn™nØ%Ø#Ÿ}™}Ø"Ÿ;™;ñ

ˆð ×%Ñ%Ó'×,Ñ,ˆØ‰>Ø!#�ÑØ×ÑˆÞØ" S§X¡X¨b£\Ñ1ˆC�Ñä�7˜I×&Ñ&ð �O‰O× Ñ   AÔ&Ø—_‘_×)Ñ)Ó+ˆFØ�O‰O× Ñ   AÔ&Ü$'¨£KˆCÐ Ñ!ð
	SØ—[‘[×&Ñ& tÓ,ˆFð
 (/§{¡{°6¸A±:°<Ð'@Ð@ˆB�^Ñ$Ø�r‹zØ‘àCEÇ8Á8ÈDÄ>ÓRÂ>¸aœŸ™×-Ñ-¨a¯h©h«jÖ9Á>�ÐR�ð %×2Ñ2Ó4×:Ñ:Ö<‰MˆEØ—m‘m D¨$Ó/×5Ñ5Ó7ˆGØÐJÓJØ" WÑ,�Ø!�‹Lñ	 =ô Ÿ*™*×*Ñ*Ö,‰JˆC�Ø�~Ø �“ñ -ð 	�‰˜ eÔ,Ü×"Ñ"Ð"ùò Søô ó 	Ø"$ˆC�ÑØŠEð	ús   Å!J Æ.7JÊJ,Ê+J,c           	      óÖ   • [        U5      nU R                  R                  UU R                  U R                  /U-   U[        R
                  R                  U R                  5      5        g)aé  
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)ÚCGIProcessProtocolr7   ÚspawnProcessr5   rn   r   Údirname©r   rt   r!   rz   Úps        r   rp   ÚCGIScript.runProcess„   sO   € ô" ˜wÓ'ˆØ�‰×"Ñ"ØØ�M‰MØ�]‰]ˆO˜eÑ#ØÜ�G‰G�O‰O˜DŸM™MÓ*õ	
r   )r7   r5   )NN)
r+   r,   r-   r.   Ú__doc__ÚisLeafr   r'   rp   r/   r*   r   r   r    r    ,   s#   † ñð €Fô
 ò?#ðB .0÷ 
r   r    c                   ó(   • \ rS rSrSrSr/ 4S jrSrg)ÚFilteredScriptéŸ   aè  
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           	      óì   • [        U5      nU R                  R                  UU R                  U R                  U R                  /U-   U[
        R                  R                  U R                  5      5        g)a  
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)rƒ   r7   r„   Úfilterr5   rn   r   r…   r†   s        r   rp   ÚFilteredScript.runProcess±   sV   € ô" ˜wÓ'ˆØ�‰×"Ñ"ØØ�K‰KØ�[‰[˜$Ÿ-™-Ð(¨5Ñ0ØÜ�G‰G�O‰O˜DŸM™MÓ*õ	
r   r*   N)r+   r,   r-   r.   r‰   r�   rp   r/   r*   r   r   rŒ   rŒ   Ÿ   s   † ñð €Fà-/÷ 
r   rŒ   c                   ó~   • \ rS rSrSrSrSrSr\" 5       r	Sr
S rS rS rS	 rS
 rS rS rS rS rS rS rS rSrg)rƒ   éÌ   r2   r   r   Fc                 ó$   • U R                  5         g r   )ÚresumeProducing©r   Úissuers     r   Úview_resumeProducingÚ'CGIProcessProtocol.view_resumeProducingÖ   s   € Ø×ÑÕr   c                 ó$   • U R                  5         g r   )ÚpauseProducingr•   s     r   Úview_pauseProducingÚ&CGIProcessProtocol.view_pauseProducingÙ   s   € Ø×ÑÕr   c                 ó$   • U R                  5         g r   )ÚstopProducingr•   s     r   Úview_stopProducingÚ%CGIProcessProtocol.view_stopProducingÜ   s   € Ø×ÑÕr   c                 ó8   • U R                   R                  5         g r   )Ú	transportr”   ©r   s    r   r”   Ú"CGIProcessProtocol.resumeProducingß   s   € Ø�‰×&Ñ&Õ(r   c                 ó8   • U R                   R                  5         g r   )r¢   rš   r£   s    r   rš   Ú!CGIProcessProtocol.pauseProducingâ   ó   € Ø�‰×%Ñ%Õ'r   c                 ó8   • U R                   R                  5         g r   )r¢   ÚloseConnectionr£   s    r   rž   Ú CGIProcessProtocol.stopProducingå   r§   r   c                 óv   • Xl         U R                   R                  5       R                  U R                  5        g r   )r!   ÚnotifyFinishÚaddBothÚ	_finished)r   r!   s     r   r   ÚCGIProcessProtocol.__init__è   s&   € ØŒØ�‰×!Ñ!Ó#×+Ñ+¨D¯N©NÕ;r   c                 óH  • U R                   R                  U S5        U R                   R                  R                  SS5        U R                   R                  R	                  5       nU(       a  U R
                  R                  U5        U R
                  R                  5         g )Nr2   r   )r!   ÚregisterProducerrI   rb   Úreadr¢   ÚwriteÚ
closeStdin)r   rI   s     r   ÚconnectionMadeÚ!CGIProcessProtocol.connectionMadeì   sm   € Ø�‰×%Ñ% d¨AÔ.Ø�‰×Ñ×!Ñ! ! QÔ'Ø—,‘,×&Ñ&×+Ñ+Ó-ˆÞØ�N‰N× Ñ  Ô)Ø�‰×!Ñ!Õ#r   c                 ó,   • U R                   U-   U l         g r   )Ú	errortext)r   Úerrors     r   ÚerrReceivedÚCGIProcessProtocol.errReceivedô   s   € ØŸ™¨%Ñ/ˆ�r   c                 ór  • U R                   (       GaË  U R                  U-   n/ nS H.  nUR                  U5      nUS:w  d  M  UR                  XT45        M0     U(       Gag  SU R                  l        UR                  5         US   u  pTUSU U l        US[        U5      S-   nU R                  R                  U5      nU Hì  nUR                  S5      n	U	S:X  a  U R                  R                  SUS9  M6  USU	 R                  5       n
X‰S-   S nU
S	:X  a)  U R                  R                  [        R                  5        U
S
:X  a,   [        USS 5      nU R                  R                  U5        M±  U
R                  5       S;  d  MÇ  U R                  R"                  R%                  X«5        Mî     X%[        U5      -   S nSU l         U R                   (       a  X l        U R                   (       d  U R                  R'                  U5        gg! [          a     U R                  R                  S5         GMu  f = f)z
Handle a chunk of input
)s   

s   

s   s   

éÿÿÿÿNr   rJ   s   : z)ignoring malformed CGI header: {header!r})r}   s   locations   statusé   zmalformed status header)s   servers   date)Úhandling_headersÚ
headertextÚfindÚappendr!   ÚdefaultContentTypeÚsortÚlenrV   Ú_logr¹   ÚlowerÚsetResponseCoder   ÚFOUNDÚintÚBaseExceptionÚresponseHeadersÚaddRawHeaderr³   )r   ÚoutputÚtextÚ
headerEndsÚ	delimiterÚ	headerendÚ	linebreakÚheadersr}   ÚbrÚ
headerNameÚ
headerTextÚ	statusNums                r   ÚoutReceivedÚCGIProcessProtocol.outReceived÷   s  € ð × × Ð Ø—?‘? VÑ+ˆDØˆJÛE�	Ø ŸI™I iÓ0�	Ø •?Ø×%Ñ% yÐ&<Ö=ñ F÷ ð 37�—‘Ô/à—‘Ô!Ø'1°!¡}Ñ$�	Ø"& z¨	Ð"2�”à%Ð&;¬¨I«¸!Ñ(;Ð<�	ØŸ/™/×/Ñ/°	Ó:�Û%�FØŸ™ UÓ+�BØ˜R“xØŸ	™	Ÿ™ØGÐPVð (ó ð &,¨C¨R [×%6Ñ%6Ó%8˜
Ø%+°©F¨HÐ%5˜
Ø%¨Ó4Ø ŸL™L×8Ñ8¼¿¹ÔDØ%¨Ó2ðHä,/°
¸2¸A°Ó,? 	ð !%§¡× <Ñ <¸YÖ Gð  *×/Ñ/Ó1Ð9MÕMØ $§¡× <Ñ <× IÑ IØ$.ö!"ñ/ &ð4 ¬#¨i«.Ñ8Ð:Ð;�Ø()�Ô%Ø×$×$Ø"&”Ø×$×$Ø�L‰L×Ñ˜vÕ&ð %øô $1ó KØ $§	¡	§¡Ð0I× JÐ JðKús   ÅHÈ%H6È5H6c                 óD  • UR                   R                  S:w  aC  U R                  R                  SU R                  R
                  UR                   R                  S9  U R                  (       a9  U R                  R                  SU R                  R
                  U R                  S9  U R                  (       a¢  U R                  R                  SU R                  R
                  U R                  S9  U R                  (       dX  U R                  R                  [        R                  " [        R                  SS	5      R                  U R                  5      5        U R                  (       d5  U R                  R!                  5         U R                  R#                  5         g g )
Nr   z*CGI {uri} exited with exit code {exitCode})r]   ÚexitCodez"Errors from CGI {uri}: {errorText})r]   Ú	errorTextz/Premature end of headers in {uri}: {headerText})r]   r×   zCGI Script Errorz Premature end of script headers.)r€   rÜ   rÆ   r¹   r!   r]   r¸   r¿   rÀ   Ú_requestFinishedr³   r	   Ú	ErrorPager   ÚINTERNAL_SERVER_ERRORr'   ÚunregisterProducerÚfinish)r   Úreasons     r   ÚprocessEndedÚCGIProcessProtocol.processEnded1  s,  € Ø�<‰<× Ñ  AÓ%Ø�I‰I�O‰OØ<Ø—L‘L×$Ñ$ØŸ™×.Ñ.ð ñ ð
 �>�>Ø�I‰I�O‰OØ4Ø—L‘L×$Ñ$ØŸ.™.ð ñ ð × × Ø�I‰I�O‰OØAØ—L‘L×$Ñ$ØŸ?™?ð ñ ð
 ×(×(Ø—‘×"Ñ"Ü×&Ò&Ü×2Ñ2Ø*Ø:ó÷ ‘f˜TŸ\™\Ó*ôð ×$×$Ø�L‰L×+Ñ+Ô-Ø�L‰L×ÑÕ!ð %r   c                 ó   • SU l         g)zK
Record the end of the response generation for the request being
serviced.
TN)rÞ   )r   Úignoreds     r   r®   ÚCGIProcessProtocol._finishedR  s   € ð
 !%ˆÕr   )rÞ   r¸   r¿   rÀ   r!   N)r+   r,   r-   r.   r¿   Úheaders_writtenrÀ   r¸   r   rÆ   rÞ   r—   r›   rŸ   r”   rš   rž   r   rµ   rº   rÙ   rä   r®   r/   r*   r   r   rƒ   rƒ   Ì   sb   † ØÐØ€OØ€JØ€IÙ‹8€DØÐòòòò)ò(ò(ò<ò$ò0ò8'òt"õB%r   rƒ   )r‰   rn   re   Útypingr   r6   r   Útwisted.loggerr   Útwisted.pythonr   Útwisted.spreadr   Útwisted.webr   r	   r
   r   r   r   r   r    rŒ   ÚProcessProtocolÚViewablerƒ   r*   r   r   Ú<module>rñ      s„   ðñó
 
Û Ý õ &Ý !Ý #Ý ß 6Ó 6ô(�8×$Ñ$ h×&7Ñ&7¸Ñ&?ô (ô*p
�×!Ñ!ô p
ôf*
�Yô *
ôZK%˜×1Ñ1°2·;±;õ K%r   