[PATCH 2 of 2] hgweb: configuration variables to customize absolute urls

Alexander Solovyov piranha at piranha.org.ua
Thu Apr 9 10:38:29 CDT 2009


# HG changeset patch
# User Alexander Solovyov <piranha at piranha.org.ua>
# Date 1239291498 -10800
# Node ID 88f917fa8c01f5514875a38ccc0073b0dd86e2cb
# Parent  ccc09593c00ff5edaf165e582c84193fdece2f71
hgweb: configuration variables to customize absolute urls

Makes possible to specify hostname, port and protocol, which is used in absolute
url building, in webdir-conf. This allows usage of hgweb behind mod_proxy or
nginx.

diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt
--- a/doc/hgrc.5.txt
+++ b/doc/hgrc.5.txt
@@ -765,6 +765,12 @@ web::
   push_ssl;;
     Whether to require that inbound pushes be transported over SSL to
     prevent password sniffing. Default is true.
+  server_name;;
+    Override SERVER_NAME variable from WSGI environment
+  server_port;;
+    Override SERVER_PORT variable from WSGI environment
+  server_proto;;
+    Override wsgi.url_scheme variable from WSGI environment
   staticurl;;
     Base URL to use for static files. If unset, static files (e.g.
     the hgicon.png favicon) will be served by the CGI script itself.
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
@@ -194,22 +194,29 @@ class hgweb(object):
             req.respond(inst, ctype)
             return tmpl('error', error=inst.message)
 
-    def templater(self, req):
+    def get_urlbase(self, req):
+        '''Determine scheme, port and server name
 
-        # determine scheme, port and server name
-        # this is needed to create absolute urls
+        This is needed to create absolute urls'''
 
-        proto = req.env.get('wsgi.url_scheme')
+        proto = self.config('web', 'server_proto')
+        if not proto:
+            proto = req.env.get('wsgi.url_scheme')
         if proto == 'https':
-            proto = 'https'
             default_port = "443"
         else:
             proto = 'http'
             default_port = "80"
 
-        port = req.env["SERVER_PORT"]
+        port = self.config('web', 'server_port') or req.env["SERVER_PORT"]
         port = port != default_port and (":" + port) or ""
-        urlbase = '%s://%s%s' % (proto, req.env['SERVER_NAME'], port)
+        name = self.config('web', 'server_name') or req.env['SERVER_NAME']
+
+        return '%s://%s%s' % (proto, name, port)
+
+    def templater(self, req):
+
+        urlbase = self.get_urlbase(req)
         staticurl = self.config("web", "staticurl") or req.url + 'static/'
         if not staticurl.endswith('/'):
             staticurl += '/'


More information about the Mercurial-devel mailing list