diff --git a/swift/common/middleware/staticweb.py b/swift/common/middleware/staticweb.py index 9638514da8..24199c9142 100644 --- a/swift/common/middleware/staticweb.py +++ b/swift/common/middleware/staticweb.py @@ -387,6 +387,8 @@ class StaticWeb(object): """ self._get_container_info(env, start_response) if not self._listings and not self._index: + if env.get('HTTP_X_WEB_MODE', 'f').lower() in TRUE_VALUES: + return HTTPNotFound()(env, start_response) return self.app(env, start_response) if env['PATH_INFO'][-1] != '/': resp = HTTPMovedPermanently( diff --git a/test/unit/common/middleware/test_staticweb.py b/test/unit/common/middleware/test_staticweb.py index 260d78e1b2..9c84835207 100644 --- a/test/unit/common/middleware/test_staticweb.py +++ b/test/unit/common/middleware/test_staticweb.py @@ -355,6 +355,16 @@ class TestStaticWeb(unittest.TestCase): resp = Request.blank('/v1/a/c1').get_response(self.test_staticweb) self.assertEquals(resp.status_int, 401) + def test_container1_web_mode_explicitly_off(self): + resp = Request.blank('/v1/a/c1', + headers={'x-web-mode': 'false'}).get_response(self.test_staticweb) + self.assertEquals(resp.status_int, 401) + + def test_container1_web_mode_explicitly_on(self): + resp = Request.blank('/v1/a/c1', + headers={'x-web-mode': 'true'}).get_response(self.test_staticweb) + self.assertEquals(resp.status_int, 404) + def test_container2(self): resp = Request.blank('/v1/a/c2').get_response(self.test_staticweb) self.assertEquals(resp.status_int, 200) @@ -362,6 +372,19 @@ class TestStaticWeb(unittest.TestCase): self.assertEquals(len(resp.body.split('\n')), int(resp.headers['x-container-object-count'])) + def test_container2_web_mode_explicitly_off(self): + resp = Request.blank('/v1/a/c2', + headers={'x-web-mode': 'false'}).get_response(self.test_staticweb) + self.assertEquals(resp.status_int, 200) + self.assertEquals(resp.content_type, 'text/plain') + self.assertEquals(len(resp.body.split('\n')), + int(resp.headers['x-container-object-count'])) + + def test_container2_web_mode_explicitly_on(self): + resp = Request.blank('/v1/a/c2', + headers={'x-web-mode': 'true'}).get_response(self.test_staticweb) + self.assertEquals(resp.status_int, 404) + def test_container2onetxt(self): resp = Request.blank( '/v1/a/c2/one.txt').get_response(self.test_staticweb) @@ -375,6 +398,19 @@ class TestStaticWeb(unittest.TestCase): self.assertEquals(len(json.loads(resp.body)), int(resp.headers['x-container-object-count'])) + def test_container2json_web_mode_explicitly_off(self): + resp = Request.blank('/v1/a/c2?format=json', + headers={'x-web-mode': 'false'}).get_response(self.test_staticweb) + self.assertEquals(resp.status_int, 200) + self.assertEquals(resp.content_type, 'application/json') + self.assertEquals(len(json.loads(resp.body)), + int(resp.headers['x-container-object-count'])) + + def test_container2json_web_mode_explicitly_on(self): + resp = Request.blank('/v1/a/c2?format=json', + headers={'x-web-mode': 'true'}).get_response(self.test_staticweb) + self.assertEquals(resp.status_int, 404) + def test_container3(self): resp = Request.blank('/v1/a/c3').get_response(self.test_staticweb) self.assertEquals(resp.status_int, 301)