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Ú z�Show 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   )Ú
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*   ú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