Skip to content

Commit 2824b9c

Browse files
committed
Send content-range when not satisfiable
1 parent b266ff4 commit 2824b9c

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Lib/http/server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def handle(self):
445445
while not self.close_connection:
446446
self.handle_one_request()
447447

448-
def send_error(self, code, message=None, explain=None):
448+
def send_error(self, code, message=None, explain=None, *, range_size=None):
449449
"""Send and log an error reply.
450450
451451
Arguments are
@@ -456,6 +456,7 @@ def send_error(self, code, message=None, explain=None):
456456
defaults to short entry matching the response code
457457
* explain: a detailed message defaults to the long entry
458458
matching the response code.
459+
* range_size: file size for use in content-range header
459460
460461
This sends an error response (so it must be called before any
461462
output has been generated), logs the error, and finally sends
@@ -493,6 +494,8 @@ def send_error(self, code, message=None, explain=None):
493494
body = content.encode('UTF-8', 'replace')
494495
self.send_header("Content-Type", self.error_content_type)
495496
self.send_header('Content-Length', str(len(body)))
497+
if code == HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE and range_size:
498+
self.send_header('Content-Range', f'bytes */{range_size}')
496499
self.end_headers()
497500

498501
if self.command != 'HEAD' and body:
@@ -783,7 +786,7 @@ def send_head(self):
783786
# none of the range values overlap the extent of
784787
# the resource
785788
f.close()
786-
self.send_error(HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
789+
self.send_error(HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE, range_size=fs.st_size)
787790
return None
788791
if end is None or end >= fs.st_size:
789792
end = fs.st_size - 1

Lib/test/test_httpservers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ def test_range_get(self):
574574

575575
# invalid ranges
576576
response = self.request(route, headers={'Range': 'bytes=100-200'})
577+
self.assertEqual(response.getheader('content-range'), 'bytes */30')
577578
self.check_status_and_reason(response, HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
578579

579580
response = self.request(route, headers={'Range': 'bytes=4-3'})

0 commit comments

Comments
 (0)