resource.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. # -*- test-case-name: twisted.web.test.test_web, twisted.web.test.test_resource -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Implementation of the lowest-level Resource class.
  6. See L{twisted.web.pages} for some utility implementations.
  7. """
  8. from __future__ import annotations
  9. __all__ = [
  10. "IResource",
  11. "getChildForRequest",
  12. "Resource",
  13. "ErrorPage",
  14. "NoResource",
  15. "ForbiddenResource",
  16. "EncodingResourceWrapper",
  17. ]
  18. import warnings
  19. from typing import Sequence
  20. from zope.interface import Attribute, Interface, implementer
  21. from incremental import Version
  22. from twisted.python.compat import nativeString
  23. from twisted.python.components import proxyForInterface
  24. from twisted.python.deprecate import deprecatedModuleAttribute
  25. from twisted.python.reflect import prefixedMethodNames
  26. from twisted.web._responses import FORBIDDEN, NOT_FOUND
  27. from twisted.web.error import UnsupportedMethod
  28. class IResource(Interface):
  29. """
  30. A web resource.
  31. """
  32. isLeaf = Attribute(
  33. """
  34. Signal if this IResource implementor is a "leaf node" or not. If True,
  35. getChildWithDefault will not be called on this Resource.
  36. """
  37. )
  38. def getChildWithDefault(name, request):
  39. """
  40. Return a child with the given name for the given request.
  41. This is the external interface used by the Resource publishing
  42. machinery. If implementing IResource without subclassing
  43. Resource, it must be provided. However, if subclassing Resource,
  44. getChild overridden instead.
  45. @param name: A single path component from a requested URL. For example,
  46. a request for I{http://example.com/foo/bar} will result in calls to
  47. this method with C{b"foo"} and C{b"bar"} as values for this
  48. argument.
  49. @type name: C{bytes}
  50. @param request: A representation of all of the information about the
  51. request that is being made for this child.
  52. @type request: L{twisted.web.server.Request}
  53. """
  54. def putChild(path: bytes, child: "IResource") -> None:
  55. """
  56. Put a child L{IResource} implementor at the given path.
  57. @param path: A single path component, to be interpreted relative to the
  58. path this resource is found at, at which to put the given child.
  59. For example, if resource A can be found at I{http://example.com/foo}
  60. then a call like C{A.putChild(b"bar", B)} will make resource B
  61. available at I{http://example.com/foo/bar}.
  62. The path component is I{not} URL-encoded -- pass C{b'foo bar'}
  63. rather than C{b'foo%20bar'}.
  64. """
  65. def render(request):
  66. """
  67. Render a request. This is called on the leaf resource for a request.
  68. @return: Either C{server.NOT_DONE_YET} to indicate an asynchronous or a
  69. C{bytes} instance to write as the response to the request. If
  70. C{NOT_DONE_YET} is returned, at some point later (for example, in a
  71. Deferred callback) call C{request.write(b"<html>")} to write data to
  72. the request, and C{request.finish()} to send the data to the
  73. browser.
  74. @raise twisted.web.error.UnsupportedMethod: If the HTTP verb
  75. requested is not supported by this resource.
  76. """
  77. def getChildForRequest(resource, request):
  78. """
  79. Traverse resource tree to find who will handle the request.
  80. """
  81. while request.postpath and not resource.isLeaf:
  82. pathElement = request.postpath.pop(0)
  83. request.prepath.append(pathElement)
  84. resource = resource.getChildWithDefault(pathElement, request)
  85. return resource
  86. @implementer(IResource)
  87. class Resource:
  88. """
  89. Define a web-accessible resource.
  90. This serves two main purposes: one is to provide a standard representation
  91. for what HTTP specification calls an 'entity', and the other is to provide
  92. an abstract directory structure for URL retrieval.
  93. """
  94. entityType = IResource
  95. allowedMethods: Sequence[bytes]
  96. server = None
  97. def __init__(self):
  98. """
  99. Initialize.
  100. """
  101. self.children = {}
  102. isLeaf = 0
  103. ### Abstract Collection Interface
  104. def listStaticNames(self):
  105. return list(self.children.keys())
  106. def listStaticEntities(self):
  107. return list(self.children.items())
  108. def listNames(self):
  109. return list(self.listStaticNames()) + self.listDynamicNames()
  110. def listEntities(self):
  111. return list(self.listStaticEntities()) + self.listDynamicEntities()
  112. def listDynamicNames(self):
  113. return []
  114. def listDynamicEntities(self, request=None):
  115. return []
  116. def getStaticEntity(self, name):
  117. return self.children.get(name)
  118. def getDynamicEntity(self, name, request):
  119. if name not in self.children:
  120. return self.getChild(name, request)
  121. else:
  122. return None
  123. def delEntity(self, name):
  124. del self.children[name]
  125. def reallyPutEntity(self, name, entity):
  126. self.children[name] = entity
  127. # Concrete HTTP interface
  128. def getChild(self, path, request):
  129. """
  130. Retrieve a 'child' resource from me.
  131. Implement this to create dynamic resource generation -- resources which
  132. are always available may be registered with self.putChild().
  133. This will not be called if the class-level variable 'isLeaf' is set in
  134. your subclass; instead, the 'postpath' attribute of the request will be
  135. left as a list of the remaining path elements.
  136. For example, the URL /foo/bar/baz will normally be::
  137. | site.resource.getChild('foo').getChild('bar').getChild('baz').
  138. However, if the resource returned by 'bar' has isLeaf set to true, then
  139. the getChild call will never be made on it.
  140. Parameters and return value have the same meaning and requirements as
  141. those defined by L{IResource.getChildWithDefault}.
  142. """
  143. return _UnsafeNoResource()
  144. def getChildWithDefault(self, path, request):
  145. """
  146. Retrieve a static or dynamically generated child resource from me.
  147. First checks if a resource was added manually by putChild, and then
  148. call getChild to check for dynamic resources. Only override if you want
  149. to affect behaviour of all child lookups, rather than just dynamic
  150. ones.
  151. This will check to see if I have a pre-registered child resource of the
  152. given name, and call getChild if I do not.
  153. @see: L{IResource.getChildWithDefault}
  154. """
  155. if path in self.children:
  156. return self.children[path]
  157. return self.getChild(path, request)
  158. def getChildForRequest(self, request):
  159. """
  160. Deprecated in favor of L{getChildForRequest}.
  161. @see: L{twisted.web.resource.getChildForRequest}.
  162. """
  163. warnings.warn(
  164. "Please use module level getChildForRequest.", DeprecationWarning, 2
  165. )
  166. return getChildForRequest(self, request)
  167. def putChild(self, path: bytes, child: IResource) -> None:
  168. """
  169. Register a static child.
  170. You almost certainly don't want '/' in your path. If you
  171. intended to have the root of a folder, e.g. /foo/, you want
  172. path to be ''.
  173. @param path: A single path component.
  174. @param child: The child resource to register.
  175. @see: L{IResource.putChild}
  176. """
  177. if not isinstance(path, bytes):
  178. raise TypeError(f"Path segment must be bytes, but {path!r} is {type(path)}")
  179. self.children[path] = child
  180. # IResource is incomplete and doesn't mention this server attribute, see
  181. # https://github.com/twisted/twisted/issues/11717
  182. child.server = self.server # type: ignore[attr-defined]
  183. def render(self, request):
  184. """
  185. Render a given resource. See L{IResource}'s render method.
  186. I delegate to methods of self with the form 'render_METHOD'
  187. where METHOD is the HTTP that was used to make the
  188. request. Examples: render_GET, render_HEAD, render_POST, and
  189. so on. Generally you should implement those methods instead of
  190. overriding this one.
  191. render_METHOD methods are expected to return a byte string which will be
  192. the rendered page, unless the return value is C{server.NOT_DONE_YET}, in
  193. which case it is this class's responsibility to write the results using
  194. C{request.write(data)} and then call C{request.finish()}.
  195. Old code that overrides render() directly is likewise expected
  196. to return a byte string or NOT_DONE_YET.
  197. @see: L{IResource.render}
  198. """
  199. m = getattr(self, "render_" + nativeString(request.method), None)
  200. if not m:
  201. try:
  202. allowedMethods = self.allowedMethods
  203. except AttributeError:
  204. allowedMethods = _computeAllowedMethods(self)
  205. raise UnsupportedMethod(allowedMethods)
  206. return m(request)
  207. def render_HEAD(self, request):
  208. """
  209. Default handling of HEAD method.
  210. I just return self.render_GET(request). When method is HEAD,
  211. the framework will handle this correctly.
  212. """
  213. return self.render_GET(request)
  214. def _computeAllowedMethods(resource):
  215. """
  216. Compute the allowed methods on a C{Resource} based on defined render_FOO
  217. methods. Used when raising C{UnsupportedMethod} but C{Resource} does
  218. not define C{allowedMethods} attribute.
  219. """
  220. allowedMethods = []
  221. for name in prefixedMethodNames(resource.__class__, "render_"):
  222. # Potentially there should be an API for encode('ascii') in this
  223. # situation - an API for taking a Python native string (bytes on Python
  224. # 2, text on Python 3) and returning a socket-compatible string type.
  225. allowedMethods.append(name.encode("ascii"))
  226. return allowedMethods
  227. class _UnsafeErrorPage(Resource):
  228. """
  229. L{_UnsafeErrorPage}, publicly available via the deprecated alias
  230. C{ErrorPage}, is a resource which responds with a particular
  231. (parameterized) status and a body consisting of HTML containing some
  232. descriptive text. This is useful for rendering simple error pages.
  233. Deprecated in Twisted 22.10.0 because it permits HTML injection; use
  234. L{twisted.web.pages.errorPage} instead.
  235. @ivar template: A native string which will have a dictionary interpolated
  236. into it to generate the response body. The dictionary has the following
  237. keys:
  238. - C{"code"}: The status code passed to L{_UnsafeErrorPage.__init__}.
  239. - C{"brief"}: The brief description passed to
  240. L{_UnsafeErrorPage.__init__}.
  241. - C{"detail"}: The detailed description passed to
  242. L{_UnsafeErrorPage.__init__}.
  243. @ivar code: An integer status code which will be used for the response.
  244. @type code: C{int}
  245. @ivar brief: A short string which will be included in the response body as
  246. the page title.
  247. @type brief: C{str}
  248. @ivar detail: A longer string which will be included in the response body.
  249. @type detail: C{str}
  250. """
  251. template = """
  252. <html>
  253. <head><title>%(code)s - %(brief)s</title></head>
  254. <body>
  255. <h1>%(brief)s</h1>
  256. <p>%(detail)s</p>
  257. </body>
  258. </html>
  259. """
  260. def __init__(self, status, brief, detail):
  261. Resource.__init__(self)
  262. self.code = status
  263. self.brief = brief
  264. self.detail = detail
  265. def render(self, request):
  266. request.setResponseCode(self.code)
  267. request.setHeader(b"content-type", b"text/html; charset=utf-8")
  268. interpolated = self.template % dict(
  269. code=self.code, brief=self.brief, detail=self.detail
  270. )
  271. if isinstance(interpolated, str):
  272. return interpolated.encode("utf-8")
  273. return interpolated
  274. def getChild(self, chnam, request):
  275. return self
  276. class _UnsafeNoResource(_UnsafeErrorPage):
  277. """
  278. L{_UnsafeNoResource}, publicly available via the deprecated alias
  279. C{NoResource}, is a specialization of L{_UnsafeErrorPage} which
  280. returns the HTTP response code I{NOT FOUND}.
  281. Deprecated in Twisted 22.10.0 because it permits HTML injection; use
  282. L{twisted.web.pages.notFound} instead.
  283. """
  284. def __init__(self, message="Sorry. No luck finding that resource."):
  285. _UnsafeErrorPage.__init__(self, NOT_FOUND, "No Such Resource", message)
  286. class _UnsafeForbiddenResource(_UnsafeErrorPage):
  287. """
  288. L{_UnsafeForbiddenResource}, publicly available via the deprecated alias
  289. C{ForbiddenResource} is a specialization of L{_UnsafeErrorPage} which
  290. returns the I{FORBIDDEN} HTTP response code.
  291. Deprecated in Twisted 22.10.0 because it permits HTML injection; use
  292. L{twisted.web.pages.forbidden} instead.
  293. """
  294. def __init__(self, message="Sorry, resource is forbidden."):
  295. _UnsafeErrorPage.__init__(self, FORBIDDEN, "Forbidden Resource", message)
  296. # Deliberately undocumented public aliases. See GHSA-vg46-2rrj-3647.
  297. ErrorPage = _UnsafeErrorPage
  298. NoResource = _UnsafeNoResource
  299. ForbiddenResource = _UnsafeForbiddenResource
  300. deprecatedModuleAttribute(
  301. Version("Twisted", 22, 10, 0),
  302. "Use twisted.web.pages.errorPage instead, which properly escapes HTML.",
  303. __name__,
  304. "ErrorPage",
  305. )
  306. deprecatedModuleAttribute(
  307. Version("Twisted", 22, 10, 0),
  308. "Use twisted.web.pages.notFound instead, which properly escapes HTML.",
  309. __name__,
  310. "NoResource",
  311. )
  312. deprecatedModuleAttribute(
  313. Version("Twisted", 22, 10, 0),
  314. "Use twisted.web.pages.forbidden instead, which properly escapes HTML.",
  315. __name__,
  316. "ForbiddenResource",
  317. )
  318. class _IEncodingResource(Interface):
  319. """
  320. A resource which knows about L{_IRequestEncoderFactory}.
  321. @since: 12.3
  322. """
  323. def getEncoder(request):
  324. """
  325. Parse the request and return an encoder if applicable, using
  326. L{_IRequestEncoderFactory.encoderForRequest}.
  327. @return: A L{_IRequestEncoder}, or L{None}.
  328. """
  329. @implementer(_IEncodingResource)
  330. class EncodingResourceWrapper(proxyForInterface(IResource)): # type: ignore[misc]
  331. """
  332. Wrap a L{IResource}, potentially applying an encoding to the response body
  333. generated.
  334. Note that the returned children resources won't be wrapped, so you have to
  335. explicitly wrap them if you want the encoding to be applied.
  336. @ivar encoders: A list of
  337. L{_IRequestEncoderFactory<twisted.web.iweb._IRequestEncoderFactory>}
  338. returning L{_IRequestEncoder<twisted.web.iweb._IRequestEncoder>} that
  339. may transform the data passed to C{Request.write}. The list must be
  340. sorted in order of priority: the first encoder factory handling the
  341. request will prevent the others from doing the same.
  342. @type encoders: C{list}.
  343. @since: 12.3
  344. """
  345. def __init__(self, original, encoders):
  346. super().__init__(original)
  347. self._encoders = encoders
  348. def getEncoder(self, request):
  349. """
  350. Browser the list of encoders looking for one applicable encoder.
  351. """
  352. for encoderFactory in self._encoders:
  353. encoder = encoderFactory.encoderForRequest(request)
  354. if encoder is not None:
  355. return encoder