static.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. # -*- test-case-name: twisted.web.test.test_static -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Static resources for L{twisted.web}.
  6. """
  7. from __future__ import annotations
  8. import errno
  9. import itertools
  10. import mimetypes
  11. import os
  12. import time
  13. import warnings
  14. from html import escape
  15. from typing import Any, Callable, Dict, Sequence
  16. from urllib.parse import quote, unquote
  17. from zope.interface import implementer
  18. from incremental import Version
  19. from typing_extensions import Literal
  20. from twisted.internet import abstract, interfaces
  21. from twisted.python import components, filepath, log
  22. from twisted.python.compat import nativeString, networkString
  23. from twisted.python.deprecate import deprecated
  24. from twisted.python.runtime import platformType
  25. from twisted.python.url import URL
  26. from twisted.python.util import InsensitiveDict
  27. from twisted.web import http, resource, server
  28. from twisted.web.util import redirectTo
  29. dangerousPathError = resource._UnsafeNoResource("Invalid request URL.")
  30. def isDangerous(path):
  31. return path == b".." or b"/" in path or networkString(os.sep) in path
  32. class Data(resource.Resource):
  33. """
  34. This is a static, in-memory resource.
  35. """
  36. def __init__(self, data, type):
  37. """
  38. @param data: The bytes that make up this data resource.
  39. @type data: L{bytes}
  40. @param type: A native string giving the Internet media type for this
  41. content.
  42. @type type: L{str}
  43. """
  44. resource.Resource.__init__(self)
  45. self.data = data
  46. self.type = type
  47. def render_GET(self, request):
  48. request.setHeader(b"content-type", networkString(self.type))
  49. request.setHeader(b"content-length", b"%d" % (len(self.data),))
  50. if request.method == b"HEAD":
  51. return b""
  52. return self.data
  53. render_HEAD = render_GET
  54. @deprecated(Version("Twisted", 16, 0, 0))
  55. def addSlash(request):
  56. """
  57. Add a trailing slash to C{request}'s URI. Deprecated, do not use.
  58. """
  59. return _addSlash(request)
  60. def _addSlash(request):
  61. """
  62. Add a trailing slash to C{request}'s URI.
  63. @param request: The incoming request to add the ending slash to.
  64. @type request: An object conforming to L{twisted.web.iweb.IRequest}
  65. @return: A URI with a trailing slash, with query and fragment preserved.
  66. @rtype: L{bytes}
  67. """
  68. url = URL.fromText(request.uri.decode("ascii"))
  69. # Add an empty path segment at the end, so that it adds a trailing slash
  70. url = url.replace(path=list(url.path) + [""])
  71. return url.asText().encode("ascii")
  72. class Redirect(resource.Resource):
  73. def __init__(self, request):
  74. resource.Resource.__init__(self)
  75. self.url = _addSlash(request)
  76. def render(self, request):
  77. return redirectTo(self.url, request)
  78. class Registry(components.Componentized):
  79. """
  80. I am a Componentized object that will be made available to internal Twisted
  81. file-based dynamic web content such as .rpy and .epy scripts.
  82. """
  83. def __init__(self):
  84. components.Componentized.__init__(self)
  85. self._pathCache = {}
  86. def cachePath(self, path, rsrc):
  87. self._pathCache[path] = rsrc
  88. def getCachedPath(self, path):
  89. return self._pathCache.get(path)
  90. def loadMimeTypes(mimetype_locations=None, init=mimetypes.init):
  91. """
  92. Produces a mapping of extensions (with leading dot) to MIME types.
  93. It does this by calling the C{init} function of the L{mimetypes} module.
  94. This will have the side effect of modifying the global MIME types cache
  95. in that module.
  96. Multiple file locations containing mime-types can be passed as a list.
  97. The files will be sourced in that order, overriding mime-types from the
  98. files sourced beforehand, but only if a new entry explicitly overrides
  99. the current entry.
  100. @param mimetype_locations: Optional. List of paths to C{mime.types} style
  101. files that should be used.
  102. @type mimetype_locations: iterable of paths or L{None}
  103. @param init: The init function to call. Defaults to the global C{init}
  104. function of the C{mimetypes} module. For internal use (testing) only.
  105. @type init: callable
  106. """
  107. init(mimetype_locations)
  108. mimetypes.types_map.update(
  109. {
  110. ".conf": "text/plain",
  111. ".diff": "text/plain",
  112. ".flac": "audio/x-flac",
  113. ".java": "text/plain",
  114. ".oz": "text/x-oz",
  115. ".swf": "application/x-shockwave-flash",
  116. ".wml": "text/vnd.wap.wml",
  117. ".xul": "application/vnd.mozilla.xul+xml",
  118. ".patch": "text/plain",
  119. }
  120. )
  121. return mimetypes.types_map
  122. def getTypeAndEncoding(filename, types, encodings, defaultType):
  123. p, ext = filepath.FilePath(filename).splitext()
  124. ext = filepath._coerceToFilesystemEncoding("", ext.lower())
  125. if ext in encodings:
  126. enc = encodings[ext]
  127. ext = os.path.splitext(p)[1].lower()
  128. else:
  129. enc = None
  130. type = types.get(ext, defaultType)
  131. return type, enc
  132. class File(resource.Resource, filepath.FilePath[str]):
  133. """
  134. File is a resource that represents a plain non-interpreted file
  135. (although it can look for an extension like .rpy or .cgi and hand the
  136. file to a processor for interpretation if you wish). Its constructor
  137. takes a file path.
  138. Alternatively, you can give a directory path to the constructor. In this
  139. case the resource will represent that directory, and its children will
  140. be files underneath that directory. This provides access to an entire
  141. filesystem tree with a single Resource.
  142. If you map the URL 'http://server/FILE' to a resource created as
  143. File('/tmp'), then http://server/FILE/ will return an HTML-formatted
  144. listing of the /tmp/ directory, and http://server/FILE/foo/bar.html will
  145. return the contents of /tmp/foo/bar.html .
  146. @cvar childNotFound: L{Resource} used to render 404 Not Found error pages.
  147. @cvar forbidden: L{Resource} used to render 403 Forbidden error pages.
  148. @ivar contentTypes: a mapping of extensions to MIME types used to set the
  149. default value for the Content-Type header.
  150. It is initialized with the values returned by L{loadMimeTypes}.
  151. @type contentTypes: C{dict}
  152. @ivar contentEncodings: a mapping of extensions to encoding types used to
  153. set default value for the Content-Encoding header.
  154. @type contentEncodings: C{dict}
  155. """
  156. contentTypes = loadMimeTypes()
  157. contentEncodings = {".gz": "gzip", ".bz2": "bzip2"}
  158. processors: Dict[str, Callable[[str, Any], Data]] = {}
  159. indexNames = ["index", "index.html", "index.htm", "index.rpy"]
  160. type = None
  161. def __init__(
  162. self,
  163. path: str,
  164. defaultType: str = "text/html",
  165. ignoredExts: Sequence[str] = (),
  166. registry: Registry | None = None,
  167. allowExt: Literal[0] = 0,
  168. ) -> None:
  169. """
  170. Create a file with the given path.
  171. @param path: The filename of the file from which this L{File} will
  172. serve data.
  173. @type path: C{str}
  174. @param defaultType: A I{major/minor}-style MIME type specifier
  175. indicating the I{Content-Type} with which this L{File}'s data
  176. will be served if a MIME type cannot be determined based on
  177. C{path}'s extension.
  178. @type defaultType: C{str}
  179. @param ignoredExts: A sequence giving the extensions of paths in the
  180. filesystem which will be ignored for the purposes of child
  181. lookup. For example, if C{ignoredExts} is C{(".bar",)} and
  182. C{path} is a directory containing a file named C{"foo.bar"}, a
  183. request for the C{"foo"} child of this resource will succeed
  184. with a L{File} pointing to C{"foo.bar"}.
  185. @param registry: The registry object being used to handle this
  186. request. If L{None}, one will be created.
  187. @type registry: L{Registry}
  188. @param allowExt: Ignored parameter, only present for backwards
  189. compatibility. Do not pass a value for this parameter.
  190. """
  191. resource.Resource.__init__(self)
  192. filepath.FilePath.__init__(self, path)
  193. self.defaultType = defaultType
  194. if ignoredExts in (0, 1) or allowExt:
  195. warnings.warn("ignoredExts should receive a list, not a boolean")
  196. if ignoredExts or allowExt:
  197. self.ignoredExts = ["*"]
  198. else:
  199. self.ignoredExts = []
  200. else:
  201. self.ignoredExts = list(ignoredExts)
  202. self.registry = registry or Registry()
  203. def ignoreExt(self, ext):
  204. """Ignore the given extension.
  205. Serve file.ext if file is requested
  206. """
  207. self.ignoredExts.append(ext)
  208. childNotFound = resource._UnsafeNoResource("File not found.")
  209. forbidden = resource._UnsafeForbiddenResource()
  210. def directoryListing(self):
  211. """
  212. Return a resource that generates an HTML listing of the
  213. directory this path represents.
  214. @return: A resource that renders the directory to HTML.
  215. @rtype: L{DirectoryLister}
  216. """
  217. path = self.path
  218. names = self.listNames()
  219. return DirectoryLister(
  220. path, names, self.contentTypes, self.contentEncodings, self.defaultType
  221. )
  222. def getChild(self, path, request):
  223. """
  224. If this L{File}"s path refers to a directory, return a L{File}
  225. referring to the file named C{path} in that directory.
  226. If C{path} is the empty string, return a L{DirectoryLister}
  227. instead.
  228. @param path: The current path segment.
  229. @type path: L{bytes}
  230. @param request: The incoming request.
  231. @type request: An that provides L{twisted.web.iweb.IRequest}.
  232. @return: A resource representing the requested file or
  233. directory, or L{NoResource} if the path cannot be
  234. accessed.
  235. @rtype: An object that provides L{resource.IResource}.
  236. """
  237. if isinstance(path, bytes):
  238. try:
  239. # Request calls urllib.unquote on each path segment,
  240. # leaving us with raw bytes.
  241. path = path.decode("utf-8")
  242. except UnicodeDecodeError:
  243. log.err(None, f"Could not decode path segment as utf-8: {path!r}")
  244. return self.childNotFound
  245. self.restat(reraise=False)
  246. if not self.isdir():
  247. return self.childNotFound
  248. if path:
  249. try:
  250. fpath = self.child(path)
  251. except filepath.InsecurePath:
  252. return self.childNotFound
  253. else:
  254. fpath = self.childSearchPreauth(*self.indexNames)
  255. if fpath is None:
  256. return self.directoryListing()
  257. if not fpath.exists():
  258. fpath = fpath.siblingExtensionSearch(*self.ignoredExts)
  259. if fpath is None:
  260. return self.childNotFound
  261. extension = fpath.splitext()[1]
  262. if platformType == "win32":
  263. # don't want .RPY to be different than .rpy, since that would allow
  264. # source disclosure.
  265. processor = InsensitiveDict(self.processors).get(extension)
  266. else:
  267. processor = self.processors.get(extension)
  268. if processor:
  269. return resource.IResource(processor(fpath.path, self.registry))
  270. return self.createSimilarFile(fpath.path)
  271. # methods to allow subclasses to e.g. decrypt files on the fly:
  272. def openForReading(self):
  273. """Open a file and return it."""
  274. return self.open()
  275. def getFileSize(self):
  276. """Return file size."""
  277. return self.getsize()
  278. def _parseRangeHeader(self, range):
  279. """
  280. Parse the value of a Range header into (start, stop) pairs.
  281. In a given pair, either of start or stop can be None, signifying that
  282. no value was provided, but not both.
  283. @return: A list C{[(start, stop)]} of pairs of length at least one.
  284. @raise ValueError: if the header is syntactically invalid or if the
  285. Bytes-Unit is anything other than "bytes'.
  286. """
  287. try:
  288. kind, value = range.split(b"=", 1)
  289. except ValueError:
  290. raise ValueError("Missing '=' separator")
  291. kind = kind.strip()
  292. if kind != b"bytes":
  293. raise ValueError(f"Unsupported Bytes-Unit: {kind!r}")
  294. unparsedRanges = list(filter(None, map(bytes.strip, value.split(b","))))
  295. parsedRanges = []
  296. for byteRange in unparsedRanges:
  297. try:
  298. start, end = byteRange.split(b"-", 1)
  299. except ValueError:
  300. raise ValueError(f"Invalid Byte-Range: {byteRange!r}")
  301. if start:
  302. try:
  303. start = int(start)
  304. except ValueError:
  305. raise ValueError(f"Invalid Byte-Range: {byteRange!r}")
  306. else:
  307. start = None
  308. if end:
  309. try:
  310. end = int(end)
  311. except ValueError:
  312. raise ValueError(f"Invalid Byte-Range: {byteRange!r}")
  313. else:
  314. end = None
  315. if start is not None:
  316. if end is not None and start > end:
  317. # Start must be less than or equal to end or it is invalid.
  318. raise ValueError(f"Invalid Byte-Range: {byteRange!r}")
  319. elif end is None:
  320. # One or both of start and end must be specified. Omitting
  321. # both is invalid.
  322. raise ValueError(f"Invalid Byte-Range: {byteRange!r}")
  323. parsedRanges.append((start, end))
  324. return parsedRanges
  325. def _rangeToOffsetAndSize(self, start, end):
  326. """
  327. Convert a start and end from a Range header to an offset and size.
  328. This method checks that the resulting range overlaps with the resource
  329. being served (and so has the value of C{getFileSize()} as an indirect
  330. input).
  331. Either but not both of start or end can be L{None}:
  332. - Omitted start means that the end value is actually a start value
  333. relative to the end of the resource.
  334. - Omitted end means the end of the resource should be the end of
  335. the range.
  336. End is interpreted as inclusive, as per RFC 2616.
  337. If this range doesn't overlap with any of this resource, C{(0, 0)} is
  338. returned, which is not otherwise a value return value.
  339. @param start: The start value from the header, or L{None} if one was
  340. not present.
  341. @param end: The end value from the header, or L{None} if one was not
  342. present.
  343. @return: C{(offset, size)} where offset is how far into this resource
  344. this resource the range begins and size is how long the range is,
  345. or C{(0, 0)} if the range does not overlap this resource.
  346. """
  347. size = self.getFileSize()
  348. if start is None:
  349. start = size - end
  350. end = size
  351. elif end is None:
  352. end = size
  353. elif end < size:
  354. end += 1
  355. elif end > size:
  356. end = size
  357. if start >= size:
  358. start = end = 0
  359. return start, (end - start)
  360. def _contentRange(self, offset, size):
  361. """
  362. Return a string suitable for the value of a Content-Range header for a
  363. range with the given offset and size.
  364. The offset and size are not sanity checked in any way.
  365. @param offset: How far into this resource the range begins.
  366. @param size: How long the range is.
  367. @return: The value as appropriate for the value of a Content-Range
  368. header.
  369. """
  370. return networkString(
  371. "bytes %d-%d/%d" % (offset, offset + size - 1, self.getFileSize())
  372. )
  373. def _doSingleRangeRequest(self, request, startAndEnd):
  374. """
  375. Set up the response for Range headers that specify a single range.
  376. This method checks if the request is satisfiable and sets the response
  377. code and Content-Range header appropriately. The return value
  378. indicates which part of the resource to return.
  379. @param request: The Request object.
  380. @param startAndEnd: A 2-tuple of start of the byte range as specified by
  381. the header and the end of the byte range as specified by the header.
  382. At most one of the start and end may be L{None}.
  383. @return: A 2-tuple of the offset and size of the range to return.
  384. offset == size == 0 indicates that the request is not satisfiable.
  385. """
  386. start, end = startAndEnd
  387. offset, size = self._rangeToOffsetAndSize(start, end)
  388. if offset == size == 0:
  389. # This range doesn't overlap with any of this resource, so the
  390. # request is unsatisfiable.
  391. request.setResponseCode(http.REQUESTED_RANGE_NOT_SATISFIABLE)
  392. request.setHeader(
  393. b"content-range", networkString("bytes */%d" % (self.getFileSize(),))
  394. )
  395. else:
  396. request.setResponseCode(http.PARTIAL_CONTENT)
  397. request.setHeader(b"content-range", self._contentRange(offset, size))
  398. return offset, size
  399. def _doMultipleRangeRequest(self, request, byteRanges):
  400. """
  401. Set up the response for Range headers that specify a single range.
  402. This method checks if the request is satisfiable and sets the response
  403. code and Content-Type and Content-Length headers appropriately. The
  404. return value, which is a little complicated, indicates which parts of
  405. the resource to return and the boundaries that should separate the
  406. parts.
  407. In detail, the return value is a tuple rangeInfo C{rangeInfo} is a
  408. list of 3-tuples C{(partSeparator, partOffset, partSize)}. The
  409. response to this request should be, for each element of C{rangeInfo},
  410. C{partSeparator} followed by C{partSize} bytes of the resource
  411. starting at C{partOffset}. Each C{partSeparator} includes the
  412. MIME-style boundary and the part-specific Content-type and
  413. Content-range headers. It is convenient to return the separator as a
  414. concrete string from this method, because this method needs to compute
  415. the number of bytes that will make up the response to be able to set
  416. the Content-Length header of the response accurately.
  417. @param request: The Request object.
  418. @param byteRanges: A list of C{(start, end)} values as specified by
  419. the header. For each range, at most one of C{start} and C{end}
  420. may be L{None}.
  421. @return: See above.
  422. """
  423. matchingRangeFound = False
  424. rangeInfo = []
  425. contentLength = 0
  426. boundary = networkString(f"{int(time.time() * 1000000):x}{os.getpid():x}")
  427. if self.type:
  428. contentType = self.type
  429. else:
  430. contentType = b"bytes" # It's what Apache does...
  431. for start, end in byteRanges:
  432. partOffset, partSize = self._rangeToOffsetAndSize(start, end)
  433. if partOffset == partSize == 0:
  434. continue
  435. contentLength += partSize
  436. matchingRangeFound = True
  437. partContentRange = self._contentRange(partOffset, partSize)
  438. partSeparator = networkString(
  439. (
  440. "\r\n"
  441. "--%s\r\n"
  442. "Content-type: %s\r\n"
  443. "Content-range: %s\r\n"
  444. "\r\n"
  445. )
  446. % (
  447. nativeString(boundary),
  448. nativeString(contentType),
  449. nativeString(partContentRange),
  450. )
  451. )
  452. contentLength += len(partSeparator)
  453. rangeInfo.append((partSeparator, partOffset, partSize))
  454. if not matchingRangeFound:
  455. request.setResponseCode(http.REQUESTED_RANGE_NOT_SATISFIABLE)
  456. request.setHeader(b"content-length", b"0")
  457. request.setHeader(
  458. b"content-range", networkString("bytes */%d" % (self.getFileSize(),))
  459. )
  460. return [], b""
  461. finalBoundary = b"\r\n--" + boundary + b"--\r\n"
  462. rangeInfo.append((finalBoundary, 0, 0))
  463. request.setResponseCode(http.PARTIAL_CONTENT)
  464. request.setHeader(
  465. b"content-type",
  466. networkString(f'multipart/byteranges; boundary="{nativeString(boundary)}"'),
  467. )
  468. request.setHeader(
  469. b"content-length", b"%d" % (contentLength + len(finalBoundary),)
  470. )
  471. return rangeInfo
  472. def _setContentHeaders(self, request, size=None):
  473. """
  474. Set the Content-length and Content-type headers for this request.
  475. This method is not appropriate for requests for multiple byte ranges;
  476. L{_doMultipleRangeRequest} will set these headers in that case.
  477. @param request: The L{twisted.web.http.Request} object.
  478. @param size: The size of the response. If not specified, default to
  479. C{self.getFileSize()}.
  480. """
  481. if size is None:
  482. size = self.getFileSize()
  483. request.setHeader(b"content-length", b"%d" % (size,))
  484. if self.type:
  485. request.setHeader(b"content-type", networkString(self.type))
  486. if self.encoding:
  487. request.setHeader(b"content-encoding", networkString(self.encoding))
  488. def makeProducer(self, request, fileForReading):
  489. """
  490. Make a L{StaticProducer} that will produce the body of this response.
  491. This method will also set the response code and Content-* headers.
  492. @param request: The L{twisted.web.http.Request} object.
  493. @param fileForReading: The file object containing the resource.
  494. @return: A L{StaticProducer}. Calling C{.start()} on this will begin
  495. producing the response.
  496. """
  497. byteRange = request.getHeader(b"range")
  498. if byteRange is None:
  499. self._setContentHeaders(request)
  500. request.setResponseCode(http.OK)
  501. return NoRangeStaticProducer(request, fileForReading)
  502. try:
  503. parsedRanges = self._parseRangeHeader(byteRange)
  504. except ValueError:
  505. log.msg(f"Ignoring malformed Range header {byteRange.decode()!r}")
  506. self._setContentHeaders(request)
  507. request.setResponseCode(http.OK)
  508. return NoRangeStaticProducer(request, fileForReading)
  509. if len(parsedRanges) == 1:
  510. offset, size = self._doSingleRangeRequest(request, parsedRanges[0])
  511. self._setContentHeaders(request, size)
  512. return SingleRangeStaticProducer(request, fileForReading, offset, size)
  513. else:
  514. rangeInfo = self._doMultipleRangeRequest(request, parsedRanges)
  515. return MultipleRangeStaticProducer(request, fileForReading, rangeInfo)
  516. def render_GET(self, request):
  517. """
  518. Begin sending the contents of this L{File} (or a subset of the
  519. contents, based on the 'range' header) to the given request.
  520. """
  521. self.restat(False)
  522. if self.type is None:
  523. self.type, self.encoding = getTypeAndEncoding(
  524. self.basename(),
  525. self.contentTypes,
  526. self.contentEncodings,
  527. self.defaultType,
  528. )
  529. if not self.exists():
  530. return self.childNotFound.render(request)
  531. if self.isdir():
  532. return self.redirect(request)
  533. request.setHeader(b"accept-ranges", b"bytes")
  534. try:
  535. fileForReading = self.openForReading()
  536. except OSError as e:
  537. if e.errno == errno.EACCES:
  538. return self.forbidden.render(request)
  539. else:
  540. raise
  541. if request.setLastModified(self.getModificationTime()) is http.CACHED:
  542. # `setLastModified` also sets the response code for us, so if the
  543. # request is cached, we close the file now that we've made sure that
  544. # the request would otherwise succeed and return an empty body.
  545. fileForReading.close()
  546. return b""
  547. if request.method == b"HEAD":
  548. # Set the content headers here, rather than making a producer.
  549. self._setContentHeaders(request)
  550. # We've opened the file to make sure it's accessible, so close it
  551. # now that we don't need it.
  552. fileForReading.close()
  553. return b""
  554. producer = self.makeProducer(request, fileForReading)
  555. producer.start()
  556. # and make sure the connection doesn't get closed
  557. return server.NOT_DONE_YET
  558. render_HEAD = render_GET
  559. def redirect(self, request):
  560. return redirectTo(_addSlash(request), request)
  561. def listNames(self):
  562. if not self.isdir():
  563. return []
  564. directory = self.listdir()
  565. directory.sort()
  566. return directory
  567. def listEntities(self):
  568. return list(
  569. map(
  570. lambda fileName, self=self: self.createSimilarFile(
  571. os.path.join(self.path, fileName)
  572. ),
  573. self.listNames(),
  574. )
  575. )
  576. def createSimilarFile(self, path):
  577. f = self.__class__(path, self.defaultType, self.ignoredExts, self.registry)
  578. # refactoring by steps, here - constructor should almost certainly take these
  579. f.processors = self.processors
  580. f.indexNames = self.indexNames[:]
  581. f.childNotFound = self.childNotFound
  582. return f
  583. @implementer(interfaces.IPullProducer)
  584. class StaticProducer:
  585. """
  586. Superclass for classes that implement the business of producing.
  587. @ivar request: The L{IRequest} to write the contents of the file to.
  588. @ivar fileObject: The file the contents of which to write to the request.
  589. """
  590. bufferSize = abstract.FileDescriptor.bufferSize
  591. def __init__(self, request, fileObject):
  592. """
  593. Initialize the instance.
  594. """
  595. self.request = request
  596. self.fileObject = fileObject
  597. def start(self):
  598. raise NotImplementedError(self.start)
  599. def resumeProducing(self):
  600. raise NotImplementedError(self.resumeProducing)
  601. def stopProducing(self):
  602. """
  603. Stop producing data.
  604. L{twisted.internet.interfaces.IProducer.stopProducing}
  605. is called when our consumer has died, and subclasses also call this
  606. method when they are done producing data.
  607. """
  608. self.fileObject.close()
  609. self.request = None
  610. class NoRangeStaticProducer(StaticProducer):
  611. """
  612. A L{StaticProducer} that writes the entire file to the request.
  613. """
  614. def start(self):
  615. self.request.registerProducer(self, False)
  616. def resumeProducing(self):
  617. if not self.request:
  618. return
  619. data = self.fileObject.read(self.bufferSize)
  620. if data:
  621. # this .write will spin the reactor, calling .doWrite and then
  622. # .resumeProducing again, so be prepared for a re-entrant call
  623. self.request.write(data)
  624. else:
  625. self.request.unregisterProducer()
  626. self.request.finish()
  627. self.stopProducing()
  628. class SingleRangeStaticProducer(StaticProducer):
  629. """
  630. A L{StaticProducer} that writes a single chunk of a file to the request.
  631. """
  632. def __init__(self, request, fileObject, offset, size):
  633. """
  634. Initialize the instance.
  635. @param request: See L{StaticProducer}.
  636. @param fileObject: See L{StaticProducer}.
  637. @param offset: The offset into the file of the chunk to be written.
  638. @param size: The size of the chunk to write.
  639. """
  640. StaticProducer.__init__(self, request, fileObject)
  641. self.offset = offset
  642. self.size = size
  643. def start(self):
  644. self.fileObject.seek(self.offset)
  645. self.bytesWritten = 0
  646. self.request.registerProducer(self, 0)
  647. def resumeProducing(self):
  648. if not self.request:
  649. return
  650. data = self.fileObject.read(min(self.bufferSize, self.size - self.bytesWritten))
  651. if data:
  652. self.bytesWritten += len(data)
  653. # this .write will spin the reactor, calling .doWrite and then
  654. # .resumeProducing again, so be prepared for a re-entrant call
  655. self.request.write(data)
  656. if self.request and self.bytesWritten == self.size:
  657. self.request.unregisterProducer()
  658. self.request.finish()
  659. self.stopProducing()
  660. class MultipleRangeStaticProducer(StaticProducer):
  661. """
  662. A L{StaticProducer} that writes several chunks of a file to the request.
  663. """
  664. def __init__(self, request, fileObject, rangeInfo):
  665. """
  666. Initialize the instance.
  667. @param request: See L{StaticProducer}.
  668. @param fileObject: See L{StaticProducer}.
  669. @param rangeInfo: A list of tuples C{[(boundary, offset, size)]}
  670. where:
  671. - C{boundary} will be written to the request first.
  672. - C{offset} the offset into the file of chunk to write.
  673. - C{size} the size of the chunk to write.
  674. """
  675. StaticProducer.__init__(self, request, fileObject)
  676. self.rangeInfo = rangeInfo
  677. def start(self):
  678. self.rangeIter = iter(self.rangeInfo)
  679. self._nextRange()
  680. self.request.registerProducer(self, 0)
  681. def _nextRange(self):
  682. self.partBoundary, partOffset, self._partSize = next(self.rangeIter)
  683. self._partBytesWritten = 0
  684. self.fileObject.seek(partOffset)
  685. def resumeProducing(self):
  686. if not self.request:
  687. return
  688. data = []
  689. dataLength = 0
  690. done = False
  691. while dataLength < self.bufferSize:
  692. if self.partBoundary:
  693. dataLength += len(self.partBoundary)
  694. data.append(self.partBoundary)
  695. self.partBoundary = None
  696. p = self.fileObject.read(
  697. min(
  698. self.bufferSize - dataLength,
  699. self._partSize - self._partBytesWritten,
  700. )
  701. )
  702. self._partBytesWritten += len(p)
  703. dataLength += len(p)
  704. data.append(p)
  705. if self.request and self._partBytesWritten == self._partSize:
  706. try:
  707. self._nextRange()
  708. except StopIteration:
  709. done = True
  710. break
  711. self.request.write(b"".join(data))
  712. if done:
  713. self.request.unregisterProducer()
  714. self.request.finish()
  715. self.stopProducing()
  716. class ASISProcessor(resource.Resource):
  717. """
  718. Serve files exactly as responses without generating a status-line or any
  719. headers. Inspired by Apache's mod_asis.
  720. """
  721. def __init__(self, path, registry=None):
  722. resource.Resource.__init__(self)
  723. self.path = path
  724. self.registry = registry or Registry()
  725. def render(self, request):
  726. request.startedWriting = 1
  727. res = File(self.path, registry=self.registry)
  728. return res.render(request)
  729. def formatFileSize(size):
  730. """
  731. Format the given file size in bytes to human readable format.
  732. """
  733. if size < 1024:
  734. return "%iB" % size
  735. elif size < (1024**2):
  736. return "%iK" % (size / 1024)
  737. elif size < (1024**3):
  738. return "%iM" % (size / (1024**2))
  739. else:
  740. return "%iG" % (size / (1024**3))
  741. class DirectoryLister(resource.Resource):
  742. """
  743. Print the content of a directory.
  744. @ivar template: page template used to render the content of the directory.
  745. It must contain the format keys B{header} and B{tableContent}.
  746. @type template: C{str}
  747. @ivar linePattern: template used to render one line in the listing table.
  748. It must contain the format keys B{class}, B{href}, B{text}, B{size},
  749. B{type} and B{encoding}.
  750. @type linePattern: C{str}
  751. @ivar contentTypes: a mapping of extensions to MIME types used to populate
  752. the information of a member of this directory.
  753. It is initialized with the value L{File.contentTypes}.
  754. @type contentTypes: C{dict}
  755. @ivar contentEncodings: a mapping of extensions to encoding types.
  756. It is initialized with the value L{File.contentEncodings}.
  757. @type contentEncodings: C{dict}
  758. @ivar defaultType: default type used when no mimetype is detected.
  759. @type defaultType: C{str}
  760. @ivar dirs: filtered content of C{path}, if the whole content should not be
  761. displayed (default to L{None}, which means the actual content of
  762. C{path} is printed).
  763. @type dirs: L{None} or C{list}
  764. @ivar path: directory which content should be listed.
  765. @type path: C{str}
  766. """
  767. template = """<html>
  768. <head>
  769. <title>%(header)s</title>
  770. <style>
  771. .even-dir { background-color: #efe0ef }
  772. .even { background-color: #eee }
  773. .odd-dir {background-color: #f0d0ef }
  774. .odd { background-color: #dedede }
  775. .icon { text-align: center }
  776. .listing {
  777. margin-left: auto;
  778. margin-right: auto;
  779. width: 50%%;
  780. padding: 0.1em;
  781. }
  782. body { border: 0; padding: 0; margin: 0; background-color: #efefef; }
  783. h1 {padding: 0.1em; background-color: #777; color: white; border-bottom: thin white dashed;}
  784. </style>
  785. </head>
  786. <body>
  787. <h1>%(header)s</h1>
  788. <table>
  789. <thead>
  790. <tr>
  791. <th>Filename</th>
  792. <th>Size</th>
  793. <th>Content type</th>
  794. <th>Content encoding</th>
  795. </tr>
  796. </thead>
  797. <tbody>
  798. %(tableContent)s
  799. </tbody>
  800. </table>
  801. </body>
  802. </html>
  803. """
  804. linePattern = """<tr class="%(class)s">
  805. <td><a href="%(href)s">%(text)s</a></td>
  806. <td>%(size)s</td>
  807. <td>%(type)s</td>
  808. <td>%(encoding)s</td>
  809. </tr>
  810. """
  811. def __init__(
  812. self,
  813. pathname,
  814. dirs=None,
  815. contentTypes=File.contentTypes,
  816. contentEncodings=File.contentEncodings,
  817. defaultType="text/html",
  818. ):
  819. resource.Resource.__init__(self)
  820. self.contentTypes = contentTypes
  821. self.contentEncodings = contentEncodings
  822. self.defaultType = defaultType
  823. # dirs allows usage of the File to specify what gets listed
  824. self.dirs = dirs
  825. self.path = pathname
  826. def _getFilesAndDirectories(self, directory):
  827. """
  828. Helper returning files and directories in given directory listing, with
  829. attributes to be used to build a table content with
  830. C{self.linePattern}.
  831. @return: tuple of (directories, files)
  832. @rtype: C{tuple} of C{list}
  833. """
  834. files = []
  835. dirs = []
  836. for path in directory:
  837. if isinstance(path, bytes):
  838. path = path.decode("utf8")
  839. url = quote(path, "/")
  840. escapedPath = escape(path)
  841. childPath = filepath.FilePath(self.path).child(path)
  842. if childPath.isdir():
  843. dirs.append(
  844. {
  845. "text": escapedPath + "/",
  846. "href": url + "/",
  847. "size": "",
  848. "type": "[Directory]",
  849. "encoding": "",
  850. }
  851. )
  852. else:
  853. mimetype, encoding = getTypeAndEncoding(
  854. path, self.contentTypes, self.contentEncodings, self.defaultType
  855. )
  856. try:
  857. size = childPath.getsize()
  858. except OSError:
  859. continue
  860. files.append(
  861. {
  862. "text": escapedPath,
  863. "href": url,
  864. "type": "[%s]" % mimetype,
  865. "encoding": (encoding and "[%s]" % encoding or ""),
  866. "size": formatFileSize(size),
  867. }
  868. )
  869. return dirs, files
  870. def _buildTableContent(self, elements):
  871. """
  872. Build a table content using C{self.linePattern} and giving elements odd
  873. and even classes.
  874. """
  875. tableContent = []
  876. rowClasses = itertools.cycle(["odd", "even"])
  877. for element, rowClass in zip(elements, rowClasses):
  878. element["class"] = rowClass
  879. tableContent.append(self.linePattern % element)
  880. return tableContent
  881. def render(self, request):
  882. """
  883. Render a listing of the content of C{self.path}.
  884. """
  885. request.setHeader(b"content-type", b"text/html; charset=utf-8")
  886. if self.dirs is None:
  887. directory = os.listdir(self.path)
  888. directory.sort()
  889. else:
  890. directory = self.dirs
  891. dirs, files = self._getFilesAndDirectories(directory)
  892. tableContent = "".join(self._buildTableContent(dirs + files))
  893. header = "Directory listing for {}".format(
  894. escape(unquote(nativeString(request.uri))),
  895. )
  896. done = self.template % {"header": header, "tableContent": tableContent}
  897. done = done.encode("utf8")
  898. return done
  899. def __repr__(self) -> str:
  900. return "<DirectoryLister of %r>" % self.path
  901. __str__ = __repr__