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
Next Next commit
Add support for HTTP Range header in SimpleHTTPServer
  • Loading branch information
lyc8503 committed Dec 15, 2024
commit e81d46ea26b7c7e5302db3609cc39c394117ae34
71 changes: 62 additions & 9 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import mimetypes
import os
import posixpath
import re
import select
import shutil
import socket # For gethostbyaddr()
Expand Down Expand Up @@ -682,7 +683,7 @@ def do_GET(self):
f = self.send_head()
if f:
try:
self.copyfile(f, self.wfile)
self.copyfile(f, self.wfile, range=self.range)
finally:
f.close()

Expand All @@ -705,6 +706,7 @@ def send_head(self):
"""
path = self.translate_path(self.path)
f = None
self.range = self.get_range()
if os.path.isdir(path):
parts = urllib.parse.urlsplit(self.path)
if not parts.path.endswith('/'):
Expand Down Expand Up @@ -769,9 +771,26 @@ def send_head(self):
f.close()
return None

self.send_response(HTTPStatus.OK)
if self.range:
start, end = self.range
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()
self.send_error(HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
return None
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-Length", str(end-start+1))

# Update range to be sent to be used later in copyfile
self.range = (start, end)
else:
self.send_response(HTTPStatus.OK)
self.send_header("Accept-Ranges", "bytes")
self.send_header("Content-Length", str(fs[6]))
self.send_header("Content-type", ctype)
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified",
self.date_time_string(fs.st_mtime))
self.end_headers()
Expand Down Expand Up @@ -868,21 +887,37 @@ def translate_path(self, path):
path += '/'
return path

def copyfile(self, source, outputfile):
"""Copy all data between two file objects.
def copyfile(self, source, outputfile, range=None):
"""Copy all data between two file objects if range is None.
Otherwise, copy data between two file objects based on the
inclusive range (start, end).

The SOURCE argument is a file object open for reading
(or anything with a read() method) and the DESTINATION
argument is a file object open for writing (or
anything with a write() method).
(or anything with read() and seek() method) and the
DESTINATION argument is a file object open for writing
(or anything with a write() method).

The only reason for overriding this would be to change
the block size or perhaps to replace newlines by CRLF
-- note however that this the default server uses this
to copy binary data as well.

"""
shutil.copyfileobj(source, outputfile)
if range is None:
shutil.copyfileobj(source, outputfile)
else:
start, end = range
length = end - start + 1
source.seek(start)
while True:
if length <= 0:
break
buf = source.read(min(length, shutil.COPY_BUFSIZE))
if not buf:
break
length -= len(buf)
outputfile.write(buf)


def guess_type(self, path):
"""Guess the type of a file.
Expand All @@ -909,6 +944,24 @@ def guess_type(self, path):
return guess
return 'application/octet-stream'

def get_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.

"""
range_header = self.headers.get('range')
if not range_header:
return None
m = re.match(r'bytes=(\d+)-(\d*)$', range_header)
if not m:
return None
start = m.group(1)
if not m.group(2):
return int(start), None
end = m.group(2)
return int(start), int(end)


# Utilities for CGIHTTPRequestHandler

Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,27 @@ def test_get(self):
finally:
os.chmod(self.tempdir, 0o755)

def test_range_get(self):
response = self.request(self.base_url + '/test')
self.assertEqual(response.getheader('accept-ranges'), 'bytes')
self.check_status_and_reason(response, HTTPStatus.OK, data=self.data)

response = self.request(self.base_url + '/test', headers={'Range': 'bytes=3-12'})
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])

response = self.request(self.base_url + '/test', headers={'Range': 'bytes=3-'})
self.assertEqual(response.getheader('content-range'), 'bytes 3-29/30')
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=100-200'})
self.check_status_and_reason(response, HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)

response = self.request(self.base_url + '/test', headers={'Range': 'bytes=wrong format'})
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for HTTP Range header in ``SimpleHTTPServer``
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