U
    
W[e.  ã                   @   s<  d Z ddlZddlZzddlZW n ek
r8   dZY nX 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 dd	lmZ dd
l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)Úpb)Ú
SIZE_LIMIT)ÚhttpÚresourceÚserverÚutilÚstatic)ÚHeaders)Ústyles)ÚaddressÚreactor)ÚLoggerc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú_ReferenceableProducerWrapperc                 C   s
   || _ d S ©N)Úproducer)Úselfr   © r   ú5/usr/lib/python3/dist-packages/twisted/web/distrib.pyÚ__init__!   s    z&_ReferenceableProducerWrapper.__init__c                 C   s   | j  ¡  d S r   )r   ZresumeProducing©r   r   r   r   Úremote_resumeProducing$   s    z4_ReferenceableProducerWrapper.remote_resumeProducingc                 C   s   | j  ¡  d S r   )r   ZpauseProducingr   r   r   r   Úremote_pauseProducing'   s    z3_ReferenceableProducerWrapper.remote_pauseProducingc                 C   s   | j  ¡  d S r   )r   ZstopProducingr   r   r   r   Úremote_stopProducing*   s    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 ]H}|| ‰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}.
        )ÚhostZclientÚTCPÚUNIX)ZINETr!   r   c                      s   t  ˆ ˆd ˆd ¡S )Né   é   )r   ZIPv4Addressr   ©ZaddrdescÚtupr   r   Ú<lambda>;   s    ÿz*Request.setCopyableState.<locals>.<lambda>c                      s   t  ˆ d ¡S )Nr"   )r   ZUNIXAddressr   )r%   r   r   r&   =   ó    )r    r!   ZrequestHeadersÚfinishÚ	setHeaderÚ	addCookieÚsetETagÚsetResponseCodeÚsetLastModifiedÚwriteN)r   Údictr   Ú
RemoteCopyÚsetCopyableStater   Zcontent_dataZcontentÚremoteZremoteMethodr(   r)   r*   r+   r,   r-   Ú_write)r   ÚstateÚkZaddrr   r$   r   r1   3   s&    
þþ
zRequest.setCopyableStatec                 C   s>   d}t }|  |||… ¡ |t 7 }|t 7 }|t|ƒkrq:qdS )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   N)r   r3   Úlen)r   ÚbytesÚstartÚendr   r   r   r.   O   s    zRequest.writec                 C   s    | j  dt|ƒ|¡ | j¡ d S )NÚregisterProducer)r2   Ú
callRemoter   Ú
addErrbackÚfail)r   r   Z	streamingr   r   r   r:   `   s    þþzRequest.registerProducerc                 C   s   | j  d¡ | j¡ d S )NÚunregisterProducer)r2   r;   r<   r=   r   r   r   r   r>   e   s    zRequest.unregisterProducerc                 C   s   | j jd|d d S )NÚ )Úfailure)Ú_logr@   ©r   r@   r   r   r   r=   h   s    zRequest.failN)	r   r   r   Ú__doc__r1   r.   r:   r>   r=   r   r   r   r   r   .   s   r   c                   @   s*   e Zd Zeƒ Zdd„ Zdd„ Zdd„ ZdS )ÚIssuec                 C   s
   || _ d S r   )Úrequest)r   rE   r   r   r   r   q   s    zIssue.__init__c                 C   s6   |t jkr2t|tƒstdƒ‚| j |¡ | j ¡  d S )Nzreturn value not a string)r   ÚNOT_DONE_YETÚ
isinstanceÚstrÚAssertionErrorrE   r.   r(   )r   Úresultr   r   r   Úfinishedt   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:)rH   rE   r.   r   Z	ErrorPager   ZINTERNAL_SERVER_ERRORr	   Z_PREÚrenderr(   rA   ÚinforB   r   r   r   Úfailedz   s    ÿþüÿ
zIssue.failedN)r   r   r   r   rA   r   rK   rN   r   r   r   r   rD   n   s   rD   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   rQ   r   r   r   r   Œ   s
    zResourceSubscription.__init__c                 C   s(   t   | j¡}d|d< d|d< g |d< |S )z<Get persistent state for this ResourceSubscription.
        NrS   r   ÚwaitingrR   )ÚcopyÚ__dict__)r   r4   r   r   r   Ú__getstate__“   s
    z!ResourceSubscription.__getstate__c                 C   sF   | j  d¡ |j | j¡ || _d| _| jD ]}|  |¡ q,g | _dS )zFI've connected to a publisher; I'll now send all my requests.
        zconnected to publisherr   N)	rA   rM   ZbrokerZnotifyOnDisconnectÚbootedrS   rT   rR   rL   )r   rS   rE   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.)rA   rM   rT   rS   rR   r.   r(   )r   rZ   rE   r   r   r   ÚnotConnected«   s    þ


z!ResourceSubscription.notConnectedc                 C   s   |   d¡ d S )Nzconnection dropped)r[   r   r   r   r   rX   º   s    zResourceSubscription.bootedc                 C   sœ   | j st| j |¡ | js–d| _t ¡ }d}| jdkrFt | j	||¡ nt 
| j| j	||¡ | ¡ }| | j| j¡ n"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"   é
   ÚunixrE   )rS   rR   ÚappendrT   r   ZPBClientFactoryr   r   ZconnectUNIXrQ   Z
connectTCPZgetRootObjectZaddCallbacksrY   r[   rD   r;   rK   rN   r   rF   )r   rE   ZbfZtimeoutÚdÚir   r   r   rL   ½   s    
zResourceSubscription.renderN)r   r   r   ZisLeafrT   r   rA   r   rW   rY   r[   rX   rL   r   r   r   r   rO   ‡   s   rO   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   s
   || _ d S r   )Úsite)r   rb   r   r   r   r   ã   s    zResourcePublisher.__init__r#   c                 C   s*   | j j d¡ | j j| j= | `| ` | `d S )NZweb)ZapplicationZ
