o bW.@s:dZddlZddlZzddlZWn eydZYnwddlmZddlmZddl m Z m Z ddl m Z ddlmZddlmZdd lmZdd lmZmZmZmZmZdd lmZGd d d ejZGdddejejZe ejeGdddZ!Gdddej"Z#Gdddej$ej%Z&Gdddej"Z'dS)z Distributed web servers. This is going to have to be refactored so that argument parsing is done by each subprocess and not by the main web server (i.e. GET, POST etc.). N)BytesIO)getDOMImplementation)addressreactor)Logger)styles)pb) SIZE_LIMIT)httpresourceserverstaticutil)Headersc@s,eZdZddZddZddZddZd S) _ReferenceableProducerWrappercC ||_dSN)producer)selfrr5/usr/lib/python3/dist-packages/twisted/web/distrib.py__init__$ z&_ReferenceableProducerWrapper.__init__cC|jdSr)rresumeProducingrrrrremote_resumeProducing'z4_ReferenceableProducerWrapper.remote_resumeProducingcCrr)rpauseProducingrrrrremote_pauseProducing*rz3_ReferenceableProducerWrapper.remote_pauseProducingcCrr)r stopProducingrrrrremote_stopProducing-rz2_ReferenceableProducerWrapper.remote_stopProducingN)__name__ __module__ __qualname__rrrr!rrrrr#s  rc@s8eZdZdZddZddZddZdd Zd d Zd S) Requestzn A request which was received by a L{ResourceSubscription} and sent via PB to a distributed node. csdD]$}||ddddfddfddd }|||<qtt|d |d <tj||t|j|_|j d |_ |j d |_ |j d |_ |j d|_ |j d|_|j d|_|j d|_dS)z Initialize this L{twisted.web.distrib.Request} based on the copied state so that it closely resembles a L{twisted.web.server.Request}. )hostclientTCPUNIX)INETr)rcstddS)N)r IPv4Addressraddrdesctuprr@sz*Request.setCopyableState..cstdS)Nr+)r UNIXAddressr)r0rrr1As)r(r)requestHeadersfinish setHeader addCookiesetETagsetResponseCodesetLastModifiedwriteN)rdictr RemoteCopysetCopyableStater content_datacontentremote remoteMethodr4r5r6r7r8r9_write)rstatekaddrrr.rr=7s&    zRequest.setCopyableStatecCs>d}t} |||||t7}|t7}|t|krdSq)z Write the given bytes to the response body. @param bytes: The bytes to write. If this is longer than 640k, it will be split up into smaller pieces. rTN)r rBlen)rbytesstartendrrrr:Ss z Request.writecCs |jdt|||jdS)NregisterProducer)r@ callRemoter addErrbackfail)rr streamingrrrrJcs zRequest.registerProducercCs|jd|jdS)NunregisterProducer)r@rKrLrMrrrrrOhszRequest.unregisterProducercCs|jjd|ddS)N)failure)_logrQrrQrrrrMksz Request.failN) r"r#r$__doc__r=r:rJrOrMrrrrr%1s r%c@s*eZdZeZddZddZddZdS)IssuecCrr)request)rrVrrrrurzIssue.__init__cCs:|tjurt|tsJd|j||jdSdS)Nzreturn value not a string)r NOT_DONE_YET isinstancestrrVr:r4)rresultrrrfinishedxs  zIssue.finishedc CsLt|}|jttjddt| |j|j |j |dS)NzServer Connection Lostz&Connection to distributed server lost:) rYrVr:r ErrorPager INTERNAL_SERVER_ERRORr_PRErenderr4rRinforSrrrfailed~s  z Issue.failedN)r"r#r$rrRrr[rarrrrrUrs  rUc@sJeZdZdZdZeZddZddZddZ d d Z d d Z d dZ dS)ResourceSubscriptionr+rcCs(tj|||_||_g|_d|_dSr)r Resourcerr&portpending publisher)rr&rdrrrrs  zResourceSubscription.__init__cCs(t|j}d|d<d|d<g|d<|S)z3Get persistent state for this ResourceSubscription.Nrfrwaitingre)copy__dict__)rrCrrr __getstate__s z!ResourceSubscription.__getstate__cCsF|jd|j|j||_d|_|jD]}||qg|_dS)z=I've connected to a publisher; I'll now send all my requests.zconnected to publisherrN) rRr`brokernotifyOnDisconnectbootedrfrgrer_)rrfrVrrr connecteds    zResourceSubscription.connectedcCsD|jjd|dd|_d|_|jD] }|d|qg|_dS)zXI can't connect to a publisher; I'll now reply to all pending requests. z3could not connect to distributed web service: {msg})msgrNz(Unable to connect to distributed server.)rRr`rgrfrer:r4)rrorVrrr notConnecteds    z!ResourceSubscription.notConnectedcCs|ddS)Nzconnection dropped)rprrrrrmrzResourceSubscription.bootedcCs|js<|j||js9d|_t}d}|jdkr#t|j ||n t |j|j ||| }| |j |jtjSt|}|jd| |j|jtjS)a:Render this request, from my server. This will always be asynchronous, and therefore return NOT_DONE_YET. It spins off a request to the pb client, and either adds it to the list of pending issues or requests it immediately, depending on if the client is already connected. r+ unixrV)rfreappendrgrPBClientFactoryr&r connectUNIXrd connectTCP getRootObject addCallbacksrnrprUrKr[rar rW)rrVbftimeoutdirrrr_s"  zResourceSubscription.renderN) r"r#r$isLeafrgrrRrrjrnrprmr_rrrrrbs   rbc@s:eZdZdZeZddZdZddZddZ d d Z d S) ResourcePublisherz L{ResourcePublisher} exposes a remote API which can be used to respond to request. @ivar site: The site which will be used for resource lookup. @type site: L{twisted.web.server.Site} cCrr)site)rrrrrrrzResourcePublisher.__init__r,cCs*|jjd|jj|j=|`|`|`dS)Nweb) application authorizerremoveIdentityservices serviceNameperspectiveNamerrrrupgradeToVersion2s  z#ResourcePublisher.upgradeToVersion2cCs|Srr)rnamerrrgetPerspectiveNamedsz%ResourcePublisher.getPerspectiveNamedcCsD|j|}|j|||}|tjur|||tjS)zK Look up the resource for the given request and render it. ) rgetResourceForrRr`r_r rWr:r4)rrVresrZrrrremote_requests     z ResourcePublisher.remote_requestN) r"r#r$rTrrRrpersistenceVersionrrrrrrrr~s r~c@s>eZdZdZdZdZdZdddZdd Zd d Z d d Z dS) UserDirectoryz A resource which lists available user resources and serves them as children. @ivar _pwd: An object like L{pwd} which is used to enumerate users and their home directories. public_htmlz.twistd-web-pba twisted.web.distrib.UserDirectory

