##// END OF EJS Templates
hgweb: rewrite path generation for index entries...
Gregory Szorc -
r36918:e473a032 default
parent child Browse files
Show More
@@ -1,525 +1,525 b''
1 1 # hgweb/hgwebdir_mod.py - Web interface for a directory of repositories.
2 2 #
3 3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 4 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 from __future__ import absolute_import
10 10
11 11 import os
12 import re
13 12 import time
14 13
15 14 from ..i18n import _
16 15
17 16 from .common import (
18 17 ErrorResponse,
19 18 HTTP_NOT_FOUND,
20 19 HTTP_OK,
21 20 HTTP_SERVER_ERROR,
22 21 cspvalues,
23 22 get_contact,
24 23 get_mtime,
25 24 ismember,
26 25 paritygen,
27 26 staticfile,
28 27 )
29 28
30 29 from .. import (
31 30 configitems,
32 31 encoding,
33 32 error,
34 33 hg,
35 34 profiling,
36 35 scmutil,
37 36 templater,
38 37 ui as uimod,
39 38 util,
40 39 )
41 40
42 41 from . import (
43 42 hgweb_mod,
44 43 request as requestmod,
45 44 webutil,
46 45 wsgicgi,
47 46 )
48 47 from ..utils import dateutil
49 48
50 49 def cleannames(items):
51 50 return [(util.pconvert(name).strip('/'), path) for name, path in items]
52 51
53 52 def findrepos(paths):
54 53 repos = []
55 54 for prefix, root in cleannames(paths):
56 55 roothead, roottail = os.path.split(root)
57 56 # "foo = /bar/*" or "foo = /bar/**" lets every repo /bar/N in or below
58 57 # /bar/ be served as as foo/N .
59 58 # '*' will not search inside dirs with .hg (except .hg/patches),
60 59 # '**' will search inside dirs with .hg (and thus also find subrepos).
61 60 try:
62 61 recurse = {'*': False, '**': True}[roottail]
63 62 except KeyError:
64 63 repos.append((prefix, root))
65 64 continue
66 65 roothead = os.path.normpath(os.path.abspath(roothead))
67 66 paths = scmutil.walkrepos(roothead, followsym=True, recurse=recurse)
68 67 repos.extend(urlrepos(prefix, roothead, paths))
69 68 return repos
70 69
71 70 def urlrepos(prefix, roothead, paths):
72 71 """yield url paths and filesystem paths from a list of repo paths
73 72
74 73 >>> conv = lambda seq: [(v, util.pconvert(p)) for v,p in seq]
75 74 >>> conv(urlrepos(b'hg', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt']))
76 75 [('hg/r', '/opt/r'), ('hg/r/r', '/opt/r/r'), ('hg', '/opt')]
77 76 >>> conv(urlrepos(b'', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt']))
78 77 [('r', '/opt/r'), ('r/r', '/opt/r/r'), ('', '/opt')]
79 78 """
80 79 for path in paths:
81 80 path = os.path.normpath(path)
82 81 yield (prefix + '/' +
83 82 util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
84 83
85 84 def readallowed(ui, req):
86 85 """Check allow_read and deny_read config options of a repo's ui object
87 86 to determine user permissions. By default, with neither option set (or
88 87 both empty), allow all users to read the repo. There are two ways a
89 88 user can be denied read access: (1) deny_read is not empty, and the
90 89 user is unauthenticated or deny_read contains user (or *), and (2)
91 90 allow_read is not empty and the user is not in allow_read. Return True
92 91 if user is allowed to read the repo, else return False."""
93 92
94 93 user = req.remoteuser
95 94
96 95 deny_read = ui.configlist('web', 'deny_read', untrusted=True)
97 96 if deny_read and (not user or ismember(ui, user, deny_read)):
98 97 return False
99 98
100 99 allow_read = ui.configlist('web', 'allow_read', untrusted=True)
101 100 # by default, allow reading if no allow_read option has been set
102 101 if not allow_read or ismember(ui, user, allow_read):
103 102 return True
104 103
105 104 return False
106 105
107 106 def archivelist(ui, nodeid, url):
108 107 allowed = ui.configlist('web', 'allow_archive', untrusted=True)
109 108 archives = []
110 109
111 110 for typ, spec in hgweb_mod.archivespecs.iteritems():
112 111 if typ in allowed or ui.configbool('web', 'allow' + typ,
113 112 untrusted=True):
114 113 archives.append({
115 114 'type': typ,
116 115 'extension': spec[2],
117 116 'node': nodeid,
118 117 'url': url,
119 118 })
120 119
121 120 return archives
122 121
123 122 def rawindexentries(ui, repos, wsgireq, req, subdir=''):
124 123 descend = ui.configbool('web', 'descend')
125 124 collapse = ui.configbool('web', 'collapse')
126 125 seenrepos = set()
127 126 seendirs = set()
128 127 for name, path in repos:
129 128
130 129 if not name.startswith(subdir):
131 130 continue
132 131 name = name[len(subdir):]
133 132 directory = False
134 133
135 134 if '/' in name:
136 135 if not descend:
137 136 continue
138 137
139 138 nameparts = name.split('/')
140 139 rootname = nameparts[0]
141 140
142 141 if not collapse:
143 142 pass
144 143 elif rootname in seendirs:
145 144 continue
146 145 elif rootname in seenrepos:
147 146 pass
148 147 else:
149 148 directory = True
150 149 name = rootname
151 150
152 151 # redefine the path to refer to the directory
153 152 discarded = '/'.join(nameparts[1:])
154 153
155 154 # remove name parts plus accompanying slash
156 155 path = path[:-len(discarded) - 1]
157 156
158 157 try:
159 158 r = hg.repository(ui, path)
160 159 directory = False
161 160 except (IOError, error.RepoError):
162 161 pass
163 162
164 parts = [name]
165 parts.insert(0, '/' + subdir.rstrip('/'))
166 if wsgireq.env['SCRIPT_NAME']:
167 parts.insert(0, wsgireq.env['SCRIPT_NAME'])
168 url = re.sub(r'/+', '/', '/'.join(parts) + '/')
163 parts = [
164 wsgireq.req.apppath.strip('/'),
165 subdir.strip('/'),
166 name.strip('/'),
167 ]
168 url = '/' + '/'.join(p for p in parts if p) + '/'
169 169
170 170 # show either a directory entry or a repository
171 171 if directory:
172 172 # get the directory's time information
173 173 try:
174 174 d = (get_mtime(path), dateutil.makedate()[1])
175 175 except OSError:
176 176 continue
177 177
178 178 # add '/' to the name to make it obvious that
179 179 # the entry is a directory, not a regular repository
180 180 row = {'contact': "",
181 181 'contact_sort': "",
182 182 'name': name + '/',
183 183 'name_sort': name,
184 184 'url': url,
185 185 'description': "",
186 186 'description_sort': "",
187 187 'lastchange': d,
188 188 'lastchange_sort': d[1] - d[0],
189 189 'archives': [],
190 190 'isdirectory': True,
191 191 'labels': [],
192 192 }
193 193
194 194 seendirs.add(name)
195 195 yield row
196 196 continue
197 197
198 198 u = ui.copy()
199 199 try:
200 200 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
201 201 except Exception as e:
202 202 u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e))
203 203 continue
204 204
205 205 def get(section, name, default=uimod._unset):
206 206 return u.config(section, name, default, untrusted=True)
207 207
208 208 if u.configbool("web", "hidden", untrusted=True):
209 209 continue
210 210
211 211 if not readallowed(u, req):
212 212 continue
213 213
214 214 # update time with local timezone
215 215 try:
216 216 r = hg.repository(ui, path)
217 217 except IOError:
218 218 u.warn(_('error accessing repository at %s\n') % path)
219 219 continue
220 220 except error.RepoError:
221 221 u.warn(_('error accessing repository at %s\n') % path)
222 222 continue
223 223 try:
224 224 d = (get_mtime(r.spath), dateutil.makedate()[1])
225 225 except OSError:
226 226 continue
227 227
228 228 contact = get_contact(get)
229 229 description = get("web", "description")
230 230 seenrepos.add(name)
231 231 name = get("web", "name", name)
232 232 row = {'contact': contact or "unknown",
233 233 'contact_sort': contact.upper() or "unknown",
234 234 'name': name,
235 235 'name_sort': name,
236 236 'url': url,
237 237 'description': description or "unknown",
238 238 'description_sort': description.upper() or "unknown",
239 239 'lastchange': d,
240 240 'lastchange_sort': d[1] - d[0],
241 241 'archives': archivelist(u, "tip", url),
242 242 'isdirectory': None,
243 243 'labels': u.configlist('web', 'labels', untrusted=True),
244 244 }
245 245
246 246 yield row
247 247
248 248 def indexentries(ui, repos, wsgireq, req, stripecount, sortcolumn='',
249 249 descending=False, subdir=''):
250 250
251 251 rows = rawindexentries(ui, repos, wsgireq, req, subdir=subdir)
252 252
253 253 sortdefault = None, False
254 254
255 255 if sortcolumn and sortdefault != (sortcolumn, descending):
256 256 sortkey = '%s_sort' % sortcolumn
257 257 rows = sorted(rows, key=lambda x: x[sortkey],
258 258 reverse=descending)
259 259
260 260 for row, parity in zip(rows, paritygen(stripecount)):
261 261 row['parity'] = parity
262 262 yield row
263 263
264 264 class hgwebdir(object):
265 265 """HTTP server for multiple repositories.
266 266
267 267 Given a configuration, different repositories will be served depending
268 268 on the request path.
269 269
270 270 Instances are typically used as WSGI applications.
271 271 """
272 272 def __init__(self, conf, baseui=None):
273 273 self.conf = conf
274 274 self.baseui = baseui
275 275 self.ui = None
276 276 self.lastrefresh = 0
277 277 self.motd = None
278 278 self.refresh()
279 279
280 280 def refresh(self):
281 281 if self.ui:
282 282 refreshinterval = self.ui.configint('web', 'refreshinterval')
283 283 else:
284 284 item = configitems.coreitems['web']['refreshinterval']
285 285 refreshinterval = item.default
286 286
287 287 # refreshinterval <= 0 means to always refresh.
288 288 if (refreshinterval > 0 and
289 289 self.lastrefresh + refreshinterval > time.time()):
290 290 return
291 291
292 292 if self.baseui:
293 293 u = self.baseui.copy()
294 294 else:
295 295 u = uimod.ui.load()
296 296 u.setconfig('ui', 'report_untrusted', 'off', 'hgwebdir')
297 297 u.setconfig('ui', 'nontty', 'true', 'hgwebdir')
298 298 # displaying bundling progress bar while serving feels wrong and may
299 299 # break some wsgi implementations.
300 300 u.setconfig('progress', 'disable', 'true', 'hgweb')
301 301
302 302 if not isinstance(self.conf, (dict, list, tuple)):
303 303 map = {'paths': 'hgweb-paths'}
304 304 if not os.path.exists(self.conf):
305 305 raise error.Abort(_('config file %s not found!') % self.conf)
306 306 u.readconfig(self.conf, remap=map, trust=True)
307 307 paths = []
308 308 for name, ignored in u.configitems('hgweb-paths'):
309 309 for path in u.configlist('hgweb-paths', name):
310 310 paths.append((name, path))
311 311 elif isinstance(self.conf, (list, tuple)):
312 312 paths = self.conf
313 313 elif isinstance(self.conf, dict):
314 314 paths = self.conf.items()
315 315
316 316 repos = findrepos(paths)
317 317 for prefix, root in u.configitems('collections'):
318 318 prefix = util.pconvert(prefix)
319 319 for path in scmutil.walkrepos(root, followsym=True):
320 320 repo = os.path.normpath(path)
321 321 name = util.pconvert(repo)
322 322 if name.startswith(prefix):
323 323 name = name[len(prefix):]
324 324 repos.append((name.lstrip('/'), repo))
325 325
326 326 self.repos = repos
327 327 self.ui = u
328 328 encoding.encoding = self.ui.config('web', 'encoding')
329 329 self.style = self.ui.config('web', 'style')
330 330 self.templatepath = self.ui.config('web', 'templates', untrusted=False)
331 331 self.stripecount = self.ui.config('web', 'stripes')
332 332 if self.stripecount:
333 333 self.stripecount = int(self.stripecount)
334 334 prefix = self.ui.config('web', 'prefix')
335 335 if prefix.startswith('/'):
336 336 prefix = prefix[1:]
337 337 if prefix.endswith('/'):
338 338 prefix = prefix[:-1]
339 339 self.prefix = prefix
340 340 self.lastrefresh = time.time()
341 341
342 342 def run(self):
343 343 if not encoding.environ.get('GATEWAY_INTERFACE',
344 344 '').startswith("CGI/1."):
345 345 raise RuntimeError("This function is only intended to be "
346 346 "called while running as a CGI script.")
347 347 wsgicgi.launch(self)
348 348
349 349 def __call__(self, env, respond):
350 350 baseurl = self.ui.config('web', 'baseurl')
351 351 wsgireq = requestmod.wsgirequest(env, respond, altbaseurl=baseurl)
352 352 return self.run_wsgi(wsgireq)
353 353
354 354 def run_wsgi(self, wsgireq):
355 355 profile = self.ui.configbool('profiling', 'enabled')
356 356 with profiling.profile(self.ui, enabled=profile):
357 357 for r in self._runwsgi(wsgireq):
358 358 yield r
359 359
360 360 def _runwsgi(self, wsgireq):
361 361 req = wsgireq.req
362 362 res = wsgireq.res
363 363
364 364 try:
365 365 self.refresh()
366 366
367 367 csp, nonce = cspvalues(self.ui)
368 368 if csp:
369 369 res.headers['Content-Security-Policy'] = csp
370 370 wsgireq.headers.append(('Content-Security-Policy', csp))
371 371
372 372 virtual = wsgireq.env.get("PATH_INFO", "").strip('/')
373 373 tmpl = self.templater(wsgireq, nonce)
374 374 ctype = tmpl('mimetype', encoding=encoding.encoding)
375 375 ctype = templater.stringify(ctype)
376 376
377 377 # Global defaults. These can be overridden by any handler.
378 378 res.status = '200 Script output follows'
379 379 res.headers['Content-Type'] = ctype
380 380
381 381 # a static file
382 382 if virtual.startswith('static/') or 'static' in req.qsparams:
383 383 if virtual.startswith('static/'):
384 384 fname = virtual[7:]
385 385 else:
386 386 fname = req.qsparams['static']
387 387 static = self.ui.config("web", "static", None,
388 388 untrusted=False)
389 389 if not static:
390 390 tp = self.templatepath or templater.templatepaths()
391 391 if isinstance(tp, str):
392 392 tp = [tp]
393 393 static = [os.path.join(p, 'static') for p in tp]
394 394
395 395 staticfile(static, fname, res)
396 396 return res.sendresponse()
397 397
398 398 # top-level index
399 399
400 400 repos = dict(self.repos)
401 401
402 402 if (not virtual or virtual == 'index') and virtual not in repos:
403 403 wsgireq.respond(HTTP_OK, ctype)
404 404 return self.makeindex(wsgireq, tmpl)
405 405
406 406 # nested indexes and hgwebs
407 407
408 408 if virtual.endswith('/index') and virtual not in repos:
409 409 subdir = virtual[:-len('index')]
410 410 if any(r.startswith(subdir) for r in repos):
411 411 wsgireq.respond(HTTP_OK, ctype)
412 412 return self.makeindex(wsgireq, tmpl, subdir)
413 413
414 414 def _virtualdirs():
415 415 # Check the full virtual path, each parent, and the root ('')
416 416 if virtual != '':
417 417 yield virtual
418 418
419 419 for p in util.finddirs(virtual):
420 420 yield p
421 421
422 422 yield ''
423 423
424 424 for virtualrepo in _virtualdirs():
425 425 real = repos.get(virtualrepo)
426 426 if real:
427 427 # Re-parse the WSGI environment to take into account our
428 428 # repository path component.
429 429 wsgireq.req = requestmod.parserequestfromenv(
430 430 wsgireq.env, wsgireq.req.bodyfh, reponame=virtualrepo,
431 431 altbaseurl=self.ui.config('web', 'baseurl'))
432 432 try:
433 433 # ensure caller gets private copy of ui
434 434 repo = hg.repository(self.ui.copy(), real)
435 435 return hgweb_mod.hgweb(repo).run_wsgi(wsgireq)
436 436 except IOError as inst:
437 437 msg = encoding.strtolocal(inst.strerror)
438 438 raise ErrorResponse(HTTP_SERVER_ERROR, msg)
439 439 except error.RepoError as inst:
440 440 raise ErrorResponse(HTTP_SERVER_ERROR, bytes(inst))
441 441
442 442 # browse subdirectories
443 443 subdir = virtual + '/'
444 444 if [r for r in repos if r.startswith(subdir)]:
445 445 wsgireq.respond(HTTP_OK, ctype)
446 446 return self.makeindex(wsgireq, tmpl, subdir)
447 447
448 448 # prefixes not found
449 449 wsgireq.respond(HTTP_NOT_FOUND, ctype)
450 450 return tmpl("notfound", repo=virtual)
451 451
452 452 except ErrorResponse as err:
453 453 wsgireq.respond(err, ctype)
454 454 return tmpl('error', error=err.message or '')
455 455 finally:
456 456 tmpl = None
457 457
458 458 def makeindex(self, wsgireq, tmpl, subdir=""):
459 459 req = wsgireq.req
460 460
461 461 self.refresh()
462 462 sortable = ["name", "description", "contact", "lastchange"]
463 463 sortcolumn, descending = None, False
464 464 if 'sort' in req.qsparams:
465 465 sortcolumn = req.qsparams['sort']
466 466 descending = sortcolumn.startswith('-')
467 467 if descending:
468 468 sortcolumn = sortcolumn[1:]
469 469 if sortcolumn not in sortable:
470 470 sortcolumn = ""
471 471
472 472 sort = [("sort_%s" % column,
473 473 "%s%s" % ((not descending and column == sortcolumn)
474 474 and "-" or "", column))
475 475 for column in sortable]
476 476
477 477 self.refresh()
478 478
479 479 entries = indexentries(self.ui, self.repos, wsgireq, req,
480 480 self.stripecount, sortcolumn=sortcolumn,
481 481 descending=descending, subdir=subdir)
482 482
483 483 return tmpl("index", entries=entries, subdir=subdir,
484 484 pathdef=hgweb_mod.makebreadcrumb('/' + subdir, self.prefix),
485 485 sortcolumn=sortcolumn, descending=descending,
486 486 **dict(sort))
487 487
488 488 def templater(self, wsgireq, nonce):
489 489
490 490 def motd(**map):
491 491 if self.motd is not None:
492 492 yield self.motd
493 493 else:
494 494 yield config('web', 'motd')
495 495
496 496 def config(section, name, default=uimod._unset, untrusted=True):
497 497 return self.ui.config(section, name, default, untrusted)
498 498
499 499 vars = {}
500 500 styles, (style, mapfile) = hgweb_mod.getstyle(wsgireq.req, config,
501 501 self.templatepath)
502 502 if style == styles[0]:
503 503 vars['style'] = style
504 504
505 505 sessionvars = webutil.sessionvars(vars, r'?')
506 506 logourl = config('web', 'logourl')
507 507 logoimg = config('web', 'logoimg')
508 508 staticurl = (config('web', 'staticurl')
509 509 or wsgireq.req.apppath + '/static/')
510 510 if not staticurl.endswith('/'):
511 511 staticurl += '/'
512 512
513 513 defaults = {
514 514 "encoding": encoding.encoding,
515 515 "motd": motd,
516 516 "url": wsgireq.req.apppath + '/',
517 517 "logourl": logourl,
518 518 "logoimg": logoimg,
519 519 "staticurl": staticurl,
520 520 "sessionvars": sessionvars,
521 521 "style": style,
522 522 "nonce": nonce,
523 523 }
524 524 tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
525 525 return tmpl
General Comments 0
You need to be logged in to leave comments. Login now