132132"""
133133
134134DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
135+ RANGE_REGEX_PATTERN = re .compile (r'bytes=(\d*)-(\d*)$' )
135136
136137class HTTPServer (socketserver .TCPServer ):
137138
@@ -775,10 +776,8 @@ def send_head(self):
775776 start , end = self .range
776777 if start is None :
777778 # `end` here means suffix length
778- start = fs .st_size - end
779+ start = max ( 0 , fs .st_size - end )
779780 end = fs .st_size - 1
780- if start < 0 :
781- start = 0
782781 if start >= fs .st_size :
783782 # 416 REQUESTED_RANGE_NOT_SATISFIABLE means that none of the range values overlap the extent of the resource
784783 f .close ()
@@ -788,7 +787,7 @@ def send_head(self):
788787 end = fs .st_size - 1
789788 self .send_response (HTTPStatus .PARTIAL_CONTENT )
790789 self .send_header ("Content-Range" , "bytes %s-%s/%s" % (start , end , fs .st_size ))
791- self .send_header ("Content-Length" , str (end - start + 1 ))
790+ self .send_header ("Content-Length" , str (end - start + 1 ))
792791
793792 # Update range to be sent to be used later in copyfile
794793 self .range = (start , end )
@@ -959,7 +958,7 @@ def parse_range(self):
959958 range_header = self .headers .get ('range' )
960959 if not range_header :
961960 return None
962- m = re .match (r'bytes=(\d*)-(\d*)$' , range_header )
961+ m = re .match (RANGE_REGEX_PATTERN , range_header )
963962 if not m :
964963 return None
965964
0 commit comments