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