comparison mercurial/hgweb/hgweb_mod.py @ 36883:061635d4221c

hgweb: add a sendtemplate() helper function This pattern is common. Let's make a helper function to reduce boilerplate. We store the "global" template on the requestcontext instance and use it. The templater used by the helper function is the same templater that's passed in as an argument to the @webcommand functions. It needs to be this way because various commands are accessing and mutating the defaults on the templater instance. Differential Revision: https://phab.mercurial-scm.org/D2799
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 10 Mar 2018 19:41:18 -0800
parents 96a93625a824
children 4daa22071d5d
comparison
equal deleted inserted replaced
36882:66f62d120ba2 36883:061635d4221c
194 filters={'websub': websubfilter}, 194 filters={'websub': websubfilter},
195 defaults=defaults, 195 defaults=defaults,
196 resources=tres) 196 resources=tres)
197 return tmpl 197 return tmpl
198 198
199 def sendtemplate(self, name, **kwargs):
200 """Helper function to send a response generated from a template."""
201 self.res.setbodygen(self.tmpl(name, **kwargs))
202 return self.res.sendresponse()
199 203
200 class hgweb(object): 204 class hgweb(object):
201 """HTTP server for individual repositories. 205 """HTTP server for individual repositories.
202 206
203 Instances of this class serve HTTP responses for a particular 207 Instances of this class serve HTTP responses for a particular
366 cmd = req.qsparams.get('cmd', '') 370 cmd = req.qsparams.get('cmd', '')
367 371
368 # process the web interface request 372 # process the web interface request
369 373
370 try: 374 try:
371 tmpl = rctx.templater(req) 375 rctx.tmpl = rctx.templater(req)
372 ctype = tmpl('mimetype', encoding=encoding.encoding) 376 ctype = rctx.tmpl('mimetype', encoding=encoding.encoding)
373 ctype = templater.stringify(ctype) 377 ctype = templater.stringify(ctype)
374 378
375 # check read permissions non-static content 379 # check read permissions non-static content
376 if cmd != 'static': 380 if cmd != 'static':
377 self.check_perm(rctx, req, None) 381 self.check_perm(rctx, req, None)
378 382
379 if cmd == '': 383 if cmd == '':
380 req.qsparams['cmd'] = tmpl.cache['default'] 384 req.qsparams['cmd'] = rctx.tmpl.cache['default']
381 cmd = req.qsparams['cmd'] 385 cmd = req.qsparams['cmd']
382 386
383 # Don't enable caching if using a CSP nonce because then it wouldn't 387 # Don't enable caching if using a CSP nonce because then it wouldn't
384 # be a nonce. 388 # be a nonce.
385 if rctx.configbool('web', 'cache') and not rctx.nonce: 389 if rctx.configbool('web', 'cache') and not rctx.nonce:
398 else: 402 else:
399 # Set some globals appropriate for web handlers. Commands can 403 # Set some globals appropriate for web handlers. Commands can
400 # override easily enough. 404 # override easily enough.
401 res.status = '200 Script output follows' 405 res.status = '200 Script output follows'
402 res.headers['Content-Type'] = ctype 406 res.headers['Content-Type'] = ctype
403 return getattr(webcommands, cmd)(rctx, wsgireq, tmpl) 407 return getattr(webcommands, cmd)(rctx, wsgireq, rctx.tmpl)
404 408
405 except (error.LookupError, error.RepoLookupError) as err: 409 except (error.LookupError, error.RepoLookupError) as err:
406 msg = pycompat.bytestr(err) 410 msg = pycompat.bytestr(err)
407 if (util.safehasattr(err, 'name') and 411 if (util.safehasattr(err, 'name') and
408 not isinstance(err, error.ManifestLookupError)): 412 not isinstance(err, error.ManifestLookupError)):
409 msg = 'revision not found: %s' % err.name 413 msg = 'revision not found: %s' % err.name
410 414
411 res.status = '404 Not Found' 415 res.status = '404 Not Found'
412 res.headers['Content-Type'] = ctype 416 res.headers['Content-Type'] = ctype
413 res.setbodygen(tmpl('error', error=msg)) 417 return rctx.sendtemplate('error', error=msg)
414 return res.sendresponse()
415 except (error.RepoError, error.RevlogError) as e: 418 except (error.RepoError, error.RevlogError) as e:
416 res.status = '500 Internal Server Error' 419 res.status = '500 Internal Server Error'
417 res.headers['Content-Type'] = ctype 420 res.headers['Content-Type'] = ctype
418 res.setbodygen(tmpl('error', error=pycompat.bytestr(e))) 421 return rctx.sendtemplate('error', error=pycompat.bytestr(e))
419 return res.sendresponse()
420 except ErrorResponse as e: 422 except ErrorResponse as e:
421 res.status = statusmessage(e.code, pycompat.bytestr(e)) 423 res.status = statusmessage(e.code, pycompat.bytestr(e))
422 res.headers['Content-Type'] = ctype 424 res.headers['Content-Type'] = ctype
423 res.setbodygen(tmpl('error', error=pycompat.bytestr(e))) 425 return rctx.sendtemplate('error', error=pycompat.bytestr(e))
424 return res.sendresponse()
425 426
426 def check_perm(self, rctx, req, op): 427 def check_perm(self, rctx, req, op):
427 for permhook in permhooks: 428 for permhook in permhooks:
428 permhook(rctx, req, op) 429 permhook(rctx, req, op)
429 430