
    \d(                         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	ej                  Zd
 Z G d dej                  Z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                      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
        ddz   g            ej         ej        d           ej        d           ej        d          d          ZdZd Zd ZeZd Zd Zd ZeZd ZeZd Zd Zd Zd Zd Zd ZeZd Z d Z!d  Z"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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    zsInstead of generating a webserver, generate a ResourcePublisher which listens on  the port given by --listen, or ~/ 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                     t           j                            |            g | d<   d | d<   g | d<   g | d<   d x| d<   | d<   d S )NindexesrootextraHeadersportsporthttps)r
   r   __init__selfs    /lib/python3.11/site-packages/twisted/web/tap.pyr+   zOptions.__init__X   sS    t$$$YV!^W'++VtG}}}    c           	          t          j        | j        t          j        dddd                    }t          j        |t          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)   msgs      r.   r9   zOptions.opt_port`   sU    
 3M;.y"aCC
 
 	c$61EEEEVr/   c           	          t          j        | j        t          j        dddd                    }t          j        |t          d           || d<   dS )	zW
        (DEPRECATED: use --listen)
        Port to listen on for Secure HTTP.
        r1   r2   r3   r   r4   r5   r*   N)r   r8   	opt_httpsr:   r;   r<   r=   r>   r?   s      r.   rB   zOptions.opt_httpsm   sU    
 3NK/	2q!DD
 
 	c$61EEEEWr/   c                 <    | d                              |           dS )zi
        Add an strports description of port to start the server on.
        [default: tcp:8080]
        r(   Nappend)r-   r)   s     r.   
