U
    �Acj(  ã                   @   sÀ   d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZ G dd	„ d	ejƒZd
d„ ZG dd„ dejƒZdd„ ZdS )z9
Support for creating a service which runs a web server.
é    N)ÚserviceÚstrports)Ú
interfacesÚreactor)Ú	deprecateÚreflectÚ
threadpoolÚusage)Úpb)ÚdemoÚdistribÚresourceÚscriptÚserverÚstaticÚtwcgiÚwsgic                   @   s"  e Zd ZdZdZddddgddd	d
gddd	dggZdddgdddggZe dddej	j
f d g¡ eje d¡e d¡e d¡dœd�ZdZdd„ Zdd„ ZeZd d!„ Zd"d#„ Zd$d%„ ZeZd&d'„ ZeZd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ ZeZd4d5„ Z d6d7„ Z!d8d9„ Z"d:d;„ Z#dS )<ÚOptionszB
    Define the options accepted by the I{twistd web} plugin.
    z[web options]ÚlogfileÚlNz/Path to web CLF (Combined Log Format) log file.ÚcertificateÚcz
server.pemz=(DEPRECATED: use --listen) SSL certificate to use for HTTPS. ÚprivkeyÚkz<(DEPRECATED: use --listen) SSL certificate to use for HTTPS.ÚnotracebacksÚnzY(DEPRECATED: Tracebacks are disabled by default. See --enable-tracebacks to turn them on.údisplay-tracebacksÚ z�Show uncaught exceptions during rendering tracebacks to the client. WARNING: This may be a security risk and expose private data!ÚpersonalzvInstead of generating a webserver, generate a ResourcePublisher which listens on  the port given by --listen, or ~/%s zif --listen is not specified.z*.logz*.pem)r   r   r   )Z
optActionsz‡This starts a webserver.  If you specify no arguments, it will be a
demo webserver that has the Test class from twisted.web.demo in it.c                 C   s@   t j | ¡ g | d< d | d< g | d< g | d< d  | d< | d< d S )NÚindexesÚrootÚextraHeadersÚportsÚportÚhttps)r	   r   Ú__init__©Úself© r(   ú3/tmp/pip-unpacked-wheel-nrvc_22y/twisted/web/tap.pyr%   X   s    zOptions.__init__c              	   C   s6   t  | jt dddd¡¡}tj|tdd� || d< dS )	zh
        (DEPRECATED: use --listen)
        Strports description of port to start the server on
        ÚTwistedé   é   r   é   ©ÚcategoryÚ
stacklevelr#   N)r   ÚgetDeprecationWarningStringÚopt_portÚincrementalÚVersionÚwarningsÚwarnÚDeprecationWarning©r'   r#   Úmsgr(   r(   r)   r2   `   s     ÿzOptions.opt_portc              	   C   s6   t  | jt dddd¡¡}tj|tdd� || d< dS )	zW
        (DEPRECATED: use --listen)
        Port to listen on for Secure HTTP.
        r*   r+   r,   r   r-   r.   r$   N)r   r1   Ú	opt_httpsr3   r4   r5   r6   r7   r8   r(   r(   r)   r:   m   s     ÿzOptions.opt_httpsc                 C   s   | d   |¡ dS )zi
        Add an strports description of port to start the server on.
        [default: tcp:8080]
        r"   N©Úappend)r'   r#   r(   r(   r)   Ú
opt_listenx   s    zOptions.opt_listenc                 C   s   | d   |¡ dS )zr
        Add the name of a file used to check for directory indexes.
        [default: index, index.html]
        r   Nr;   )r'   Z	indexNamer(   r(   r)   Ú	opt_index   s    zOptions.opt_indexc                 C   s   t  ¡ | d< dS )zc
        Makes a server with ~/public_html and ~/.twistd-web-pb support for
        users.
        r    N)r   ÚUserDirectoryr&   r(   r(   r)   Úopt_userˆ   s    zOptions.opt_userc                 C   s>   t  tj |¡¡| d< tjtjdœ| d _t	j
| d jd< dS )zö
        <path> is either a specific file or a directory to be set as the root
        of the web server. Use this if you have a directory full of HTML, cgi,
        epy, or rpy files or any other files that you want to be served up raw.
        r    )z.epyz.rpyz.cgiN)r   ÚFileÚosÚpathÚabspathr   ZPythonScriptZResourceScriptÚ
