##// END OF EJS Templates
hgweb: avoid initialization race (issue3953)
Matt Mackall -
r20168:d4be314b stable
parent child Browse files
Show More
@@ -1,391 +1,393
1 1 # hgweb/hgweb_mod.py - Web interface for a repository.
2 2 #
3 3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 4 # Copyright 2005-2007 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 import os
10 10 from mercurial import ui, hg, hook, error, encoding, templater, util, repoview
11 11 from mercurial.templatefilters import websub
12 12 from mercurial.i18n import _
13 13 from common import get_stat, ErrorResponse, permhooks, caching
14 14 from common import HTTP_OK, HTTP_NOT_MODIFIED, HTTP_BAD_REQUEST
15 15 from common import HTTP_NOT_FOUND, HTTP_SERVER_ERROR
16 16 from request import wsgirequest
17 17 import webcommands, protocol, webutil, re
18 18
19 19 perms = {
20 20 'changegroup': 'pull',
21 21 'changegroupsubset': 'pull',
22 22 'getbundle': 'pull',
23 23 'stream_out': 'pull',
24 24 'listkeys': 'pull',
25 25 'unbundle': 'push',
26 26 'pushkey': 'push',
27 27 }
28 28
29 29 def makebreadcrumb(url, prefix=''):
30 30 '''Return a 'URL breadcrumb' list
31 31
32 32 A 'URL breadcrumb' is a list of URL-name pairs,
33 33 corresponding to each of the path items on a URL.
34 34 This can be used to create path navigation entries.
35 35 '''
36 36 if url.endswith('/'):
37 37 url = url[:-1]
38 38 if prefix:
39 39 url = '/' + prefix + url
40 40 relpath = url
41 41 if relpath.startswith('/'):
42 42 relpath = relpath[1:]
43 43
44 44 breadcrumb = []
45 45 urlel = url
46 46 pathitems = [''] + relpath.split('/')
47 47 for pathel in reversed(pathitems):
48 48 if not pathel or not urlel:
49 49 break
50 50 breadcrumb.append({'url': urlel, 'name': pathel})
51 51 urlel = os.path.dirname(urlel)
52 52 return reversed(breadcrumb)
53 53
54 54
55 55 class hgweb(object):
56 56 def __init__(self, repo, name=None, baseui=None):
57 57 if isinstance(repo, str):
58 58 if baseui:
59 59 u = baseui.copy()
60 60 else:
61 61 u = ui.ui()
62 self.repo = hg.repository(u, repo)
62 r = hg.repository(u, repo)
63 63 else:
64 self.repo = repo
64 r = repo
65 65
66 self.repo = self._getview(self.repo)
67 self.repo.ui.setconfig('ui', 'report_untrusted', 'off')
68 self.repo.baseui.setconfig('ui', 'report_untrusted', 'off')
69 self.repo.ui.setconfig('ui', 'nontty', 'true')
70 self.repo.baseui.setconfig('ui', 'nontty', 'true')
66 r = self._getview(r)
67 r.ui.setconfig('ui', 'report_untrusted', 'off')
68 r.baseui.setconfig('ui', 'report_untrusted', 'off')
69 r.ui.setconfig('ui', 'nontty', 'true')
70 r.baseui.setconfig('ui', 'nontty', 'true')
71 self.repo = r
71 72 hook.redirect(True)
72 73 self.mtime = -1
73 74 self.size = -1
74 75 self.reponame = name
75 76 self.archives = 'zip', 'gz', 'bz2'
76 77 self.stripecount = 1
77 78 # a repo owner may set web.templates in .hg/hgrc to get any file
78 79 # readable by the user running the CGI script
79 80 self.templatepath = self.config('web', 'templates')
80 81 self.websubtable = self.loadwebsub()
81 82
82 83 # The CGI scripts are often run by a user different from the repo owner.
83 84 # Trust the settings from the .hg/hgrc files by default.
84 85 def config(self, section, name, default=None, untrusted=True):
85 86 return self.repo.ui.config(section, name, default,
86 87 untrusted=untrusted)
87 88
88 89 def configbool(self, section, name, default=False, untrusted=True):
89 90 return self.repo.ui.configbool(section, name, default,
90 91 untrusted=untrusted)
91 92
92 93 def configlist(self, section, name, default=None, untrusted=True):
93 94 return self.repo.ui.configlist(section, name, default,
94 95 untrusted=untrusted)
95 96
96 97 def _getview(self, repo):
97 viewconfig = self.config('web', 'view', 'served')
98 viewconfig = repo.ui.config('web', 'view', 'served',
99 untrusted=True)
98 100 if viewconfig == 'all':
99 101 return repo.unfiltered()
100 102 elif viewconfig in repoview.filtertable:
101 103 return repo.filtered(viewconfig)
102 104 else:
103 105 return repo.filtered('served')
104 106
105 107 def refresh(self, request=None):
106 108 st = get_stat(self.repo.spath)
107 109 # compare changelog size in addition to mtime to catch
108 110 # rollbacks made less than a second ago
109 111 if st.st_mtime != self.mtime or st.st_size != self.size:
110 112 self.mtime = st.st_mtime
111 113 self.size = st.st_size
112 114 r = hg.repository(self.repo.baseui, self.repo.root)
113 115 self.repo = self._getview(r)
114 116 self.maxchanges = int(self.config("web", "maxchanges", 10))
115 117 self.stripecount = int(self.config("web", "stripes", 1))
116 118 self.maxshortchanges = int(self.config("web", "maxshortchanges",
117 119 60))
118 120 self.maxfiles = int(self.config("web", "maxfiles", 10))
119 121 self.allowpull = self.configbool("web", "allowpull", True)
120 122 encoding.encoding = self.config("web", "encoding",
121 123 encoding.encoding)
122 124 if request:
123 125 self.repo.ui.environ = request.env
124 126
125 127 def run(self):
126 128 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
127 129 raise RuntimeError("This function is only intended to be "
128 130 "called while running as a CGI script.")
129 131 import mercurial.hgweb.wsgicgi as wsgicgi
130 132 wsgicgi.launch(self)
131 133
132 134 def __call__(self, env, respond):
133 135 req = wsgirequest(env, respond)
134 136 return self.run_wsgi(req)
135 137
136 138 def run_wsgi(self, req):
137 139
138 140 self.refresh(req)
139 141
140 142 # work with CGI variables to create coherent structure
141 143 # use SCRIPT_NAME, PATH_INFO and QUERY_STRING as well as our REPO_NAME
142 144
143 145 req.url = req.env['SCRIPT_NAME']
144 146 if not req.url.endswith('/'):
145 147 req.url += '/'
146 148 if 'REPO_NAME' in req.env:
147 149 req.url += req.env['REPO_NAME'] + '/'
148 150
149 151 if 'PATH_INFO' in req.env:
150 152 parts = req.env['PATH_INFO'].strip('/').split('/')
151 153 repo_parts = req.env.get('REPO_NAME', '').split('/')
152 154 if parts[:len(repo_parts)] == repo_parts:
153 155 parts = parts[len(repo_parts):]
154 156 query = '/'.join(parts)
155 157 else:
156 158 query = req.env['QUERY_STRING'].split('&', 1)[0]
157 159 query = query.split(';', 1)[0]
158 160
159 161 # process this if it's a protocol request
160 162 # protocol bits don't need to create any URLs
161 163 # and the clients always use the old URL structure
162 164
163 165 cmd = req.form.get('cmd', [''])[0]
164 166 if protocol.iscmd(cmd):
165 167 try:
166 168 if query:
167 169 raise ErrorResponse(HTTP_NOT_FOUND)
168 170 if cmd in perms:
169 171 self.check_perm(req, perms[cmd])
170 172 return protocol.call(self.repo, req, cmd)
171 173 except ErrorResponse, inst:
172 174 # A client that sends unbundle without 100-continue will
173 175 # break if we respond early.
174 176 if (cmd == 'unbundle' and
175 177 (req.env.get('HTTP_EXPECT',
176 178 '').lower() != '100-continue') or
177 179 req.env.get('X-HgHttp2', '')):
178 180 req.drain()
179 181 else:
180 182 req.headers.append(('Connection', 'Close'))
181 183 req.respond(inst, protocol.HGTYPE,
182 184 body='0\n%s\n' % inst.message)
183 185 return ''
184 186
185 187 # translate user-visible url structure to internal structure
186 188
187 189 args = query.split('/', 2)
188 190 if 'cmd' not in req.form and args and args[0]:
189 191
190 192 cmd = args.pop(0)
191 193 style = cmd.rfind('-')
192 194 if style != -1:
193 195 req.form['style'] = [cmd[:style]]
194 196 cmd = cmd[style + 1:]
195 197
196 198 # avoid accepting e.g. style parameter as command
197 199 if util.safehasattr(webcommands, cmd):
198 200 req.form['cmd'] = [cmd]
199 201 else:
200 202 cmd = ''
201 203
202 204 if cmd == 'static':
203 205 req.form['file'] = ['/'.join(args)]
204 206 else:
205 207 if args and args[0]:
206 208 node = args.pop(0)
207 209 req.form['node'] = [node]
208 210 if args:
209 211 req.form['file'] = args
210 212
211 213 ua = req.env.get('HTTP_USER_AGENT', '')
212 214 if cmd == 'rev' and 'mercurial' in ua:
213 215 req.form['style'] = ['raw']
214 216
215 217 if cmd == 'archive':
216 218 fn = req.form['node'][0]
217 219 for type_, spec in self.archive_specs.iteritems():
218 220 ext = spec[2]
219 221 if fn.endswith(ext):
220 222 req.form['node'] = [fn[:-len(ext)]]
221 223 req.form['type'] = [type_]
222 224
223 225 # process the web interface request
224 226
225 227 try:
226 228 tmpl = self.templater(req)
227 229 ctype = tmpl('mimetype', encoding=encoding.encoding)
228 230 ctype = templater.stringify(ctype)
229 231
230 232 # check read permissions non-static content
231 233 if cmd != 'static':
232 234 self.check_perm(req, None)
233 235
234 236 if cmd == '':
235 237 req.form['cmd'] = [tmpl.cache['default']]
236 238 cmd = req.form['cmd'][0]
237 239
238 240 if self.configbool('web', 'cache', True):
239 241 caching(self, req) # sets ETag header or raises NOT_MODIFIED
240 242 if cmd not in webcommands.__all__:
241 243 msg = 'no such method: %s' % cmd
242 244 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
243 245 elif cmd == 'file' and 'raw' in req.form.get('style', []):
244 246 self.ctype = ctype
245 247 content = webcommands.rawfile(self, req, tmpl)
246 248 else:
247 249 content = getattr(webcommands, cmd)(self, req, tmpl)
248 250 req.respond(HTTP_OK, ctype)
249 251
250 252 return content
251 253
252 254 except (error.LookupError, error.RepoLookupError), err:
253 255 req.respond(HTTP_NOT_FOUND, ctype)
254 256 msg = str(err)
255 257 if (util.safehasattr(err, 'name') and
256 258 not isinstance(err, error.ManifestLookupError)):
257 259 msg = 'revision not found: %s' % err.name
258 260 return tmpl('error', error=msg)
259 261 except (error.RepoError, error.RevlogError), inst:
260 262 req.respond(HTTP_SERVER_ERROR, ctype)
261 263 return tmpl('error', error=str(inst))
262 264 except ErrorResponse, inst:
263 265 req.respond(inst, ctype)
264 266 if inst.code == HTTP_NOT_MODIFIED:
265 267 # Not allowed to return a body on a 304
266 268 return ['']
267 269 return tmpl('error', error=inst.message)
268 270
269 271 def loadwebsub(self):
270 272 websubtable = []
271 273 websubdefs = self.repo.ui.configitems('websub')
272 274 # we must maintain interhg backwards compatibility
273 275 websubdefs += self.repo.ui.configitems('interhg')
274 276 for key, pattern in websubdefs:
275 277 # grab the delimiter from the character after the "s"
276 278 unesc = pattern[1]
277 279 delim = re.escape(unesc)
278 280
279 281 # identify portions of the pattern, taking care to avoid escaped
280 282 # delimiters. the replace format and flags are optional, but
281 283 # delimiters are required.
282 284 match = re.match(
283 285 r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$'
284 286 % (delim, delim, delim), pattern)
285 287 if not match:
286 288 self.repo.ui.warn(_("websub: invalid pattern for %s: %s\n")
287 289 % (key, pattern))
288 290 continue
289 291
290 292 # we need to unescape the delimiter for regexp and format
291 293 delim_re = re.compile(r'(?<!\\)\\%s' % delim)
292 294 regexp = delim_re.sub(unesc, match.group(1))
293 295 format = delim_re.sub(unesc, match.group(2))
294 296
295 297 # the pattern allows for 6 regexp flags, so set them if necessary
296 298 flagin = match.group(3)
297 299 flags = 0
298 300 if flagin:
299 301 for flag in flagin.upper():
300 302 flags |= re.__dict__[flag]
301 303
302 304 try:
303 305 regexp = re.compile(regexp, flags)
304 306 websubtable.append((regexp, format))
305 307 except re.error:
306 308 self.repo.ui.warn(_("websub: invalid regexp for %s: %s\n")
307 309 % (key, regexp))
308 310 return websubtable
309 311
310 312 def templater(self, req):
311 313
312 314 # determine scheme, port and server name
313 315 # this is needed to create absolute urls
314 316
315 317 proto = req.env.get('wsgi.url_scheme')
316 318 if proto == 'https':
317 319 proto = 'https'
318 320 default_port = "443"
319 321 else:
320 322 proto = 'http'
321 323 default_port = "80"
322 324
323 325 port = req.env["SERVER_PORT"]
324 326 port = port != default_port and (":" + port) or ""
325 327 urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
326 328 logourl = self.config("web", "logourl", "http://mercurial.selenic.com/")
327 329 logoimg = self.config("web", "logoimg", "hglogo.png")
328 330 staticurl = self.config("web", "staticurl") or req.url + 'static/'
329 331 if not staticurl.endswith('/'):
330 332 staticurl += '/'
331 333
332 334 # some functions for the templater
333 335
334 336 def motd(**map):
335 337 yield self.config("web", "motd", "")
336 338
337 339 # figure out which style to use
338 340
339 341 vars = {}
340 342 styles = (
341 343 req.form.get('style', [None])[0],
342 344 self.config('web', 'style'),
343 345 'paper',
344 346 )
345 347 style, mapfile = templater.stylemap(styles, self.templatepath)
346 348 if style == styles[0]:
347 349 vars['style'] = style
348 350
349 351 start = req.url[-1] == '?' and '&' or '?'
350 352 sessionvars = webutil.sessionvars(vars, start)
351 353
352 354 if not self.reponame:
353 355 self.reponame = (self.config("web", "name")
354 356 or req.env.get('REPO_NAME')
355 357 or req.url.strip('/') or self.repo.root)
356 358
357 359 def websubfilter(text):
358 360 return websub(text, self.websubtable)
359 361
360 362 # create the templater
361 363
362 364 tmpl = templater.templater(mapfile,
363 365 filters={"websub": websubfilter},
364 366 defaults={"url": req.url,
365 367 "logourl": logourl,
366 368 "logoimg": logoimg,
367 369 "staticurl": staticurl,
368 370 "urlbase": urlbase,
369 371 "repo": self.reponame,
370 372 "encoding": encoding.encoding,
371 373 "motd": motd,
372 374 "sessionvars": sessionvars,
373 375 "pathdef": makebreadcrumb(req.url),
374 376 })
375 377 return tmpl
376 378
377 379 def archivelist(self, nodeid):
378 380 allowed = self.configlist("web", "allow_archive")
379 381 for i, spec in self.archive_specs.iteritems():
380 382 if i in allowed or self.configbool("web", "allow" + i):
381 383 yield {"type" : i, "extension" : spec[2], "node" : nodeid}
382 384
383 385 archive_specs = {
384 386 'bz2': ('application/x-bzip2', 'tbz2', '.tar.bz2', None),
385 387 'gz': ('application/x-gzip', 'tgz', '.tar.gz', None),
386 388 'zip': ('application/zip', 'zip', '.zip', None),
387 389 }
388 390
389 391 def check_perm(self, req, op):
390 392 for hook in permhooks:
391 393 hook(self, req, op)
General Comments 0
You need to be logged in to leave comments. Login now