From 22f88ce4af7061823100bb715e643695a3676c37 Mon Sep 17 00:00:00 2001 From: Scott Simpson Date: Wed, 20 Apr 2011 15:10:02 -0700 Subject: [PATCH 1/3] adding Accept-Ranges: bytes header to HEAD and GET requests. --- swift/proxy/server.py | 3 ++ test/unit/proxy/test_server.py | 72 +++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/swift/proxy/server.py b/swift/proxy/server.py index a8c5d26fd1..2cb329e064 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -645,6 +645,7 @@ class Controller(object): raise res.app_iter = file_iter() update_headers(res, source.getheaders()) + update_headers(res, {'accept-ranges':'bytes'}) res.status = source.status res.content_length = source.getheader('Content-Length') if source.getheader('Content-Type'): @@ -654,6 +655,7 @@ class Controller(object): elif 200 <= source.status <= 399: res = status_map[source.status](request=req) update_headers(res, source.getheaders()) + update_headers(res, {'accept-ranges':'bytes'}) if req.method == 'HEAD': res.content_length = source.getheader('Content-Length') if source.getheader('Content-Type'): @@ -828,6 +830,7 @@ class ObjectController(Controller): resp) resp.content_length = content_length resp.last_modified = last_modified + resp.headers['accept-ranges'] = 'bytes' return resp diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index efd1ee857d..cfeda5a8f2 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -976,6 +976,9 @@ class TestObjectController(unittest.TestCase): if expected < 400: self.assert_('x-works' in res.headers) self.assertEquals(res.headers['x-works'], 'yes') + self.assert_('accept-ranges' in res.headers) + self.assertEquals(res.headers['accept-ranges'], 'bytes') + test_status_map((200, 404, 404), 200) test_status_map((200, 500, 404), 200) test_status_map((304, 500, 404), 304) @@ -1247,7 +1250,7 @@ class TestObjectController(unittest.TestCase): resp = controller.best_response(req, [200] * 3, ['OK'] * 3, [''] * 3, 'Object', etag='68b329da9893e34099c7d8ad5cb9c940') self.assertEquals(resp.etag, '68b329da9893e34099c7d8ad5cb9c940') - + def test_proxy_passes_content_type(self): with save_globals(): req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'}) @@ -2487,7 +2490,29 @@ class TestObjectController(unittest.TestCase): self.assert_(res.client_disconnect) finally: self.app.object_chunk_size = orig_object_chunk_size + + def test_response_get_accept_ranges_header(self): + with save_globals(): + req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'}) + self.app.update_request(req) + controller = proxy_server.ObjectController(self.app, 'account', + 'container', 'object') + proxy_server.http_connect = fake_http_connect(200, 200, 200) + resp = controller.GET(req) + self.assert_('accept-ranges' in resp.headers) + self.assertEquals(resp.headers['accept-ranges'], 'bytes') + def test_response_head_accept_ranges_header(self): + with save_globals(): + req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'}) + self.app.update_request(req) + controller = proxy_server.ObjectController(self.app, 'account', + 'container', 'object') + proxy_server.http_connect = fake_http_connect(200, 200, 200) + resp = controller.HEAD(req) + self.assert_('accept-ranges' in resp.headers) + self.assertEquals(resp.headers['accept-ranges'], 'bytes') + def test_GET_calls_authorize(self): called = [False] @@ -2827,6 +2852,28 @@ class TestContainerController(unittest.TestCase): finally: self.app.object_chunk_size = orig_object_chunk_size + def test_response_get_accept_ranges_header(self): + with save_globals(): + proxy_server.http_connect = fake_http_connect(200, 200, body='{}') + controller = proxy_server.ContainerController(self.app, 'account', + 'container') + req = Request.blank('/a/c?format=json') + self.app.update_request(req) + res = controller.GET(req) + self.assert_('accept-ranges' in res.headers) + self.assertEqual(res.headers['accept-ranges'], 'bytes') + + def test_response_head_accept_ranges_header(self): + with save_globals(): + proxy_server.http_connect = fake_http_connect(200, 200, body='{}') + controller = proxy_server.ContainerController(self.app, 'account', + 'container') + req = Request.blank('/a/c?format=json') + self.app.update_request(req) + res = controller.HEAD(req) + self.assert_('accept-ranges' in res.headers) + self.assertEqual(res.headers['accept-ranges'], 'bytes') + def test_PUT_metadata(self): self.metadata_helper('PUT') @@ -3132,7 +3179,28 @@ class TestAccountController(unittest.TestCase): res.body self.assert_(hasattr(res, 'bytes_transferred')) self.assertEquals(res.bytes_transferred, 2) - + + def test_response_get_accept_ranges_header(self): + with save_globals(): + proxy_server.http_connect = fake_http_connect(200, 200, body='{}') + controller = proxy_server.AccountController(self.app, 'account') + req = Request.blank('/a?format=json') + self.app.update_request(req) + res = controller.GET(req) + self.assert_('accept-ranges' in res.headers) + self.assertEqual(res.headers['accept-ranges'], 'bytes') + + def test_response_head_accept_ranges_header(self): + with save_globals(): + proxy_server.http_connect = fake_http_connect(200, 200, body='{}') + controller = proxy_server.AccountController(self.app, 'account') + req = Request.blank('/a?format=json') + self.app.update_request(req) + res = controller.HEAD(req) + res.body + self.assert_('accept-ranges' in res.headers) + self.assertEqual(res.headers['accept-ranges'], 'bytes') + def test_response_client_disconnect_attr(self): with save_globals(): proxy_server.http_connect = fake_http_connect(200, 200, body='{}') From 9014cb81a83820357ef3d8205e0323638ba594b3 Mon Sep 17 00:00:00 2001 From: Scott Simpson Date: Thu, 21 Apr 2011 11:51:09 -0700 Subject: [PATCH 2/3] pep8 the file --- swift/proxy/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swift/proxy/server.py b/swift/proxy/server.py index 2cb329e064..99b7201ab4 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -645,7 +645,7 @@ class Controller(object): raise res.app_iter = file_iter() update_headers(res, source.getheaders()) - update_headers(res, {'accept-ranges':'bytes'}) + update_headers(res, {'accept-ranges': 'bytes'}) res.status = source.status res.content_length = source.getheader('Content-Length') if source.getheader('Content-Type'): @@ -655,7 +655,7 @@ class Controller(object): elif 200 <= source.status <= 399: res = status_map[source.status](request=req) update_headers(res, source.getheaders()) - update_headers(res, {'accept-ranges':'bytes'}) + update_headers(res, {'accept-ranges': 'bytes'}) if req.method == 'HEAD': res.content_length = source.getheader('Content-Length') if source.getheader('Content-Type'): From 995bd5822d84ef39b5a98bc46f985dc2b58a3b04 Mon Sep 17 00:00:00 2001 From: Scott Simpson Date: Thu, 21 Apr 2011 15:09:04 -0500 Subject: [PATCH 3/3] fixing unit test --- test/unit/proxy/test_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index cfeda5a8f2..a254ac1ad6 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -2504,7 +2504,7 @@ class TestObjectController(unittest.TestCase): def test_response_head_accept_ranges_header(self): with save_globals(): - req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'}) + req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'HEAD'}) self.app.update_request(req) controller = proxy_server.ObjectController(self.app, 'account', 'container', 'object')