static.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/env python
  2. """static - A stupidly simple WSGI way to serve static (or mixed) content.
  3. (See the docstrings of the various functions and classes.)
  4. Copyright (C) 2006-2009 Luke Arno - http://lukearno.com/
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to:
  15. The Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor,
  17. Boston, MA 02110-1301, USA.
  18. Luke Arno can be found at http://lukearno.com/
  19. """
  20. import mimetypes
  21. import time
  22. import string
  23. import sys
  24. from os import path, stat, getcwd
  25. from email.utils import formatdate, parsedate
  26. from fnmatch import fnmatch
  27. from wsgiref import util
  28. from wsgiref.validate import validator
  29. from wsgiref.headers import Headers
  30. from wsgiref.simple_server import make_server
  31. from optparse import OptionParser
  32. try: from pkg_resources import resource_filename, Requirement
  33. except: pass
  34. try: import kid
  35. except: pass
  36. class MagicError(Exception): pass
  37. class StatusApp:
  38. """Used by WSGI apps to return some HTTP status."""
  39. def __init__(self, status, message=None):
  40. self.status = status
  41. if message is None:
  42. self.message = status
  43. else:
  44. self.message = message
  45. def __call__(self, environ, start_response, headers=[]):
  46. if self.message:
  47. Headers(headers).add_header('Content-type', 'text/plain')
  48. start_response(self.status, headers)
  49. if environ['REQUEST_METHOD'] == 'HEAD':
  50. return [""]
  51. else:
  52. return [self.message]
  53. class Cling(object):
  54. """A stupidly simple way to serve static content via WSGI.
  55. Serve the file of the same path as PATH_INFO in self.datadir.
  56. Look up the Content-type in self.content_types by extension
  57. or use 'text/plain' if the extension is not found.
  58. Serve up the contents of the file or delegate to self.not_found.
  59. """
  60. block_size = 16 * 4096
  61. index_file = 'index.html'
  62. not_found = StatusApp('404 Not Found')
  63. not_modified = StatusApp('304 Not Modified', "")
  64. moved_permanently = StatusApp('301 Moved Permanently')
  65. method_not_allowed = StatusApp('405 Method Not Allowed')
  66. success_no_content = StatusApp('204 No Content', "")
  67. server_error = StatusApp('500 Internal Server Error')
  68. def __init__(self, root, **kw):
  69. """Just set the root and any other attribs passes via **kw."""
  70. self.root = root
  71. for k, v in kw.iteritems():
  72. setattr(self, k, v)
  73. def __call__(self, environ, start_response):
  74. """Respond to a request when called in the usual WSGI way."""
  75. path_info = environ.get('PATH_INFO', '')
  76. full_path = self._full_path(path_info)
  77. if not self._is_under_root(full_path):
  78. return self.not_found(environ, start_response)
  79. if path.isdir(full_path):
  80. if full_path[-1] <> '/' or full_path == self.root:
  81. location = util.request_uri(environ, include_query=False) + '/'
  82. if environ.get('QUERY_STRING'):
  83. location += '?' + environ.get('QUERY_STRING')
  84. headers = [('Location', location)]
  85. return self.moved_permanently(environ, start_response, headers)
  86. else:
  87. full_path = self._full_path(path_info + self.index_file)
  88. try:
  89. sz = int(environ['CONTENT_LENGTH'])
  90. except:
  91. sz = 0
  92. if environ['REQUEST_METHOD'] == 'PUT' and sz > 0:
  93. for putglob in self.puttable:
  94. if fnmatch(path_info, putglob):
  95. data = environ['wsgi.input'].read(sz)
  96. try:
  97. with open(full_path, "wb") as f: f.write(data)
  98. return self.success_no_content(environ, start_response)
  99. except:
  100. print sys.exc_info()[1]
  101. return self.server_error(environ, start_response)
  102. if environ['REQUEST_METHOD'] not in ('GET', 'HEAD'):
  103. headers = [('Allow', 'GET, HEAD')]
  104. return self.method_not_allowed(environ, start_response, headers)
  105. content_type = self._guess_type(full_path)
  106. try:
  107. etag, last_modified = self._conditions(full_path, environ)
  108. headers = [('Date', formatdate(time.time())),
  109. ('Last-Modified', last_modified),
  110. ('ETag', etag)]
  111. if_modified = environ.get('HTTP_IF_MODIFIED_SINCE')
  112. if if_modified and (parsedate(if_modified)
  113. >= parsedate(last_modified)):
  114. return self.not_modified(environ, start_response, headers)
  115. if_none = environ.get('HTTP_IF_NONE_MATCH')
  116. if if_none and (if_none == '*' or etag in if_none):
  117. return self.not_modified(environ, start_response, headers)
  118. file_like = self._file_like(full_path)
  119. headers.append(('Content-Type', content_type))
  120. start_response("200 OK", headers)
  121. if environ['REQUEST_METHOD'] == 'GET':
  122. return self._body(full_path, environ, file_like)
  123. else:
  124. return ['']
  125. except (IOError, OSError), e:
  126. print e
  127. return self.not_found(environ, start_response)
  128. def _full_path(self, path_info):
  129. """Return the full path from which to read."""
  130. return self.root + path_info
  131. def _is_under_root(self, full_path):
  132. """Guard against arbitrary file retrieval."""
  133. if (path.abspath(full_path) + path.sep)\
  134. .startswith(path.abspath(self.root) + path.sep):
  135. return True
  136. else:
  137. return False
  138. def _guess_type(self, full_path):
  139. """Guess the mime type using the mimetypes module."""
  140. return mimetypes.guess_type(full_path)[0] or 'text/plain'
  141. def _conditions(self, full_path, environ):
  142. """Return a tuple of etag, last_modified by mtime from stat."""
  143. mtime = stat(full_path).st_mtime
  144. return str(mtime), formatdate(mtime)
  145. def _file_like(self, full_path):
  146. """Return the appropriate file object."""
  147. return open(full_path, 'rb')
  148. def _body(self, full_path, environ, file_like):
  149. """Return an iterator over the body of the response."""
  150. way_to_send = environ.get('wsgi.file_wrapper', iter_and_close)
  151. return way_to_send(file_like, self.block_size)
  152. def iter_and_close(file_like, block_size):
  153. """Yield file contents by block then close the file."""
  154. while 1:
  155. try:
  156. block = file_like.read(block_size)
  157. if block: yield block
  158. else: raise StopIteration
  159. except StopIteration, si:
  160. file_like.close()
  161. return
  162. def cling_wrap(package_name, dir_name, **kw):
  163. """Return a Cling that serves from the given package and dir_name.
  164. This uses pkg_resources.resource_filename which is not the
  165. recommended way, since it extracts the files.
  166. I think this works fine unless you have some _very_ serious
  167. requirements for static content, in which case you probably
  168. shouldn't be serving it through a WSGI app, IMHO. YMMV.
  169. """
  170. resource = Requirement.parse(package_name)
  171. return Cling(resource_filename(resource, dir_name), **kw)
  172. def command():
  173. usage = "%prog [--help] [-d DIR] [-l [HOST][:PORT]] [-p GLOB[,GLOB...]]"
  174. parser = OptionParser(usage=usage, version="static 0.3.6")
  175. parser.add_option("-d", "--dir", dest="rootdir", default=".",
  176. help="Root directory to serve. Defaults to '.' .", metavar="DIR")
  177. parser.add_option("-l", "--listen", dest="listen", default="127.0.0.1:8888",
  178. help="Listen on this interface (given by its hostname or IP) and port."+
  179. " HOST defaults to 127.0.0.1. PORT defaults to 8888. "+
  180. "Leave HOST empty to listen on all interfaces (INSECURE!).",
  181. metavar="[HOST][:PORT]")
  182. parser.add_option("-p", "--puttable", dest="puttable", default="",
  183. help="Comma or space-separated list of request paths for which to"+
  184. " permit PUT requests. Each path is a glob pattern that may "+
  185. "contain wildcard characters '*' and/or '?'. "+
  186. "'*' matches any sequence of characters, including the empty"+
  187. " string. '?' matches exactly 1 arbitrary character. "+
  188. "NOTE: Both '*' and '?' match slashes and dots. "+
  189. "I.e. --puttable=* makes every file under DIR writable!",
  190. metavar="GLOB[,GLOB...]")
  191. parser.add_option("--validate", dest="validate", action="store_true",
  192. default=False,
  193. help="Enable HTTP validation. You don't need this unless "+
  194. "you're developing static.py itself.")
  195. options, args = parser.parse_args()
  196. if len(args) > 0:
  197. parser.print_help(sys.stderr)
  198. sys.exit(1)
  199. parts = options.listen.split(":")
  200. if len(parts) == 1:
  201. try: # if the the listen argument consists only of a port number
  202. port = int(parts[0])
  203. host = None
  204. except: # could not parse as port number => must be a host IP or name
  205. host = parts[0]
  206. port = None
  207. elif len(parts) == 2:
  208. host, port = parts
  209. else:
  210. sys.exit("Invalid host:port specification.")
  211. if not host:
  212. host = '0.0.0.0'
  213. if not port:
  214. port = 8888
  215. try:
  216. port = int(port)
  217. if port <= 0 or port > 65535: raise ValueError
  218. except:
  219. sys.exit("Invalid host:port specification.")
  220. puttable = set(path.abspath(p) for p in
  221. options.puttable.replace(","," ").split())
  222. if puttable and host not in ('127.0.0.1', 'localhost'):
  223. print("Permitting PUT access for non-localhost connections may be unwise.")
  224. options.rootdir = path.abspath(options.rootdir)
  225. for p in puttable:
  226. if not p.startswith(options.rootdir):
  227. sys.exit("puttable path '%s' not under root '%s'" % (p, options.rootdir))
  228. # cut off root prefix from puttable paths
  229. puttable = set(p[len(options.rootdir):] for p in puttable)
  230. app = Cling(options.rootdir, puttable=puttable)
  231. if options.validate:
  232. app = validator(app)
  233. try:
  234. print "Serving %s to http://%s:%d" % (options.rootdir, host, port)
  235. if puttable:
  236. print("The following paths (relative to server root) may be "+
  237. "OVERWRITTEN via HTTP PUT.")
  238. for p in puttable:
  239. print p
  240. make_server(host, port, app).serve_forever()
  241. except KeyboardInterrupt, ki:
  242. print "Ciao, baby!"
  243. except:
  244. sys.exit("Problem initializing server: %s" % sys.exc_info()[1])
  245. if __name__ == '__main__':
  246. command()