From 5d564a98b06711646f5ca421241230810b9734da Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Tue, 2 Nov 2010 16:04:15 +0000 Subject: [PATCH] support text/xml --- swift/account/server.py | 19 ++++++++----------- swift/container/server.py | 19 ++++++++----------- test/unit/container/test_server.py | 9 ++++++++- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/swift/account/server.py b/swift/account/server.py index a51bf64054..9b98773897 100644 --- a/swift/account/server.py +++ b/swift/account/server.py @@ -199,16 +199,15 @@ class AccountController(object): except UnicodeDecodeError, err: return HTTPBadRequest(body='parameters not utf8', content_type='text/plain', request=req) - header_format = req.accept.best_match(['text/plain', - 'application/json', - 'application/xml']) - format = query_format or header_format or 'text/plain' - if '/' in format: - format = format.split('/')[-1] + if query_format: + req.accept = 'application/%s' % query_format.lower() + out_content_type = req.accept.best_match( + ['text/plain', 'application/json', + 'application/xml', 'text/xml'], + default_match='text/plain') account_list = broker.list_containers_iter(limit, marker, prefix, delimiter) - if format == 'json': - out_content_type = 'application/json' + if out_content_type == 'application/json': json_pattern = ['"name":%s', '"count":%s', '"bytes":%s'] json_pattern = '{' + ','.join(json_pattern) + '}' json_out = [] @@ -220,8 +219,7 @@ class AccountController(object): json_out.append(json_pattern % (name, object_count, bytes_used)) account_list = '[' + ','.join(json_out) + ']' - elif format == 'xml': - out_content_type = 'application/xml' + elif out_content_type.endswith('/xml'): output_list = ['', '' % account] for (name, object_count, bytes_used, is_subdir) in account_list: @@ -238,7 +236,6 @@ class AccountController(object): else: if not account_list: return HTTPNoContent(request=req, headers=resp_headers) - out_content_type = 'text/plain' account_list = '\n'.join(r[0] for r in account_list) + '\n' ret = Response(body=account_list, request=req, headers=resp_headers) ret.content_type = out_content_type diff --git a/swift/container/server.py b/swift/container/server.py index 7656b6fc75..6dbbc940e0 100644 --- a/swift/container/server.py +++ b/swift/container/server.py @@ -278,16 +278,15 @@ class ContainerController(object): except UnicodeDecodeError, err: return HTTPBadRequest(body='parameters not utf8', content_type='text/plain', request=req) - header_format = req.accept.best_match(['text/plain', - 'application/json', - 'application/xml']) - format = query_format or header_format or 'text/plain' - if '/' in format: - format = format.split('/')[-1] + if query_format: + req.accept = 'application/%s' % query_format.lower() + out_content_type = req.accept.best_match( + ['text/plain', 'application/json', + 'application/xml', 'text/xml'], + default_match='text/plain') container_list = broker.list_objects_iter(limit, marker, prefix, delimiter, path) - if format == 'json': - out_content_type = 'application/json' + if out_content_type == 'application/json': json_pattern = ['"name":%s', '"hash":"%s"', '"bytes":%s', '"content_type":%s, "last_modified":"%s"'] json_pattern = '{' + ','.join(json_pattern) + '}' @@ -307,8 +306,7 @@ class ContainerController(object): content_type, created_at)) container_list = '[' + ','.join(json_out) + ']' - elif format == 'xml': - out_content_type = 'application/xml' + elif out_content_type.endswith('/xml'): xml_output = [] for (name, created_at, size, content_type, etag) in container_list: # escape name and format date here @@ -330,7 +328,6 @@ class ContainerController(object): else: if not container_list: return HTTPNoContent(request=req, headers=resp_headers) - out_content_type = 'text/plain' container_list = '\n'.join(r[0] for r in container_list) + '\n' ret = Response(body=container_list, request=req, headers=resp_headers) ret.content_type = out_content_type diff --git a/test/unit/container/test_server.py b/test/unit/container/test_server.py index a80090185c..fdad12c0f8 100644 --- a/test/unit/container/test_server.py +++ b/test/unit/container/test_server.py @@ -619,7 +619,7 @@ class TestContainerController(unittest.TestCase): self.assertEquals(resp.body, xml_body) for xml_accept in ('application/xml', 'application/xml;q=1.0,*/*;q=0.9', - '*/*;q=0.9,application/xml;q=1.0'): + '*/*;q=0.9,application/xml;q=1.0', 'application/xml,text/xml'): req = Request.blank('/sda1/p/a/xmlc', environ={'REQUEST_METHOD': 'GET'}) req.accept = xml_accept @@ -629,6 +629,13 @@ class TestContainerController(unittest.TestCase): self.assertEquals(resp.content_type, 'application/xml', 'Invalid content_type for Accept: %s' % xml_accept) + req = Request.blank('/sda1/p/a/xmlc', + environ={'REQUEST_METHOD': 'GET'}) + req.accept = 'text/xml' + resp = self.controller.GET(req) + self.assertEquals(resp.content_type, 'text/xml') + self.assertEquals(resp.body, xml_body) + def test_GET_marker(self): # make a container req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'PUT',