Commit 6b600cb1 authored by Naoki Kambe's avatar Naoki Kambe
Browse files

[917] add two changes to stats_httpd.py and its unittest

 - change the relative path to the absolute path because in the case of the
   relative path refering to the other item is failed

 - The too long path is to be NotFound

 - change the condition of addition of anchor tag if depth levels of path are
   equal to or greater than 3, do not add the anchor tag.
parent c383ebc7
......@@ -92,12 +92,18 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
req_path = os.path.normpath(req_path)
path_dirs = req_path.split('/')
path_dirs = [ d for d in filter(None, path_dirs) ]
req_path = '/'+"/".join(path_dirs)
# The too long path is to be NotFound
# in case of /bind10/statistics/xxx/YYY/zzz/aaa
if len(path_dirs) > 5:
self.send_error(404)
return None
module_name = None
item_name = None
# in case of /bind10/statistics/xxx/YYY/zzz/
# in case of /bind10/statistics/xxx/YYY/zzz
if len(path_dirs) >= 5:
item_name = path_dirs[4]
# in case of /bind10/statistics/xxx/YYY/
# in case of /bind10/statistics/xxx/YYY
if len(path_dirs) >= 4:
module_name = path_dirs[3]
if req_path.startswith(XML_URL_PATH):
......@@ -592,7 +598,7 @@ class StatsHttpd:
def xsl_handler(self, module_name=None, item_name=None):
"""Handler which just returns the body of XSL document"""
def stats_spec2xsl(stats_spec, xsl_elem, path='.'):
def stats_spec2xsl(stats_spec, xsl_elem, path=XML_URL_PATH):
"""Internal use for xsl_handler. Reads stats_spec
specified as first arguments, and modify the xml object
specified as second argument. xsl_elem must be
......@@ -687,10 +693,15 @@ class StatsHttpd:
"title" : item_spec["item_description"] \
if "item_description" in item_spec \
else "" })
a = xml.etree.ElementTree.Element(
"a", attrib={ "href": urllib.parse.quote(path + "/" + item_spec["item_name"]) })
a.text = item_spec[ "item_title" if "item_title" in item_spec else "item_name" ]
td.append(a)
# if the path length is equal to or shorter than
# XML_URL_PATH + /Module/Item, add the anchor tag.
if len(path.split('/')) <= len((XML_URL_PATH + '/Module/Item').split('/')):
a = xml.etree.ElementTree.Element(
"a", attrib={ "href": urllib.parse.quote(path + "/" + item_spec["item_name"]) })
a.text = item_spec[ "item_title" if "item_title" in item_spec else "item_name" ]
td.append(a)
else:
td.text = item_spec[ "item_title" if "item_title" in item_spec else "item_name" ]
tr.append(td)
td = xml.etree.ElementTree.Element("td")
stats_spec2xsl(item_spec, td, path)
......@@ -711,9 +722,9 @@ class StatsHttpd:
"xsl:template",
attrib={'match': "bind10:statistics"})
if module_name is not None and item_name is not None:
stats_spec2xsl([ stats_spec ], xsd_root, './' + module_name)
stats_spec2xsl([ stats_spec ], xsd_root, XML_URL_PATH + '/' + module_name)
elif module_name is not None:
stats_spec2xsl(stats_spec, xsd_root, './' + module_name)
stats_spec2xsl(stats_spec, xsd_root, XML_URL_PATH + '/' + module_name)
else:
stats_spec2xsl(stats_spec, xsd_root)
# The coding conversion is tricky. xml..tostring() of Python 3.2
......
......@@ -283,6 +283,13 @@ class TestHttpHandler(unittest.TestCase):
response = self.client.getresponse()
self.assertEqual(response.status, 404)
# 404 NotFound (too long path)
self.client._http_vsn_str = 'HTTP/1.0'
self.client.putrequest('GET', '/bind10/statistics/xml/Boss/boot_time/a')
self.client.endheaders()
response = self.client.getresponse()
self.assertEqual(response.status, 404)
# 404 NotFound (nonexistent module name)
self.client._http_vsn_str = 'HTTP/1.0'
self.client.putrequest('GET', '/bind10/statistics/xml/Foo')
......@@ -1048,7 +1055,7 @@ class TestStatsHttpd(unittest.TestCase):
}
xsl_body1 = self.stats_httpd.open_template(
stats_httpd.XSL_TEMPLATE_LOCATION).substitute(
xsl_string='<xsl:template match="bind10:statistics"><table><tr><th>Module Names</th><th>Module Items</th></tr><xsl:for-each select="Dummy"><tr><td><a href="./Dummy">Dummy</a></td><td><table><tr><th>Item Names</th><th>Item Values</th></tr><tr><td class="title" title="foo bar"><a href="./Dummy/foo">Foo</a></td><td><xsl:value-of select="foo" /></td></tr><xsl:for-each select="foo2"><tr><td class="title" title="Foo bar"><a href="./Dummy/foo2">Foo bar</a></td><td><table><tr><th>Item Names</th><th>Item Values</th></tr><xsl:for-each select="foo2-1"><tr><td class="title" title="">foo2-1</td><td><table><tr><th>Item Names</th><th>Item Values</th></tr><tr><td class="title" title="Foo bar"><a href="./Dummy/foo2/foo2-1/foo2-1-1">Foo2 1 1</a></td><td><xsl:value-of select="foo2-1-1" /></td></tr><tr><td class="title" title="Foo bar"><a href="./Dummy/foo2/foo2-1/foo2-1-2">Foo2 1 2</a></td><td><xsl:value-of select="foo2-1-2" /></td></tr><tr><td class="title" title="Foo bar"><a href="./Dummy/foo2/foo2-1/foo2-1-3">Foo2 1 3</a></td><td><xsl:value-of select="foo2-1-3" /></td></tr></table></td></tr></xsl:for-each></table></td></tr></xsl:for-each></table></td></tr></xsl:for-each></table></xsl:template>',
xsl_string='<xsl:template match="bind10:statistics"><table><tr><th>Module Names</th><th>Module Items</th></tr><xsl:for-each select="Dummy"><tr><td><a href="' + stats_httpd.XML_URL_PATH + '/Dummy">Dummy</a></td><td><table><tr><th>Item Names</th><th>Item Values</th></tr><tr><td class="title" title="foo bar"><a href="' + stats_httpd.XML_URL_PATH + '/Dummy/foo">Foo</a></td><td><xsl:value-of select="foo" /></td></tr><xsl:for-each select="foo2"><tr><td class="title" title="Foo bar"><a href="' + stats_httpd.XML_URL_PATH + '/Dummy/foo2">Foo bar</a></td><td><table><tr><th>Item Names</th><th>Item Values</th></tr><xsl:for-each select="foo2-1"><tr><td class="title" title="">foo2-1</td><td><table><tr><th>Item Names</th><th>Item Values</th></tr><tr><td class="title" title="Foo bar">Foo2 1 1</td><td><xsl:value-of select="foo2-1-1" /></td></tr><tr><td class="title" title="Foo bar">Foo2 1 2</td><td><xsl:value-of select="foo2-1-2" /></td></tr><tr><td class="title" title="Foo bar">Foo2 1 3</td><td><xsl:value-of select="foo2-1-3" /></td></tr></table></td></tr></xsl:for-each></table></td></tr></xsl:for-each></table></td></tr></xsl:for-each></table></xsl:template>',
xsd_namespace=stats_httpd.XSD_NAMESPACE)
xsl_body2 = self.stats_httpd.xsl_handler()
self.assertEqual(type(xsl_body1), str)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment