PageRenderTime 39ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/mercurial/hgweb/hgweb_mod.py

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