##// END OF EJS Templates
wireprotoserver: rename hgweb.protocol to wireprotoserver (API)...
Gregory Szorc -
r35874:ef3a24a0 default
parent child Browse files
Show More
@@ -1,491 +1,491 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 from __future__ import absolute_import
10 10
11 11 import contextlib
12 12 import os
13 13
14 14 from .common import (
15 15 ErrorResponse,
16 16 HTTP_BAD_REQUEST,
17 17 HTTP_NOT_FOUND,
18 18 HTTP_NOT_MODIFIED,
19 19 HTTP_OK,
20 20 HTTP_SERVER_ERROR,
21 21 caching,
22 22 cspvalues,
23 23 permhooks,
24 24 )
25 25 from .request import wsgirequest
26 26
27 27 from .. import (
28 28 encoding,
29 29 error,
30 30 hg,
31 31 hook,
32 32 profiling,
33 33 pycompat,
34 34 repoview,
35 35 templatefilters,
36 36 templater,
37 37 ui as uimod,
38 38 util,
39 wireprotoserver,
39 40 )
40 41
41 42 from . import (
42 protocol,
43 43 webcommands,
44 44 webutil,
45 45 wsgicgi,
46 46 )
47 47
48 48 perms = {
49 49 'changegroup': 'pull',
50 50 'changegroupsubset': 'pull',
51 51 'getbundle': 'pull',
52 52 'stream_out': 'pull',
53 53 'listkeys': 'pull',
54 54 'unbundle': 'push',
55 55 'pushkey': 'push',
56 56 }
57 57
58 58 archivespecs = util.sortdict((
59 59 ('zip', ('application/zip', 'zip', '.zip', None)),
60 60 ('gz', ('application/x-gzip', 'tgz', '.tar.gz', None)),
61 61 ('bz2', ('application/x-bzip2', 'tbz2', '.tar.bz2', None)),
62 62 ))
63 63
64 64 def getstyle(req, configfn, templatepath):
65 65 fromreq = req.form.get('style', [None])[0]
66 66 if fromreq is not None:
67 67 fromreq = pycompat.sysbytes(fromreq)
68 68 styles = (
69 69 fromreq,
70 70 configfn('web', 'style'),
71 71 'paper',
72 72 )
73 73 return styles, templater.stylemap(styles, templatepath)
74 74
75 75 def makebreadcrumb(url, prefix=''):
76 76 '''Return a 'URL breadcrumb' list
77 77
78 78 A 'URL breadcrumb' is a list of URL-name pairs,
79 79 corresponding to each of the path items on a URL.
80 80 This can be used to create path navigation entries.
81 81 '''
82 82 if url.endswith('/'):
83 83 url = url[:-1]
84 84 if prefix:
85 85 url = '/' + prefix + url
86 86 relpath = url
87 87 if relpath.startswith('/'):
88 88 relpath = relpath[1:]
89 89
90 90 breadcrumb = []
91 91 urlel = url
92 92 pathitems = [''] + relpath.split('/')
93 93 for pathel in reversed(pathitems):
94 94 if not pathel or not urlel:
95 95 break
96 96 breadcrumb.append({'url': urlel, 'name': pathel})
97 97 urlel = os.path.dirname(urlel)
98 98 return reversed(breadcrumb)
99 99
100 100 class requestcontext(object):
101 101 """Holds state/context for an individual request.
102 102
103 103 Servers can be multi-threaded. Holding state on the WSGI application
104 104 is prone to race conditions. Instances of this class exist to hold
105 105 mutable and race-free state for requests.
106 106 """
107 107 def __init__(self, app, repo):
108 108 self.repo = repo
109 109 self.reponame = app.reponame
110 110
111 111 self.archivespecs = archivespecs
112 112
113 113 self.maxchanges = self.configint('web', 'maxchanges')
114 114 self.stripecount = self.configint('web', 'stripes')
115 115 self.maxshortchanges = self.configint('web', 'maxshortchanges')
116 116 self.maxfiles = self.configint('web', 'maxfiles')
117 117 self.allowpull = self.configbool('web', 'allow-pull')
118 118
119 119 # we use untrusted=False to prevent a repo owner from using
120 120 # web.templates in .hg/hgrc to get access to any file readable
121 121 # by the user running the CGI script
122 122 self.templatepath = self.config('web', 'templates', untrusted=False)
123 123
124 124 # This object is more expensive to build than simple config values.
125 125 # It is shared across requests. The app will replace the object
126 126 # if it is updated. Since this is a reference and nothing should
127 127 # modify the underlying object, it should be constant for the lifetime
128 128 # of the request.
129 129 self.websubtable = app.websubtable
130 130
131 131 self.csp, self.nonce = cspvalues(self.repo.ui)
132 132
133 133 # Trust the settings from the .hg/hgrc files by default.
134 134 def config(self, section, name, default=uimod._unset, untrusted=True):
135 135 return self.repo.ui.config(section, name, default,
136 136 untrusted=untrusted)
137 137
138 138 def configbool(self, section, name, default=uimod._unset, untrusted=True):
139 139 return self.repo.ui.configbool(section, name, default,
140 140 untrusted=untrusted)
141 141
142 142 def configint(self, section, name, default=uimod._unset, untrusted=True):
143 143 return self.repo.ui.configint(section, name, default,
144 144 untrusted=untrusted)
145 145
146 146 def configlist(self, section, name, default=uimod._unset, untrusted=True):
147 147 return self.repo.ui.configlist(section, name, default,
148 148 untrusted=untrusted)
149 149
150 150 def archivelist(self, nodeid):
151 151 allowed = self.configlist('web', 'allow_archive')
152 152 for typ, spec in self.archivespecs.iteritems():
153 153 if typ in allowed or self.configbool('web', 'allow%s' % typ):
154 154 yield {'type': typ, 'extension': spec[2], 'node': nodeid}
155 155
156 156 def templater(self, req):
157 157 # determine scheme, port and server name
158 158 # this is needed to create absolute urls
159 159
160 160 proto = req.env.get('wsgi.url_scheme')
161 161 if proto == 'https':
162 162 proto = 'https'
163 163 default_port = '443'
164 164 else:
165 165 proto = 'http'
166 166 default_port = '80'
167 167
168 168 port = req.env[r'SERVER_PORT']
169 169 port = port != default_port and (r':' + port) or r''
170 170 urlbase = r'%s://%s%s' % (proto, req.env[r'SERVER_NAME'], port)
171 171 logourl = self.config('web', 'logourl')
172 172 logoimg = self.config('web', 'logoimg')
173 173 staticurl = self.config('web', 'staticurl') or req.url + 'static/'
174 174 if not staticurl.endswith('/'):
175 175 staticurl += '/'
176 176
177 177 # some functions for the templater
178 178
179 179 def motd(**map):
180 180 yield self.config('web', 'motd')
181 181
182 182 # figure out which style to use
183 183
184 184 vars = {}
185 185 styles, (style, mapfile) = getstyle(req, self.config,
186 186 self.templatepath)
187 187 if style == styles[0]:
188 188 vars['style'] = style
189 189
190 190 start = '&' if req.url[-1] == r'?' else '?'
191 191 sessionvars = webutil.sessionvars(vars, start)
192 192
193 193 if not self.reponame:
194 194 self.reponame = (self.config('web', 'name', '')
195 195 or req.env.get('REPO_NAME')
196 196 or req.url.strip('/') or self.repo.root)
197 197
198 198 def websubfilter(text):
199 199 return templatefilters.websub(text, self.websubtable)
200 200
201 201 # create the templater
202 202
203 203 defaults = {
204 204 'url': req.url,
205 205 'logourl': logourl,
206 206 'logoimg': logoimg,
207 207 'staticurl': staticurl,
208 208 'urlbase': urlbase,
209 209 'repo': self.reponame,
210 210 'encoding': encoding.encoding,
211 211 'motd': motd,
212 212 'sessionvars': sessionvars,
213 213 'pathdef': makebreadcrumb(req.url),
214 214 'style': style,
215 215 'nonce': self.nonce,
216 216 }
217 217 tmpl = templater.templater.frommapfile(mapfile,
218 218 filters={'websub': websubfilter},
219 219 defaults=defaults)
220 220 return tmpl
221 221
222 222
223 223 class hgweb(object):
224 224 """HTTP server for individual repositories.
225 225
226 226 Instances of this class serve HTTP responses for a particular
227 227 repository.
228 228
229 229 Instances are typically used as WSGI applications.
230 230
231 231 Some servers are multi-threaded. On these servers, there may
232 232 be multiple active threads inside __call__.
233 233 """
234 234 def __init__(self, repo, name=None, baseui=None):
235 235 if isinstance(repo, str):
236 236 if baseui:
237 237 u = baseui.copy()
238 238 else:
239 239 u = uimod.ui.load()
240 240 r = hg.repository(u, repo)
241 241 else:
242 242 # we trust caller to give us a private copy
243 243 r = repo
244 244
245 245 r.ui.setconfig('ui', 'report_untrusted', 'off', 'hgweb')
246 246 r.baseui.setconfig('ui', 'report_untrusted', 'off', 'hgweb')
247 247 r.ui.setconfig('ui', 'nontty', 'true', 'hgweb')
248 248 r.baseui.setconfig('ui', 'nontty', 'true', 'hgweb')
249 249 # resolve file patterns relative to repo root
250 250 r.ui.setconfig('ui', 'forcecwd', r.root, 'hgweb')
251 251 r.baseui.setconfig('ui', 'forcecwd', r.root, 'hgweb')
252 252 # displaying bundling progress bar while serving feel wrong and may
253 253 # break some wsgi implementation.
254 254 r.ui.setconfig('progress', 'disable', 'true', 'hgweb')
255 255 r.baseui.setconfig('progress', 'disable', 'true', 'hgweb')
256 256 self._repos = [hg.cachedlocalrepo(self._webifyrepo(r))]
257 257 self._lastrepo = self._repos[0]
258 258 hook.redirect(True)
259 259 self.reponame = name
260 260
261 261 def _webifyrepo(self, repo):
262 262 repo = getwebview(repo)
263 263 self.websubtable = webutil.getwebsubs(repo)
264 264 return repo
265 265
266 266 @contextlib.contextmanager
267 267 def _obtainrepo(self):
268 268 """Obtain a repo unique to the caller.
269 269
270 270 Internally we maintain a stack of cachedlocalrepo instances
271 271 to be handed out. If one is available, we pop it and return it,
272 272 ensuring it is up to date in the process. If one is not available,
273 273 we clone the most recently used repo instance and return it.
274 274
275 275 It is currently possible for the stack to grow without bounds
276 276 if the server allows infinite threads. However, servers should
277 277 have a thread limit, thus establishing our limit.
278 278 """
279 279 if self._repos:
280 280 cached = self._repos.pop()
281 281 r, created = cached.fetch()
282 282 else:
283 283 cached = self._lastrepo.copy()
284 284 r, created = cached.fetch()
285 285 if created:
286 286 r = self._webifyrepo(r)
287 287
288 288 self._lastrepo = cached
289 289 self.mtime = cached.mtime
290 290 try:
291 291 yield r
292 292 finally:
293 293 self._repos.append(cached)
294 294
295 295 def run(self):
296 296 """Start a server from CGI environment.
297 297
298 298 Modern servers should be using WSGI and should avoid this
299 299 method, if possible.
300 300 """
301 301 if not encoding.environ.get('GATEWAY_INTERFACE',
302 302 '').startswith("CGI/1."):
303 303 raise RuntimeError("This function is only intended to be "
304 304 "called while running as a CGI script.")
305 305 wsgicgi.launch(self)
306 306
307 307 def __call__(self, env, respond):
308 308 """Run the WSGI application.
309 309
310 310 This may be called by multiple threads.
311 311 """
312 312 req = wsgirequest(env, respond)
313 313 return self.run_wsgi(req)
314 314
315 315 def run_wsgi(self, req):
316 316 """Internal method to run the WSGI application.
317 317
318 318 This is typically only called by Mercurial. External consumers
319 319 should be using instances of this class as the WSGI application.
320 320 """
321 321 with self._obtainrepo() as repo:
322 322 profile = repo.ui.configbool('profiling', 'enabled')
323 323 with profiling.profile(repo.ui, enabled=profile):
324 324 for r in self._runwsgi(req, repo):
325 325 yield r
326 326
327 327 def _runwsgi(self, req, repo):
328 328 rctx = requestcontext(self, repo)
329 329
330 330 # This state is global across all threads.
331 331 encoding.encoding = rctx.config('web', 'encoding')
332 332 rctx.repo.ui.environ = req.env
333 333
334 334 if rctx.csp:
335 335 # hgwebdir may have added CSP header. Since we generate our own,
336 336 # replace it.
337 337 req.headers = [h for h in req.headers
338 338 if h[0] != 'Content-Security-Policy']
339 339 req.headers.append(('Content-Security-Policy', rctx.csp))
340 340
341 341 # work with CGI variables to create coherent structure
342 342 # use SCRIPT_NAME, PATH_INFO and QUERY_STRING as well as our REPO_NAME
343 343
344 344 req.url = req.env[r'SCRIPT_NAME']
345 345 if not req.url.endswith('/'):
346 346 req.url += '/'
347 347 if req.env.get('REPO_NAME'):
348 348 req.url += req.env[r'REPO_NAME'] + r'/'
349 349
350 350 if r'PATH_INFO' in req.env:
351 351 parts = req.env[r'PATH_INFO'].strip('/').split('/')
352 352 repo_parts = req.env.get(r'REPO_NAME', r'').split(r'/')
353 353 if parts[:len(repo_parts)] == repo_parts:
354 354 parts = parts[len(repo_parts):]
355 355 query = '/'.join(parts)
356 356 else:
357 357 query = req.env[r'QUERY_STRING'].partition(r'&')[0]
358 358 query = query.partition(r';')[0]
359 359
360 360 # process this if it's a protocol request
361 361 # protocol bits don't need to create any URLs
362 362 # and the clients always use the old URL structure
363 363
364 364 cmd = pycompat.sysbytes(req.form.get(r'cmd', [r''])[0])
365 if protocol.iscmd(cmd):
365 if wireprotoserver.iscmd(cmd):
366 366 try:
367 367 if query:
368 368 raise ErrorResponse(HTTP_NOT_FOUND)
369 369 if cmd in perms:
370 370 self.check_perm(rctx, req, perms[cmd])
371 return protocol.call(rctx.repo, req, cmd)
371 return wireprotoserver.call(rctx.repo, req, cmd)
372 372 except ErrorResponse as inst:
373 373 # A client that sends unbundle without 100-continue will
374 374 # break if we respond early.
375 375 if (cmd == 'unbundle' and
376 376 (req.env.get('HTTP_EXPECT',
377 377 '').lower() != '100-continue') or
378 378 req.env.get('X-HgHttp2', '')):
379 379 req.drain()
380 380 else:
381 381 req.headers.append((r'Connection', r'Close'))
382 req.respond(inst, protocol.HGTYPE,
382 req.respond(inst, wireprotoserver.HGTYPE,
383 383 body='0\n%s\n' % inst)
384 384 return ''
385 385
386 386 # translate user-visible url structure to internal structure
387 387
388 388 args = query.split('/', 2)
389 389 if r'cmd' not in req.form and args and args[0]:
390 390 cmd = args.pop(0)
391 391 style = cmd.rfind('-')
392 392 if style != -1:
393 393 req.form['style'] = [cmd[:style]]
394 394 cmd = cmd[style + 1:]
395 395
396 396 # avoid accepting e.g. style parameter as command
397 397 if util.safehasattr(webcommands, cmd):
398 398 req.form[r'cmd'] = [cmd]
399 399
400 400 if cmd == 'static':
401 401 req.form['file'] = ['/'.join(args)]
402 402 else:
403 403 if args and args[0]:
404 404 node = args.pop(0).replace('%2F', '/')
405 405 req.form['node'] = [node]
406 406 if args:
407 407 req.form['file'] = args
408 408
409 409 ua = req.env.get('HTTP_USER_AGENT', '')
410 410 if cmd == 'rev' and 'mercurial' in ua:
411 411 req.form['style'] = ['raw']
412 412
413 413 if cmd == 'archive':
414 414 fn = req.form['node'][0]
415 415 for type_, spec in rctx.archivespecs.iteritems():
416 416 ext = spec[2]
417 417 if fn.endswith(ext):
418 418 req.form['node'] = [fn[:-len(ext)]]
419 419 req.form['type'] = [type_]
420 420
421 421 # process the web interface request
422 422
423 423 try:
424 424 tmpl = rctx.templater(req)
425 425 ctype = tmpl('mimetype', encoding=encoding.encoding)
426 426 ctype = templater.stringify(ctype)
427 427
428 428 # check read permissions non-static content
429 429 if cmd != 'static':
430 430 self.check_perm(rctx, req, None)
431 431
432 432 if cmd == '':
433 433 req.form[r'cmd'] = [tmpl.cache['default']]
434 434 cmd = req.form[r'cmd'][0]
435 435
436 436 # Don't enable caching if using a CSP nonce because then it wouldn't
437 437 # be a nonce.
438 438 if rctx.configbool('web', 'cache') and not rctx.nonce:
439 439 caching(self, req) # sets ETag header or raises NOT_MODIFIED
440 440 if cmd not in webcommands.__all__:
441 441 msg = 'no such method: %s' % cmd
442 442 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
443 443 elif cmd == 'file' and r'raw' in req.form.get(r'style', []):
444 444 rctx.ctype = ctype
445 445 content = webcommands.rawfile(rctx, req, tmpl)
446 446 else:
447 447 content = getattr(webcommands, cmd)(rctx, req, tmpl)
448 448 req.respond(HTTP_OK, ctype)
449 449
450 450 return content
451 451
452 452 except (error.LookupError, error.RepoLookupError) as err:
453 453 req.respond(HTTP_NOT_FOUND, ctype)
454 454 msg = str(err)
455 455 if (util.safehasattr(err, 'name') and
456 456 not isinstance(err, error.ManifestLookupError)):
457 457 msg = 'revision not found: %s' % err.name
458 458 return tmpl('error', error=msg)
459 459 except (error.RepoError, error.RevlogError) as inst:
460 460 req.respond(HTTP_SERVER_ERROR, ctype)
461 461 return tmpl('error', error=str(inst))
462 462 except ErrorResponse as inst:
463 463 req.respond(inst, ctype)
464 464 if inst.code == HTTP_NOT_MODIFIED:
465 465 # Not allowed to return a body on a 304
466 466 return ['']
467 467 return tmpl('error', error=str(inst))
468 468
469 469 def check_perm(self, rctx, req, op):
470 470 for permhook in permhooks:
471 471 permhook(rctx, req, op)
472 472
473 473 def getwebview(repo):
474 474 """The 'web.view' config controls changeset filter to hgweb. Possible
475 475 values are ``served``, ``visible`` and ``all``. Default is ``served``.
476 476 The ``served`` filter only shows changesets that can be pulled from the
477 477 hgweb instance. The``visible`` filter includes secret changesets but
478 478 still excludes "hidden" one.
479 479
480 480 See the repoview module for details.
481 481
482 482 The option has been around undocumented since Mercurial 2.5, but no
483 483 user ever asked about it. So we better keep it undocumented for now."""
484 484 # experimental config: web.view
485 485 viewconfig = repo.ui.config('web', 'view', untrusted=True)
486 486 if viewconfig == 'all':
487 487 return repo.unfiltered()
488 488 elif viewconfig in repoview.filtertable:
489 489 return repo.filtered(viewconfig)
490 490 else:
491 491 return repo.filtered('served')
@@ -1,201 +1,200 b''
1 #
2 1 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
3 2 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 3 #
5 4 # This software may be used and distributed according to the terms of the
6 5 # GNU General Public License version 2 or any later version.
7 6
8 7 from __future__ import absolute_import
9 8
10 9 import cgi
11 10 import struct
12 11
13 from .common import (
12 from .hgweb.common import (
14 13 HTTP_OK,
15 14 )
16
17 from .. import (
15 from . import (
18 16 error,
19 17 pycompat,
20 18 util,
21 19 wireproto,
22 20 )
21
23 22 stringio = util.stringio
24 23
25 24 urlerr = util.urlerr
26 25 urlreq = util.urlreq
27 26
28 27 HGTYPE = 'application/mercurial-0.1'
29 28 HGTYPE2 = 'application/mercurial-0.2'
30 29 HGERRTYPE = 'application/hg-error'
31 30
32 31 def decodevaluefromheaders(req, headerprefix):
33 32 """Decode a long value from multiple HTTP request headers.
34 33
35 34 Returns the value as a bytes, not a str.
36 35 """
37 36 chunks = []
38 37 i = 1
39 38 prefix = headerprefix.upper().replace(r'-', r'_')
40 39 while True:
41 40 v = req.env.get(r'HTTP_%s_%d' % (prefix, i))
42 41 if v is None:
43 42 break
44 43 chunks.append(pycompat.bytesurl(v))
45 44 i += 1
46 45
47 46 return ''.join(chunks)
48 47
49 48 class webproto(wireproto.abstractserverproto):
50 49 def __init__(self, req, ui):
51 50 self.req = req
52 51 self.response = ''
53 52 self.ui = ui
54 53 self.name = 'http'
55 54
56 55 def getargs(self, args):
57 56 knownargs = self._args()
58 57 data = {}
59 58 keys = args.split()
60 59 for k in keys:
61 60 if k == '*':
62 61 star = {}
63 62 for key in knownargs.keys():
64 63 if key != 'cmd' and key not in keys:
65 64 star[key] = knownargs[key][0]
66 65 data['*'] = star
67 66 else:
68 67 data[k] = knownargs[k][0]
69 68 return [data[k] for k in keys]
70 69 def _args(self):
71 70 args = self.req.form.copy()
72 71 if pycompat.ispy3:
73 72 args = {k.encode('ascii'): [v.encode('ascii') for v in vs]
74 73 for k, vs in args.items()}
75 74 postlen = int(self.req.env.get(r'HTTP_X_HGARGS_POST', 0))
76 75 if postlen:
77 76 args.update(cgi.parse_qs(
78 77 self.req.read(postlen), keep_blank_values=True))
79 78 return args
80 79
81 80 argvalue = decodevaluefromheaders(self.req, r'X-HgArg')
82 81 args.update(cgi.parse_qs(argvalue, keep_blank_values=True))
83 82 return args
84 83 def getfile(self, fp):
85 84 length = int(self.req.env[r'CONTENT_LENGTH'])
86 85 # If httppostargs is used, we need to read Content-Length
87 86 # minus the amount that was consumed by args.
88 87 length -= int(self.req.env.get(r'HTTP_X_HGARGS_POST', 0))
89 88 for s in util.filechunkiter(self.req, limit=length):
90 89 fp.write(s)
91 90 def redirect(self):
92 91 self.oldio = self.ui.fout, self.ui.ferr
93 92 self.ui.ferr = self.ui.fout = stringio()
94 93 def restore(self):
95 94 val = self.ui.fout.getvalue()
96 95 self.ui.ferr, self.ui.fout = self.oldio
97 96 return val
98 97
99 98 def _client(self):
100 99 return 'remote:%s:%s:%s' % (
101 100 self.req.env.get('wsgi.url_scheme') or 'http',
102 101 urlreq.quote(self.req.env.get('REMOTE_HOST', '')),
103 102 urlreq.quote(self.req.env.get('REMOTE_USER', '')))
104 103
105 104 def responsetype(self, prefer_uncompressed):
106 105 """Determine the appropriate response type and compression settings.
107 106
108 107 Returns a tuple of (mediatype, compengine, engineopts).
109 108 """
110 109 # Determine the response media type and compression engine based
111 110 # on the request parameters.
112 111 protocaps = decodevaluefromheaders(self.req, r'X-HgProto').split(' ')
113 112
114 113 if '0.2' in protocaps:
115 114 # All clients are expected to support uncompressed data.
116 115 if prefer_uncompressed:
117 116 return HGTYPE2, util._noopengine(), {}
118 117
119 118 # Default as defined by wire protocol spec.
120 119 compformats = ['zlib', 'none']
121 120 for cap in protocaps:
122 121 if cap.startswith('comp='):
123 122 compformats = cap[5:].split(',')
124 123 break
125 124
126 125 # Now find an agreed upon compression format.
127 126 for engine in wireproto.supportedcompengines(self.ui, self,
128 127 util.SERVERROLE):
129 128 if engine.wireprotosupport().name in compformats:
130 129 opts = {}
131 130 level = self.ui.configint('server',
132 131 '%slevel' % engine.name())
133 132 if level is not None:
134 133 opts['level'] = level
135 134
136 135 return HGTYPE2, engine, opts
137 136
138 137 # No mutually supported compression format. Fall back to the
139 138 # legacy protocol.
140 139
141 140 # Don't allow untrusted settings because disabling compression or
142 141 # setting a very high compression level could lead to flooding
143 142 # the server's network or CPU.
144 143 opts = {'level': self.ui.configint('server', 'zliblevel')}
145 144 return HGTYPE, util.compengines['zlib'], opts
146 145
147 146 def iscmd(cmd):
148 147 return cmd in wireproto.commands
149 148
150 149 def call(repo, req, cmd):
151 150 p = webproto(req, repo.ui)
152 151
153 152 def genversion2(gen, engine, engineopts):
154 153 # application/mercurial-0.2 always sends a payload header
155 154 # identifying the compression engine.
156 155 name = engine.wireprotosupport().name
157 156 assert 0 < len(name) < 256
158 157 yield struct.pack('B', len(name))
159 158 yield name
160 159
161 160 for chunk in gen:
162 161 yield chunk
163 162
164 163 rsp = wireproto.dispatch(repo, p, cmd)
165 164 if isinstance(rsp, bytes):
166 165 req.respond(HTTP_OK, HGTYPE, body=rsp)
167 166 return []
168 167 elif isinstance(rsp, wireproto.streamres_legacy):
169 168 gen = rsp.gen
170 169 req.respond(HTTP_OK, HGTYPE)
171 170 return gen
172 171 elif isinstance(rsp, wireproto.streamres):
173 172 gen = rsp.gen
174 173
175 174 # This code for compression should not be streamres specific. It
176 175 # is here because we only compress streamres at the moment.
177 176 mediatype, engine, engineopts = p.responsetype(rsp.prefer_uncompressed)
178 177 gen = engine.compressstream(gen, engineopts)
179 178
180 179 if mediatype == HGTYPE2:
181 180 gen = genversion2(gen, engine, engineopts)
182 181
183 182 req.respond(HTTP_OK, mediatype)
184 183 return gen
185 184 elif isinstance(rsp, wireproto.pushres):
186 185 val = p.restore()
187 186 rsp = '%d\n%s' % (rsp.res, val)
188 187 req.respond(HTTP_OK, HGTYPE, body=rsp)
189 188 return []
190 189 elif isinstance(rsp, wireproto.pusherr):
191 190 # drain the incoming bundle
192 191 req.drain()
193 192 p.restore()
194 193 rsp = '0\n%s\n' % rsp.res
195 194 req.respond(HTTP_OK, HGTYPE, body=rsp)
196 195 return []
197 196 elif isinstance(rsp, wireproto.ooberror):
198 197 rsp = rsp.message
199 198 req.respond(HTTP_OK, HGERRTYPE, body=rsp)
200 199 return []
201 200 raise error.ProgrammingError('hgweb.protocol internal failure', rsp)
General Comments 0
You need to be logged in to leave comments. Login now