[PATCH 1 of 1] hgwebdir_mod.py can now handle 'baseurl' configurations with leading slash

Matt Mackall mpm at selenic.com
Mon Aug 1 13:10:11 CDT 2011


On Mon, 2011-08-01 at 12:03 +0200, wujek.srujek at googlemail.com wrote:
> # HG changeset patch
> # User wujek
> # Date 1312184890 -7200
> # Node ID 060a118fc62277c5cd71751503db19a7b4183a21
> # Parent  cc2c22511707b13d045f17bb34e865d2393c53cf
> hgwebdir_mod.py can now handle 'baseurl' configurations with leading slash

I've queued the fix for stable, without the test.

Testing hgweb's path handling is known to be extremely trick. We can't
reasonably emulate all the existing web environments and configs in our
test suite. So a doctest-style unit test is the right way to move
forward here. That means:

a) making a purely functional helper function that does the path
manipulations (no environment or config inputs)
b) replace the current inline code with a call to the helper function
c) test all the interesting permutations in a doctest

Here's what I've done on the default branch:

diff -r 00862c7d3eec mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py	Mon Aug 01 12:54:00 2011 -0500
+++ b/mercurial/hgweb/hgwebdir_mod.py	Mon Aug 01 13:08:14 2011 -0500
@@ -51,6 +51,29 @@
         yield (prefix + '/' +
                util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
 
+def geturlcgivars(baseurl, port):
+    """
+    Extract CGI variables from baseurl
+
+    >>> geturlcgivars("http://host.org/base", "80")
+    ('host.org', '80', '/base')
+    >>> geturlcgivars("http://host.org:8000/base", "80")
+    ('host.org', '8000', '/base')
+    >>> geturlcgivars('/base', 8000)
+    ('', '8000', '/base')
+    >>> geturlcgivars("base", '8000')
+    ('', '8000', '/base')
+    """
+    u = util.url(baseurl)
+    name = u.host or ''
+    if u.port:
+        port = u.port
+    path = u.path
+    if not path.startswith('/'):
+        path = '/' + path
+
+    return name, str(port), path
+
 class hgwebdir(object):
     refreshinterval = 20
 
@@ -366,11 +389,7 @@
 
     def updatereqenv(self, env):
         if self._baseurl is not None:
-            u = util.url(self._baseurl)
-            env['SERVER_NAME'] = u.host
-            if u.port:
-                env['SERVER_PORT'] = u.port
-            path = u.path
-            if not path.startswith('/'):
-                path = '/' + path
+            name, port, path = geturlcgivars(self._baseurl, env['SERVER_PORT'])
+            env['SERVER_NAME'] = name
+            env['SERVER_PORT'] = port
             env['SCRIPT_NAME'] = path


-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list