authorizerZremoveIdentityÚservicesZserviceNameZ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k	r>| |¡ | ¡  tjS )zK
        Look up the resource for the given request and render it.
        )	rb   ZgetResourceForrA   rM   rL   r   rF   r.   r(   )r   rE   ÚresrJ   r   r   r   Úremote_requestó   s    


z ResourcePublisher.remote_requestN)r   r   r   rC   r   rA   r   ZpersistenceVersionrd   rf   rh   r   r   r   r   ra   Ù   s   ra   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.
    Z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 krt}|| _d S r   )r   rP   r   ÚpwdÚ_pwd)r   ZuserDatabaser   r   r   r   7  s    zUserDirectory.__init__c                 C   s    g }| j  ¡ D ]Œ}|\}}}}}}}	| d¡d }
|
s:|}
tj tj || j¡¡rd| ||
d f¡ tj || j	¡}tj |¡r|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))
rk   ÚgetpwallÚsplitÚosÚpathÚexistsÚjoinÚuserDirNamer^   ÚuserSocketName)r   ÚusersÚuserre   ZpasswdZuidÚgidZgecosÚdirÚshellZrealnameÚ
twistdsockZlinkNamer   r   r   Ú_users>  s    zUserDirectory._usersc                 C   s”   t ƒ }| ddd¡}|j}|  ¡ D ]T\}}| 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.
        NZulÚaZhrefú/Zlirv   zutf-8)r   ZcreateDocumentZdocumentElementr|   ZcreateElementZsetAttributeZcreateTextNodeZappendChildÚtemplateZtoxmlÚencode)r   rE   ZdomImplZnewDocZlistingÚlinkÚtextZlinkElementZtextNodeÚitemZhtmlDocr   r   r   Ú
render_GETR  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k
r€   t ¡  Y S X |r°tj || j	¡}t
d|ƒ}|  ||¡ |S tj || j¡}tj |¡sÔt ¡ S t |¡S d S )Nr?   rm   r"   r   r]   )r6   rk   ÚgetpwnamÚKeyErrorr   Z
NoResourcerp   rq   rs   ru   rO   ZputChildrt   rr   r
   ZFile)r   re   rE   ZtdZusernameÚsubÚpw_nameÚ	pw_passwdÚpw_uidÚpw_gidÚpw_gecosÚpw_dirÚpw_shellr{   Zrsrq   r   r   r   ÚgetChildh  s,    
ÿ
zUserDirectory.getChild)N)r   r   r   rC   rt   ru   r   r   r|   r„   r   r   r   r   r   ri     s   *
ri   )(rC   rp   rU   rj   ÚImportErrorÚior   Zxml.dom.minidomr   Ztwisted.spreadr   Ztwisted.spread.bananar   Ztwisted.webr   r   r   r	   r
   Ztwisted.web.http_headersr   Ztwisted.persistedr   Ztwisted.internetr   r   Ztwisted.loggerr   ZReferenceabler   r0   r   ZsetUnjellyableForClassrD   rP   rO   ZRootZ	Versionedra   ri   r   r   r   r   Ú<module>   s*   
>R(