twisted.web.distrib.UserDirectory

%(users)s NcCs"tj||dur t}||_dSr)r rcrpwd_pwd)r userDatabaserrrr7s  zUserDirectory.__init__c Csg}|jD]F}|\}}}}}}} |dd} | s|} tjtj||jr2||| dftj||j } tj| rM|d} || | dfq|S)z| Return a list of two-tuples giving links to user resources and text to associate with those links. ,rz (file).twistdz (twistd)) rgetpwallsplitospathexistsjoin userDirNamersuserSocketName) rusersuserrpasswduidgidgecosdirshellrealname twistdsocklinkNamerrr_users=s zUserDirectory._usersc Cst}|ddd}|j}|D]*\}}|d}|d|d||}|||d} | ||| q|jd| i} | dS) zm Render as HTML a listing of all known users with links to their personal resources. Nulahref/lirzutf-8) rcreateDocumentdocumentElementr createElement setAttributecreateTextNode appendChildtemplatetoxmlencode) rrVdomImplnewDoclistinglinktext linkElementtextNodeitemhtmlDocrrr render_GETPs       zUserDirectory.render_GETcCs|dkr|Sd}|t| d|kr|dt| }d}n|}d}z|j|\}}}} } } } Wn ty?tYSw|rWtj| |j } t d| }| |||Stj| |j }tj |sitSt|S)NrPrr+rrr)rFrgetpwnamKeyErrorr NoResourcerrrrrbputChildrrr File)rrrVtdusernamesubpw_name pw_passwdpw_uidpw_gidpw_gecospw_dirpw_shellrrsrrrrgetChildes<       zUserDirectory.getChildr) r"r#r$rTrrrrrrrrrrrrs * r)(rTrhrr ImportErroriorxml.dom.minidomrtwisted.internetrrtwisted.loggerrtwisted.persistedrtwisted.spreadrtwisted.spread.bananar twisted.webr r r r rtwisted.web.http_headersr Referenceablerr<r%setUnjellyableForClassrUrcrbRoot Versionedr~rrrrrs0         >N'