def __init__(self, request, stats): self.total = stats.st_size _range = request.headers.get("Range") if _range is None: raise HeaderNotFound("Range Header Not Found") unit, _, value = tuple(map(str.strip, _range.partition("="))) if unit != "bytes": raise InvalidRangeType( "%s is not a valid Range Type" % (unit,), self ) start_b, _, end_b = tuple(map(str.strip, value.partition("-"))) try: self.start = int(start_b) if start_b else None except ValueError: raise ContentRangeError( "'%s' is invalid for Content Range" % (start_b,), self ) try: self.end = int(end_b) if end_b else None except ValueError: raise ContentRangeError( "'%s' is invalid for Content Range" % (end_b,), self ) if self.end is None: if self.start is None: raise ContentRangeError( "Invalid for Content Range parameters", self ) else: # this case represents `Content-Range: bytes 5-` self.end = self.total - 1 else: if self.start is None: # this case represents `Content-Range: bytes -5` self.start = self.total - self.end self.end = self.total - 1 if self.start >= self.end: raise ContentRangeError( "Invalid for Content Range parameters", self ) self.size = self.end - self.start + 1 self.headers = { "Content-Range": "bytes %s-%s/%s" % (self.start, self.end, self.total) }
def __init__(self, request, stats): self.total = stats.st_size _range = request.headers.get('Range') if _range is None: raise HeaderNotFound('Range Header Not Found') unit, _, value = tuple(map(str.strip, _range.partition('='))) if unit != 'bytes': raise InvalidRangeType('%s is not a valid Range Type' % (unit, ), self) start_b, _, end_b = tuple(map(str.strip, value.partition('-'))) try: self.start = int(start_b) if start_b else None except ValueError: raise ContentRangeError( '\'%s\' is invalid for Content Range' % (start_b, ), self) try: self.end = int(end_b) if end_b else None except ValueError: raise ContentRangeError( '\'%s\' is invalid for Content Range' % (end_b, ), self) if self.end is None: if self.start is None: raise ContentRangeError('Invalid for Content Range parameters', self) else: # this case represents `Content-Range: bytes 5-` self.end = self.total else: if self.start is None: # this case represents `Content-Range: bytes -5` self.start = self.total - self.end self.end = self.total if self.start >= self.end: raise ContentRangeError('Invalid for Content Range parameters', self) self.size = self.end - self.start self.headers = { 'Content-Range': "bytes %s-%s/%s" % (self.start, self.end, self.total) }