processorsr   Z	CGIScript)r'   rC   r(   r(   r)   Úopt_path‘   s
    þzOptions.opt_pathc                 C   sB   t | d tjƒst d¡‚| dd¡\}}t |¡| d j|< dS )zh
        `ext=class' where `class' is added as a Processor for files ending
        with `ext'.
        r    z*You can only use --processor after --path.ú=é   N)	Ú
isinstancer   rA   r	   Ú
UsageErrorÚsplitr   Ú
namedClassrE   )r'   ÚprocÚextÚklassr(   r(   r)   Úopt_processorž   s    
zOptions.opt_processorc                 C   s   t  |¡}|ƒ | d< dS )zN
        Create a Resource subclass with a zero-argument constructor.
        r    N)r   rL   )r'   Z	classNameZclassObjr(   r(   r)   Ú	opt_class¨   s    
zOptions.opt_classc                 C   s   t  |¡| d< dS )zP
        An .rpy file to be used as the root resource of the webserver.
        r    N)r   ZResourceScriptWrapper)r'   Únamer(   r(   r)   Úopt_resource_script¯   s    zOptions.opt_resource_scriptc              	   C   sr   zt  |¡}W n( ttfk
r6   t d|›�¡‚Y nX t ¡ }t 	|j
¡ t dd|j¡ t t||¡| d< dS )zo
        The FQPN of a WSGI application object to serve as the root resource of
        the webserver.
        zNo such WSGI application: ÚafterÚshutdownr    N)r   ZnamedAnyÚAttributeErrorÚ
ValueErrorr	   rJ   r   Z
ThreadPoolr   ZcallWhenRunningÚstartZaddSystemEventTriggerÚstopr   ZWSGIResource)r'   rR   ZapplicationÚpoolr(   r(   r)   Úopt_wsgiµ   s    zOptions.opt_wsgic                 C   s(   t | d tjƒst d¡‚|| d _dS )zA
        Specify the default mime-type for static files.
        r    z*You can only use --mime_type after --path.N)rI   r   rA   r	   rJ   ÚdefaultType)r'   r\   r(   r(   r)   Úopt_mime_typeÃ   s    
zOptions.opt_mime_typec                 C   s,   t | d tjƒst d¡‚| d  d¡ dS )zT
        Specify whether or not a request for 'foo' should return 'foo.ext'
        r    z1You can only use --allow_ignore_ext after --path.Ú*N©rI   r   rA   r	   rJ   Z	ignoreExtr&   r(   r(   r)   Úopt_allow_ignore_extÍ   s
    ÿzOptions.opt_allow_ignore_extc                 C   s,   t | d tjƒst d¡‚| d  |¡ dS )zT
        Specify an extension to ignore.  These will be processed in order.
        r    z+You can only use --ignore_ext after --path.Nr_   )r'   rN   r(   r(   r)   Úopt_ignore_ext×   s    
zOptions.opt_ignore_extc                 C   s.   |  dd¡\}}| d  | ¡ | ¡ f¡ dS )z
        Specify an additional header to be included in all responses. Specified
        as "HeaderName: HeaderValue".
        ú:rH   r!   N)rK   r<   Ústrip)r'   ÚheaderrR   Úvaluer(   r(   r)   Úopt_add_headerß   s    zOptions.opt_add_headerc                 C   sØ   | d dk	r| d   | d ¡ | d dk	r€zt d¡ W n tk
rV   t d¡‚Y nX d | d | d | d	 ¡}| d   |¡ t| d ƒd
krÔ| d rÆtj	 
tj	 dtjj¡¡}| d   d| ¡ n| d   d¡ dS )a@  
        Set up conditional defaults and check for dependencies.

        If SSL is not available but an HTTPS server was configured, raise a
        L{UsageError} indicating that this is not possible.

        If no server port was supplied, select a default appropriate for the
        other options supplied.
        r#   Nr"   r$   zOpenSSL.SSLzSSL support not installedz$ssl:port={}:privateKey={}:certKey={}r   r   r   r   ú~zunix:ztcp:8080)r<   r   ZnamedModuleÚImportErrorr	   rJ   ÚformatÚlenrB   rC   Ú
expanduserÚjoinr   r?   ÚuserSocketName)r'   Z
sslStrportrC   r(   r(   r)   ÚpostOptionsç   s(    
ýÿzOptions.postOptions)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZsynopsisZoptParametersZoptFlagsr<   r   r?   rm   r	   ZCompletionsZCompleteFilesZcompDataZlongdescr%   r2   Zopt_pr:   r=   r>   Zopt_ir@   Zopt_urF   rP   rQ   rS   r[   r]   Zopt_mr`   ra   rf   rn   r(   r(   r(   r)   r      sx   
üüøü	ü÷þýýÿýÿ

r   c                 C   s   t  t | ¡¡S )zÈ
    Create and return a factory which will respond to I{distrib} requests
    against the given site.

    @type site: L{twisted.web.server.Site}
    @rtype: L{twisted.internet.protocol.Factory}
    )r
   ZPBServerFactoryr   ZResourcePublisher)Úsiter(   r(   r)   ÚmakePersonalServerFactory  s    rt   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_AddHeadersResourcec                 C   s   || _ || _d S ©N)Ú_originalResourceÚ_headers)r'   ZoriginalResourceÚheadersr(   r(   r)   r%     s    z_AddHeadersResource.__init__c                 C   s,   | j D ]\}}|j ||¡ q| j ||¡S rv   )rx   ZresponseHeadersZaddRawHeaderrw   ÚgetChildWithDefault)r'   rR   Úrequestr   Úvr(   r(   r)   rz     s    z'_AddHeadersResource.getChildWithDefaultN)ro   rp   rq   r%   rz   r(   r(   r(   r)   ru     s   ru   c              	   C   s  t  ¡ }| d r0| d }| d r8| d | d _nt ¡ }t|tjƒrT|j 	t
j|¡ | d rjt|| d ƒ}| d r†tj|| d d�}n
t |¡}| d ržd|_| d rÎt d	t d
ddd¡¡}tj|tdd� | d rÞt|ƒ}| d D ]}t  ||¡}| |¡ qæ|S )Nr    r   r!   r   )ZlogPathr   Tr   z--notracebacksr*   é   é   r   r-   r.   r   r"   )r   ZMultiServiceZ
indexNamesr   ZTestrI   r   rA   ÚregistryZsetComponentr   ZIServiceCollectionru   r   ZSiteZdisplayTracebacksr   Z_getDeprecationWarningStringr3   r4   r5   r6   r7   rt   r   ZsetServiceParent)ÚconfigÚsr    rs   r9   r#   Zsvcr(   r(   r)   ÚmakeService  s6    
 ÿr‚   )rr   rB   r5   r3   Ztwisted.applicationr   r   Ztwisted.internetr   r   Ztwisted.pythonr   r   r   r	   Ztwisted.spreadr
   Ztwisted.webr   r   r   r   r   r   r   r   r   rt   ÚResourceru   r‚   r(   r(   r(   r)   Ú<module>   s   ( s