Merge "Add option for label to static web listings"

This commit is contained in:
Jenkins 2016-02-24 03:30:07 +00:00 committed by Gerrit Code Review
commit f75e4f2d77
2 changed files with 29 additions and 7 deletions

View File

@ -1,4 +1,4 @@
# Copyright (c) 2010-2012 OpenStack Foundation # Copyright (c) 2010-2016 OpenStack Foundation
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -68,6 +68,12 @@ the .../listing.css style sheet. If you "view source" in your browser on a
listing page, you will see the well defined document structure that can be listing page, you will see the well defined document structure that can be
styled. styled.
By default, the listings will be rendered with a label of
"Listing of /v1/account/container/path". This can be altered by
setting a ``X-Container-Meta-Web-Listings-Label: <label>``. For example,
if the label is set to "example.com", a label of
"Listing of example.com/path" will be used instead.
The content-type of directory marker objects can be modified by setting The content-type of directory marker objects can be modified by setting
the ``X-Container-Meta-Web-Directory-Type`` header. If the header is not set, the ``X-Container-Meta-Web-Directory-Type`` header. If the header is not set,
application/directory is used by default. Directory marker objects are application/directory is used by default. Directory marker objects are
@ -150,7 +156,7 @@ class _StaticWebContext(WSGIContext):
self.agent = '%(orig)s StaticWeb' self.agent = '%(orig)s StaticWeb'
# Results from the last call to self._get_container_info. # Results from the last call to self._get_container_info.
self._index = self._error = self._listings = self._listings_css = \ self._index = self._error = self._listings = self._listings_css = \
self._dir_type = None self._dir_type = self._listings_label = None
def _error_response(self, response, env, start_response): def _error_response(self, response, env, start_response):
""" """
@ -199,6 +205,7 @@ class _StaticWebContext(WSGIContext):
self._index = meta.get('web-index', '').strip() self._index = meta.get('web-index', '').strip()
self._error = meta.get('web-error', '').strip() self._error = meta.get('web-error', '').strip()
self._listings = meta.get('web-listings', '').strip() self._listings = meta.get('web-listings', '').strip()
self._listings_label = meta.get('web-listings-label', '').strip()
self._listings_css = meta.get('web-listings-css', '').strip() self._listings_css = meta.get('web-listings-css', '').strip()
self._dir_type = meta.get('web-directory-type', '').strip() self._dir_type = meta.get('web-directory-type', '').strip()
@ -210,12 +217,18 @@ class _StaticWebContext(WSGIContext):
:param start_response: The original WSGI start_response hook. :param start_response: The original WSGI start_response hook.
:param prefix: Any prefix desired for the container listing. :param prefix: Any prefix desired for the container listing.
""" """
label = env['PATH_INFO']
if self._listings_label:
groups = env['PATH_INFO'].split('/')
label = '{0}/{1}'.format(self._listings_label,
'/'.join(groups[4:]))
if not config_true_value(self._listings): if not config_true_value(self._listings):
body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ' \ body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ' \
'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n' \ 'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n' \
'<html>\n' \ '<html>\n' \
'<head>\n' \ '<head>\n' \
'<title>Listing of %s</title>\n' % cgi.escape(env['PATH_INFO']) '<title>Listing of %s</title>\n' % cgi.escape(label)
if self._listings_css: if self._listings_css:
body += ' <link rel="stylesheet" type="text/css" ' \ body += ' <link rel="stylesheet" type="text/css" ' \
'href="%s" />\n' % self._build_css_path(prefix or '') 'href="%s" />\n' % self._build_css_path(prefix or '')
@ -261,8 +274,7 @@ class _StaticWebContext(WSGIContext):
'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n' \ 'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n' \
'<html>\n' \ '<html>\n' \
' <head>\n' \ ' <head>\n' \
' <title>Listing of %s</title>\n' % \ ' <title>Listing of %s</title>\n' % cgi.escape(label)
cgi.escape(env['PATH_INFO'])
if self._listings_css: if self._listings_css:
body += ' <link rel="stylesheet" type="text/css" ' \ body += ' <link rel="stylesheet" type="text/css" ' \
'href="%s" />\n' % (self._build_css_path(prefix)) 'href="%s" />\n' % (self._build_css_path(prefix))
@ -281,8 +293,7 @@ class _StaticWebContext(WSGIContext):
' <th class="colname">Name</th>\n' \ ' <th class="colname">Name</th>\n' \
' <th class="colsize">Size</th>\n' \ ' <th class="colsize">Size</th>\n' \
' <th class="coldate">Date</th>\n' \ ' <th class="coldate">Date</th>\n' \
' </tr>\n' % \ ' </tr>\n' % cgi.escape(label)
cgi.escape(env['PATH_INFO'])
if prefix: if prefix:
body += ' <tr id="parent" class="item">\n' \ body += ' <tr id="parent" class="item">\n' \
' <td class="colname"><a href="../">../</a></td>\n' \ ' <td class="colname"><a href="../">../</a></td>\n' \

View File

@ -39,6 +39,8 @@ meta_map = {
'web-listings-css': 'listing.css'}}, 'web-listings-css': 'listing.css'}},
'c6': {'meta': {'web-listings': 't', 'c6': {'meta': {'web-listings': 't',
'web-error': 'error.html'}}, 'web-error': 'error.html'}},
'c6b': {'meta': {'web-listings': 't',
'web-listings-label': 'foo'}},
'c7': {'meta': {'web-listings': 'f'}}, 'c7': {'meta': {'web-listings': 'f'}},
'c8': {'meta': {'web-error': 'error.html', 'c8': {'meta': {'web-error': 'error.html',
'web-listings': 't', 'web-listings': 't',
@ -186,6 +188,8 @@ class FakeApp(object):
return Response(status='404 Not Found')(env, start_response) return Response(status='404 Not Found')(env, start_response)
elif env['PATH_INFO'] == '/v1/a/c6': elif env['PATH_INFO'] == '/v1/a/c6':
return self.listing(env, start_response) return self.listing(env, start_response)
elif env['PATH_INFO'] == '/v1/a/c6b':
return self.listing(env, start_response)
elif env['PATH_INFO'] == '/v1/a/c6/subdir': elif env['PATH_INFO'] == '/v1/a/c6/subdir':
return Response(status='404 Not Found')(env, start_response) return Response(status='404 Not Found')(env, start_response)
elif env['PATH_INFO'] == '/v1/a/c6/401error.html': elif env['PATH_INFO'] == '/v1/a/c6/401error.html':
@ -651,6 +655,13 @@ class TestStaticWeb(unittest.TestCase):
self.assertEqual(resp.status_int, 401) self.assertEqual(resp.status_int, 401)
self.assertNotIn("Hey, you're not authorized to see this!", resp.body) self.assertNotIn("Hey, you're not authorized to see this!", resp.body)
def test_container6blisting(self):
label = 'Listing of {0}/'.format(
meta_map['c6b']['meta']['web-listings-label'])
resp = Request.blank('/v1/a/c6b/').get_response(self.test_staticweb)
self.assertEqual(resp.status_int, 200)
self.assertIn(label, resp.body)
def test_container7listing(self): def test_container7listing(self):
resp = Request.blank('/v1/a/c7/').get_response(self.test_staticweb) resp = Request.blank('/v1/a/c7/').get_response(self.test_staticweb)
self.assertEqual(resp.status_int, 404) self.assertEqual(resp.status_int, 404)