fileapp.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
  2. # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  3. # (c) 2005 Ian Bicking, Clark C. Evans and contributors
  4. # This module is part of the Python Paste Project and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """
  7. This module handles sending static content such as in-memory data or
  8. files. At this time it has cache helpers and understands the
  9. if-modified-since request header.
  10. """
  11. import os, time, mimetypes, zipfile, tarfile
  12. from paste.httpexceptions import *
  13. from paste.httpheaders import *
  14. CACHE_SIZE = 4096
  15. BLOCK_SIZE = 4096 * 16
  16. __all__ = ['DataApp', 'FileApp', 'DirectoryApp', 'ArchiveStore']
  17. class DataApp(object):
  18. """
  19. Returns an application that will send content in a single chunk,
  20. this application has support for setting cache-control and for
  21. responding to conditional (or HEAD) requests.
  22. Constructor Arguments:
  23. ``content`` the content being sent to the client
  24. ``headers`` the headers to send /w the response
  25. The remaining ``kwargs`` correspond to headers, where the
  26. underscore is replaced with a dash. These values are only
  27. added to the headers if they are not already provided; thus,
  28. they can be used for default values. Examples include, but
  29. are not limited to:
  30. ``content_type``
  31. ``content_encoding``
  32. ``content_location``
  33. ``cache_control()``
  34. This method provides validated construction of the ``Cache-Control``
  35. header as well as providing for automated filling out of the
  36. ``EXPIRES`` header for HTTP/1.0 clients.
  37. ``set_content()``
  38. This method provides a mechanism to set the content after the
  39. application has been constructed. This method does things
  40. like changing ``Last-Modified`` and ``Content-Length`` headers.
  41. """
  42. allowed_methods = ('GET', 'HEAD')
  43. def __init__(self, content, headers=None, allowed_methods=None,
  44. **kwargs):
  45. assert isinstance(headers, (type(None), list))
  46. self.expires = None
  47. self.content = None
  48. self.content_length = None
  49. self.last_modified = 0
  50. if allowed_methods is not None:
  51. self.allowed_methods = allowed_methods
  52. self.headers = headers or []
  53. for (k, v) in kwargs.items():
  54. header = get_header(k)
  55. header.update(self.headers, v)
  56. ACCEPT_RANGES.update(self.headers, bytes=True)
  57. if not CONTENT_TYPE(self.headers):
  58. CONTENT_TYPE.update(self.headers)
  59. if content is not None:
  60. self.set_content(content)
  61. def cache_control(self, **kwargs):
  62. self.expires = CACHE_CONTROL.apply(self.headers, **kwargs) or None
  63. return self
  64. def set_content(self, content, last_modified=None):
  65. assert content is not None
  66. if last_modified is None:
  67. self.last_modified = time.time()
  68. else:
  69. self.last_modified = last_modified
  70. self.content = content
  71. self.content_length = len(content)
  72. LAST_MODIFIED.update(self.headers, time=self.last_modified)
  73. return self
  74. def content_disposition(self, **kwargs):
  75. CONTENT_DISPOSITION.apply(self.headers, **kwargs)
  76. return self
  77. def __call__(self, environ, start_response):
  78. method = environ['REQUEST_METHOD'].upper()
  79. if method not in self.allowed_methods:
  80. exc = HTTPMethodNotAllowed(
  81. 'You cannot %s a file' % method,
  82. headers=[('Allow', ','.join(self.allowed_methods))])
  83. return exc(environ, start_response)
  84. return self.get(environ, start_response)
  85. def calculate_etag(self):
  86. return str(self.last_modified) + '-' + str(self.content_length)
  87. def get(self, environ, start_response):
  88. headers = self.headers[:]
  89. current_etag = self.calculate_etag()
  90. ETAG.update(headers, current_etag)
  91. if self.expires is not None:
  92. EXPIRES.update(headers, delta=self.expires)
  93. try:
  94. client_etags = IF_NONE_MATCH.parse(environ)
  95. if client_etags:
  96. for etag in client_etags:
  97. if etag == current_etag or etag == '*':
  98. # horribly inefficient, n^2 performance, yuck!
  99. for head in list_headers(entity=True):
  100. head.delete(headers)
  101. start_response('304 Not Modified', headers)
  102. return ['']
  103. except HTTPBadRequest, exce:
  104. return exce.wsgi_application(environ, start_response)
  105. # If we get If-None-Match and If-Modified-Since, and
  106. # If-None-Match doesn't match, then we should not try to
  107. # figure out If-Modified-Since (which has 1-second granularity
  108. # and just isn't as accurate)
  109. if not client_etags:
  110. try:
  111. client_clock = IF_MODIFIED_SINCE.parse(environ)
  112. if client_clock >= int(self.last_modified):
  113. # horribly inefficient, n^2 performance, yuck!
  114. for head in list_headers(entity=True):
  115. head.delete(headers)
  116. start_response('304 Not Modified', headers)
  117. return [''] # empty body
  118. except HTTPBadRequest, exce:
  119. return exce.wsgi_application(environ, start_response)
  120. (lower, upper) = (0, self.content_length - 1)
  121. range = RANGE.parse(environ)
  122. if range and 'bytes' == range[0] and 1 == len(range[1]):
  123. (lower, upper) = range[1][0]
  124. upper = upper or (self.content_length - 1)
  125. if upper >= self.content_length or lower > upper:
  126. return HTTPRequestRangeNotSatisfiable((
  127. "Range request was made beyond the end of the content,\r\n"
  128. "which is %s long.\r\n Range: %s\r\n") % (
  129. self.content_length, RANGE(environ))
  130. ).wsgi_application(environ, start_response)
  131. content_length = upper - lower + 1
  132. CONTENT_RANGE.update(headers, first_byte=lower, last_byte=upper,
  133. total_length = self.content_length)
  134. CONTENT_LENGTH.update(headers, content_length)
  135. if content_length == self.content_length:
  136. start_response('200 OK', headers)
  137. else:
  138. start_response('206 Partial Content', headers)
  139. if self.content is not None:
  140. return [self.content[lower:upper+1]]
  141. return (lower, content_length)
  142. class FileApp(DataApp):
  143. """
  144. Returns an application that will send the file at the given
  145. filename. Adds a mime type based on ``mimetypes.guess_type()``.
  146. See DataApp for the arguments beyond ``filename``.
  147. """
  148. def __init__(self, filename, headers=None, **kwargs):
  149. self.filename = filename
  150. content_type, content_encoding = self.guess_type()
  151. if content_type and 'content_type' not in kwargs:
  152. kwargs['content_type'] = content_type
  153. if content_encoding and 'content_encoding' not in kwargs:
  154. kwargs['content_encoding'] = content_encoding
  155. DataApp.__init__(self, None, headers, **kwargs)
  156. def guess_type(self):
  157. return mimetypes.guess_type(self.filename)
  158. def update(self, force=False):
  159. stat = os.stat(self.filename)
  160. if not force and stat.st_mtime == self.last_modified:
  161. return
  162. self.last_modified = stat.st_mtime
  163. if stat.st_size < CACHE_SIZE:
  164. fh = open(self.filename,"rb")
  165. self.set_content(fh.read(), stat.st_mtime)
  166. fh.close()
  167. else:
  168. self.content = None
  169. self.content_length = stat.st_size
  170. # This is updated automatically if self.set_content() is
  171. # called
  172. LAST_MODIFIED.update(self.headers, time=self.last_modified)
  173. def get(self, environ, start_response):
  174. is_head = environ['REQUEST_METHOD'].upper() == 'HEAD'
  175. if 'max-age=0' in CACHE_CONTROL(environ).lower():
  176. self.update(force=True) # RFC 2616 13.2.6
  177. else:
  178. self.update()
  179. if not self.content:
  180. if not os.path.exists(self.filename):
  181. exc = HTTPNotFound(
  182. 'The resource does not exist',
  183. comment="No file at %r" % self.filename)
  184. return exc(environ, start_response)
  185. try:
  186. file = open(self.filename, 'rb')
  187. except (IOError, OSError), e:
  188. exc = HTTPForbidden(
  189. 'You are not permitted to view this file (%s)' % e)
  190. return exc.wsgi_application(
  191. environ, start_response)
  192. retval = DataApp.get(self, environ, start_response)
  193. if isinstance(retval, list):
  194. # cached content, exception, or not-modified
  195. if is_head:
  196. return ['']
  197. return retval
  198. (lower, content_length) = retval
  199. if is_head:
  200. return ['']
  201. file.seek(lower)
  202. file_wrapper = environ.get('wsgi.file_wrapper', None)
  203. if file_wrapper:
  204. return file_wrapper(file, BLOCK_SIZE)
  205. else:
  206. return _FileIter(file, size=content_length)
  207. class _FileIter(object):
  208. def __init__(self, file, block_size=None, size=None):
  209. self.file = file
  210. self.size = size
  211. self.block_size = block_size or BLOCK_SIZE
  212. def __iter__(self):
  213. return self
  214. def next(self):
  215. chunk_size = self.block_size
  216. if self.size is not None:
  217. if chunk_size > self.size:
  218. chunk_size = self.size
  219. self.size -= chunk_size
  220. data = self.file.read(chunk_size)
  221. if not data:
  222. raise StopIteration
  223. return data
  224. def close(self):
  225. self.file.close()
  226. class DirectoryApp(object):
  227. """
  228. Returns an application that dispatches requests to corresponding FileApps based on PATH_INFO.
  229. FileApp instances are cached. This app makes sure not to serve any files that are not in a subdirectory.
  230. To customize FileApp creation override ``DirectoryApp.make_fileapp``
  231. """
  232. def __init__(self, path):
  233. self.path = os.path.abspath(path)
  234. if not self.path.endswith(os.path.sep):
  235. self.path += os.path.sep
  236. assert os.path.isdir(self.path)
  237. self.cached_apps = {}
  238. make_fileapp = FileApp
  239. def __call__(self, environ, start_response):
  240. path_info = environ['PATH_INFO']
  241. app = self.cached_apps.get(path_info)
  242. if app is None:
  243. path = os.path.join(self.path, path_info.lstrip('/'))
  244. if not os.path.normpath(path).startswith(self.path):
  245. app = HTTPForbidden()
  246. elif os.path.isfile(path):
  247. app = self.make_fileapp(path)
  248. self.cached_apps[path_info] = app
  249. else:
  250. app = HTTPNotFound(comment=path)
  251. return app(environ, start_response)
  252. class ArchiveStore(object):
  253. """
  254. Returns an application that serves up a DataApp for items requested
  255. in a given zip or tar archive.
  256. Constructor Arguments:
  257. ``filepath`` the path to the archive being served
  258. ``cache_control()``
  259. This method provides validated construction of the ``Cache-Control``
  260. header as well as providing for automated filling out of the
  261. ``EXPIRES`` header for HTTP/1.0 clients.
  262. """
  263. def __init__(self, filepath):
  264. if zipfile.is_zipfile(filepath):
  265. self.archive = zipfile.ZipFile(filepath,"r")
  266. elif tarfile.is_tarfile(filepath):
  267. self.archive = tarfile.TarFileCompat(filepath,"r")
  268. else:
  269. raise AssertionError("filepath '%s' is not a zip or tar " % filepath)
  270. self.expires = None
  271. self.last_modified = time.time()
  272. self.cache = {}
  273. def cache_control(self, **kwargs):
  274. self.expires = CACHE_CONTROL.apply(self.headers, **kwargs) or None
  275. return self
  276. def __call__(self, environ, start_response):
  277. path = environ.get("PATH_INFO","")
  278. if path.startswith("/"):
  279. path = path[1:]
  280. application = self.cache.get(path)
  281. if application:
  282. return application(environ, start_response)
  283. try:
  284. info = self.archive.getinfo(path)
  285. except KeyError:
  286. exc = HTTPNotFound("The file requested, '%s', was not found." % path)
  287. return exc.wsgi_application(environ, start_response)
  288. if info.filename.endswith("/"):
  289. exc = HTTPNotFound("Path requested, '%s', is not a file." % path)
  290. return exc.wsgi_application(environ, start_response)
  291. content_type, content_encoding = mimetypes.guess_type(info.filename)
  292. app = DataApp(None, content_type = content_type,
  293. content_encoding = content_encoding)
  294. app.set_content(self.archive.read(path),
  295. time.mktime(info.date_time + (0,0,0)))
  296. self.cache[path] = app
  297. app.expires = self.expires
  298. return app(environ, start_response)