o b(@sdZddlZddlZddlZddlmZmZddlmZm Z ddl m Z m Z m Z mZddlmZddlmZmZmZmZmZmZmZmZGdd d ejZd d ZGd d d ejZddZdS)z9 Support for creating a service which runs a web server. N)servicestrports) interfacesreactor) deprecatereflect threadpoolusage)pb)demodistribresourcescriptserverstatictwcgiwsgic@seZdZdZdZgdgdgdgZgdgdgZedd d ej j fd ge j e d e d e d ddZdZddZddZeZddZddZddZeZddZeZddZdd Zd!d"Zd#d$Zd%d&Zd'd(ZeZd)d*Z d+d,Z!d-d.Z"d/d0Z#d1S)2OptionszB Define the options accepted by the I{twistd web} plugin. z [web options])logfilelNz/Path to web CLF (Combined Log Format) log file.) certificatec server.pemz=(DEPRECATED: use --listen) SSL certificate to use for HTTPS. )privkeykrz<(DEPRECATED: use --listen) SSL certificate to use for HTTPS.) notracebacksnzY(DEPRECATED: Tracebacks are disabled by default. See --enable-tracebacks to turn them on.)display-tracebackszShow uncaught exceptions during rendering tracebacks to the client. WARNING: This may be a security risk and expose private data!personalrzvInstead of generating a webserver, generate a ResourcePublisher which listens on the port given by --listen, or ~/%s zif --listen is not specified.z*.logz*.pem)rrr) 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.cCs@tj|g|d<d|d<g|d<g|d<d|d<|d<dS)Nindexesroot extraHeadersportsporthttps)r r__init__selfr*1/usr/lib/python3/dist-packages/twisted/web/tap.pyr'Xs zOptions.__init__c C6t|jtdddd}tj|tdd||d<dS) zh (DEPRECATED: use --listen) Strports description of port to start the server on Twistedrcategory stacklevelr%N)rgetDeprecationWarningStringopt_port incrementalVersionwarningswarnDeprecationWarningr)r%msgr*r*r+r5`  zOptions.opt_portc Cr,) zW (DEPRECATED: use --listen) Port to listen on for Secure HTTP. r-r.r/rr0r1r&N)rr4 opt_httpsr6r7r8r9r:r;r*r*r+r>mr=zOptions.opt_httpscC|d|dS)zi Add an strports description of port to start the server on. [default: tcp:8080] r$Nappend)r)r%r*r*r+ opt_listenxzOptions.opt_listencCr?)zr Add the name of a file used to check for directory indexes. [default: index, index.html] r!Nr@)r) indexNamer*r*r+ opt_indexrCzOptions.opt_indexcCst|d<dS)zc Makes a server with ~/public_html and ~/.twistd-web-pb support for users. r"N)r UserDirectoryr(r*r*r+opt_userszOptions.opt_usercCs>ttj||d<tjtjd|d_t j |djd<dS)z 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) rFileospathabspathr PythonScriptResourceScript processorsr CGIScript)r)rJr*r*r+opt_paths  zOptions.opt_pathcCsBt|dtjs td|dd\}}t||dj|<dS)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) isinstancerrHr UsageErrorsplitr namedClassrN)r)procextklassr*r*r+ opt_processors zOptions.opt_processorcCst|}||d<dS)zN Create a Resource subclass with a zero-argument constructor. r"N)rrV)r) classNameclassObjr*r*r+ opt_classs zOptions.opt_classcCst||d<dS)zP An .rpy file to be used as the root resource of the webserver. r"N)rResourceScriptWrapper)r)namer*r*r+opt_resource_scriptszOptions.opt_resource_scriptc Cslzt|}Wnttfytd|wt}t |j t dd|j t t|||d<dS)zo The FQPN of a WSGI application object to serve as the root resource of the webserver. zNo such WSGI application: aftershutdownr"N)rnamedAnyAttributeError ValueErrorr rTr ThreadPoolrcallWhenRunningstartaddSystemEventTriggerstopr WSGIResource)r)r_ applicationpoolr*r*r+opt_wsgis zOptions.opt_wsgicCs(t|dtjs td||d_dS)zA Specify the default mime-type for static files. r"z*You can only use --mime_type after --path.N)rSrrHr rT defaultType)r)ror*r*r+ opt_mime_types zOptions.opt_mime_typecCs,t|dtjs td|dddS)zT Specify whether or not a request for 'foo' should return 'foo.ext' r"z1You can only use --allow_ignore_ext after --path.*NrSrrHr rT ignoreExtr(r*r*r+opt_allow_ignore_exts zOptions.opt_allow_ignore_extcCs,t|dtjs td|d|dS)zT Specify an extension to ignore. These will be processed in order. r"z+You can only use --ignore_ext after --path.Nrr)r)rXr*r*r+opt_ignore_exts zOptions.opt_ignore_extcCs.|dd\}}|d||fdS)z Specify an additional header to be included in all responses. Specified as "HeaderName: HeaderValue". :rRr#N)rUrAstrip)r)headerr_valuer*r*r+opt_add_headerszOptions.opt_add_headercCs|ddur|d|d|ddur=ztdWn ty(tdwd|d|d|d }|d|t|dd krj|d ratj tj d t j j}|dd |dS|dddSdS)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.SSLzSSL support not installedz$ssl:port={}:privateKey={}:certKey={}rrrr~zunix:ztcp:8080)rAr namedModule ImportErrorr rTformatlenrIrJ expanduserjoinr rFuserSocketName)r) sslStrportrJr*r*r+ postOptionss,    zOptions.postOptionsN)$__name__ __module__ __qualname____doc__synopsis optParametersoptFlagsrAr rFrr Completions CompleteFilescompDatalongdescr'r5opt_pr>rBrEopt_irGopt_urPrZr]r`rnrpopt_mrtrurzrr*r*r*r+rs^       rcCstt|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+makePersonalServerFactorysrc@seZdZddZddZdS)_AddHeadersResourcecCs||_||_dSN)_originalResource_headers)r)originalResourceheadersr*r*r+r's z_AddHeadersResource.__init__cCs,|jD] \}}|j||q|j||Sr)rresponseHeaders addRawHeaderrgetChildWithDefault)r)r_requestrvr*r*r+rsz'_AddHeadersResource.getChildWithDefaultN)rrrr'rr*r*r*r+rs rc Cst}|dr|d}|dr|d|d_nt}t|tjr*|j t j ||dr5t ||d}|drCt j||dd}nt |}|drOd|_|drgtd td d d d }tj|tdd|drot|}|dD] }t||}||qs|S)Nr"r!r#r)logPathrTrz--notracebacksr-rr0r1rr$)r MultiService indexNamesr TestrSrrHregistry setComponentrIServiceCollectionrrSitedisplayTracebacksr_getDeprecationWarningStringr6r7r8r9r:rrsetServiceParent)configsr"rr<r%svcr*r*r+ makeServices6     r)rrIr8r6twisted.applicationrrtwisted.internetrrtwisted.pythonrrrr twisted.spreadr twisted.webr r r rrrrrrrResourcerrr*r*r*r+s (s