o
    b(                     @   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g dg dg dgZg dg d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d%d& Zd'd( ZeZd)d* Z d+d, Z!d-d. Z"d/d0 Z#d1S )2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c
server.pemz=(DEPRECATED: use --listen) SSL certificate to use for HTTPS. )privkeykr   z<(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 zShow uncaught exceptions during rendering tracebacks to the client. WARNING: This may be a security risk and expose private data!personalr   zvInstead 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   )
optActionszThis 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*   1/usr/lib/python3/dist-packages/twisted/web/tap.pyr'   X   s   zOptions.__init__c              	   C   6   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+   r5   `   
   zOptions.opt_portc              	   C   r,   )	zW
        (DEPRECATED: use --listen)
        Port to listen on for Secure HTTP.
        r-   r.   r/   r   r0   r1   r&   N)r   r4   	opt_httpsr6   r7   r8   r9   r:   r;   r*   r*   r+   r>   m   r=   zOptions.opt_httpsc                 C      | 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      zOptions.opt_listenc                 C   r?   )zr
        Add the name of a file used to check for directory indexes.
        [default: index, index.html]
        r!   Nr@   )r)   	indexNamer*   r*   r+   	opt_index   rC   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   PythonScriptResourceScript
processorsr   	CGIScript)r)   rJ   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   rH   r	   
UsageErrorsplitr   
namedClassrN   )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   rV   )r)   	className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   ResourceScriptWrapper)r)   namer*   r*   r+   opt_resource_script   s   zOptions.opt_resource_scriptc              	   C   sl   zt |}W n ttfy   td|w 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   namedAnyAttributeError
ValueErrorr	   rT   r   
ThreadPoolr   callWhenRunningstartaddSystemEventTriggerstopr   WSGIResource)r)   r_   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)rS   r   rH   r	   rT   defaultType)r)   ro   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rS   r   rH   r	   rT   	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.Nrr   )r)   rX   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".
        :rR   r#   N)rU   rA   strip)r)   headerr_   valuer*   r*   r+   opt_add_header   s   zOptions.opt_add_headerc                 C   s   | d dur| d  | d  | d dur=ztd W n ty(   tdw d| d | d | d	 }| d  | t| d d
krj| d ratj	
tj	dtjj}| d  d|  dS | d  d dS 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)rA   r   namedModuleImportErrorr	   rT   formatlenrI   rJ   
expanduserjoinr   rF   userSocketName)r)   
sslStrportrJ   r*   r*   r+   postOptions   s,   

zOptions.postOptionsN)$__name__
__module____qualname____doc__synopsisoptParametersoptFlagsrA   r   rF   r   r	   CompletionsCompleteFilescompDatalongdescr'   r5   opt_pr>   rB   rE   opt_irG   opt_urP   rZ   r]   r`   rn   rp   opt_mrt   ru   rz   r   r*   r*   r*   r+   r      s^    

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
   PBServerFactoryr   ResourcePublisher)siter*   r*   r+   makePersonalServerFactory  s   r   c                   @   s   e Zd Zdd Zdd ZdS )_AddHeadersResourcec                 C   s   || _ || _d S N)_originalResource_headers)r)   originalResourceheadersr*   r*   r+   r'     s   
z_AddHeadersResource.__init__c                 C   s,   | j D ]\}}|j|| q| j||S r   )r   responseHeadersaddRawHeaderr   getChildWithDefault)r)   r_   requestr   vr*   r*   r+   r     s   z'_AddHeadersResource.getChildWithDefaultN)r   r   r   r'   r   r*   r*   r*   r+   r     s    r   c              	   C   s  t  }| d r| d }| d r| d | d _nt }t|tjr*|j	t
j| | d r5t|| d }| d rCtj|| d d}nt|}| d rOd|_| d rgtd	td
ddd}tj|tdd | d rot|}| d D ]}t ||}|| qs|S )Nr"   r!   r#   r   )logPathr   Tr   z--notracebacksr-         r   r0   r1   r   r$   )r   MultiService
indexNamesr   TestrS   r   rH   registrysetComponentr   IServiceCollectionr   r   SitedisplayTracebacksr   _getDeprecationWarningStringr6   r7   r8   r9   r:   r   r   setServiceParent)configsr"   r   r<   r%   svcr*   r*   r+   makeService  s6   
r   )r   rI   r8   r6   twisted.applicationr   r   twisted.internetr   r   twisted.pythonr   r   r   r	   twisted.spreadr
   twisted.webr   r   r   r   r   r   r   r   r   r   Resourcer   r   r*   r*   r*   r+   <module>   s   ( s