Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ValueError exceptions when doing a range request. #42

Merged
merged 1 commit into from Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Expand Up @@ -10,7 +10,9 @@ New:

Fixes:

- *add item here*
- Handle ``ValueError`` exceptions when doing a range request.
This fixes `issue #39 <https://github.com/plone/plone.app.blob/issues/39>`_.
[batlock666]


1.5.17 (2016-02-15)
Expand Down
19 changes: 11 additions & 8 deletions src/plone/app/blob/download.py
Expand Up @@ -71,12 +71,15 @@ def handleRequestRange(instance, length, REQUEST, RESPONSE):
ranges = None
RESPONSE.setHeader('Accept-Ranges', 'bytes')
if ranges and len(ranges) == 1:
[(start, end)] = expandRanges(ranges, length)
size = end - start
RESPONSE.setHeader('Content-Length', size)
RESPONSE.setHeader(
'Content-Range',
'bytes %d-%d/%d' % (start, end - 1, length))
RESPONSE.setStatus(206) # Partial content
return dict(start=start, end=end)
try:
[(start, end)] = expandRanges(ranges, length)
size = end - start
RESPONSE.setHeader('Content-Length', size)
RESPONSE.setHeader(
'Content-Range',
'bytes %d-%d/%d' % (start, end - 1, length))
RESPONSE.setStatus(206) # Partial content
return dict(start=start, end=end)
except ValueError:
return {}
return {}
15 changes: 15 additions & 0 deletions src/plone/app/blob/tests/test_integration.py
Expand Up @@ -149,6 +149,21 @@ def testRangeSupport(self):
iterator = blob.download(request)
self.assertEqual(data[-20:], ''.join(iterator))

def testOutsideRange(self):
# ranges outside the file size also have to work
blob = self.folder['blob']
blob.setTitle('foo')
blob.setFile(getData('plone.pdf'))
data = blob.getFile().getBlob().open('r').read()
l = len(data)
request = self.folder.REQUEST
request.environ['HTTP_RANGE'] = 'bytes={}-{}'.format(l * 2, l * 3)
iterator = blob.download(request)
self.assertEqual(data, ''.join(iterator))
request.environ['HTTP_RANGE'] = 'bytes={}-'.format(l * 2)
iterator = blob.download(request)
self.assertEqual(data, ''.join(iterator))

def testIcon(self):
blob = self.folder.blob
blob.update(file=getImage())
Expand Down