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
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
Use precompiled pattern and some small improvements
  • Loading branch information
lyc8503 committed Dec 15, 2024
commit d23a60db52319ce3798c949998fce186ba3321e9
9 changes: 4 additions & 5 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"""

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

class HTTPServer(socketserver.TCPServer):

Expand Down Expand Up @@ -775,10 +776,8 @@ def send_head(self):
start, end = self.range
if start is None:
# `end` here means suffix length
start = fs.st_size - end
start = max(0, 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 All @@ -788,7 +787,7 @@ def send_head(self):
end = fs.st_size - 1
self.send_response(HTTPStatus.PARTIAL_CONTENT)
self.send_header("Content-Range", "bytes %s-%s/%s" % (start, end, fs.st_size))
self.send_header("Content-Length", str(end-start+1))
self.send_header("Content-Length", str(end - start + 1))

# Update range to be sent to be used later in copyfile
self.range = (start, end)
Expand Down Expand Up @@ -959,7 +958,7 @@ 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(RANGE_REGEX_PATTERN, range_header)
if not m:
return None

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