o
    bW.                     @   s:  d Z ddlZddlZzddlZW n ey   dZY nw 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mZmZmZmZ ddlmZ G dd dejZG dd dejejZe eje G dd dZ!G dd dej"Z#G dd dej$ej%Z&G dd dej"Z'dS )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)addressreactor)Logger)styles)pb)
SIZE_LIMIT)httpresourceserverstaticutil)Headersc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_ReferenceableProducerWrapperc                 C   
   || _ d S N)producer)selfr    r   5/usr/lib/python3/dist-packages/twisted/web/distrib.py__init__$      
z&_ReferenceableProducerWrapper.__init__c                 C      | j   d S r   )r   resumeProducingr   r   r   r   remote_resumeProducing'      z4_ReferenceableProducerWrapper.remote_resumeProducingc                 C   r   r   )r   pauseProducingr   r   r   r   remote_pauseProducing*   r   z3_ReferenceableProducerWrapper.remote_pauseProducingc                 C   r   r   )r   stopProducingr   r   r   r   remote_stopProducing-   r   z2_ReferenceableProducerWrapper.remote_stopProducingN)__name__
__module____qualname__r   r   r   r!   r   r   r   r   r   #   s
    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )Requestzn
    A request which was received by a L{ResourceSubscription} and sent via
    PB to a distributed node.
    c                    s   dD ]$}|| dddd    fddfddd	   }|||< qt t|d
 |d
< tj| | t| j| _| j	d| _
| j	d| _| j	d| _| j	d| _| j	d| _| j	d| _| j	d| _dS )z
        Initialize this L{twisted.web.distrib.Request} based on the copied
        state so that it closely resembles a L{twisted.web.server.Request}.
        )hostclientTCPUNIX)INETr)   r   c                      s   t  d d S )N      )r   IPv4Addressr   addrdesctupr   r   <lambda>@   s    z*Request.setCopyableState.<locals>.<lambda>c                      s   t  d S )Nr+   )r   UNIXAddressr   )r0   r   r   r1   A   s    )r(   r)   requestHeadersfinish	setHeader	addCookiesetETagsetResponseCodesetLastModifiedwriteN)r   dictr   
RemoteCopysetCopyableStater   content_datacontentremoteremoteMethodr4   r5   r6   r7   r8   r9   _write)r   statekaddrr   r.   r   r=   7   s&   

zRequest.setCopyableStatec                 C   s>   d}t }	 | |||  |t 7 }|t 7 }|t|krdS q)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.
        r   TN)r	   rB   len)r   bytesstartendr   r   r   r:   S   s   zRequest.writec                 C   s    | j dt||| j d S )NregisterProducer)r@   
callRemoter   
addErrbackfail)r   r   	streamingr   r   r   rJ   c   s   
zRequest.registerProducerc                 C   s   | j d| j d S )NunregisterProducer)r@   rK   rL   rM   r   r   r   r   rO   h   s   zRequest.unregisterProducerc                 C   s   | j jd|d d S )N )failure)_logrQ   r   rQ   r   r   r   rM   k   s   zRequest.failN)	r"   r#   r$   __doc__r=   r:   rJ   rO   rM   r   r   r   r   r%   1   s    r%   c                   @   s*   e Zd Ze Zdd Zdd Zdd ZdS )Issuec                 C   r   r   )request)r   rV   r   r   r   r   u   r   zIssue.__init__c                 C   s:   |t jurt|tsJ d| j| | j  d S d S )Nzreturn value not a string)r   NOT_DONE_YET
isinstancestrrV   r:   r4   )r   resultr   r   r   finishedx   s
   
zIssue.finishedc              
   C   sL   t |}| jttjddt| 	| j | j
  | j| d S )NzServer Connection Lostz&Connection to distributed server lost:)rY   rV   r:   r   	ErrorPager
   INTERNAL_SERVER_ERRORr   _PRErenderr4   rR   inforS   r   r   r   failed~   s   
zIssue.failedN)r"   r#   r$   r   rR   r   r[   ra   r   r   r   r   rU   r   s
    rU   c                   @   sJ   e Zd ZdZdZe Zdd Zdd Zdd Z	d	d
 Z
dd Zdd ZdS )ResourceSubscriptionr+   r   c                 C   s(   t j|  || _|| _g | _d | _d S r   )r   Resourcer   r&   portpending	publisher)r   r&   rd   r   r   r   r      s
   
zResourceSubscription.__init__c                 C   s(   t  | j}d|d< d|d< g |d< |S )z3Get persistent state for this ResourceSubscription.Nrf   r   waitingre   )copy__dict__)r   rC   r   r   r   __getstate__   s
   z!ResourceSubscription.__getstate__c                 C   sF   | j d |j| j || _d| _| jD ]}| | qg | _dS )z=I've connected to a publisher; I'll now send all my requests.zconnected to publisherr   N)	rR   r`   brokernotifyOnDisconnectbootedrf   rg   re   r_   )r   rf   rV   r   r   r   	connected   s   

zResourceSubscription.connectedc                 C   sD   | j jd|d d| _d| _| jD ]}|d |  qg | _dS )zXI can't connect to a publisher; I'll now reply to all pending
        requests.
        z3could not connect to distributed web service: {msg})msgr   Nz(Unable to connect to distributed server.)rR   r`   rg   rf   re   r:   r4   )r   ro   rV   r   r   r   notConnected   s   



