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
Apply suggestions from code review
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
  • Loading branch information
lyc8503 and picnixz authored Jan 2, 2025
commit aa9ec6b7ef25b281b3b2a961180c2ed1bfabba85
19 changes: 11 additions & 8 deletions Doc/library/http.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,17 @@ provides three different variants:
readable description of the error. The *explain* argument can be used to
provide more detailed information about the error; it will be formatted
using the :attr:`error_message_format` attribute and emitted, after
a complete set of headers, as the response body. The *extra_headers*
argument can be a key-value tuple list which specifies extra headers to
be sent in the response. The :attr:`responses` attribute holds the
default values for *message* and *explain* that will be used if no value
is provided; for unknown codes the default value for both is the string
``???``. The body will be empty if the method is HEAD or the response
code is one of the following: :samp:`1{xx}`, ``204 No Content``,
``205 Reset Content``, ``304 Not Modified``.
a complete set of headers, as the response body.

The *extra_headers* argument can be a key-value tuple list which
specifies additional headers to be sent in the response (for
instance, ``[("Content-Range", "bytes 3-14/42")]``).

The :attr:`responses` attribute holds the default values for *message*
and *explain* that will be used if no value is provided; for unknown codes
the default value for both is the string ``???``. The body will be empty if
the method is HEAD or the response code is one of the following: :samp:`1{xx}`,
``204 No Content``, ``205 Reset Content``, or ``304 Not Modified``.

.. versionchanged:: 3.4
The error response includes a Content-Length header.
Expand Down
16 changes: 9 additions & 7 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 | re.ASCII)
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$', re.ASCII | re.IGNORECASE)

class HTTPServer(socketserver.TCPServer):

Expand Down Expand Up @@ -495,8 +495,8 @@ def send_error(self, code, message=None, explain=None, extra_headers=None):
self.send_header("Content-Type", self.error_content_type)
self.send_header('Content-Length', str(len(body)))
if extra_headers is not None:
for (keyword, value) in extra_headers:
self.send_header(keyword, value)
for name, value in extra_headers:
self.send_header(name, value)
self.end_headers()

if self.command != 'HEAD' and body:
Expand Down Expand Up @@ -779,9 +779,9 @@ def send_head(self):
if self._range:
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
assert end is not None
# `end` here means suffix length
start = max(0, fs.st_size - end)
end = fs.st_size - 1
if start >= fs.st_size:
Expand Down Expand Up @@ -958,8 +958,10 @@ def guess_type(self, path):

def parse_range(self):
"""Return a tuple of (start, end) representing the range header in
the HTTP request. If the range header is missing or not resolvable,
None is returned. This only supports single part ranges.
the HTTP request. If the range header is missing, not resolvable,
or trivial (namely "byte=-"), this returns None.

This currently only supports single part ranges.

"""
range_header = self.headers.get('range')
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