diff --git a/swift/proxy/controllers/base.py b/swift/proxy/controllers/base.py index 554469cc06..70940f9c16 100644 --- a/swift/proxy/controllers/base.py +++ b/swift/proxy/controllers/base.py @@ -696,7 +696,12 @@ class ResumingGetter(object): If we have no Range header, this is a no-op. """ if 'Range' in self.backend_headers: - req_range = Range(self.backend_headers['Range']) + try: + req_range = Range(self.backend_headers['Range']) + except ValueError: + # there's a Range header, but it's garbage, so get rid of it + self.backend_headers.pop('Range') + return begin, end = req_range.ranges.pop(0) if len(req_range.ranges) > 0: self.backend_headers['Range'] = str(req_range) diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index 04eb3d6eb7..539d6ef102 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -1457,6 +1457,46 @@ class TestObjectController(unittest.TestCase): 'bytes 4123-4523/5800') self.assertEqual(second_range_body, obj[4123:4524]) + @unpatch_policies + def test_GET_bad_range_zero_byte(self): + prolis = _test_sockets[0] + prosrv = _test_servers[0] + sock = connect_tcp(('localhost', prolis.getsockname()[1])) + fd = sock.makefile() + + path = '/v1/a/c/o.zerobyte' + fd.write('PUT %s HTTP/1.1\r\n' + 'Host: localhost\r\n' + 'Connection: close\r\n' + 'X-Storage-Token: t\r\n' + 'Content-Length: 0\r\n' + 'Content-Type: application/octet-stream\r\n' + '\r\n' % (path,)) + fd.flush() + headers = readuntil2crlfs(fd) + exp = 'HTTP/1.1 201' + self.assertEqual(headers[:len(exp)], exp) + + # bad byte-range + req = Request.blank( + path, + environ={'REQUEST_METHOD': 'GET'}, + headers={'Content-Type': 'application/octet-stream', + 'Range': 'bytes=spaghetti-carbonara'}) + res = req.get_response(prosrv) + self.assertEqual(res.status_int, 200) + self.assertEqual(res.body, '') + + # not a byte-range + req = Request.blank( + path, + environ={'REQUEST_METHOD': 'GET'}, + headers={'Content-Type': 'application/octet-stream', + 'Range': 'Kotta'}) + res = req.get_response(prosrv) + self.assertEqual(res.status_int, 200) + self.assertEqual(res.body, '') + @unpatch_policies def test_GET_ranges_resuming(self): prolis = _test_sockets[0]