127127"""
128128
129129DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
130+ RANGE_REGEX_PATTERN = re .compile (r'bytes=(\d*)-(\d*)$' )
130131
131132class HTTPServer (socketserver .TCPServer ):
132133
@@ -769,10 +770,8 @@ def send_head(self):
769770 start , end = self .range
770771 if start is None :
771772 # `end` here means suffix length
772- start = fs .st_size - end
773+ start = max ( 0 , fs .st_size - end )
773774 end = fs .st_size - 1
774- if start < 0 :
775- start = 0
776775 if start >= fs .st_size :
777776 # 416 REQUESTED_RANGE_NOT_SATISFIABLE means that none of the range values overlap the extent of the resource
778777 f .close ()
@@ -782,7 +781,7 @@ def send_head(self):
782781 end = fs .st_size - 1
783782 self .send_response (HTTPStatus .PARTIAL_CONTENT )
784783 self .send_header ("Content-Range" , "bytes %s-%s/%s" % (start , end , fs .st_size ))
785- self .send_header ("Content-Length" , str (end - start + 1 ))
784+ self .send_header ("Content-Length" , str (end - start + 1 ))
786785
787786 # Update range to be sent to be used later in copyfile
788787 self .range = (start , end )
@@ -952,7 +951,7 @@ def parse_range(self):
952951 range_header = self .headers .get ('range' )
953952 if not range_header :
954953 return None
955- m = re .match (r'bytes=(\d*)-(\d*)$' , range_header )
954+ m = re .match (RANGE_REGEX_PATTERN , range_header )
956955 if not m :
957956 return None
958957
0 commit comments