o
    Óó·i(  ã                   @   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   )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)   úUC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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*   r4   `   ó
   ÿzOptions.opt_portc              	   C   r+   )	zW
        (DEPRECATED: use --listen)
        Port to listen on for Secure HTTP.
        r,   r-   r.   r   r/   r0   r%   N)r   r3   Ú	opt_httpsr5   r6   r7   r8   r9   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(   Z	indexNamer)   r)   r*   Ú	opt_index   rB   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(   rH   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   rF   r	   Ú
UsageErrorÚsplitr   Ú
namedClassrJ   )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   rQ   )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   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   ZnamedAnyÚAttributeErrorÚ
ValueErrorr	   rO   r   Z
ThreadPoolr   ZcallWhenRunningÚstartZaddSystemEventTriggerÚstopr   ZWSGIResource)r(   rW   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)rN   r   rF   r	   rO   ÚdefaultType)r(   ra   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©rN   r   rF   r	   rO   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.Nrd   )r(   rS   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".
        ú:rM   r"   N)rP   r@   Ústrip)r(   ÚheaderrW   Ú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)r@   r   ZnamedModuleÚImportErrorr	   rO   ÚformatÚlenrG   rH   Ú
expanduserÚjoinr   rD   ÚuserSocketName)r(   Z
sslStrportrH   r)   r)   r*   ÚpostOptionsç   s,   

ÿýÿùzOptions.postOptionsN)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZsynopsisZoptParametersZoptFlagsr@   r   rD   rr   r	   ZCompletionsZCompleteFilesZcompDataZlongdescr&   r4   Zopt_pr=   rA   rC   Zopt_irE   Zopt_urK   rU   rV   rX   r`   rb   Zopt_mre   rf   rk   rs   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}
    N)r
   ZPBServerFactoryr   ZResourcePublisher)Úsiter)   r)   r*   ÚmakePersonalServerFactory  s   ry   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 r{   )r}   ZresponseHeadersZaddRawHeaderr|   ÚgetChildWithDefault)r(   rW   Úrequestr   Úvr)   r)   r*   r     s   z'_AddHeadersResource.getChildWithDefaultN)rt   ru   rv   r&   r   r)   r)   r)   r*   rz     s    rz   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   )ZlogPathr   Tr   z--notracebacksr,   é   é   r   r/   r0   r   r#   )r   ZMultiServiceZ
indexNamesr   ZTestrN   r   rF   ÚregistryZsetComponentr   ZIServiceCollectionrz   r   ZSiteZdisplayTracebacksr   Z_getDeprecationWarningStringr5   r6   r7   r8   r9   ry   r   ZsetServiceParent)ÚconfigÚsr!   rx   r;   r$   Zsvcr)   r)   r*   ÚmakeService  s6   €
ÿr‡   )rw   rG   r7   r5   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   ry   ÚResourcerz   r‡   r)   r)   r)   r*   Ú<module>   s   ( s