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