3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
7 export "BUP_ARGV_${arg_i}"="$arg"
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/../../config/bin/python" || exit $?
13 exec "$bup_python" "$0"
17 from __future__ import absolute_import, print_function
18 from collections import namedtuple
19 import mimetypes, os, posixpath, signal, stat, sys, time, urllib, webbrowser
20 from binascii import hexlify
22 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
24 from bup import compat, options, git, vfs
25 from bup.helpers import (chunkyreader, debug1, format_filesize, handle_ctrl_c,
27 from bup.metadata import Metadata
28 from bup.path import resource_path
29 from bup.repo import LocalRepo
30 from bup.io import path_msg
33 from tornado import gen
34 from tornado.httpserver import HTTPServer
35 from tornado.ioloop import IOLoop
36 from tornado.netutil import bind_unix_socket
39 log('error: cannot find the python "tornado" module; please install it\n')
43 # FIXME: right now the way hidden files are handled causes every
44 # directory to be traversed twice.
49 def http_date_from_utc_ns(utc_ns):
50 return time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime(utc_ns / 10**9))
53 def _compute_breadcrumbs(path, show_hidden=False):
54 """Returns a list of breadcrumb objects for a path."""
56 breadcrumbs.append((b'[root]', b'/'))
57 path_parts = path.split(b'/')[1:-1]
59 for part in path_parts:
60 full_path += part + b"/"
63 url_append = b'?hidden=1'
64 breadcrumbs.append((part, full_path+url_append))
68 def _contains_hidden_files(repo, dir_item):
69 """Return true if the directory contains items with names other than
70 '.' and '..' that begin with '.'
73 for name, item in vfs.contents(repo, dir_item, want_meta=False):
74 if name in (b'.', b'..'):
76 if name.startswith(b'.'):
81 def _dir_contents(repo, resolution, show_hidden=False):
82 """Yield the display information for the contents of dir_item."""
84 url_query = b'?hidden=1' if show_hidden else b''
86 def display_info(name, item, resolved_item, display_name=None):
87 # link should be based on fully resolved type to avoid extra
89 link = tornado.escape.url_escape(name, plus=False)
90 if stat.S_ISDIR(vfs.item_mode(resolved_item)):
92 link = link.encode('ascii')
94 size = vfs.item_size(repo, item)
95 if opt.human_readable:
96 display_size = format_filesize(size)
101 mode = vfs.item_mode(item)
102 if stat.S_ISDIR(mode):
103 display_name = name + b'/'
104 elif stat.S_ISLNK(mode):
105 display_name = name + b'@'
109 return display_name, link + url_query, display_size
111 dir_item = resolution[-1][1]
112 for name, item in vfs.contents(repo, dir_item):
114 if (name not in (b'.', b'..')) and name.startswith(b'.'):
117 yield display_info(name, item, item, b'.')
118 parent_item = resolution[-2][1] if len(resolution) > 1 else dir_item
119 yield display_info(b'..', parent_item, parent_item, b'..')
121 res = vfs.try_resolve(repo, name, parent=resolution, want_meta=False)
122 res_name, res_item = res[-1]
123 yield display_info(name, item, res_item)
126 class BupRequestHandler(tornado.web.RequestHandler):
128 def initialize(self, repo=None):
131 def decode_argument(self, value, name=None):
134 return super(BupRequestHandler, self).decode_argument(value, name)
137 return self._process_request(path)
139 def head(self, path):
140 return self._process_request(path)
142 def _process_request(self, path):
143 print('Handling request for %s' % path)
145 # Set want_meta because dir metadata won't be fetched, and if
146 # it's not a dir, then we're going to want the metadata.
147 res = vfs.resolve(self.repo, path, want_meta=True)
148 leaf_name, leaf_item = res[-1]
152 mode = vfs.item_mode(leaf_item)
153 if stat.S_ISDIR(mode):
154 self._list_directory(path, res)
156 self._get_file(self.repo, path, res)
158 def _list_directory(self, path, resolution):
159 """Helper to produce a directory listing.
161 Return value is either a file object, or None (indicating an
162 error). In either case, the headers are sent.
164 if not path.endswith(b'/') and len(path) > 0:
165 print('Redirecting from %s to %s' % (path_msg(path), path_msg(path + b'/')))
166 return self.redirect(path + b'/', permanent=True)
168 hidden_arg = self.request.arguments.get('hidden', [0])[-1]
170 show_hidden = int(hidden_arg)
171 except ValueError as e:
175 'list-directory.html',
177 breadcrumbs=_compute_breadcrumbs(path, show_hidden),
178 files_hidden=_contains_hidden_files(self.repo, resolution[-1][1]),
179 hidden_shown=show_hidden,
180 dir_contents=_dir_contents(self.repo, resolution,
181 show_hidden=show_hidden))
184 def _get_file(self, repo, path, resolved):
185 """Process a request on a file.
187 Return value is either a file object, or None (indicating an error).
188 In either case, the headers are sent.
190 file_item = resolved[-1][1]
191 file_item = vfs.augment_item_meta(repo, file_item, include_size=True)
192 meta = file_item.meta
193 ctype = self._guess_type(path)
194 self.set_header("Last-Modified", http_date_from_utc_ns(meta.mtime))
195 self.set_header("Content-Type", ctype)
197 self.set_header("Content-Length", str(meta.size))
198 assert len(file_item.oid) == 20
199 self.set_header("Etag", hexlify(file_item.oid))
200 if self.request.method != 'HEAD':
201 with vfs.fopen(self.repo, file_item) as f:
203 for blob in chunkyreader(f):
207 def _guess_type(self, path):
208 """Guess the type of a file.
210 Argument is a PATH (a filename).
212 Return value is a string of the form type/subtype,
213 usable for a MIME Content-type header.
215 The default implementation looks the file's extension
216 up in the table self.extensions_map, using application/octet-stream
217 as a default; however it would be permissible (if
218 slow) to look inside the data to make a better guess.
220 base, ext = posixpath.splitext(path)
221 if ext in self.extensions_map:
222 return self.extensions_map[ext]
224 if ext in self.extensions_map:
225 return self.extensions_map[ext]
227 return self.extensions_map['']
229 if not mimetypes.inited:
230 mimetypes.init() # try to read system mime.types
231 extensions_map = mimetypes.types_map.copy()
232 extensions_map.update({
233 '': 'text/plain', # Default
242 def handle_sigterm(signum, frame):
244 debug1('\nbup-web: signal %d received\n' % signum)
245 log('Shutdown requested\n')
251 signal.signal(signal.SIGTERM, handle_sigterm)
253 UnixAddress = namedtuple('UnixAddress', ['path'])
254 InetAddress = namedtuple('InetAddress', ['host', 'port'])
257 bup web [[hostname]:port]
260 human-readable display human readable file sizes (i.e. 3.9K, 4.7M)
261 browser show repository in default browser (incompatible with unix://)
263 o = options.Options(optspec)
264 opt, flags, extra = o.parse(compat.argv[1:])
267 o.fatal("at most one argument expected")
270 address = InetAddress(host='127.0.0.1', port=8080)
273 if bind_url.startswith('unix://'):
274 address = UnixAddress(path=bind_url[len('unix://'):])
276 addr_parts = extra[0].split(':', 1)
277 if len(addr_parts) == 1:
281 host, port = addr_parts
284 except (TypeError, ValueError) as ex:
285 o.fatal('port must be an integer, not %r' % port)
286 address = InetAddress(host=host, port=port)
288 git.check_repo_or_die()
292 template_path = resource_path(b'web').decode('utf-8'),
293 static_path = resource_path(b'web/static').decode('utf-8'),
296 # Disable buffering on stdout, for debug messages
298 sys.stdout._line_buffering = True
299 except AttributeError:
300 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
302 application = tornado.web.Application([
303 (r"(?P<path>/.*)", BupRequestHandler, dict(repo=LocalRepo())),
306 http_server = HTTPServer(application)
307 io_loop_pending = IOLoop.instance()
309 if isinstance(address, InetAddress):
310 sockets = tornado.netutil.bind_sockets(address.port, address.host)
311 http_server.add_sockets(sockets)
312 print('Serving HTTP on %s:%d...' % sockets[0].getsockname())
314 browser_addr = 'http://' + address[0] + ':' + str(address[1])
315 io_loop_pending.add_callback(lambda : webbrowser.open(browser_addr))
316 elif isinstance(address, UnixAddress):
317 unix_socket = bind_unix_socket(address.path)
318 http_server.add_socket(unix_socket)
319 print('Serving HTTP on filesystem socket %r' % address.path)
321 log('error: unexpected address %r', address)
324 io_loop = io_loop_pending
328 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))