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