Skip to content

gh-86809: Add support for HTTP Range header in HTTPServer #118949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Send content-range when not satisfiable
  • Loading branch information
lyc8503 committed Jan 1, 2025
commit 2824b9cc7f2f669deafdefec55ecdf7a17d0b717
7 changes: 5 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def handle(self):
while not self.close_connection:
self.handle_one_request()

def send_error(self, code, message=None, explain=None):
def send_error(self, code, message=None, explain=None, *, range_size=None):
"""Send and log an error reply.

Arguments are
Expand All @@ -456,6 +456,7 @@ def send_error(self, code, message=None, explain=None):
defaults to short entry matching the response code
* explain: a detailed message defaults to the long entry
matching the response code.
* range_size: file size for use in content-range header

This sends an error response (so it must be called before any
output has been generated), logs the error, and finally sends
Expand Down Expand Up @@ -493,6 +494,8 @@ def send_error(self, code, message=None, explain=None):
body = content.encode('UTF-8', 'replace')
self.send_header("Content-Type", self.error_content_type)
self.send_header('Content-Length', str(len(body)))
if code == HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE and range_size:
self.send_header('Content-Range', f'bytes */{range_size}')
self.end_headers()

if self.command != 'HEAD' and body:
Expand Down Expand Up @@ -783,7 +786,7 @@ def send_head(self):
# none of the range values overlap the extent of
# the resource
f.close()
self.send_error(HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
self.send_error(HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE, range_size=fs.st_size)
return None
if end is None or end >= fs.st_size:
end = fs.st_size - 1
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ def test_range_get(self):

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

response = self.request(route, headers={'Range': 'bytes=4-3'})
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy