hgwebdir_mod.py
536 lines
| 18.3 KiB
| text/x-python
|
PythonLexer
|
r2391 | # hgweb/hgwebdir_mod.py - Web interface for a directory of repositories. | ||
|
r2356 | # | ||
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | ||||
|
r2859 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
|
r2356 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2356 | |||
|
r27046 | from __future__ import absolute_import | ||
|
r36929 | import gc | ||
|
r27046 | import os | ||
import time | ||||
from ..i18n import _ | ||||
from .common import ( | ||||
ErrorResponse, | ||||
HTTP_SERVER_ERROR, | ||||
|
r30766 | cspvalues, | ||
|
r27046 | get_contact, | ||
get_mtime, | ||||
ismember, | ||||
paritygen, | ||||
staticfile, | ||||
|
r36923 | statusmessage, | ||
|
r27046 | ) | ||
from .. import ( | ||||
|
r34241 | configitems, | ||
|
r27046 | encoding, | ||
error, | ||||
hg, | ||||
|
r29787 | profiling, | ||
|
r34354 | pycompat, | ||
|
r27046 | scmutil, | ||
templater, | ||||
ui as uimod, | ||||
util, | ||||
) | ||||
from . import ( | ||||
hgweb_mod, | ||||
|
r36822 | request as requestmod, | ||
|
r27046 | webutil, | ||
wsgicgi, | ||||
) | ||||
|
r36625 | from ..utils import dateutil | ||
|
r2356 | |||
|
r8215 | def cleannames(items): | ||
return [(util.pconvert(name).strip('/'), path) for name, path in items] | ||||
|
r2356 | |||
|
r8529 | def findrepos(paths): | ||
|
r9723 | repos = [] | ||
|
r8529 | for prefix, root in cleannames(paths): | ||
roothead, roottail = os.path.split(root) | ||||
|
r17104 | # "foo = /bar/*" or "foo = /bar/**" lets every repo /bar/N in or below | ||
# /bar/ be served as as foo/N . | ||||
# '*' will not search inside dirs with .hg (except .hg/patches), | ||||
# '**' will search inside dirs with .hg (and thus also find subrepos). | ||||
|
r8529 | try: | ||
recurse = {'*': False, '**': True}[roottail] | ||||
except KeyError: | ||||
|
r9723 | repos.append((prefix, root)) | ||
|
r8529 | continue | ||
|
r11677 | roothead = os.path.normpath(os.path.abspath(roothead)) | ||
|
r13975 | paths = scmutil.walkrepos(roothead, followsym=True, recurse=recurse) | ||
|
r13402 | repos.extend(urlrepos(prefix, roothead, paths)) | ||
|
r9723 | return repos | ||
|
r8529 | |||
|
r13402 | def urlrepos(prefix, roothead, paths): | ||
"""yield url paths and filesystem paths from a list of repo paths | ||||
|
r13538 | >>> conv = lambda seq: [(v, util.pconvert(p)) for v,p in seq] | ||
|
r34133 | >>> conv(urlrepos(b'hg', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt'])) | ||
|
r13403 | [('hg/r', '/opt/r'), ('hg/r/r', '/opt/r/r'), ('hg', '/opt')] | ||
|
r34133 | >>> conv(urlrepos(b'', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt'])) | ||
|
r13402 | [('r', '/opt/r'), ('r/r', '/opt/r/r'), ('', '/opt')] | ||
""" | ||||
for path in paths: | ||||
path = os.path.normpath(path) | ||||
|
r13403 | yield (prefix + '/' + | ||
util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path | ||||
|
r13402 | |||
|
r36906 | def readallowed(ui, req): | ||
"""Check allow_read and deny_read config options of a repo's ui object | ||||
to determine user permissions. By default, with neither option set (or | ||||
both empty), allow all users to read the repo. There are two ways a | ||||
user can be denied read access: (1) deny_read is not empty, and the | ||||
user is unauthenticated or deny_read contains user (or *), and (2) | ||||
allow_read is not empty and the user is not in allow_read. Return True | ||||
if user is allowed to read the repo, else return False.""" | ||||
user = req.remoteuser | ||||
deny_read = ui.configlist('web', 'deny_read', untrusted=True) | ||||
if deny_read and (not user or ismember(ui, user, deny_read)): | ||||
return False | ||||
allow_read = ui.configlist('web', 'allow_read', untrusted=True) | ||||
# by default, allow reading if no allow_read option has been set | ||||
if not allow_read or ismember(ui, user, allow_read): | ||||
return True | ||||
return False | ||||
|
r36907 | def archivelist(ui, nodeid, url): | ||
allowed = ui.configlist('web', 'allow_archive', untrusted=True) | ||||
archives = [] | ||||
for typ, spec in hgweb_mod.archivespecs.iteritems(): | ||||
if typ in allowed or ui.configbool('web', 'allow' + typ, | ||||
untrusted=True): | ||||
archives.append({ | ||||
'type': typ, | ||||
'extension': spec[2], | ||||
'node': nodeid, | ||||
'url': url, | ||||
}) | ||||
return archives | ||||
|
r36920 | def rawindexentries(ui, repos, req, subdir=''): | ||
|
r36908 | descend = ui.configbool('web', 'descend') | ||
collapse = ui.configbool('web', 'collapse') | ||||
seenrepos = set() | ||||
seendirs = set() | ||||
for name, path in repos: | ||||
if not name.startswith(subdir): | ||||
continue | ||||
name = name[len(subdir):] | ||||
directory = False | ||||
if '/' in name: | ||||
if not descend: | ||||
continue | ||||
nameparts = name.split('/') | ||||
rootname = nameparts[0] | ||||
if not collapse: | ||||
pass | ||||
elif rootname in seendirs: | ||||
continue | ||||
elif rootname in seenrepos: | ||||
pass | ||||
else: | ||||
directory = True | ||||
name = rootname | ||||
# redefine the path to refer to the directory | ||||
discarded = '/'.join(nameparts[1:]) | ||||
# remove name parts plus accompanying slash | ||||
path = path[:-len(discarded) - 1] | ||||
try: | ||||
r = hg.repository(ui, path) | ||||
directory = False | ||||
except (IOError, error.RepoError): | ||||
pass | ||||
|
r36918 | parts = [ | ||
|
r36920 | req.apppath.strip('/'), | ||
|
r36918 | subdir.strip('/'), | ||
name.strip('/'), | ||||
] | ||||
url = '/' + '/'.join(p for p in parts if p) + '/' | ||||
|
r15003 | |||
|
r36908 | # show either a directory entry or a repository | ||
if directory: | ||||
# get the directory's time information | ||||
try: | ||||
d = (get_mtime(path), dateutil.makedate()[1]) | ||||
except OSError: | ||||
continue | ||||
# add '/' to the name to make it obvious that | ||||
# the entry is a directory, not a regular repository | ||||
row = {'contact': "", | ||||
'contact_sort': "", | ||||
'name': name + '/', | ||||
'name_sort': name, | ||||
'url': url, | ||||
'description': "", | ||||
'description_sort': "", | ||||
'lastchange': d, | ||||
'lastchange_sort': d[1] - d[0], | ||||
'archives': [], | ||||
'isdirectory': True, | ||||
'labels': [], | ||||
} | ||||
seendirs.add(name) | ||||
yield row | ||||
continue | ||||
u = ui.copy() | ||||
try: | ||||
u.readconfig(os.path.join(path, '.hg', 'hgrc')) | ||||
except Exception as e: | ||||
u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e)) | ||||
continue | ||||
def get(section, name, default=uimod._unset): | ||||
return u.config(section, name, default, untrusted=True) | ||||
if u.configbool("web", "hidden", untrusted=True): | ||||
continue | ||||
if not readallowed(u, req): | ||||
continue | ||||
|
r15003 | |||
|
r36908 | # update time with local timezone | ||
try: | ||||
r = hg.repository(ui, path) | ||||
except IOError: | ||||
u.warn(_('error accessing repository at %s\n') % path) | ||||
continue | ||||
except error.RepoError: | ||||
u.warn(_('error accessing repository at %s\n') % path) | ||||
continue | ||||
try: | ||||
d = (get_mtime(r.spath), dateutil.makedate()[1]) | ||||
except OSError: | ||||
continue | ||||
contact = get_contact(get) | ||||
description = get("web", "description") | ||||
seenrepos.add(name) | ||||
name = get("web", "name", name) | ||||
row = {'contact': contact or "unknown", | ||||
'contact_sort': contact.upper() or "unknown", | ||||
'name': name, | ||||
'name_sort': name, | ||||
'url': url, | ||||
'description': description or "unknown", | ||||
'description_sort': description.upper() or "unknown", | ||||
'lastchange': d, | ||||
'lastchange_sort': d[1] - d[0], | ||||
'archives': archivelist(u, "tip", url), | ||||
'isdirectory': None, | ||||
'labels': u.configlist('web', 'labels', untrusted=True), | ||||
} | ||||
yield row | ||||
|
r36920 | def indexentries(ui, repos, req, stripecount, sortcolumn='', | ||
|
r36910 | descending=False, subdir=''): | ||
|
r36909 | |||
|
r36920 | rows = rawindexentries(ui, repos, req, subdir=subdir) | ||
|
r36909 | |||
sortdefault = None, False | ||||
if sortcolumn and sortdefault != (sortcolumn, descending): | ||||
sortkey = '%s_sort' % sortcolumn | ||||
rows = sorted(rows, key=lambda x: x[sortkey], | ||||
reverse=descending) | ||||
for row, parity in zip(rows, paritygen(stripecount)): | ||||
row['parity'] = parity | ||||
yield row | ||||
|
r15003 | |||
|
r2356 | class hgwebdir(object): | ||
|
r26132 | """HTTP server for multiple repositories. | ||
Given a configuration, different repositories will be served depending | ||||
on the request path. | ||||
Instances are typically used as WSGI applications. | ||||
""" | ||||
|
r8191 | def __init__(self, conf, baseui=None): | ||
|
r8371 | self.conf = conf | ||
self.baseui = baseui | ||||
|
r26072 | self.ui = None | ||
|
r8371 | self.lastrefresh = 0 | ||
|
r9903 | self.motd = None | ||
|
r8371 | self.refresh() | ||
|
r2356 | |||
|
r8371 | def refresh(self): | ||
|
r26072 | if self.ui: | ||
|
r34241 | refreshinterval = self.ui.configint('web', 'refreshinterval') | ||
else: | ||||
item = configitems.coreitems['web']['refreshinterval'] | ||||
refreshinterval = item.default | ||||
|
r26072 | |||
# refreshinterval <= 0 means to always refresh. | ||||
if (refreshinterval > 0 and | ||||
self.lastrefresh + refreshinterval > time.time()): | ||||
|
r8371 | return | ||
if self.baseui: | ||||
|
r11239 | u = self.baseui.copy() | ||
|
r2356 | else: | ||
|
r30559 | u = uimod.ui.load() | ||
|
r20790 | u.setconfig('ui', 'report_untrusted', 'off', 'hgwebdir') | ||
u.setconfig('ui', 'nontty', 'true', 'hgwebdir') | ||||
|
r25488 | # displaying bundling progress bar while serving feels wrong and may | ||
# break some wsgi implementations. | ||||
u.setconfig('progress', 'disable', 'true', 'hgweb') | ||||
|
r8136 | |||
|
r8529 | if not isinstance(self.conf, (dict, list, tuple)): | ||
map = {'paths': 'hgweb-paths'} | ||||
|
r13214 | if not os.path.exists(self.conf): | ||
|
r26587 | raise error.Abort(_('config file %s not found!') % self.conf) | ||
|
r11239 | u.readconfig(self.conf, remap=map, trust=True) | ||
|
r13667 | paths = [] | ||
for name, ignored in u.configitems('hgweb-paths'): | ||||
for path in u.configlist('hgweb-paths', name): | ||||
paths.append((name, path)) | ||||
|
r8529 | elif isinstance(self.conf, (list, tuple)): | ||
paths = self.conf | ||||
elif isinstance(self.conf, dict): | ||||
paths = self.conf.items() | ||||
|
r8345 | |||
|
r11239 | repos = findrepos(paths) | ||
for prefix, root in u.configitems('collections'): | ||||
prefix = util.pconvert(prefix) | ||||
|
r13975 | for path in scmutil.walkrepos(root, followsym=True): | ||
|
r11239 | repo = os.path.normpath(path) | ||
name = util.pconvert(repo) | ||||
if name.startswith(prefix): | ||||
name = name[len(prefix):] | ||||
repos.append((name.lstrip('/'), repo)) | ||||
self.repos = repos | ||||
self.ui = u | ||||
|
r34236 | encoding.encoding = self.ui.config('web', 'encoding') | ||
|
r34243 | self.style = self.ui.config('web', 'style') | ||
|
r34245 | self.templatepath = self.ui.config('web', 'templates', untrusted=False) | ||
|
r34242 | self.stripecount = self.ui.config('web', 'stripes') | ||
|
r8621 | if self.stripecount: | ||
self.stripecount = int(self.stripecount) | ||||
|
r34240 | prefix = self.ui.config('web', 'prefix') | ||
|
r18515 | if prefix.startswith('/'): | ||
prefix = prefix[1:] | ||||
if prefix.endswith('/'): | ||||
prefix = prefix[:-1] | ||||
self.prefix = prefix | ||||
|
r8371 | self.lastrefresh = time.time() | ||
|
r2356 | |||
|
r2535 | def run(self): | ||
|
r30636 | if not encoding.environ.get('GATEWAY_INTERFACE', | ||
'').startswith("CGI/1."): | ||||
|
r8663 | raise RuntimeError("This function is only intended to be " | ||
"called while running as a CGI script.") | ||||
|
r5566 | wsgicgi.launch(self) | ||
def __call__(self, env, respond): | ||||
|
r36916 | baseurl = self.ui.config('web', 'baseurl') | ||
|
r36928 | req = requestmod.parserequestfromenv(env, altbaseurl=baseurl) | ||
res = requestmod.wsgiresponse(req, respond) | ||||
|
r7336 | |||
|
r36928 | return self.run_wsgi(req, res) | ||
|
r7336 | |||
|
r36928 | def run_wsgi(self, req, res): | ||
r32788 | profile = self.ui.configbool('profiling', 'enabled') | |||
with profiling.profile(self.ui, enabled=profile): | ||||
|
r36929 | try: | ||
|
r36996 | for r in self._runwsgi(req, res): | ||
|
r36929 | yield r | ||
finally: | ||||
# There are known cycles in localrepository that prevent | ||||
# those objects (and tons of held references) from being | ||||
# collected through normal refcounting. We mitigate those | ||||
# leaks by performing an explicit GC on every request. | ||||
# TODO remove this once leaks are fixed. | ||||
# TODO only run this on requests that create localrepository | ||||
# instances instead of every request. | ||||
gc.collect() | ||||
|
r29786 | |||
|
r36928 | def _runwsgi(self, req, res): | ||
|
r5601 | try: | ||
|
r25083 | self.refresh() | ||
|
r5603 | |||
|
r30766 | csp, nonce = cspvalues(self.ui) | ||
if csp: | ||||
|
r36889 | res.headers['Content-Security-Policy'] = csp | ||
|
r30766 | |||
|
r36919 | virtual = req.dispatchpath.strip('/') | ||
|
r30766 | tmpl = self.templater(req, nonce) | ||
|
r37004 | ctype = tmpl.render('mimetype', {'encoding': encoding.encoding}) | ||
|
r5760 | |||
|
r36889 | # Global defaults. These can be overridden by any handler. | ||
res.status = '200 Script output follows' | ||||
res.headers['Content-Type'] = ctype | ||||
|
r5760 | |||
|
r25083 | # a static file | ||
|
r36881 | if virtual.startswith('static/') or 'static' in req.qsparams: | ||
|
r25083 | if virtual.startswith('static/'): | ||
fname = virtual[7:] | ||||
else: | ||||
|
r36881 | fname = req.qsparams['static'] | ||
|
r25083 | static = self.ui.config("web", "static", None, | ||
untrusted=False) | ||||
if not static: | ||||
tp = self.templatepath or templater.templatepaths() | ||||
if isinstance(tp, str): | ||||
tp = [tp] | ||||
static = [os.path.join(p, 'static') for p in tp] | ||||
|
r36889 | |||
staticfile(static, fname, res) | ||||
return res.sendresponse() | ||||
|
r5603 | |||
|
r25083 | # top-level index | ||
|
r31482 | |||
repos = dict(self.repos) | ||||
|
r32004 | if (not virtual or virtual == 'index') and virtual not in repos: | ||
|
r36921 | return self.makeindex(req, res, tmpl) | ||
|
r5601 | |||
|
r25083 | # nested indexes and hgwebs | ||
|
r6210 | |||
|
r31482 | if virtual.endswith('/index') and virtual not in repos: | ||
subdir = virtual[:-len('index')] | ||||
if any(r.startswith(subdir) for r in repos): | ||||
|
r36921 | return self.makeindex(req, res, tmpl, subdir) | ||
|
r31482 | |||
|
r32004 | def _virtualdirs(): | ||
# Check the full virtual path, each parent, and the root ('') | ||||
if virtual != '': | ||||
yield virtual | ||||
for p in util.finddirs(virtual): | ||||
yield p | ||||
yield '' | ||||
for virtualrepo in _virtualdirs(): | ||||
|
r25083 | real = repos.get(virtualrepo) | ||
if real: | ||||
|
r36913 | # Re-parse the WSGI environment to take into account our | ||
# repository path component. | ||||
|
r36928 | req = requestmod.parserequestfromenv( | ||
req.rawenv, reponame=virtualrepo, | ||||
|
r36916 | altbaseurl=self.ui.config('web', 'baseurl')) | ||
|
r25083 | try: | ||
# ensure caller gets private copy of ui | ||||
repo = hg.repository(self.ui.copy(), real) | ||||
|
r36928 | return hgweb_mod.hgweb(repo).run_wsgi(req, res) | ||
|
r25660 | except IOError as inst: | ||
|
r34024 | msg = encoding.strtolocal(inst.strerror) | ||
|
r25083 | raise ErrorResponse(HTTP_SERVER_ERROR, msg) | ||
|
r25660 | except error.RepoError as inst: | ||
|
r34354 | raise ErrorResponse(HTTP_SERVER_ERROR, bytes(inst)) | ||
|
r5601 | |||
|
r25083 | # browse subdirectories | ||
subdir = virtual + '/' | ||||
if [r for r in repos if r.startswith(subdir)]: | ||||
|
r36921 | return self.makeindex(req, res, tmpl, subdir) | ||
|
r5603 | |||
|
r25083 | # prefixes not found | ||
|
r36923 | res.status = '404 Not Found' | ||
res.setbodygen(tmpl('notfound', repo=virtual)) | ||||
return res.sendresponse() | ||||
|
r5760 | |||
|
r36923 | except ErrorResponse as e: | ||
res.status = statusmessage(e.code, pycompat.bytestr(e)) | ||||
res.setbodygen(tmpl('error', error=e.message or '')) | ||||
return res.sendresponse() | ||||
|
r5601 | finally: | ||
tmpl = None | ||||
|
r36921 | def makeindex(self, req, res, tmpl, subdir=""): | ||
|
r8371 | self.refresh() | ||
|
r5601 | sortable = ["name", "description", "contact", "lastchange"] | ||
|
r36909 | sortcolumn, descending = None, False | ||
|
r36905 | if 'sort' in req.qsparams: | ||
sortcolumn = req.qsparams['sort'] | ||||
|
r5601 | descending = sortcolumn.startswith('-') | ||
if descending: | ||||
sortcolumn = sortcolumn[1:] | ||||
if sortcolumn not in sortable: | ||||
sortcolumn = "" | ||||
|
r4841 | |||
|
r5601 | sort = [("sort_%s" % column, | ||
"%s%s" % ((not descending and column == sortcolumn) | ||||
and "-" or "", column)) | ||||
for column in sortable] | ||||
|
r5928 | |||
|
r8371 | self.refresh() | ||
|
r6221 | |||
|
r36920 | entries = indexentries(self.ui, self.repos, req, | ||
|
r36909 | self.stripecount, sortcolumn=sortcolumn, | ||
descending=descending, subdir=subdir) | ||||
|
r6221 | |||
|
r36921 | res.setbodygen(tmpl( | ||
'index', | ||||
entries=entries, | ||||
subdir=subdir, | ||||
pathdef=hgweb_mod.makebreadcrumb('/' + subdir, self.prefix), | ||||
sortcolumn=sortcolumn, | ||||
descending=descending, | ||||
**dict(sort))) | ||||
return res.sendresponse() | ||||
|
r5602 | |||
|
r30766 | def templater(self, req, nonce): | ||
|
r5602 | |||
def motd(**map): | ||||
if self.motd is not None: | ||||
yield self.motd | ||||
else: | ||||
|
r34588 | yield config('web', 'motd') | ||
|
r5602 | |||
|
r34223 | def config(section, name, default=uimod._unset, untrusted=True): | ||
|
r8191 | return self.ui.config(section, name, default, untrusted) | ||
|
r5602 | |||
|
r8216 | vars = {} | ||
|
r34516 | styles, (style, mapfile) = hgweb_mod.getstyle(req, config, | ||
self.templatepath) | ||||
|
r9842 | if style == styles[0]: | ||
vars['style'] = style | ||||
|
r10282 | |||
|
r36823 | sessionvars = webutil.sessionvars(vars, r'?') | ||
|
r34613 | logourl = config('web', 'logourl') | ||
|
r34612 | logoimg = config('web', 'logoimg') | ||
|
r36911 | staticurl = (config('web', 'staticurl') | ||
|
r36922 | or req.apppath + '/static/') | ||
|
r5602 | if not staticurl.endswith('/'): | ||
staticurl += '/' | ||||
|
r28954 | defaults = { | ||
"encoding": encoding.encoding, | ||||
"motd": motd, | ||||
|
r36922 | "url": req.apppath + '/', | ||
|
r28954 | "logourl": logourl, | ||
"logoimg": logoimg, | ||||
"staticurl": staticurl, | ||||
"sessionvars": sessionvars, | ||||
"style": style, | ||||
|
r30766 | "nonce": nonce, | ||
|
r28954 | } | ||
tmpl = templater.templater.frommapfile(mapfile, defaults=defaults) | ||||
|
r5602 | return tmpl | ||