hgweb: support for generating and parsing NWI URLs
authorBrendan Cully <brendan@kublai.com>
Wed, 04 Oct 2006 17:04:40 -0700
changeset 3263 3207e30bf468
parent 3262 1e322b44b366
child 3264 fc34c031ff54
hgweb: support for generating and parsing NWI URLs
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/hgwebdir_mod.py
mercurial/hgweb/server.py
--- a/mercurial/hgweb/hgweb_mod.py	Wed Oct 04 17:04:40 2006 -0700
+++ b/mercurial/hgweb/hgweb_mod.py	Wed Oct 04 17:04:40 2006 -0700
@@ -648,16 +648,43 @@
         def rewrite_request(req):
             '''translate new web interface to traditional format'''
 
+            def spliturl(req):
+                def firstitem(query):
+                    return query.split('&', 1)[0].split(';', 1)[0]
+
+                base = ''
+                if req.env.has_key('REPO_NAME'):
+                    base = '/' + req.env['REPO_NAME']
+                elif req.env.get('SCRIPT_NAME'):
+                    base = req.env['SCRIPT_NAME']
+
+                pi = req.env.get('PATH_INFO')
+                if pi:
+                    if pi.startswith(base):
+                        if len(pi) > len(base):
+                            base += '/'
+                            query = pi[len(base):]
+                        else:
+                            if req.env.has_key('REPO_NAME'):
+                                # We are using hgwebdir
+                                base += '/'
+                            else:
+                                base += '?'
+                            query = firstitem(req.env['QUERY_STRING'])
+                    else:
+                        base += '/'
+                        query = pi[1:]
+                else:
+                    base += '?'
+                    query = firstitem(req.env['QUERY_STRING'])
+
+                return (base, query)
+
+            req.url, query = spliturl(req)
+
             if req.form.has_key('cmd'):
                 # old style
                 return
-            if req.env.has_key('SCRIPT_URL'):
-                # run through web server
-                base = req.env['SCRIPT_URL']
-                # strip repo and leading '/' or '?'
-                query = req.env['REQUEST_URI'][len(base)+1:]
-            else:
-                query = req.env['PATH_INFO'][1:]
 
             args = query.split('/', 2)
             if not args or not args[0]:
@@ -668,7 +695,9 @@
             if style != -1:
                 req.form['style'] = [cmd[:style]]
                 cmd = cmd[style+1:]
-            req.form['cmd'] = [cmd]
+            # avoid accepting e.g. style parameter as command
+            if hasattr(self, 'do_' + cmd):
+                req.form['cmd'] = [cmd]
 
             if args and args[0]:
                 node = args.pop(0)
@@ -701,18 +730,21 @@
             if os.path.isfile(p):
                 m = p
 
-        port = req.env["SERVER_PORT"]
-        port = port != "80" and (":" + port) or ""
-        uri = req.env["REQUEST_URI"]
-        if "?" in uri:
-            uri = uri.split("?")[0]
-        url = "http://%s%s%s" % (req.env["SERVER_NAME"], port, uri)
+        if not req.url:
+            port = req.env["SERVER_PORT"]
+            port = port != "80" and (":" + port) or ""
+            uri = req.env["REQUEST_URI"]
+            if "?" in uri:
+                uri = uri.split("?")[0]
+            req.url = "http://%s%s%s" % (req.env["SERVER_NAME"], port, uri)
+
         if not self.reponame:
             self.reponame = (self.repo.ui.config("web", "name")
-                             or uri.strip('/') or self.repo.root)
+                             or req.env.get('REPO_NAME')
+                             or req.url.strip('/') or self.repo.root)
 
         self.t = templater.templater(m, templater.common_filters,
-                                     defaults={"url": url,
+                                     defaults={"url": req.url,
                                                "repo": self.reponame,
                                                "header": header,
                                                "footer": footer,
--- a/mercurial/hgweb/hgwebdir_mod.py	Wed Oct 04 17:04:40 2006 -0700
+++ b/mercurial/hgweb/hgwebdir_mod.py	Wed Oct 04 17:04:40 2006 -0700
@@ -143,7 +143,12 @@
                     yield row
 
         virtual = req.env.get("PATH_INFO", "").strip('/')
-        if virtual:
+        if virtual.startswith('static/'):
+            static = os.path.join(templater.templatepath(), 'static')
+            fname = virtual[7:]
+            req.write(staticfile(static, fname, req) or
+                      tmpl('error', error='%r not found' % fname))
+        elif virtual:
             while virtual:
                 real = dict(self.repos).get(virtual)
                 if real:
--- a/mercurial/hgweb/server.py	Wed Oct 04 17:04:40 2006 -0700
+++ b/mercurial/hgweb/server.py	Wed Oct 04 17:04:40 2006 -0700
@@ -71,7 +71,7 @@
         env['REQUEST_METHOD'] = self.command
         env['SERVER_NAME'] = self.server.server_name
         env['SERVER_PORT'] = str(self.server.server_port)
-        env['REQUEST_URI'] = "/"
+        env['REQUEST_URI'] = self.path
         env['PATH_INFO'] = path_info
         if query:
             env['QUERY_STRING'] = query