Handle byte range with stop > content_length

This commit is contained in:
Kovid Goyal 2015-05-22 17:57:23 +05:30
parent 3d51b47f57
commit a02a437b1c
2 changed files with 5 additions and 0 deletions

View File

@ -47,6 +47,7 @@ def get_ranges(headervalue, content_length):
continue continue
if stop < start: if stop < start:
continue continue
stop = min(stop, content_length - 1)
result.append(Range(start, stop, stop - start + 1)) result.append(Range(start, stop, stop - start + 1))
elif stop: elif stop:
# Negative subscript (last N bytes) # Negative subscript (last N bytes)

View File

@ -241,6 +241,10 @@ class TestHTTP(BaseTest):
self.ae(type('')(r.getheader('Content-Range')), 'bytes */%d' % len(fdata)) self.ae(type('')(r.getheader('Content-Range')), 'bytes */%d' % len(fdata))
self.ae(r.status, httplib.REQUESTED_RANGE_NOT_SATISFIABLE) self.ae(r.status, httplib.REQUESTED_RANGE_NOT_SATISFIABLE)
conn.request('GET', '/test', headers={'Range':'bytes=0-1000000'})
r = conn.getresponse()
self.ae(r.status, httplib.PARTIAL_CONTENT), self.ae(r.read(), fdata)
conn.request('GET', '/test', headers={'Range':'bytes=25-50', 'If-Range':etag}) conn.request('GET', '/test', headers={'Range':'bytes=25-50', 'If-Range':etag})
r = conn.getresponse() r = conn.getresponse()
self.ae(int(r.getheader('Content-Length')), 26) self.ae(int(r.getheader('Content-Length')), 26)