Package backend :: Package common :: Module byterange
[hide private]
[frames] | no frames]

Source Code for Module backend.common.byterange

  1  # 
  2  # Copyright (c) 2008--2017 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16   
 17  import re 
 18  import sys 
 19   
 20  # common module 
 21  from spacewalk.common.usix import raise_with_tb 
 22  from spacewalk.common.rhnLog import log_debug 
 23  from spacewalk.common.rhnException import rhnException 
 24   
 25  # pylint: disable=W0710 
 26   
 27  # Parses the HTTP header value and stores in the flags  a list of (start, end) 
 28  # tuples that are more pythonic than the RFC semantics 
 29   
 30   
31 -def parse_byteranges(byterange_header, file_size=None):
32 log_debug(4, "Parsing byte range", byterange_header) 33 regexp = re.compile(r"^bytes\s*=\s*(.*)$") 34 mo = regexp.match(byterange_header) 35 if not mo: 36 raise InvalidByteRangeException 37 38 arr = mo.groups()[0].split(",") 39 regexp = re.compile(r"^([^-]*)-([^-]*)$") 40 41 if len(arr) > 1: 42 # We don't support very fancy byte ranges yet 43 raise UnsatisfyableByteRangeException 44 45 mo = regexp.match(arr[0]) 46 if not mo: 47 # Invalid byterange 48 raise InvalidByteRangeException 49 try: 50 start, end = list(map(_str2int, mo.groups())) 51 except ValueError: 52 # Invalid 53 raise_with_tb(InvalidByteRangeException, sys.exc_info()[2]) 54 if start is not None: 55 if start < 0: 56 # Invalid 57 raise InvalidByteRangeException 58 if file_size is not None: 59 if start >= file_size: 60 raise UnsatisfyableByteRangeException 61 if end is not None: 62 if start > end: 63 # Invalid 64 raise InvalidByteRangeException 65 end = end + 1 66 else: 67 if file_size: 68 end = file_size 69 else: 70 # No start specified 71 if end is None: 72 # Invalid 73 raise InvalidByteRangeException 74 if end <= 0: 75 # Invalid 76 raise InvalidByteRangeException 77 if file_size: 78 if end > file_size: 79 raise UnsatisfyableByteRangeException 80 start = file_size - end 81 end = file_size 82 else: 83 start = -end 84 end = None 85 86 byteranges = (start, end) 87 88 log_debug(4, "Request byterange", byteranges) 89 return byteranges
90 91
92 -def _str2int(val):
93 val = val.strip() 94 if val == "": 95 return None 96 97 return int(val)
98 99
100 -def get_content_range(start, end, total_length=None):
101 if total_length is None: 102 total_length = "*" 103 end = end - 1 104 content_range = "bytes %d-%d/%s" % (start, end, total_length) 105 return content_range
106 107
108 -class InvalidByteRangeException(rhnException):
109 pass
110 111
112 -class UnsatisfyableByteRangeException(rhnException):
113 pass
114