hgweb: don't choke when an inexistent style is requested (issue1901)
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 12 Nov 2009 16:39:11 +0100
changeset 9842 d3dbdca92458
parent 9841 7cd6dee6fe37
child 9843 d1043c2ffe6c
hgweb: don't choke when an inexistent style is requested (issue1901)
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/hgwebdir_mod.py
mercurial/templater.py
tests/test-hgweb
tests/test-hgweb.out
--- a/mercurial/hgweb/hgweb_mod.py	Thu Nov 12 10:29:40 2009 -0500
+++ b/mercurial/hgweb/hgweb_mod.py	Thu Nov 12 16:39:11 2009 +0100
@@ -237,14 +237,17 @@
         # figure out which style to use
 
         vars = {}
-        style = self.config("web", "style", "paper")
-        if 'style' in req.form:
-            style = req.form['style'][0]
+        styles = (
+            req.form.get('style', [None])[0],
+            self.config('web', 'style'),
+            'paper',
+        )
+        style, mapfile = templater.stylemap(styles, self.templatepath)
+        if style == styles[0]:
             vars['style'] = style
 
         start = req.url[-1] == '?' and '&' or '?'
         sessionvars = webutil.sessionvars(vars, start)
-        mapfile = templater.stylemap(style, self.templatepath)
 
         if not self.reponame:
             self.reponame = (self.config("web", "name")
--- a/mercurial/hgweb/hgwebdir_mod.py	Thu Nov 12 10:29:40 2009 -0500
+++ b/mercurial/hgweb/hgwebdir_mod.py	Thu Nov 12 16:39:11 2009 +0100
@@ -315,18 +315,21 @@
             url += '/'
 
         vars = {}
-        style = self.style
-        if 'style' in req.form:
-            vars['style'] = style = req.form['style'][0]
+        styles = (
+            req.form.get('style', [None])[0],
+            config('web', 'style'),
+            'paper'
+        )
+        style, mapfile = templater.stylemap(styles)
+        if style == styles[0]:
+            vars['style'] = style
+        
         start = url[-1] == '?' and '&' or '?'
         sessionvars = webutil.sessionvars(vars, start)
-
         staticurl = config('web', 'staticurl') or url + 'static/'
         if not staticurl.endswith('/'):
             staticurl += '/'
 
-        style = 'style' in req.form and req.form['style'][0] or self.style
-        mapfile = templater.stylemap(style)
         tmpl = templater.templater(mapfile,
                                    defaults={"header": header,
                                              "footer": footer,
--- a/mercurial/templater.py	Thu Nov 12 10:29:40 2009 -0500
+++ b/mercurial/templater.py	Thu Nov 12 16:39:11 2009 +0100
@@ -220,7 +220,7 @@
 
     return normpaths
 
-def stylemap(style, paths=None):
+def stylemap(styles, paths=None):
     """Return path to mapfile for a given style.
 
     Searches mapfile in the following locations:
@@ -234,12 +234,20 @@
     elif isinstance(paths, str):
         paths = [paths]
 
-    locations = style and [os.path.join(style, "map"), "map-" + style] or []
-    locations.append("map")
-    for path in paths:
-        for location in locations:
-            mapfile = os.path.join(path, location)
-            if os.path.isfile(mapfile):
-                return mapfile
+    if isinstance(styles, str):
+        styles = [styles]
+
+    for style in styles:
+    	
+        if not style:
+            continue
+        locations = [os.path.join(style, 'map'), 'map-' + style]
+        locations.append('map')
+
+        for path in paths:
+            for location in locations:
+                mapfile = os.path.join(path, location)
+                if os.path.isfile(mapfile):
+                    return style, mapfile
 
     raise RuntimeError("No hgweb templates found in %r" % paths)
--- a/tests/test-hgweb	Thu Nov 12 10:29:40 2009 -0500
+++ b/tests/test-hgweb	Thu Nov 12 16:39:11 2009 +0100
@@ -7,8 +7,10 @@
 echo foo > da/foo
 echo foo > foo
 hg ci -Ambase
+
 hg serve -n test -p $HGPORT -d --pid-file=hg.pid -A access.log -E errors.log
 cat hg.pid >> $DAEMON_PIDS
+
 echo % manifest
 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/?style=raw')
 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/da?style=raw')
@@ -30,6 +32,9 @@
 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/bork'
 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/diff/tip/bork?style=raw'
 
+echo % try bad style
+("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/?style=foobar')
+
 echo % stop and restart
 "$TESTDIR/killdaemons.py"
 hg serve -p $HGPORT -d --pid-file=hg.pid -A access.log
--- a/tests/test-hgweb.out	Thu Nov 12 10:29:40 2009 -0500
+++ b/tests/test-hgweb.out	Thu Nov 12 16:39:11 2009 +0100
@@ -148,8 +148,96 @@
 
 
 error: bork@2ef0ac749a14: not found in manifest
+% try bad style
+200 Script output follows
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
+<head>
+<link rel="icon" href="/static/hgicon.png" type="image/png" />
+<meta name="robots" content="index, nofollow" />
+<link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
+
+<title>test: 2ef0ac749a14 /</title>
+</head>
+<body>
+
+<div class="container">
+<div class="menu">
+<div class="logo">
+<a href="http://mercurial.selenic.com/">
+<img src="/static/hglogo.png" alt="mercurial" /></a>
+</div>
+<ul>
+<li><a href="/shortlog/2ef0ac749a14">log</a></li>
+<li><a href="/graph/2ef0ac749a14">graph</a></li>
+<li><a href="/tags">tags</a></li>
+<li><a href="/branches">branches</a></li>
+</ul>
+<ul>
+<li><a href="/rev/2ef0ac749a14">changeset</a></li>
+<li class="active">browse</li>
+</ul>
+<ul>
+
+</ul>
+</div>
+
+<div class="main">
+<h2><a href="/">test</a></h2>
+<h3>directory / @ 0:2ef0ac749a14 <span class="tag">tip</span> </h3>
+
+<form class="search" action="/log">
+
+<p><input name="rev" id="search1" type="text" size="30" /></p>
+<div id="hint">find changesets by author, revision,
+files, or words in the commit message</div>
+</form>
+
+<table class="bigtable">
+<tr>
+  <th class="name">name</th>
+  <th class="size">size</th>
+  <th class="permissions">permissions</th>
+</tr>
+<tr class="fileline parity0">
+  <td class="name"><a href="/file/2ef0ac749a14/">[up]</a></td>
+  <td class="size"></td>
+  <td class="permissions">drwxr-xr-x</td>
+</tr>
+
+<tr class="fileline parity1">
+<td class="name">
+<a href="/file/2ef0ac749a14/da">
+<img src="/static/coal-folder.png" alt="dir."/> da/
+</a>
+<a href="/file/2ef0ac749a14/da/">
+
+</a>
+</td>
+<td class="size"></td>
+<td class="permissions">drwxr-xr-x</td>
+</tr>
+
+<tr class="fileline parity0">
+<td class="filename">
+<a href="/file/2ef0ac749a14/foo">
+<img src="/static/coal-file.png" alt="file"/> foo
+</a>
+</td>
+<td class="size">4</td>
+<td class="permissions">-rw-r--r--</td>
+</tr>
+</table>
+</div>
+</div>
+
+
+</body>
+</html>
+
 % stop and restart
-9 log lines written
+10 log lines written
 % static file
 200 Script output follows