Skip to content

Commit b266ff4

Browse files
committed
Error handling and testcase improve
1 parent ca84b1c commit b266ff4

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Lib/http/server.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"""
133133

134134
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
135-
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$')
135+
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$', re.IGNORECASE)
136136

137137
class HTTPServer(socketserver.TCPServer):
138138

@@ -916,16 +916,13 @@ def copyfile(self, source, outputfile, *, range=None):
916916
start, end = range
917917
length = end - start + 1
918918
source.seek(start)
919-
while True:
920-
if length <= 0:
921-
break
919+
while length > 0:
922920
buf = source.read(min(length, shutil.COPY_BUFSIZE))
923921
if not buf:
924-
break
922+
raise EOFError('File shrank after size was checked')
925923
length -= len(buf)
926924
outputfile.write(buf)
927925

928-
929926
def guess_type(self, path):
930927
"""Guess the type of a file.
931928

Lib/test/test_httpservers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def test_range_get(self):
546546
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)
547547

548548
# valid ranges
549-
response = self.request(route, headers={'Range': 'bytes=3-12'})
549+
response = self.request(route, headers={'Range': 'bYtEs=3-12'}) # case insensitive
550550
self.assertEqual(response.getheader('content-range'), 'bytes 3-12/30')
551551
self.assertEqual(response.getheader('content-length'), '10')
552552
self.check_status_and_reason(response, HTTPStatus.PARTIAL_CONTENT, data=self.data[3:13])
@@ -585,6 +585,10 @@ def test_range_get(self):
585585
response = self.request(route, headers={'Range': 'bytes=-'})
586586
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)
587587

588+
# multipart ranges (not supported currently)
589+
response = self.request(route, headers={'Range': 'bytes=1-2, 4-7'})
590+
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)
591+
588592
def test_head(self):
589593
response = self.request(
590594
self.base_url + '/test', method='HEAD')

0 commit comments

Comments
 (0)