[PATCH] minimal DAV support for hgweb

Johan Euphrosine proppy at aminche.com
Thu Nov 22 08:56:38 CST 2007


# HG changeset patch
# User Johan Euphrosine <proppy at aminche.com>
# Date 1195742675 0
# Node ID fda1df4d64d7bbbebb6bf7bd47949b5f69c8d6ef
# Parent  00b812ad67cbd31d7f4309d8a96f61e31b33f473
minimal DAV support for hgweb

Attached is a patch adding PUT method handling for hgweb:
PUT /raw-file/tip/test
Content-length: 7
content
commit the body of the request: 'content' to /test file with tip as
the parent revision.

diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -897,15 +897,26 @@ class hgweb(object):
         self.do_changeset(req)
 
     def do_file(self, req):
-        path = self.cleanpath(req.form.get('file', [''])[0])
-        if path:
-            try:
-                req.write(self.filerevision(self.filectx(req)))
-                return
-            except revlog.LookupError:
-                pass
-
-        req.write(self.manifest(self.changectx(req), path))
+        if req.env['REQUEST_METHOD'] == 'GET':
+            path = self.cleanpath(req.form.get('file', [''])[0])
+            if path:
+                try:
+                    req.write(self.filerevision(self.filectx(req)))
+                    return
+                except revlog.LookupError:
+                    pass
+            req.write(self.manifest(self.changectx(req), path))
+        elif req.env['REQUEST_METHOD'] == 'PUT':
+            input = req.env['wsgi.input']
+            length = int(req.env['CONTENT_LENGTH'])
+            body = input.read(length)
+            path = self.cleanpath(req.form['file'][0])
+            p0 = self.changectx(req).node()
+            self.repo.wwrite(path, body, "")
+            self.repo.add([path])
+            result = self.repo.rawcommit([path], "PUT %s" % path, req.env['REMOTE_ADDR'], None, p0, None)
+            req.httphdr("application/mercurial-0.1", headers={})
+            req.write(hex(result))
 
     def do_diff(self, req):
         self.do_filediff(req)
diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py
+++ b/mercurial/hgweb/server.py
@@ -74,6 +74,9 @@ class _hgwebhandler(object, BaseHTTPServ
                            self.path, tb)
 
     def do_GET(self):
+        self.do_POST()
+
+    def do_PUT(self):
         self.do_POST()
 
     def do_hgweb(self):
diff --git a/tests/test-webdav b/tests/test-webdav
new file mode 100644
--- /dev/null
+++ b/tests/test-webdav
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+hg init test
+cd test
+hg serve -p $HGPORT -d --pid-file=hg.pid
+cat >put.py <<ENDSOME
+#!/usr/bin/env python
+import httplib
+import sys
+h = httplib.HTTPConnection("localhost:$HGPORT")
+h.request("PUT", "/raw-file/tip/test", sys.stdin.read())
+print h.getresponse().read()
+ENDSOME
+chmod +x put.py
+echo % testing put1
+rev=`echo test | ./put.py`
+("$TESTDIR/get-with-headers.py" localhost:$HGPORT "/raw-file/tip/test") | grep test > /dev/null && echo OK
+echo % testing put2
+newrev=`echo test2 | ./put.py`
+("$TESTDIR/get-with-headers.py" localhost:$HGPORT "/raw-file/tip/test") | grep test2 > /dev/null && echo OK
+echo % testing put1.parent == put2
+("$TESTDIR/get-with-headers.py" localhost:$HGPORT "/raw-rev/$newrev") | grep Parent | grep $rev > /dev/null && echo OK
+kill `cat hg.pid`
diff --git a/tests/test-webdav.out b/tests/test-webdav.out
new file mode 100644
--- /dev/null
+++ b/tests/test-webdav.out
@@ -0,0 +1,6 @@
+% testing put1
+OK
+% testing put2
+OK
+% testing put1.parent == put2
+OK


More information about the Mercurial-devel mailing list