Example #1
0
 def handle_request_range(self, file):
     # check if we have a range in the request
     ranges = None
     header_range = self.request.getHeader('Range', None)
     if_range = self.request.getHeader('If-Range', None)
     if header_range is not None:
         ranges = parseRange(header_range)
         if if_range is not None:
             # We delete the ranges, which causes us to skip to the 200
             # response.
             return {}
         # XXX: multipart ranges not implemented
         if ranges and len(ranges) == 1:
             try:
                 length = file.getSize()
                 [(start, end)] = expandRanges(ranges, length)
                 size = end - start
                 self.request.response.setHeader('Content-Length', size)
                 self.request.response.setHeader(
                     'Content-Range',
                     'bytes {0}-{1}/{2}'.format(start, end - 1, length))
                 self.request.response.setStatus(206)  # Partial content
                 return dict(start=start, end=end)
             except ValueError:
                 return {}
     return {}
Example #2
0
def handleRequestRange(instance, length, REQUEST, RESPONSE):
    # check if we have a range in the request
    ranges = None
    range = REQUEST.get_header('Range', None)
    request_range = REQUEST.get_header('Request-Range', None)
    if request_range is not None:
        # Netscape 2 through 4 and MSIE 3 implement a draft version
        # Later on, we need to serve a different mime-type as well.
        range = request_range
    if_range = REQUEST.get_header('If-Range', None)
    if range is not None:
        ranges = parseRange(range)
        if if_range is not None:
            # Only send ranges if the data isn't modified, otherwise send
            # the whole object. Support both ETags and Last-Modified dates!
            if len(if_range) > 1 and if_range[:2] == 'ts':
                # ETag:
                if if_range != instance.http__etag():
                    # Modified, so send a normal response. We delete
                    # the ranges, which causes us to skip to the 200
                    # response.
                    ranges = None
            else:
                # Date
                date = if_range.split(';')[0]
                try:
                    mod_since = long(DateTime(date).timeTime())
                except Exception:
                    mod_since = None
                if mod_since is not None:
                    if instance._p_mtime:
                        last_mod = long(instance._p_mtime)
                    else:
                        last_mod = long(0)
                    if last_mod > mod_since:
                        # Modified, so send a normal response. We delete
                        # the ranges, which causes us to skip to the 200
                        # response.
                        ranges = None
            RESPONSE.setHeader('Accept-Ranges', 'bytes')
        if ranges and len(ranges) == 1:
            try:
                [(start, end)] = expandRanges(ranges, length)
                size = end - start
                RESPONSE.setHeader('Content-Length', size)
                RESPONSE.setHeader(
                    'Content-Range',
                    'bytes {0}-{1}/{2}'.format(start, end - 1, length))
                RESPONSE.setStatus(206)  # Partial content
                return dict(start=start, end=end)
            except ValueError:
                return {}
    return {}
Example #3
0
def handleRequestRange(instance, length, REQUEST, RESPONSE):
    # check if we have a range in the request
    ranges = None
    range = REQUEST.get_header('Range', None)
    request_range = REQUEST.get_header('Request-Range', None)
    if request_range is not None:
        # Netscape 2 through 4 and MSIE 3 implement a draft version
        # Later on, we need to serve a different mime-type as well.
        range = request_range
    if_range = REQUEST.get_header('If-Range', None)
    if range is not None:
        ranges = parseRange(range)
        if if_range is not None:
            # Only send ranges if the data isn't modified, otherwise send
            # the whole object. Support both ETags and Last-Modified dates!
            if len(if_range) > 1 and if_range[:2] == 'ts':
                # ETag:
                if if_range != instance.http__etag():
                    # Modified, so send a normal response. We delete
                    # the ranges, which causes us to skip to the 200
                    # response.
                    ranges = None
            else:
                # Date
                date = if_range.split(';')[0]
                try:
                    mod_since = long(DateTime(date).timeTime())
                except Exception:
                    mod_since = None
                if mod_since is not None:
                    if instance._p_mtime:
                        last_mod = long(instance._p_mtime)
                    else:
                        last_mod = 0
                    if last_mod > mod_since:
                        # Modified, so send a normal response. We delete
                        # the ranges, which causes us to skip to the 200
                        # response.
                        ranges = None
            RESPONSE.setHeader('Accept-Ranges', 'bytes')
        if ranges and len(ranges) == 1:
            try:
                [(start, end)] = expandRanges(ranges, length)
                size = end - start
                RESPONSE.setHeader('Content-Length', size)
                RESPONSE.setHeader(
                    'Content-Range',
                    'bytes {0}-{1}/{2}'.format(start, end - 1, length))
                RESPONSE.setStatus(206)  # Partial content
                return dict(start=start, end=end)
            except ValueError:
                return {}
    return {}
Example #4
0
 def have_ranges(self):
     """Return range information if partial content was requested.
     """
     range_header = self.request.environ.get('HTTP_RANGE', None)
     if range_header is not None:
         range_if_header = self.request.environ.get('HTTP_IF_RANGE', None)
         if range_if_header:
             # If there is an If-Range header, with a date
             # prior to the modification, return all the file.
             if_date = parse_datetime(range_if_header)
             if (if_date and self._modification_datetime
                     and self._modification_datetime > if_date):
                 return (None, None, None)
         ranges = parseRange(range_header)
         if len(ranges) == 1:
             size = self.context.get_file_size()
             satisfiable = expandRanges(ranges, size)
             if len(satisfiable) == 1:
                 return (satisfiable[0][0], satisfiable[0][1] - 1, size)
             return (None, None, size)
     return (None, None, None)
Example #5
0
 def have_ranges(self):
     """Return range information if partial content was requested.
     """
     range_header = self.request.environ.get('HTTP_RANGE', None)
     if range_header is not None:
         range_if_header = self.request.environ.get('HTTP_IF_RANGE', None)
         if range_if_header:
             # If there is an If-Range header, with a date
             # prior to the modification, return all the file.
             if_date = parse_datetime(range_if_header)
             if (if_date and self._modification_datetime and
                 self._modification_datetime > if_date):
                 return (None, None, None)
         ranges = parseRange(range_header)
         if len(ranges) == 1:
             size = self.context.get_file_size()
             satisfiable = expandRanges(ranges, size)
             if len(satisfiable) == 1:
                 return (satisfiable[0][0], satisfiable[0][1] - 1, size)
             return (None, None, size)
     return (None, None, None)
 def expectSets(self, sets, size, expect):
     result = expandRanges(sets, size)
     self.assertTrue(
         result == expect,
         'Expected %r, got %r' % (expect, result))
 def expectSets(self, sets, size, expect):
     result = expandRanges(sets, size)
     self.assertTrue(result == expect,
                     'Expected %s, got %s' % ( ` expect `, ` result `))
Example #8
0
 def expectSets(self, sets, size, expect):
     result = expandRanges(sets, size)
     self.failUnless(result == expect,
         'Expected %s, got %s' % (`expect`, `result`))
 def expectSets(self, sets, size, expect):
     result = expandRanges(sets, size)
     self.assertTrue(result == expect,
         'Expected %s, got %s' % (`expect`, `result`))
 def expectSets(self, sets, size, expect):
     result = expandRanges(sets, size)
     self.assertTrue(result == expect,
                     'Expected %r, got %r' % (expect, result))
Example #11
0
 def expectSets(self, sets, size, expect):
     result = expandRanges(sets, size)
     self.assertTrue(result == expect,
                     f'Expected {expect!r}, got {result!r}')