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