z!ResourceSubscription.notConnectedc                 C   s   |  d d S )Nzconnection dropped)rp   r   r   r   r   rm      r   zResourceSubscription.bootedc                 C   s   | j s<| j| | js9d| _t }d}| jdkr#t| j	|| n
t
| j| j	|| | }|| j| j tjS t|}| j d||j|j tjS )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   )rf   re   appendrg   r   PBClientFactoryr&   r   connectUNIXrd   
connectTCPgetRootObjectaddCallbacksrn   rp   rU   rK   r[   ra   r   rW   )r   rV   bftimeoutdir   r   r   r_      s"   
zResourceSubscription.renderN)r"   r#   r$   isLeafrg   r   rR   r   rj   rn   rp   rm   r_   r   r   r   r   rb      s    
rb   c                   @   s:   e Zd ZdZe Zdd ZdZdd Zdd Z	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}
    c                 C   r   r   )site)r   r   r   r   r   r      r   zResourcePublisher.__init__r,   c                 C   s*   | j jd | j j| j= | `| ` | `d S )Nweb)application
authorizerremoveIdentityservicesserviceNameperspectiveNamer   r   r   r   upgradeToVersion2   s
   z#ResourcePublisher.upgradeToVersion2c                 C   s   | S r   r   )r   namer   r   r   getPerspectiveNamed   s   z%ResourcePublisher.getPerspectiveNamedc                 C   sD   | j |}| j| ||}|tjur|| |  tjS )zK
        Look up the resource for the given request and render it.
        )	r   getResourceForrR   r`   r_   r   rW   r:   r4   )r   rV   resrZ   r   r   r   remote_request   s   


z ResourcePublisher.remote_requestN)r"   r#   r$   rT   r   rR   r   persistenceVersionr   r   r   r   r   r   r   r~      s    r~   c                   @   s>   e Zd ZdZdZdZdZdddZdd	 Zd
d Z	dd Z
dS )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  
<html>
    <head>
    <title>twisted.web.distrib.UserDirectory</title>
    <style>

    a
    {
        font-family: Lucida, Verdana, Helvetica, Arial, sans-serif;
        color: #369;
        text-decoration: none;
    }

    th
    {
        font-family: Lucida, Verdana, Helvetica, Arial, sans-serif;
        font-weight: bold;
        text-decoration: none;
        text-align: left;
    }

    pre, code
    {
        font-family: "Courier New", Courier, monospace;
    }

    p, body, td, ol, ul, menu, blockquote, div
    {
        font-family: Lucida, Verdana, Helvetica, Arial, sans-serif;
        color: #000;
    }
    </style>
    </head>

    <body>
    <h1>twisted.web.distrib.UserDirectory</h1>

    %(users)s
</body>
</html>
Nc                 C   s"   t j|  |d u rt}|| _d S r   )r   rc   r   pwd_pwd)r   userDatabaser   r   r   r   7  s   
zUserDirectory.__init__c                 C   s   g }| j  D ]F}|\}}}}}}}	|dd }
|
s|}
tjtj|| jr2|||
d f tj|| j	}tj|rM|d }|||
d f q|S )z|
        Return a list of two-tuples giving links to user resources and text to
        associate with those links.
        ,r   z (file).twistdz	 (twistd))
r   getpwallsplitospathexistsjoinuserDirNamers   userSocketName)r   usersuserr   passwduidgidgecosdirshellrealname
twistdsocklinkNamer   r   r   _users=  s   zUserDirectory._usersc                 C   s   t  }|ddd}|j}|  D ]*\}}|d}|d|d  ||}|| |d}	|	| ||	 q| jd|	 i }
|

dS )	zm
        Render as HTML a listing of all known users with links to their
        personal resources.
        Nulahref/lir   zutf-8)r   createDocumentdocumentElementr   createElementsetAttributecreateTextNodeappendChildtemplatetoxmlencode)r   rV   domImplnewDoclistinglinktextlinkElementtextNodeitemhtmlDocr   r   r   
render_GETP  s   





zUserDirectory.render_GETc                 C   s   |dkr| S d}|t | d  |kr|d t |  }d}n|}d}z| j|\}}}}	}
}}W n ty?   t  Y S w |rWtj|| j	}t
d|}| || |S tj|| j}tj|sit S t|S )NrP   r   r+   r   rr   )rF   r   getpwnamKeyErrorr   
NoResourcer   r   r   r   rb   putChildr   r   r   File)r   r   rV   tdusernamesubpw_name	pw_passwdpw_uidpw_gidpw_gecospw_dirpw_shellr   rsr   r   r   r   getChilde  s<   
	

zUserDirectory.getChildr   )r"   r#   r$   rT   r   r   r   r   r   r   r   r   r   r   r   r     s    
*r   )(rT   rh   r   r   ImportErrorior   xml.dom.minidomr   twisted.internetr   r   twisted.loggerr   twisted.persistedr   twisted.spreadr   twisted.spread.bananar	   twisted.webr
   r   r   r   r   twisted.web.http_headersr   Referenceabler   r<   r%   setUnjellyableForClassrU   rc   rb   Root	Versionedr~   r   r   r   r   r   <module>   s0   >N'