Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hgweb/protocol.py @ 11595:368cd5325348
protocol: move hgweb protocol support back into protocol.py
- introduce iscmd
- simplify error handling
- remove unneeded imports
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Thu, 15 Jul 2010 15:05:04 -0500 |
parents | 67863f9d805f |
children | 83070a9cd526 |
comparison
equal
deleted
inserted
replaced
11594:67863f9d805f | 11595:368cd5325348 |
---|---|
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
4 # | 4 # |
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 import cStringIO, zlib, tempfile, errno, os, sys, urllib, copy | 8 import cStringIO, zlib, sys, urllib |
9 from mercurial import util, streamclone, pushkey | 9 from mercurial import util, wireproto |
10 from mercurial.node import bin, hex | 10 from common import HTTP_OK |
11 from mercurial import changegroup as changegroupmod | |
12 from common import ErrorResponse, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR | |
13 | |
14 # __all__ is populated with the allowed commands. Be sure to add to it if | |
15 # you're adding a new command, or the new command won't work. | |
16 | |
17 __all__ = [ | |
18 'lookup', 'heads', 'branches', 'between', 'changegroup', | |
19 'changegroupsubset', 'capabilities', 'unbundle', 'stream_out', | |
20 'branchmap', 'pushkey', 'listkeys' | |
21 ] | |
22 | 11 |
23 HGTYPE = 'application/mercurial-0.1' | 12 HGTYPE = 'application/mercurial-0.1' |
13 | |
14 class webproto(object): | |
15 def __init__(self, req): | |
16 self.req = req | |
17 self.response = '' | |
18 def getargs(self, args): | |
19 data = {} | |
20 keys = args.split() | |
21 for k in keys: | |
22 if k == '*': | |
23 star = {} | |
24 for key in self.req.form.keys(): | |
25 if key not in keys: | |
26 star[key] = self.req.form[key][0] | |
27 data['*'] = star | |
28 else: | |
29 data[k] = self.req.form[k][0] | |
30 return [data[k] for k in keys] | |
31 def sendchangegroup(self, cg): | |
32 self.req.respond(HTTP_OK, HGTYPE) | |
33 z = zlib.compressobj() | |
34 while 1: | |
35 chunk = cg.read(4096) | |
36 if not chunk: | |
37 break | |
38 self.req.write(z.compress(chunk)) | |
39 self.req.write(z.flush()) | |
40 def sendstream(self, source): | |
41 self.req.respond(HTTP_OK, HGTYPE) | |
42 for chunk in source: | |
43 self.req.write(chunk) | |
44 def respond(self, s): | |
45 self.req.respond(HTTP_OK, HGTYPE, length=len(s)) | |
46 self.response = s | |
47 def getfile(self, fp): | |
48 length = int(self.req.env['CONTENT_LENGTH']) | |
49 for s in util.filechunkiter(self.req, limit=length): | |
50 fp.write(s) | |
51 def redirect(self): | |
52 self.oldio = sys.stdout, sys.stderr | |
53 sys.stderr = sys.stdout = cStringIO.StringIO() | |
54 def respondpush(self, ret): | |
55 val = sys.stdout.getvalue() | |
56 sys.stdout, sys.stderr = self.oldio | |
57 self.req.respond(HTTP_OK, HGTYPE) | |
58 self.response = '%d\n%s' % (ret, val) | |
59 def _client(self): | |
60 return 'remote:%s:%s:%s' % ( | |
61 self.req.env.get('wsgi.url_scheme') or 'http', | |
62 urllib.quote(self.req.env.get('REMOTE_HOST', '')), | |
63 urllib.quote(self.req.env.get('REMOTE_USER', ''))) | |
64 | |
65 def iscmd(cmd): | |
66 return cmd in wireproto.commands | |
67 | |
68 def call(repo, req, cmd): | |
69 p = webproto(req) | |
70 r = wireproto.dispatch(repo, p, cmd) | |
71 yield p.response |