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
Update RFC reference and some minor code fixes
  • Loading branch information
lyc8503 committed Jan 2, 2025
commit 77436b38269ad975a026ab7926f46a4aacc8d88c
2 changes: 1 addition & 1 deletion Doc/library/http.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ provides three different variants:

.. versionchanged:: next
Added support for HTTP single-part range requests on files, as specified
in :rfc:`7233`.
in :rfc:`9110#section-14`.

A lot of the work, such as parsing the request, is done by the base class
:class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET`
Expand Down
4 changes: 3 additions & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,11 @@ http
(Contributed by Yorik Hansen in :gh:`123430`.)

* Added support for HTTP single-part range requests on files to
:class:`~http.server.SimpleHTTPRequestHandler`, as specified in :rfc:`7233`.
:class:`~http.server.SimpleHTTPRequestHandler`, as specified in
:rfc:`9110#section-14`.
(Contributed by Andy Ling in :gh:`86809`.)


inspect
-------

Expand Down
6 changes: 4 additions & 2 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*)$', re.IGNORECASE)
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$', re.IGNORECASE | re.ASCII)

class HTTPServer(socketserver.TCPServer):

Expand Down Expand Up @@ -779,6 +779,8 @@ def send_head(self):
start, end = self._range
if start is None:
# `end` here means suffix length
# parse_range() collapses (None, None) to None
# and thus `end` can not be None here
start = max(0, fs.st_size - end)
end = fs.st_size - 1
if start >= fs.st_size:
Expand All @@ -791,7 +793,7 @@ def send_head(self):
if end is None or end >= fs.st_size:
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-Range", f"bytes {start}-{end}/{fs.st_size}")
self.send_header("Content-Length", str(end - start + 1))

# Update range to be sent to be used later in copyfile
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Added support for HTTP single-part range requests on files to :class:`~http.server.SimpleHTTPRequestHandler`, as specified in :rfc:`7233`.
Added support for HTTP single-part range requests on files to :class:`~http.server.SimpleHTTPRequestHandler`, as specified in :rfc:`9110#section-14`.
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