changeset 11582:26c7d4fc31bf

protocol: use new wireproto infrastructure in ssh - add protocol helper - insert wireproto into dispatcher - drop duplicate functions from hgweb implementation
author Matt Mackall <mpm@selenic.com>
date Wed, 14 Jul 2010 15:25:15 -0500
parents 4530b3307fb9
children 944c23762c3c
files mercurial/hgweb/hgweb_mod.py mercurial/hgweb/protocol.py tests/test-hgweb-commands tests/test-hgweb-commands.out
diffstat 4 files changed, 34 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/hgweb_mod.py	Wed Jul 14 15:25:15 2010 -0500
+++ b/mercurial/hgweb/hgweb_mod.py	Wed Jul 14 15:25:15 2010 -0500
@@ -7,7 +7,7 @@
 # GNU General Public License version 2 or any later version.
 
 import os
-from mercurial import ui, hg, hook, error, encoding, templater
+from mercurial import ui, hg, hook, error, encoding, templater, wireproto
 from common import get_mtime, ErrorResponse, permhooks
 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
 from request import wsgirequest
@@ -22,6 +22,33 @@
     'pushkey': 'push',
 }
 
+class webproto(object):
+    def __init__(self, req):
+        self.req = req
+        self.response = ''
+    def getargs(self, args):
+        data = {}
+        keys = args.split()
+        for k in keys:
+            if k == '*':
+                star = {}
+                for key in self.req.form.keys():
+                    if key not in keys:
+                        star[key] = self.req.form[key][0]
+                data['*'] = star
+            else:
+                data[k] = self.req.form[k][0]
+        return [data[k] for k in keys]
+    def respond(self, s):
+        HGTYPE = 'application/mercurial-0.1'
+        self.req.respond(HTTP_OK, HGTYPE, length=len(s))
+        self.response = s
+
+def callproto(repo, req, cmd):
+    p = webproto(req)
+    r = wireproto.dispatch(repo, p, cmd)
+    yield p.response
+
 class hgweb(object):
     def __init__(self, repo, name=None, baseui=None):
         if isinstance(repo, str):
@@ -123,6 +150,8 @@
                         if cmd == 'unbundle':
                             req.drain()
                         raise
+                if cmd in wireproto.commands:
+                    return callproto(self.repo, req, cmd)
                 method = getattr(protocol, cmd)
                 return method(self.repo, req)
             except ErrorResponse, inst:
--- a/mercurial/hgweb/protocol.py	Wed Jul 14 15:25:15 2010 -0500
+++ b/mercurial/hgweb/protocol.py	Wed Jul 14 15:25:15 2010 -0500
@@ -23,51 +23,6 @@
 HGTYPE = 'application/mercurial-0.1'
 basecaps = 'lookup changegroupsubset branchmap pushkey'.split()
 
-def lookup(repo, req):
-    try:
-        r = hex(repo.lookup(req.form['key'][0]))
-        success = 1
-    except Exception, inst:
-        r = str(inst)
-        success = 0
-    resp = "%s %s\n" % (success, r)
-    req.respond(HTTP_OK, HGTYPE, length=len(resp))
-    yield resp
-
-def heads(repo, req):
-    resp = " ".join(map(hex, repo.heads())) + "\n"
-    req.respond(HTTP_OK, HGTYPE, length=len(resp))
-    yield resp
-
-def branchmap(repo, req):
-    branches = repo.branchmap()
-    heads = []
-    for branch, nodes in branches.iteritems():
-        branchname = urllib.quote(branch)
-        branchnodes = [hex(node) for node in nodes]
-        heads.append('%s %s' % (branchname, ' '.join(branchnodes)))
-    resp = '\n'.join(heads)
-    req.respond(HTTP_OK, HGTYPE, length=len(resp))
-    yield resp
-
-def branches(repo, req):
-    nodes = []
-    if 'nodes' in req.form:
-        nodes = map(bin, req.form['nodes'][0].split(" "))
-    resp = cStringIO.StringIO()
-    for b in repo.branches(nodes):
-        resp.write(" ".join(map(hex, b)) + "\n")
-    resp = resp.getvalue()
-    req.respond(HTTP_OK, HGTYPE, length=len(resp))
-    yield resp
-
-def between(repo, req):
-    pairs = [map(bin, p.split("-"))
-             for p in req.form['pairs'][0].split(" ")]
-    resp = ''.join(" ".join(map(hex, b)) + "\n" for b in repo.between(pairs))
-    req.respond(HTTP_OK, HGTYPE, length=len(resp))
-    yield resp
-
 def changegroup(repo, req):
     req.respond(HTTP_OK, HGTYPE)
     nodes = []
@@ -204,22 +159,3 @@
             yield chunk
     except streamclone.StreamException, inst:
         yield str(inst)
-
-def pushkey(repo, req):
-    namespace = req.form['namespace'][0]
-    key = req.form['key'][0]
-    old = req.form['old'][0]
-    new = req.form['new'][0]
-
-    r = repo.pushkey(namespace, key, old, new)
-    r = '%d\n' % int(r)
-    req.respond(HTTP_OK, HGTYPE, length=len(r))
-    yield r
-
-def listkeys(repo, req):
-    namespace = req.form['namespace'][0]
-    d = repo.listkeys(namespace).items()
-    t = '\n'.join(['%s\t%s' % (k.encode('string-escape'),
-                               v.encode('string-escape')) for k, v in d])
-    req.respond(HTTP_OK, HGTYPE, length=len(t))
-    yield t
--- a/tests/test-hgweb-commands	Wed Jul 14 15:25:15 2010 -0500
+++ b/tests/test-hgweb-commands	Wed Jul 14 15:25:15 2010 -0500
@@ -46,9 +46,9 @@
 echo % heads
 "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '?cmd=heads'
 echo % lookup
-"$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '?cmd=lookup&node=1'
+"$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '?cmd=lookup&key=1'
 echo % branches
-"$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '?cmd=branches'
+"$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '?cmd=branches&nodes=0000000000000000000000000000000000000000'
 echo % changegroup
 "$TESTDIR/get-with-headers.py" 127.0.0.1:$HGPORT '?cmd=changegroup' \
     | $TESTDIR/printrepr.py
--- a/tests/test-hgweb-commands.out	Wed Jul 14 15:25:15 2010 -0500
+++ b/tests/test-hgweb-commands.out	Wed Jul 14 15:25:15 2010 -0500
@@ -852,11 +852,11 @@
 % lookup
 200 Script output follows
 
-0 'key'
+1 a4f92ed23982be056b9852de5dfe873eaac7f0de
 % branches
 200 Script output follows
 
-1d22e65f027e5a0609357e7d8e7508cd2ba5d2fe 2ef0ac749a14e4f57a5a822464a0902c6f7f448f 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
+0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000
 % changegroup
 200 Script output follows