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