You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tap.py 10KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. # -*- test-case-name: twisted.web.test.test_tap -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Support for creating a service which runs a web server.
  6. """
  7. from __future__ import absolute_import, division
  8. import os
  9. import warnings
  10. import incremental
  11. from twisted.application import service, strports
  12. from twisted.internet import interfaces, reactor
  13. from twisted.python import usage, reflect, threadpool, deprecate
  14. from twisted.spread import pb
  15. from twisted.web import distrib
  16. from twisted.web import resource, server, static, script, demo, wsgi
  17. from twisted.web import twcgi
  18. class Options(usage.Options):
  19. """
  20. Define the options accepted by the I{twistd web} plugin.
  21. """
  22. synopsis = "[web options]"
  23. optParameters = [["logfile", "l", None,
  24. "Path to web CLF (Combined Log Format) log file."],
  25. ["certificate", "c", "server.pem",
  26. "(DEPRECATED: use --listen) "
  27. "SSL certificate to use for HTTPS. "],
  28. ["privkey", "k", "server.pem",
  29. "(DEPRECATED: use --listen) "
  30. "SSL certificate to use for HTTPS."],
  31. ]
  32. optFlags = [
  33. ["notracebacks", "n", (
  34. "(DEPRECATED: Tracebacks are disabled by default. "
  35. "See --enable-tracebacks to turn them on.")],
  36. ["display-tracebacks", "", (
  37. "Show uncaught exceptions during rendering tracebacks to "
  38. "the client. WARNING: This may be a security risk and "
  39. "expose private data!")],
  40. ]
  41. optFlags.append([
  42. "personal", "",
  43. "Instead of generating a webserver, generate a "
  44. "ResourcePublisher which listens on the port given by "
  45. "--listen, or ~/%s " % (distrib.UserDirectory.userSocketName,) +
  46. "if --listen is not specified."])
  47. compData = usage.Completions(
  48. optActions={"logfile" : usage.CompleteFiles("*.log"),
  49. "certificate" : usage.CompleteFiles("*.pem"),
  50. "privkey" : usage.CompleteFiles("*.pem")}
  51. )
  52. longdesc = """\
  53. This starts a webserver. If you specify no arguments, it will be a
  54. demo webserver that has the Test class from twisted.web.demo in it."""
  55. def __init__(self):
  56. usage.Options.__init__(self)
  57. self['indexes'] = []
  58. self['root'] = None
  59. self['extraHeaders'] = []
  60. self['ports'] = []
  61. self['port'] = self['https'] = None
  62. def opt_port(self, port):
  63. """
  64. (DEPRECATED: use --listen)
  65. Strports description of port to start the server on
  66. """
  67. msg = deprecate.getDeprecationWarningString(
  68. self.opt_port, incremental.Version('Twisted', 18, 4, 0))
  69. warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
  70. self['port'] = port
  71. opt_p = opt_port
  72. def opt_https(self, port):
  73. """
  74. (DEPRECATED: use --listen)
  75. Port to listen on for Secure HTTP.
  76. """
  77. msg = deprecate.getDeprecationWarningString(
  78. self.opt_https, incremental.Version('Twisted', 18, 4, 0))
  79. warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
  80. self['https'] = port
  81. def opt_listen(self, port):
  82. """
  83. Add an strports description of port to start the server on.
  84. [default: tcp:8080]
  85. """
  86. self['ports'].append(port)
  87. def opt_index(self, indexName):
  88. """
  89. Add the name of a file used to check for directory indexes.
  90. [default: index, index.html]
  91. """
  92. self['indexes'].append(indexName)
  93. opt_i = opt_index
  94. def opt_user(self):
  95. """
  96. Makes a server with ~/public_html and ~/.twistd-web-pb support for
  97. users.
  98. """
  99. self['root'] = distrib.UserDirectory()
  100. opt_u = opt_user
  101. def opt_path(self, path):
  102. """
  103. <path> is either a specific file or a directory to be set as the root
  104. of the web server. Use this if you have a directory full of HTML, cgi,
  105. epy, or rpy files or any other files that you want to be served up raw.
  106. """
  107. self['root'] = static.File(os.path.abspath(path))
  108. self['root'].processors = {
  109. '.epy': script.PythonScript,
  110. '.rpy': script.ResourceScript,
  111. }
  112. self['root'].processors['.cgi'] = twcgi.CGIScript
  113. def opt_processor(self, proc):
  114. """
  115. `ext=class' where `class' is added as a Processor for files ending
  116. with `ext'.
  117. """
  118. if not isinstance(self['root'], static.File):
  119. raise usage.UsageError(
  120. "You can only use --processor after --path.")
  121. ext, klass = proc.split('=', 1)
  122. self['root'].processors[ext] = reflect.namedClass(klass)
  123. def opt_class(self, className):
  124. """
  125. Create a Resource subclass with a zero-argument constructor.
  126. """
  127. classObj = reflect.namedClass(className)
  128. self['root'] = classObj()
  129. def opt_resource_script(self, name):
  130. """
  131. An .rpy file to be used as the root resource of the webserver.
  132. """
  133. self['root'] = script.ResourceScriptWrapper(name)
  134. def opt_wsgi(self, name):
  135. """
  136. The FQPN of a WSGI application object to serve as the root resource of
  137. the webserver.
  138. """
  139. try:
  140. application = reflect.namedAny(name)
  141. except (AttributeError, ValueError):
  142. raise usage.UsageError("No such WSGI application: %r" % (name,))
  143. pool = threadpool.ThreadPool()
  144. reactor.callWhenRunning(pool.start)
  145. reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
  146. self['root'] = wsgi.WSGIResource(reactor, pool, application)
  147. def opt_mime_type(self, defaultType):
  148. """
  149. Specify the default mime-type for static files.
  150. """
  151. if not isinstance(self['root'], static.File):
  152. raise usage.UsageError(
  153. "You can only use --mime_type after --path.")
  154. self['root'].defaultType = defaultType
  155. opt_m = opt_mime_type
  156. def opt_allow_ignore_ext(self):
  157. """
  158. Specify whether or not a request for 'foo' should return 'foo.ext'
  159. """
  160. if not isinstance(self['root'], static.File):
  161. raise usage.UsageError("You can only use --allow_ignore_ext "
  162. "after --path.")
  163. self['root'].ignoreExt('*')
  164. def opt_ignore_ext(self, ext):
  165. """
  166. Specify an extension to ignore. These will be processed in order.
  167. """
  168. if not isinstance(self['root'], static.File):
  169. raise usage.UsageError("You can only use --ignore_ext "
  170. "after --path.")
  171. self['root'].ignoreExt(ext)
  172. def opt_add_header(self, header):
  173. """
  174. Specify an additional header to be included in all responses. Specified
  175. as "HeaderName: HeaderValue".
  176. """
  177. name, value = header.split(':', 1)
  178. self['extraHeaders'].append((name.strip(), value.strip()))
  179. def postOptions(self):
  180. """
  181. Set up conditional defaults and check for dependencies.
  182. If SSL is not available but an HTTPS server was configured, raise a
  183. L{UsageError} indicating that this is not possible.
  184. If no server port was supplied, select a default appropriate for the
  185. other options supplied.
  186. """
  187. if self['port'] is not None:
  188. self['ports'].append(self['port'])
  189. if self['https'] is not None:
  190. try:
  191. reflect.namedModule('OpenSSL.SSL')
  192. except ImportError:
  193. raise usage.UsageError("SSL support not installed")
  194. sslStrport = 'ssl:port={}:privateKey={}:certKey={}'.format(
  195. self['https'],
  196. self['privkey'],
  197. self['certificate'],
  198. )
  199. self['ports'].append(sslStrport)
  200. if len(self['ports']) == 0:
  201. if self['personal']:
  202. path = os.path.expanduser(
  203. os.path.join('~', distrib.UserDirectory.userSocketName))
  204. self['ports'].append('unix:' + path)
  205. else:
  206. self['ports'].append('tcp:8080')
  207. def makePersonalServerFactory(site):
  208. """
  209. Create and return a factory which will respond to I{distrib} requests
  210. against the given site.
  211. @type site: L{twisted.web.server.Site}
  212. @rtype: L{twisted.internet.protocol.Factory}
  213. """
  214. return pb.PBServerFactory(distrib.ResourcePublisher(site))
  215. class _AddHeadersResource(resource.Resource):
  216. def __init__(self, originalResource, headers):
  217. self._originalResource = originalResource
  218. self._headers = headers
  219. def getChildWithDefault(self, name, request):
  220. for k, v in self._headers:
  221. request.responseHeaders.addRawHeader(k, v)
  222. return self._originalResource.getChildWithDefault(name, request)
  223. def makeService(config):
  224. s = service.MultiService()
  225. if config['root']:
  226. root = config['root']
  227. if config['indexes']:
  228. config['root'].indexNames = config['indexes']
  229. else:
  230. # This really ought to be web.Admin or something
  231. root = demo.Test()
  232. if isinstance(root, static.File):
  233. root.registry.setComponent(interfaces.IServiceCollection, s)
  234. if config['extraHeaders']:
  235. root = _AddHeadersResource(root, config['extraHeaders'])
  236. if config['logfile']:
  237. site = server.Site(root, logPath=config['logfile'])
  238. else:
  239. site = server.Site(root)
  240. if config["display-tracebacks"]:
  241. site.displayTracebacks = True
  242. # Deprecate --notracebacks/-n
  243. if config["notracebacks"]:
  244. msg = deprecate._getDeprecationWarningString(
  245. "--notracebacks", incremental.Version('Twisted', 19, 7, 0))
  246. warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
  247. if config['personal']:
  248. site = makePersonalServerFactory(site)
  249. for port in config['ports']:
  250. svc = strports.service(port, site)
  251. svc.setServiceParent(s)
  252. return s