opt_listenzOptions.opt_listenx   s"    
 	WT"""""r/   c                 <    | d                              |           dS )zr
        Add the name of a file used to check for directory indexes.
        [default: index, index.html]
        r%   NrD   )r-   	indexNames     r.   	opt_indexzOptions.opt_index   s"    
 	Yy)))))r/   c                 2    t          j                    | d<   dS )zc
        Makes a server with ~/public_html and ~/.twistd-web-pb support for
        users.
        r&   N)r   UserDirectoryr,   s    r.   opt_userzOptions.opt_user   s    
 ,..Vr/   c                     t          j        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-   rP   s     r.   opt_pathzOptions.opt_path   s_     {27??4#8#899V')#
 #
V +0/V'''r/   c                     t          | d         t          j                  st          j        d          |                    dd          \  }}t          j        |          | 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   rN   r
   
UsageErrorsplitr   
namedClassrT   )r-   procextklasss       r.   opt_processorzOptions.opt_processor   sh    
 $v,44 	Q"#OPPPZZQ''
U'.'9%'@'@V$$$r/   c                 H    t          j        |          } |            | d<   dS )zN
        Create a Resource subclass with a zero-argument constructor.
        r&   N)r   r]   )r-   	classNameclassObjs      r.   	opt_classzOptions.opt_class   s'     %i00xzzVr/   c                 4    t          j        |          | d<   dS )zP
        An .rpy file to be used as the root resource of the webserver.
        r&   N)r   ResourceScriptWrapper)r-   names     r.   opt_resource_scriptzOptions.opt_resource_script   s     3D99Vr/   c                 T   	 t          j        |          }n,# t          t          f$ r t	          j        d|          w xY wt          j                    }t          j	        |j
                   t          j        dd|j                   t          j        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
   r[   r	   
ThreadPoolr   callWhenRunningstartaddSystemEventTriggerstopr   WSGIResource)r-   rh   applicationpools       r.   opt_wsgizOptions.opt_wsgi   s    
	J!*400KK
+ 	J 	J 	J"#H#H#HIII	J$&&
+++%gz49EEE($DDVs	    )A c                     t          | d         t          j                  st          j        d          || d         _        dS )zA
        Specify the default mime-type for static files.
        r&   z*You can only use --mime_type after --path.N)rZ   r   rN   r
   r[   defaultType)r-   rz   s     r.   opt_mime_typezOptions.opt_mime_type   sA     $v,44 	Q"#OPPP#.V   r/   c                     t          | d         t          j                  st          j        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rZ   r   rN   r
   r[   	ignoreExtr,   s    r.   opt_allow_ignore_extzOptions.opt_allow_ignore_ext   sT     $v,44 	"F   	Vs#####r/   c                     t          | d         t          j                  st          j        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-   r_   s     r.   opt_ignore_extzOptions.opt_ignore_ext   sM     $v,44 	U"#STTTVs#####r/   c                     |                     dd          \  }}| d                             |                                |                                f           dS )z
        Specify an additional header to be included in all responses. Specified
        as "HeaderName: HeaderValue".
        :rY   r'   N)r\   rE   strip)r-   headerrh   values       r.   opt_add_headerzOptions.opt_add_header   sO    
 ll3**e^##TZZ\\5;;==$ABBBBBr/   c                    | d         !| d                              | d                    | d         |	 t          j        d           n"# t          $ r t	          j        d          w xY wd                    | d         | d         | d	                   }| d                              |           t          | d                   d
k    r| d         rlt          j	        
                    t          j	                            dt          j        j                            }| d                              d|z              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)rE   r   namedModuleImportErrorr
   r[   formatlenrO   rP   
expanduserjoinr   rK   userSocketName)r-   
sslStrportrP   s      r.   postOptionszOptions.postOptions   sb    <#M  f...=$D#M2222 D D D&'BCCCD?FFWY]# J
 M  ,,,tG}""J 1w))GLLg&;&JKK  W$$Wt^44444W$$Z00000 #"s   A A'N)$__name__
__module____qualname____doc__synopsisoptParametersoptFlagsrE   r   rK   r   r
   CompletionsCompleteFilescompDatalongdescr+   r9   opt_prB   rF   rI   opt_irL   opt_urV   ra   re   ri   rx   r{   opt_mr   r   r   r    r/   r.   r   r      s3         H 	RQQ	
 	
 	
	
 	
 	
M"	
 	
 	
	
 	
 	
H( OOB %,$9$H$H$HK ..	
	 	 	 !u *u*733.5.w77*u*733
 
  HGH, , ,	 	 	 E	 	 	# # #* * * E/ / / E: : :A A A" " ": : :E E E/ / / E$ $ $$ $ $C C C1 1 1 1 1r/   r   c                 N    t          j        t          j        |                     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)sites    r.   makePersonalServerFactoryr     s      g7==>>>r/   c                       e Zd Zd Zd ZdS )_AddHeadersResourcec                 "    || _         || _        d S N)_originalResource_headers)r-   originalResourceheaderss      r.   r+   z_AddHeadersResource.__init__  s    !1r/   c                     | j         D ] \  }}|j                            ||           !| j                            ||          S r   )r   responseHeadersaddRawHeaderr   getChildWithDefault)r-   rh   requestr   vs        r.   r   z'_AddHeadersResource.getChildWithDefault  sL    M 	7 	7DAq#00A6666%99$HHHr/   N)r   r   r   r+   r   r   r/   r.   r   r     s7             I I I I Ir/   r   c           	      (   t          j                    }| d         r$| d         }| d         r| d         | d         _        nt          j                    }t          |t          j                  r%|j        	                    t          j        |           | d         rt          || d                   }| d         rt          j        || d                   }nt          j        |          }| d         rd|_        | d         rFt!          j        d	t%          j        d
ddd                    }t)          j        |t,          d           | d         rt/          |          }| d         D ],}t1          j         ||          }|                    |           -|S )Nr&   r%   r'   r   )logPathr   Tr   z--notracebacksr1         r   r4   r5   r!   r(   )r   MultiService
indexNamesr   TestrZ   r   rN   registrysetComponentr   IServiceCollectionr   r   SitedisplayTracebacksr   _getDeprecationWarningStringr:   r;   r<   r=   r>   r   r   setServiceParent)configsr&   r   r@   r)   svcs          r.   makeServicer     s   Af~ f~) 	:(.y(9F6N% y{{$$$ E"":#@!DDDn A"4)?@@i !{4	):;;;{4  "# &!% n F4k1)RAFF
 
 	c$61EEEEj /(..w    tT**QHr/   )r   rO   r<   r:   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.   <module>r      sc  
 
 
			      1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 @ @ @ @ @ @ @ @ @ @ @ @       T T T T T T T T T T T T T T T T T T T To1 o1 o1 o1 o1em o1 o1 o1d? ? ?I I I I I(+ I I I$ $ $ $ $r/   