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.

static.py 37KB

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