
    Ϫf(                         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j4                        Zd
 Z G d dej8                        Zd Zy)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j                  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#y")#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                  j                  |        g | d<   d | d<   g | d<   g | d<   d x| d<   | d<   y )NindexesrootextraHeadersportsporthttps)r
   r   __init__selfs    1/usr/lib/python3/dist-packages/twisted/web/tap.pyr+   zOptions.__init__X   sK    t$YV!^W'++VtG}    c           	          t        j                  | j                  t        j                  dddd            }t        j                  |t        d       || d<   y)	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`   sJ    
 33MM;..y"aC
 	c$61EVr/   c           	          t        j                  | j                  t        j                  dddd            }t        j                  |t        d       || d<   y)	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   sJ    
 33NNK//	2q!D
 	c$61EWr/   c                 ,    | d   j                  |       y)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   j                  |       y)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<   y)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                  j	                  |            | d<   t
        j                  t
        j                  d| d   _        t        j                  | d   j                  d<   y)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]     {{277??4#89V''))#
V +0//V'r/   c                     t        | d   t        j                        st        j                  d      |j                  dd      \  }}t        j                  |      | d   j                  |<   y)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   s[    
 $v,4""#OPPZZQ'
U'.'9'9%'@V$r/   c                 B    t        j                  |      } |       | d<   y)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zVr/   c                 4    t        j                  |      | d<   y)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     33D9Vr/   c                 j   	 t        j                  |      }t        j                         }t        j                  |j                         t        j                  dd|j                         t        j                  t        ||      | d<   y# t        t        f$ r t	        j
                  d|      w xY w)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0K $$&

+%%gz499E(($DV 
+ 	J""%?x#HII	Js   B
 
(B2c                 |    t        | d   t        j                        st        j                  d      || d   _        y)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   s4     $v,4""#OPP#.V r/   c                     t        | d   t        j                        st        j                  d      | d   j                  d       y)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   s@     $v,4""F  	Vs#r/   c                     t        | d   t        j                        st        j                  d      | d   j                  |       y)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   s:     $v,4""#STTVs#r/   c                     |j                  dd      \  }}| d   j                  |j                         |j                         f       y)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   s<    
 ll3*e^##TZZ\5;;=$ABr/   c                 F   | d   | d   j                  | d          | d   F	 t        j                  d       dj                  | d   | d   | d	         }| d   j                  |       t        | d         d
k(  r| d   rmt        j                  j                  t        j                  j                  dt        j                  j                              }| d   j                  d|z          y| d   j                  d       yy# t        $ r t	        j
                  d      w xY w)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   s    <#M  f.=$D##M2 @FFWY]#J
 M  ,tG}"Jww))GGLLg&;&;&J&JK W$$Wt^4W$$Z0 #  D&&'BCCDs   D D 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      s0    H 	R	
	
M"	
	
H( OO %,$9$9$H$HK ..	
	 !u  *u**73.5..w7*u**73
HGH,	 E	#* E/ E:A":E/ E$$C1r/   r   c                 R    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7=>>r/   c                       e Zd Zd Zd Zy)_AddHeadersResourcec                      || _         || _        y N)_originalResource_headers)r-   originalResourceheaderss      r.   r+   z_AddHeadersResource.__init__  s    !1r/   c                     | j                   D ]!  \  }}|j                  j                  ||       # | j                  j	                  ||      S r   )r   responseHeadersaddRawHeaderr   getChildWithDefault)r-   rh   requestr   vs        r.   r   z'_AddHeadersResource.getChildWithDefault  sH    MM 	7DAq##00A6	7%%99$HHr/   N)r   r   r   r+   r   r   r/   r.   r   r     s     Ir/   r   c           	         t        j                         }| d   r| d   }| d   r"| d   | d   _        nt        j                         }t        |t        j                        r*|j                  j                  t        j                  |       | d   rt        || d         }| d   rt        j                  || d         }nt        j                  |      }| d   rd|_        | d   rHt!        j"                  d	t%        j&                  d
ddd            }t)        j*                  |t,        d       | d   rt/        |      }| d   D ])  }t1        j                   ||      }|j3                  |       + |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     sR   Af~f~)(.y(9F6N% yy{$$"":#@#@!Dn"4)?@i{{4	):;{{4 "#!% n44k11)RAF
 	c$61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      s^   

 
   1 0 @ @  T T To1emm o1d?I(++ I$r/   