Skip to content

Commit 22f88ce

Browse files
committed
adding Accept-Ranges: bytes header to HEAD and GET requests.
1 parent 8137aa6 commit 22f88ce

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

swift/proxy/server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ def file_iter():
645645
raise
646646
res.app_iter = file_iter()
647647
update_headers(res, source.getheaders())
648+
update_headers(res, {'accept-ranges':'bytes'})
648649
res.status = source.status
649650
res.content_length = source.getheader('Content-Length')
650651
if source.getheader('Content-Type'):
@@ -654,6 +655,7 @@ def file_iter():
654655
elif 200 <= source.status <= 399:
655656
res = status_map[source.status](request=req)
656657
update_headers(res, source.getheaders())
658+
update_headers(res, {'accept-ranges':'bytes'})
657659
if req.method == 'HEAD':
658660
res.content_length = source.getheader('Content-Length')
659661
if source.getheader('Content-Type'):
@@ -828,6 +830,7 @@ def head_response(environ, start_response):
828830
resp)
829831
resp.content_length = content_length
830832
resp.last_modified = last_modified
833+
resp.headers['accept-ranges'] = 'bytes'
831834

832835
return resp
833836

test/unit/proxy/test_server.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,9 @@ def test_status_map(statuses, expected):
976976
if expected < 400:
977977
self.assert_('x-works' in res.headers)
978978
self.assertEquals(res.headers['x-works'], 'yes')
979+
self.assert_('accept-ranges' in res.headers)
980+
self.assertEquals(res.headers['accept-ranges'], 'bytes')
981+
979982
test_status_map((200, 404, 404), 200)
980983
test_status_map((200, 500, 404), 200)
981984
test_status_map((304, 500, 404), 304)
@@ -1247,7 +1250,7 @@ def test_best_response_sets_etag(self):
12471250
resp = controller.best_response(req, [200] * 3, ['OK'] * 3, [''] * 3,
12481251
'Object', etag='68b329da9893e34099c7d8ad5cb9c940')
12491252
self.assertEquals(resp.etag, '68b329da9893e34099c7d8ad5cb9c940')
1250-
1253+
12511254
def test_proxy_passes_content_type(self):
12521255
with save_globals():
12531256
req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'})
@@ -2487,7 +2490,29 @@ def test_response_client_disconnect_attr(self):
24872490
self.assert_(res.client_disconnect)
24882491
finally:
24892492
self.app.object_chunk_size = orig_object_chunk_size
2493+
2494+
def test_response_get_accept_ranges_header(self):
2495+
with save_globals():
2496+
req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'})
2497+
self.app.update_request(req)
2498+
controller = proxy_server.ObjectController(self.app, 'account',
2499+
'container', 'object')
2500+
proxy_server.http_connect = fake_http_connect(200, 200, 200)
2501+
resp = controller.GET(req)
2502+
self.assert_('accept-ranges' in resp.headers)
2503+
self.assertEquals(resp.headers['accept-ranges'], 'bytes')
24902504

2505+
def test_response_head_accept_ranges_header(self):
2506+
with save_globals():
2507+
req = Request.blank('/a/c/o', environ={'REQUEST_METHOD': 'GET'})
2508+
self.app.update_request(req)
2509+
controller = proxy_server.ObjectController(self.app, 'account',
2510+
'container', 'object')
2511+
proxy_server.http_connect = fake_http_connect(200, 200, 200)
2512+
resp = controller.HEAD(req)
2513+
self.assert_('accept-ranges' in resp.headers)
2514+
self.assertEquals(resp.headers['accept-ranges'], 'bytes')
2515+
24912516
def test_GET_calls_authorize(self):
24922517
called = [False]
24932518

@@ -2827,6 +2852,28 @@ def test_response_client_disconnect_attr(self):
28272852
finally:
28282853
self.app.object_chunk_size = orig_object_chunk_size
28292854

2855+
def test_response_get_accept_ranges_header(self):
2856+
with save_globals():
2857+
proxy_server.http_connect = fake_http_connect(200, 200, body='{}')
2858+
controller = proxy_server.ContainerController(self.app, 'account',
2859+
'container')
2860+
req = Request.blank('/a/c?format=json')
2861+
self.app.update_request(req)
2862+
res = controller.GET(req)
2863+
self.assert_('accept-ranges' in res.headers)
2864+
self.assertEqual(res.headers['accept-ranges'], 'bytes')
2865+
2866+
def test_response_head_accept_ranges_header(self):
2867+
with save_globals():
2868+
proxy_server.http_connect = fake_http_connect(200, 200, body='{}')
2869+
controller = proxy_server.ContainerController(self.app, 'account',
2870+
'container')
2871+
req = Request.blank('/a/c?format=json')
2872+
self.app.update_request(req)
2873+
res = controller.HEAD(req)
2874+
self.assert_('accept-ranges' in res.headers)
2875+
self.assertEqual(res.headers['accept-ranges'], 'bytes')
2876+
28302877
def test_PUT_metadata(self):
28312878
self.metadata_helper('PUT')
28322879

@@ -3132,7 +3179,28 @@ def test_response_bytes_transferred_attr(self):
31323179
res.body
31333180
self.assert_(hasattr(res, 'bytes_transferred'))
31343181
self.assertEquals(res.bytes_transferred, 2)
3135-
3182+
3183+
def test_response_get_accept_ranges_header(self):
3184+
with save_globals():
3185+
proxy_server.http_connect = fake_http_connect(200, 200, body='{}')
3186+
controller = proxy_server.AccountController(self.app, 'account')
3187+
req = Request.blank('/a?format=json')
3188+
self.app.update_request(req)
3189+
res = controller.GET(req)
3190+
self.assert_('accept-ranges' in res.headers)
3191+
self.assertEqual(res.headers['accept-ranges'], 'bytes')
3192+
3193+
def test_response_head_accept_ranges_header(self):
3194+
with save_globals():
3195+
proxy_server.http_connect = fake_http_connect(200, 200, body='{}')
3196+
controller = proxy_server.AccountController(self.app, 'account')
3197+
req = Request.blank('/a?format=json')
3198+
self.app.update_request(req)
3199+
res = controller.HEAD(req)
3200+
res.body
3201+
self.assert_('accept-ranges' in res.headers)
3202+
self.assertEqual(res.headers['accept-ranges'], 'bytes')
3203+
31363204
def test_response_client_disconnect_attr(self):
31373205
with save_globals():
31383206
proxy_server.http_connect = fake_http_connect(200, 200, body='{}')

0 commit comments

Comments
 (0)