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
Add support for suffix in Range header
  • Loading branch information
lyc8503 committed Dec 15, 2024
commit ac7b0fa852fdbf542c0d6772e44f0984cd0a29f5
22 changes: 16 additions & 6 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,12 @@ def send_head(self):

if self.range:
start, end = self.range
if start is None:
# `end` here means suffix length
start = fs.st_size - end
end = fs.st_size - 1
if start < 0:
start = 0
if start >= fs.st_size:
# 416 REQUESTED_RANGE_NOT_SATISFIABLE means that none of the range values overlap the extent of the resource
f.close()
Expand Down Expand Up @@ -953,15 +959,19 @@ def parse_range(self):
range_header = self.headers.get('range')
if not range_header:
return None
m = re.match(r'bytes=(\d+)-(\d*)$', range_header)
m = re.match(r'bytes=(\d*)-(\d*)$', range_header)
if not m:
return None
start = int(m.group(1))
if not m.group(2):
return start, None
end = int(m.group(2))
if start > end:

start = int(m.group(1)) if m.group(1) else None
end = int(m.group(2)) if m.group(2) else None

if start is None and end is None:
return None

if start is not None and end is not None and start > end:
return None

return start, end


Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ def test_range_get(self):
self.assertEqual(response.getheader('content-length'), '27')
self.check_status_and_reason(response, HTTPStatus.PARTIAL_CONTENT, data=self.data[3:])

response = self.request(self.base_url + '/test', headers={'Range': 'bytes=-5'})
self.assertEqual(response.getheader('content-range'), 'bytes 25-29/30')
self.assertEqual(response.getheader('content-length'), '5')
self.check_status_and_reason(response, HTTPStatus.PARTIAL_CONTENT, data=self.data[25:])

response = self.request(self.base_url + '/test', headers={'Range': 'bytes=100-200'})
self.check_status_and_reason(response, HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)

Expand Down
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