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
Error handling and testcase improve
  • Loading branch information
lyc8503 committed Jan 1, 2025
commit b266ff45f537394aa94df557ff58e59e7206dd73
9 changes: 3 additions & 6 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*)$')
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$', re.IGNORECASE)

class HTTPServer(socketserver.TCPServer):

Expand Down Expand Up @@ -916,16 +916,13 @@ def copyfile(self, source, outputfile, *, range=None):
start, end = range
length = end - start + 1
source.seek(start)
while True:
if length <= 0:
break
while length > 0:
buf = source.read(min(length, shutil.COPY_BUFSIZE))
if not buf:
break
raise EOFError('File shrank after size was checked')
length -= len(buf)
outputfile.write(buf)


def guess_type(self, path):
"""Guess the type of a file.

Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_range_get(self):
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)

# valid ranges
response = self.request(route, headers={'Range': 'bytes=3-12'})
response = self.request(route, headers={'Range': 'bYtEs=3-12'}) # case insensitive
self.assertEqual(response.getheader('content-range'), 'bytes 3-12/30')
self.assertEqual(response.getheader('content-length'), '10')
self.check_status_and_reason(response, HTTPStatus.PARTIAL_CONTENT, data=self.data[3:13])
Expand Down Expand Up @@ -585,6 +585,10 @@ def test_range_get(self):
response = self.request(route, headers={'Range': 'bytes=-'})
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)

# multipart ranges (not supported currently)
response = self.request(route, headers={'Range': 'bytes=1-2, 4-7'})
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)

def test_head(self):
response = self.request(
self.base_url + '/test', method='HEAD')
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