[PATCH] hgweb: fix url base generation to handle absolute paths in baseurl

Alexander Solovyov piranha at piranha.org.ua
Tue May 12 12:52:24 CDT 2009


# HG changeset patch
# User Alexander Solovyov <piranha at piranha.org.ua>
# Date 1242150637 -10800
# Node ID 54e482b3886802178da460bebfca299f3867be51
# Parent  1f0f01bc86a505ce227d6778af38bc67cff7e85e
hgweb: fix url base generation to handle absolute paths in baseurl

Adds possibility to specify hostname, port and protocol, used in absolute url
building (f.e. in atom urls), in webdir-conf via web.baseurl. This allows usage
of hgweb behind mod_proxy or nginx.

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
@@ -106,7 +106,7 @@ class hgweb(object):
         # work with CGI variables to create coherent structure
         # use SCRIPT_NAME, PATH_INFO and QUERY_STRING as well as our REPO_NAME
 
-        req.url = req.env['SCRIPT_NAME']
+        req.url = self.config('web', 'baseurl', req.env['SCRIPT_NAME'])
         if not req.url.endswith('/'):
             req.url += '/'
         if 'REPO_NAME' in req.env:
@@ -201,17 +201,24 @@ class hgweb(object):
         # determine scheme, port and server name
         # this is needed to create absolute urls
 
-        proto = req.env.get('wsgi.url_scheme')
-        if proto == 'https':
-            proto = 'https'
-            default_port = "443"
+        if not '://' in req.url:
+            proto = req.env.get('wsgi.url_scheme')
+            if proto == 'https':
+                proto = 'https'
+                default_port = "443"
+            else:
+                proto = 'http'
+                default_port = "80"
+
+            if not req.url.startswith('//'):
+                port = req.env["SERVER_PORT"]
+                port = port != default_port and (":" + port) or ""
+                urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
+            else:
+                urlbase = proto + '://'
         else:
-            proto = 'http'
-            default_port = "80"
+            urlbase = ''
 
-        port = req.env["SERVER_PORT"]
-        port = port != default_port and (":" + port) or ""
-        urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
         staticurl = self.config("web", "staticurl") or req.url + 'static/'
         if not staticurl.endswith('/'):
             staticurl += '/'
diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py
+++ b/mercurial/hgweb/hgwebdir_mod.py
@@ -43,7 +43,6 @@ class hgwebdir(object):
         self.stripecount = self.ui.config('web', 'stripes', 1)
         if self.stripecount:
             self.stripecount = int(self.stripecount)
-        self._baseurl = self.ui.config('web', 'baseurl')
 
         if self.repos:
             return
@@ -206,11 +205,15 @@ class hgwebdir(object):
                     continue
 
                 parts = [name]
+                baseurl = self.ui.config('web', 'baseurl')
                 if 'PATH_INFO' in req.env:
                     parts.insert(0, req.env['PATH_INFO'].rstrip('/'))
-                if req.env['SCRIPT_NAME']:
+                if req.env['SCRIPT_NAME'] and baseurl is None:
                     parts.insert(0, req.env['SCRIPT_NAME'])
-                url = ('/'.join(parts).replace("//", "/")) + '/'
+                url = ('/'.join(parts)).replace('//', '/') + '/'
+                if baseurl is not None:
+                    baseurl = baseurl.rstrip('/')
+                    url = baseurl + url
 
                 # update time with local timezone
                 try:
@@ -260,9 +263,6 @@ class hgwebdir(object):
                             and "-" or "", column))
                 for column in sortable]
 
-        if self._baseurl is not None:
-            req.env['SCRIPT_NAME'] = self._baseurl
-
         return tmpl("index", entries=entries, subdir=subdir,
                     sortcolumn=sortcolumn, descending=descending,
                     **dict(sort))
@@ -284,10 +284,7 @@ class hgwebdir(object):
         def config(section, name, default=None, untrusted=True):
             return self.ui.config(section, name, default, untrusted)
 
-        if self._baseurl is not None:
-            req.env['SCRIPT_NAME'] = self._baseurl
-
-        url = req.env.get('SCRIPT_NAME', '')
+        url = self.ui.config('web', 'baseurl', req.env.get('SCRIPT_NAME', ''))
         if not url.endswith('/'):
             url += '/'
 


More information about the Mercurial-devel mailing list