tap.py 10 KB

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