##// END OF EJS Templates
hgweb: always clear report_untrusted and interactive
Matt Mackall -
r10995:5efbfa66 default
parent child Browse files
Show More
@@ -1,289 +1,289 b''
1 # hgweb/hgweb_mod.py - Web interface for a repository.
1 # hgweb/hgweb_mod.py - Web interface for a repository.
2 #
2 #
3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
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.
7 # GNU General Public License version 2 or any later version.
8
8
9 import os
9 import os
10 from mercurial import ui, hg, hook, error, encoding, templater
10 from mercurial import ui, hg, hook, error, encoding, templater
11 from common import get_mtime, ErrorResponse, permhooks
11 from common import get_mtime, ErrorResponse, permhooks
12 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
12 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
13 from request import wsgirequest
13 from request import wsgirequest
14 import webcommands, protocol, webutil
14 import webcommands, protocol, webutil
15
15
16 perms = {
16 perms = {
17 'changegroup': 'pull',
17 'changegroup': 'pull',
18 'changegroupsubset': 'pull',
18 'changegroupsubset': 'pull',
19 'unbundle': 'push',
19 'unbundle': 'push',
20 'stream_out': 'pull',
20 'stream_out': 'pull',
21 }
21 }
22
22
23 class hgweb(object):
23 class hgweb(object):
24 def __init__(self, repo, name=None, baseui=None):
24 def __init__(self, repo, name=None, baseui=None):
25 if isinstance(repo, str):
25 if isinstance(repo, str):
26 if baseui:
26 if baseui:
27 u = baseui.copy()
27 u = baseui.copy()
28 else:
28 else:
29 u = ui.ui()
29 u = ui.ui()
30 u.setconfig('ui', 'report_untrusted', 'off')
31 u.setconfig('ui', 'interactive', 'off')
32 self.repo = hg.repository(u, repo)
30 self.repo = hg.repository(u, repo)
33 else:
31 else:
34 self.repo = repo
32 self.repo = repo
35
33
34 self.repo.ui.setconfig('ui', 'report_untrusted', 'off')
35 self.repo.ui.setconfig('ui', 'interactive', 'off')
36 hook.redirect(True)
36 hook.redirect(True)
37 self.mtime = -1
37 self.mtime = -1
38 self.reponame = name
38 self.reponame = name
39 self.archives = 'zip', 'gz', 'bz2'
39 self.archives = 'zip', 'gz', 'bz2'
40 self.stripecount = 1
40 self.stripecount = 1
41 # a repo owner may set web.templates in .hg/hgrc to get any file
41 # a repo owner may set web.templates in .hg/hgrc to get any file
42 # readable by the user running the CGI script
42 # readable by the user running the CGI script
43 self.templatepath = self.config('web', 'templates')
43 self.templatepath = self.config('web', 'templates')
44
44
45 # The CGI scripts are often run by a user different from the repo owner.
45 # The CGI scripts are often run by a user different from the repo owner.
46 # Trust the settings from the .hg/hgrc files by default.
46 # Trust the settings from the .hg/hgrc files by default.
47 def config(self, section, name, default=None, untrusted=True):
47 def config(self, section, name, default=None, untrusted=True):
48 return self.repo.ui.config(section, name, default,
48 return self.repo.ui.config(section, name, default,
49 untrusted=untrusted)
49 untrusted=untrusted)
50
50
51 def configbool(self, section, name, default=False, untrusted=True):
51 def configbool(self, section, name, default=False, untrusted=True):
52 return self.repo.ui.configbool(section, name, default,
52 return self.repo.ui.configbool(section, name, default,
53 untrusted=untrusted)
53 untrusted=untrusted)
54
54
55 def configlist(self, section, name, default=None, untrusted=True):
55 def configlist(self, section, name, default=None, untrusted=True):
56 return self.repo.ui.configlist(section, name, default,
56 return self.repo.ui.configlist(section, name, default,
57 untrusted=untrusted)
57 untrusted=untrusted)
58
58
59 def refresh(self, request=None):
59 def refresh(self, request=None):
60 if request:
60 if request:
61 self.repo.ui.environ = request.env
61 self.repo.ui.environ = request.env
62 mtime = get_mtime(self.repo.spath)
62 mtime = get_mtime(self.repo.spath)
63 if mtime != self.mtime:
63 if mtime != self.mtime:
64 self.mtime = mtime
64 self.mtime = mtime
65 self.repo = hg.repository(self.repo.ui, self.repo.root)
65 self.repo = hg.repository(self.repo.ui, self.repo.root)
66 self.maxchanges = int(self.config("web", "maxchanges", 10))
66 self.maxchanges = int(self.config("web", "maxchanges", 10))
67 self.stripecount = int(self.config("web", "stripes", 1))
67 self.stripecount = int(self.config("web", "stripes", 1))
68 self.maxshortchanges = int(self.config("web", "maxshortchanges", 60))
68 self.maxshortchanges = int(self.config("web", "maxshortchanges", 60))
69 self.maxfiles = int(self.config("web", "maxfiles", 10))
69 self.maxfiles = int(self.config("web", "maxfiles", 10))
70 self.allowpull = self.configbool("web", "allowpull", True)
70 self.allowpull = self.configbool("web", "allowpull", True)
71 encoding.encoding = self.config("web", "encoding",
71 encoding.encoding = self.config("web", "encoding",
72 encoding.encoding)
72 encoding.encoding)
73
73
74 def run(self):
74 def run(self):
75 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
75 if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."):
76 raise RuntimeError("This function is only intended to be "
76 raise RuntimeError("This function is only intended to be "
77 "called while running as a CGI script.")
77 "called while running as a CGI script.")
78 import mercurial.hgweb.wsgicgi as wsgicgi
78 import mercurial.hgweb.wsgicgi as wsgicgi
79 wsgicgi.launch(self)
79 wsgicgi.launch(self)
80
80
81 def __call__(self, env, respond):
81 def __call__(self, env, respond):
82 req = wsgirequest(env, respond)
82 req = wsgirequest(env, respond)
83 return self.run_wsgi(req)
83 return self.run_wsgi(req)
84
84
85 def run_wsgi(self, req):
85 def run_wsgi(self, req):
86
86
87 self.refresh(req)
87 self.refresh(req)
88
88
89 # work with CGI variables to create coherent structure
89 # work with CGI variables to create coherent structure
90 # use SCRIPT_NAME, PATH_INFO and QUERY_STRING as well as our REPO_NAME
90 # use SCRIPT_NAME, PATH_INFO and QUERY_STRING as well as our REPO_NAME
91
91
92 req.url = req.env['SCRIPT_NAME']
92 req.url = req.env['SCRIPT_NAME']
93 if not req.url.endswith('/'):
93 if not req.url.endswith('/'):
94 req.url += '/'
94 req.url += '/'
95 if 'REPO_NAME' in req.env:
95 if 'REPO_NAME' in req.env:
96 req.url += req.env['REPO_NAME'] + '/'
96 req.url += req.env['REPO_NAME'] + '/'
97
97
98 if 'PATH_INFO' in req.env:
98 if 'PATH_INFO' in req.env:
99 parts = req.env['PATH_INFO'].strip('/').split('/')
99 parts = req.env['PATH_INFO'].strip('/').split('/')
100 repo_parts = req.env.get('REPO_NAME', '').split('/')
100 repo_parts = req.env.get('REPO_NAME', '').split('/')
101 if parts[:len(repo_parts)] == repo_parts:
101 if parts[:len(repo_parts)] == repo_parts:
102 parts = parts[len(repo_parts):]
102 parts = parts[len(repo_parts):]
103 query = '/'.join(parts)
103 query = '/'.join(parts)
104 else:
104 else:
105 query = req.env['QUERY_STRING'].split('&', 1)[0]
105 query = req.env['QUERY_STRING'].split('&', 1)[0]
106 query = query.split(';', 1)[0]
106 query = query.split(';', 1)[0]
107
107
108 # process this if it's a protocol request
108 # process this if it's a protocol request
109 # protocol bits don't need to create any URLs
109 # protocol bits don't need to create any URLs
110 # and the clients always use the old URL structure
110 # and the clients always use the old URL structure
111
111
112 cmd = req.form.get('cmd', [''])[0]
112 cmd = req.form.get('cmd', [''])[0]
113 if cmd and cmd in protocol.__all__:
113 if cmd and cmd in protocol.__all__:
114 if query:
114 if query:
115 raise ErrorResponse(HTTP_NOT_FOUND)
115 raise ErrorResponse(HTTP_NOT_FOUND)
116 try:
116 try:
117 if cmd in perms:
117 if cmd in perms:
118 try:
118 try:
119 self.check_perm(req, perms[cmd])
119 self.check_perm(req, perms[cmd])
120 except ErrorResponse, inst:
120 except ErrorResponse, inst:
121 if cmd == 'unbundle':
121 if cmd == 'unbundle':
122 req.drain()
122 req.drain()
123 raise
123 raise
124 method = getattr(protocol, cmd)
124 method = getattr(protocol, cmd)
125 return method(self.repo, req)
125 return method(self.repo, req)
126 except ErrorResponse, inst:
126 except ErrorResponse, inst:
127 req.respond(inst, protocol.HGTYPE)
127 req.respond(inst, protocol.HGTYPE)
128 if not inst.message:
128 if not inst.message:
129 return []
129 return []
130 return '0\n%s\n' % inst.message,
130 return '0\n%s\n' % inst.message,
131
131
132 # translate user-visible url structure to internal structure
132 # translate user-visible url structure to internal structure
133
133
134 args = query.split('/', 2)
134 args = query.split('/', 2)
135 if 'cmd' not in req.form and args and args[0]:
135 if 'cmd' not in req.form and args and args[0]:
136
136
137 cmd = args.pop(0)
137 cmd = args.pop(0)
138 style = cmd.rfind('-')
138 style = cmd.rfind('-')
139 if style != -1:
139 if style != -1:
140 req.form['style'] = [cmd[:style]]
140 req.form['style'] = [cmd[:style]]
141 cmd = cmd[style + 1:]
141 cmd = cmd[style + 1:]
142
142
143 # avoid accepting e.g. style parameter as command
143 # avoid accepting e.g. style parameter as command
144 if hasattr(webcommands, cmd):
144 if hasattr(webcommands, cmd):
145 req.form['cmd'] = [cmd]
145 req.form['cmd'] = [cmd]
146 else:
146 else:
147 cmd = ''
147 cmd = ''
148
148
149 if cmd == 'static':
149 if cmd == 'static':
150 req.form['file'] = ['/'.join(args)]
150 req.form['file'] = ['/'.join(args)]
151 else:
151 else:
152 if args and args[0]:
152 if args and args[0]:
153 node = args.pop(0)
153 node = args.pop(0)
154 req.form['node'] = [node]
154 req.form['node'] = [node]
155 if args:
155 if args:
156 req.form['file'] = args
156 req.form['file'] = args
157
157
158 ua = req.env.get('HTTP_USER_AGENT', '')
158 ua = req.env.get('HTTP_USER_AGENT', '')
159 if cmd == 'rev' and 'mercurial' in ua:
159 if cmd == 'rev' and 'mercurial' in ua:
160 req.form['style'] = ['raw']
160 req.form['style'] = ['raw']
161
161
162 if cmd == 'archive':
162 if cmd == 'archive':
163 fn = req.form['node'][0]
163 fn = req.form['node'][0]
164 for type_, spec in self.archive_specs.iteritems():
164 for type_, spec in self.archive_specs.iteritems():
165 ext = spec[2]
165 ext = spec[2]
166 if fn.endswith(ext):
166 if fn.endswith(ext):
167 req.form['node'] = [fn[:-len(ext)]]
167 req.form['node'] = [fn[:-len(ext)]]
168 req.form['type'] = [type_]
168 req.form['type'] = [type_]
169
169
170 # process the web interface request
170 # process the web interface request
171
171
172 try:
172 try:
173 tmpl = self.templater(req)
173 tmpl = self.templater(req)
174 ctype = tmpl('mimetype', encoding=encoding.encoding)
174 ctype = tmpl('mimetype', encoding=encoding.encoding)
175 ctype = templater.stringify(ctype)
175 ctype = templater.stringify(ctype)
176
176
177 # check read permissions non-static content
177 # check read permissions non-static content
178 if cmd != 'static':
178 if cmd != 'static':
179 self.check_perm(req, None)
179 self.check_perm(req, None)
180
180
181 if cmd == '':
181 if cmd == '':
182 req.form['cmd'] = [tmpl.cache['default']]
182 req.form['cmd'] = [tmpl.cache['default']]
183 cmd = req.form['cmd'][0]
183 cmd = req.form['cmd'][0]
184
184
185 if cmd not in webcommands.__all__:
185 if cmd not in webcommands.__all__:
186 msg = 'no such method: %s' % cmd
186 msg = 'no such method: %s' % cmd
187 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
187 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
188 elif cmd == 'file' and 'raw' in req.form.get('style', []):
188 elif cmd == 'file' and 'raw' in req.form.get('style', []):
189 self.ctype = ctype
189 self.ctype = ctype
190 content = webcommands.rawfile(self, req, tmpl)
190 content = webcommands.rawfile(self, req, tmpl)
191 else:
191 else:
192 content = getattr(webcommands, cmd)(self, req, tmpl)
192 content = getattr(webcommands, cmd)(self, req, tmpl)
193 req.respond(HTTP_OK, ctype)
193 req.respond(HTTP_OK, ctype)
194
194
195 return content
195 return content
196
196
197 except error.LookupError, err:
197 except error.LookupError, err:
198 req.respond(HTTP_NOT_FOUND, ctype)
198 req.respond(HTTP_NOT_FOUND, ctype)
199 msg = str(err)
199 msg = str(err)
200 if 'manifest' not in msg:
200 if 'manifest' not in msg:
201 msg = 'revision not found: %s' % err.name
201 msg = 'revision not found: %s' % err.name
202 return tmpl('error', error=msg)
202 return tmpl('error', error=msg)
203 except (error.RepoError, error.RevlogError), inst:
203 except (error.RepoError, error.RevlogError), inst:
204 req.respond(HTTP_SERVER_ERROR, ctype)
204 req.respond(HTTP_SERVER_ERROR, ctype)
205 return tmpl('error', error=str(inst))
205 return tmpl('error', error=str(inst))
206 except ErrorResponse, inst:
206 except ErrorResponse, inst:
207 req.respond(inst, ctype)
207 req.respond(inst, ctype)
208 return tmpl('error', error=inst.message)
208 return tmpl('error', error=inst.message)
209
209
210 def templater(self, req):
210 def templater(self, req):
211
211
212 # determine scheme, port and server name
212 # determine scheme, port and server name
213 # this is needed to create absolute urls
213 # this is needed to create absolute urls
214
214
215 proto = req.env.get('wsgi.url_scheme')
215 proto = req.env.get('wsgi.url_scheme')
216 if proto == 'https':
216 if proto == 'https':
217 proto = 'https'
217 proto = 'https'
218 default_port = "443"
218 default_port = "443"
219 else:
219 else:
220 proto = 'http'
220 proto = 'http'
221 default_port = "80"
221 default_port = "80"
222
222
223 port = req.env["SERVER_PORT"]
223 port = req.env["SERVER_PORT"]
224 port = port != default_port and (":" + port) or ""
224 port = port != default_port and (":" + port) or ""
225 urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
225 urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
226 staticurl = self.config("web", "staticurl") or req.url + 'static/'
226 staticurl = self.config("web", "staticurl") or req.url + 'static/'
227 if not staticurl.endswith('/'):
227 if not staticurl.endswith('/'):
228 staticurl += '/'
228 staticurl += '/'
229
229
230 # some functions for the templater
230 # some functions for the templater
231
231
232 def header(**map):
232 def header(**map):
233 yield tmpl('header', encoding=encoding.encoding, **map)
233 yield tmpl('header', encoding=encoding.encoding, **map)
234
234
235 def footer(**map):
235 def footer(**map):
236 yield tmpl("footer", **map)
236 yield tmpl("footer", **map)
237
237
238 def motd(**map):
238 def motd(**map):
239 yield self.config("web", "motd", "")
239 yield self.config("web", "motd", "")
240
240
241 # figure out which style to use
241 # figure out which style to use
242
242
243 vars = {}
243 vars = {}
244 styles = (
244 styles = (
245 req.form.get('style', [None])[0],
245 req.form.get('style', [None])[0],
246 self.config('web', 'style'),
246 self.config('web', 'style'),
247 'paper',
247 'paper',
248 )
248 )
249 style, mapfile = templater.stylemap(styles, self.templatepath)
249 style, mapfile = templater.stylemap(styles, self.templatepath)
250 if style == styles[0]:
250 if style == styles[0]:
251 vars['style'] = style
251 vars['style'] = style
252
252
253 start = req.url[-1] == '?' and '&' or '?'
253 start = req.url[-1] == '?' and '&' or '?'
254 sessionvars = webutil.sessionvars(vars, start)
254 sessionvars = webutil.sessionvars(vars, start)
255
255
256 if not self.reponame:
256 if not self.reponame:
257 self.reponame = (self.config("web", "name")
257 self.reponame = (self.config("web", "name")
258 or req.env.get('REPO_NAME')
258 or req.env.get('REPO_NAME')
259 or req.url.strip('/') or self.repo.root)
259 or req.url.strip('/') or self.repo.root)
260
260
261 # create the templater
261 # create the templater
262
262
263 tmpl = templater.templater(mapfile,
263 tmpl = templater.templater(mapfile,
264 defaults={"url": req.url,
264 defaults={"url": req.url,
265 "staticurl": staticurl,
265 "staticurl": staticurl,
266 "urlbase": urlbase,
266 "urlbase": urlbase,
267 "repo": self.reponame,
267 "repo": self.reponame,
268 "header": header,
268 "header": header,
269 "footer": footer,
269 "footer": footer,
270 "motd": motd,
270 "motd": motd,
271 "sessionvars": sessionvars
271 "sessionvars": sessionvars
272 })
272 })
273 return tmpl
273 return tmpl
274
274
275 def archivelist(self, nodeid):
275 def archivelist(self, nodeid):
276 allowed = self.configlist("web", "allow_archive")
276 allowed = self.configlist("web", "allow_archive")
277 for i, spec in self.archive_specs.iteritems():
277 for i, spec in self.archive_specs.iteritems():
278 if i in allowed or self.configbool("web", "allow" + i):
278 if i in allowed or self.configbool("web", "allow" + i):
279 yield {"type" : i, "extension" : spec[2], "node" : nodeid}
279 yield {"type" : i, "extension" : spec[2], "node" : nodeid}
280
280
281 archive_specs = {
281 archive_specs = {
282 'bz2': ('application/x-tar', 'tbz2', '.tar.bz2', None),
282 'bz2': ('application/x-tar', 'tbz2', '.tar.bz2', None),
283 'gz': ('application/x-tar', 'tgz', '.tar.gz', None),
283 'gz': ('application/x-tar', 'tgz', '.tar.gz', None),
284 'zip': ('application/zip', 'zip', '.zip', None),
284 'zip': ('application/zip', 'zip', '.zip', None),
285 }
285 }
286
286
287 def check_perm(self, req, op):
287 def check_perm(self, req, op):
288 for hook in permhooks:
288 for hook in permhooks:
289 hook(self, req, op)
289 hook(self, req, op)
General Comments 0
You need to be logged in to leave comments. Login now