X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=cmd%2Fweb-cmd.py;h=eeb9107d3e93e19e98c2c1bb61a7cf68d571d82b;hb=6b6559e405d264d4127211b935b21a3dda93ad93;hp=29523158ee0882065ceeb39322423781dabedb80;hpb=98805befbcccd27869d9f5f70132a3e847477d75;p=bup.git diff --git a/cmd/web-cmd.py b/cmd/web-cmd.py index 2952315..eeb9107 100755 --- a/cmd/web-cmd.py +++ b/cmd/web-cmd.py @@ -1,137 +1,195 @@ -#!/usr/bin/env python -import sys, stat, cgi, shutil, urllib, mimetypes, posixpath -import BaseHTTPServer +#!/bin/sh +"""": # -*-python-*- +bup_python="$(dirname "$0")/bup-python" || exit $? +exec "$bup_python" "$0" ${1+"$@"} +""" +# end of bup preamble + +from __future__ import absolute_import, print_function +from collections import namedtuple +import mimetypes, os, posixpath, signal, stat, sys, time, urllib, webbrowser + from bup import options, git, vfs -from bup.helpers import * +from bup.helpers import (chunkyreader, debug1, format_filesize, handle_ctrl_c, + log, saved_errors) +from bup.metadata import Metadata +from bup.path import resource_path +from bup.repo import LocalRepo + try: - from cStringIO import StringIO + from tornado import gen + from tornado.httpserver import HTTPServer + from tornado.ioloop import IOLoop + from tornado.netutil import bind_unix_socket + import tornado.web except ImportError: - from StringIO import StringIO + log('error: cannot find the python "tornado" module; please install it\n') + sys.exit(1) + + +# FIXME: right now the way hidden files are handled causes every +# directory to be traversed twice. handle_ctrl_c() -class BupHTTPServer(BaseHTTPServer.HTTPServer): - def handle_error(self, request, client_address): - # If we get a KeyboardInterrupt error than just reraise it - # so that we cause the server to exit. - if sys.exc_info()[0] == KeyboardInterrupt: - raise -class BupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): - server_version = 'BupHTTP/%s' % version_tag() - protocol_version = 'HTTP/1.1' - def do_GET(self): - self._process_request() +def http_date_from_utc_ns(utc_ns): + return time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime(utc_ns / 10**9)) - def do_HEAD(self): - self._process_request() - def _process_request(self): - """Common code for GET and HEAD commands. +def _compute_breadcrumbs(path, show_hidden=False): + """Returns a list of breadcrumb objects for a path.""" + breadcrumbs = [] + breadcrumbs.append(('[root]', '/')) + path_parts = path.split('/')[1:-1] + full_path = '/' + for part in path_parts: + full_path += part + "/" + url_append = "" + if show_hidden: + url_append = '?hidden=1' + breadcrumbs.append((part, full_path+url_append)) + return breadcrumbs - This sends the response code and MIME headers along with the content - of the response. - """ - path = urllib.unquote(self.path) - try: - n = top.resolve(path) - except vfs.NoSuchFile: + +def _contains_hidden_files(repo, dir_item): + """Return true if the directory contains items with names other than + '.' and '..' that begin with '.' + + """ + for name, item in vfs.contents(repo, dir_item, want_meta=False): + if name in ('.', '..'): + continue + if name.startswith('.'): + return True + return False + + +def _dir_contents(repo, resolution, show_hidden=False): + """Yield the display information for the contents of dir_item.""" + + url_query = '?hidden=1' if show_hidden else '' + + def display_info(name, item, resolved_item, display_name=None): + # link should be based on fully resolved type to avoid extra + # HTTP redirect. + if stat.S_ISDIR(vfs.item_mode(resolved_item)): + link = urllib.quote(name) + '/' + else: + link = urllib.quote(name) + + size = vfs.item_size(repo, item) + if opt.human_readable: + display_size = format_filesize(size) + else: + display_size = size + + if not display_name: + mode = vfs.item_mode(item) + if stat.S_ISDIR(mode): + display_name = name + '/' + elif stat.S_ISLNK(mode): + display_name = name + '@' + else: + display_name = name + + return display_name, link + url_query, display_size + + dir_item = resolution[-1][1] + for name, item in vfs.contents(repo, dir_item): + if not show_hidden: + if (name not in ('.', '..')) and name.startswith('.'): + continue + if name == '.': + yield display_info(name, item, item, '.') + parent_item = resolution[-2][1] if len(resolution) > 1 else dir_item + yield display_info('..', parent_item, parent_item, '..') + continue + res = vfs.try_resolve(repo, name, parent=resolution, want_meta=False) + res_name, res_item = res[-1] + yield display_info(name, item, res_item) + + +class BupRequestHandler(tornado.web.RequestHandler): + + def initialize(self, repo=None): + self.repo = repo + + def decode_argument(self, value, name=None): + if name == 'path': + return value + return super(BupRequestHandler, self).decode_argument(value, name) + + def get(self, path): + return self._process_request(path) + + def head(self, path): + return self._process_request(path) + + def _process_request(self, path): + path = urllib.unquote(path) + print('Handling request for %s' % path) + # Set want_meta because dir metadata won't be fetched, and if + # it's not a dir, then we're going to want the metadata. + res = vfs.resolve(self.repo, path, want_meta=True) + leaf_name, leaf_item = res[-1] + if not leaf_item: self.send_error(404) return - f = None - if stat.S_ISDIR(n.mode): - self._list_directory(path, n) + mode = vfs.item_mode(leaf_item) + if stat.S_ISDIR(mode): + self._list_directory(path, res) else: - self._get_file(path, n) + self._get_file(self.repo, path, res) - def _list_directory(self, path, n): + def _list_directory(self, path, resolution): """Helper to produce a directory listing. Return value is either a file object, or None (indicating an error). In either case, the headers are sent. """ - if not path.endswith('/'): - # redirect browser - doing basically what apache does - self.send_response(301) - self.send_header("Location", path + "/") - self.send_header("Content-Length", 0) - self.end_headers() - return + if not path.endswith('/') and len(path) > 0: + print('Redirecting from %s to %s' % (path, path + '/')) + return self.redirect(path + '/', permanent=True) - # Note that it is necessary to buffer the output into a StringIO here - # so that we can compute the content length before we send the - # content. The only other option would be to do chunked encoding, or - # not support content length. - f = StringIO() - displaypath = cgi.escape(path) - f.write(""" - -
-Name | -Size | - |
---|---|---|
%s | -%s | -