comparison mercurial/hgweb/hgweb_mod.py @ 36863:1a1972b1a1ff

hgweb: use our new request object for "style" parameter The "style" parameter is kind of wonky because it is explicitly set and has lookups in random locations. Let's port it to qsparams first because it isn't straightforward. There is subtle change in behavior. But I don't think it is worth calling out in a BC. Our multidict's __getitem__ returns the last set value for a key, not the first. So if the query string set a variable multiple times, before we would get the first value and now we would get the last value. It makes no sense to specify these things multiple times. And I think last write wins is more sensible than first write wins. Differential Revision: https://phab.mercurial-scm.org/D2779
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 10 Mar 2018 11:46:52 -0800
parents a88d68dc3ee8
children 4e06e8336634
comparison
equal deleted inserted replaced
36862:ec0af9c59270 36863:1a1972b1a1ff
51 ('gz', ('application/x-gzip', 'tgz', '.tar.gz', None)), 51 ('gz', ('application/x-gzip', 'tgz', '.tar.gz', None)),
52 ('bz2', ('application/x-bzip2', 'tbz2', '.tar.bz2', None)), 52 ('bz2', ('application/x-bzip2', 'tbz2', '.tar.bz2', None)),
53 )) 53 ))
54 54
55 def getstyle(req, configfn, templatepath): 55 def getstyle(req, configfn, templatepath):
56 fromreq = req.form.get('style', [None])[0]
57 styles = ( 56 styles = (
58 fromreq, 57 req.qsparams.get('style', None),
59 configfn('web', 'style'), 58 configfn('web', 'style'),
60 'paper', 59 'paper',
61 ) 60 )
62 return styles, templater.stylemap(styles, templatepath) 61 return styles, templater.stylemap(styles, templatepath)
63 62
158 yield self.config('web', 'motd') 157 yield self.config('web', 'motd')
159 158
160 # figure out which style to use 159 # figure out which style to use
161 160
162 vars = {} 161 vars = {}
163 styles, (style, mapfile) = getstyle(wsgireq, self.config, 162 styles, (style, mapfile) = getstyle(wsgireq.req, self.config,
164 self.templatepath) 163 self.templatepath)
165 if style == styles[0]: 164 if style == styles[0]:
166 vars['style'] = style 165 vars['style'] = style
167 166
168 sessionvars = webutil.sessionvars(vars, '?') 167 sessionvars = webutil.sessionvars(vars, '?')
335 args = query.split('/', 2) 334 args = query.split('/', 2)
336 if 'cmd' not in wsgireq.form and args and args[0]: 335 if 'cmd' not in wsgireq.form and args and args[0]:
337 cmd = args.pop(0) 336 cmd = args.pop(0)
338 style = cmd.rfind('-') 337 style = cmd.rfind('-')
339 if style != -1: 338 if style != -1:
340 wsgireq.form['style'] = [cmd[:style]] 339 req.qsparams['style'] = cmd[:style]
341 cmd = cmd[style + 1:] 340 cmd = cmd[style + 1:]
342 341
343 # avoid accepting e.g. style parameter as command 342 # avoid accepting e.g. style parameter as command
344 if util.safehasattr(webcommands, cmd): 343 if util.safehasattr(webcommands, cmd):
345 wsgireq.form['cmd'] = [cmd] 344 wsgireq.form['cmd'] = [cmd]
353 if args: 352 if args:
354 wsgireq.form['file'] = args 353 wsgireq.form['file'] = args
355 354
356 ua = req.headers.get('User-Agent', '') 355 ua = req.headers.get('User-Agent', '')
357 if cmd == 'rev' and 'mercurial' in ua: 356 if cmd == 'rev' and 'mercurial' in ua:
358 wsgireq.form['style'] = ['raw'] 357 req.qsparams['style'] = 'raw'
359 358
360 if cmd == 'archive': 359 if cmd == 'archive':
361 fn = wsgireq.form['node'][0] 360 fn = wsgireq.form['node'][0]
362 for type_, spec in rctx.archivespecs.iteritems(): 361 for type_, spec in rctx.archivespecs.iteritems():
363 ext = spec[2] 362 ext = spec[2]
387 if rctx.configbool('web', 'cache') and not rctx.nonce: 386 if rctx.configbool('web', 'cache') and not rctx.nonce:
388 caching(self, wsgireq) # sets ETag header or raises NOT_MODIFIED 387 caching(self, wsgireq) # sets ETag header or raises NOT_MODIFIED
389 if cmd not in webcommands.__all__: 388 if cmd not in webcommands.__all__:
390 msg = 'no such method: %s' % cmd 389 msg = 'no such method: %s' % cmd
391 raise ErrorResponse(HTTP_BAD_REQUEST, msg) 390 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
392 elif cmd == 'file' and 'raw' in wsgireq.form.get('style', []): 391 elif cmd == 'file' and req.qsparams.get('style') == 'raw':
393 rctx.ctype = ctype 392 rctx.ctype = ctype
394 content = webcommands.rawfile(rctx, wsgireq, tmpl) 393 content = webcommands.rawfile(rctx, wsgireq, tmpl)
395 else: 394 else:
396 content = getattr(webcommands, cmd)(rctx, wsgireq, tmpl) 395 content = getattr(webcommands, cmd)(rctx, wsgireq, tmpl)
397 wsgireq.respond(HTTP_OK, ctype) 396 wsgireq.respond(HTTP_OK, ctype)