Skip to content

Commit

Permalink
Error handling and testcase improve
Browse files Browse the repository at this point in the history
  • Loading branch information
lyc8503 committed Jan 1, 2025
1 parent ca84b1c commit b266ff4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
9 changes: 3 additions & 6 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"""

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

class HTTPServer(socketserver.TCPServer):

Expand Down Expand Up @@ -916,16 +916,13 @@ def copyfile(self, source, outputfile, *, range=None):
start, end = range
length = end - start + 1
source.seek(start)
while True:
if length <= 0:
break
while length > 0:
buf = source.read(min(length, shutil.COPY_BUFSIZE))
if not buf:
break
raise EOFError('File shrank after size was checked')
length -= len(buf)
outputfile.write(buf)


def guess_type(self, path):
"""Guess the type of a file.
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_range_get(self):
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)

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

# multipart ranges (not supported currently)
response = self.request(route, headers={'Range': 'bytes=1-2, 4-7'})
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)

def test_head(self):
response = self.request(
self.base_url + '/test', method='HEAD')
Expand Down

0 comments on commit b266ff4

Please sign in to comment.