Skip to content

Commit 34372c5

Browse files
committed
add range request support for test web server
1 parent 8f213fa commit 34372c5

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

test/common.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,23 +2020,39 @@ class TestServerHandler(SimpleHTTPRequestHandler):
20202020
# Request header handler for default do_GET() path in
20212021
# SimpleHTTPRequestHandler.do_GET(self) below.
20222022
def send_head(self):
2023+
path = self.translate_path(self.path)
2024+
try:
2025+
f = open(path, 'rb')
2026+
fs = os.fstat(f.fileno())
2027+
except IOError:
2028+
self.send_error(404, "File not found: " + path)
2029+
return None
20232030
if self.path.endswith('.js'):
2024-
path = self.translate_path(self.path)
2025-
try:
2026-
f = open(path, 'rb')
2027-
except IOError:
2028-
self.send_error(404, "File not found: " + path)
2029-
return None
20302031
self.send_response(200)
20312032
self.send_header('Content-type', 'application/javascript')
20322033
self.send_header('Connection', 'close')
2034+
self.send_header('Content-length', fs[6])
2035+
self.end_headers()
2036+
return f
2037+
elif self.headers.get("Range"):
2038+
self.send_response(206)
2039+
ctype = self.guess_type(path)
2040+
self.send_header('Content-Type', ctype)
2041+
pieces = self.headers.get("Range").split("=")[1].split("-")
2042+
start = int(pieces[0]) if pieces[0] != '' else 0
2043+
end = int(pieces[1]) if pieces[1] != '' else fs[6] - 1
2044+
end = min(fs[6] - 1, end)
2045+
length = end - start + 1
2046+
self.send_header('Content-Range', "bytes " + str(start) + "-" + str(end) + "/" + str(fs[6]))
2047+
self.send_header('Content-Length', str(length))
20332048
self.end_headers()
20342049
return f
20352050
else:
20362051
return SimpleHTTPRequestHandler.send_head(self)
20372052

20382053
# Add COOP, COEP, CORP, and no-caching headers
20392054
def end_headers(self):
2055+
self.send_header('Accept-Ranges', 'bytes')
20402056
self.send_header('Access-Control-Allow-Origin', '*')
20412057
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
20422058
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
@@ -2131,7 +2147,23 @@ def do_GET(self):
21312147
# Use SimpleHTTPServer default file serving operation for GET.
21322148
if DEBUG:
21332149
print('[simple HTTP serving:', unquote_plus(self.path), ']')
2134-
SimpleHTTPRequestHandler.do_GET(self)
2150+
if self.headers.get("Range"):
2151+
self.send_response(206)
2152+
path = self.translate_path(self.path)
2153+
data = read_binary(path)
2154+
ctype = self.guess_type(path)
2155+
self.send_header('Content-type', ctype)
2156+
pieces = self.headers.get("range").split("=")[1].split("-")
2157+
start = int(pieces[0]) if pieces[0] != '' else 0
2158+
end = int(pieces[1]) if pieces[1] != '' else len(data) - 1
2159+
end = min(len(data) - 1, end)
2160+
length = end - start + 1
2161+
self.send_header('Content-Length', str(length))
2162+
self.send_header('Content-Range', "bytes " + str(start) + "-" + str(end) + "/" + str(len(data)))
2163+
self.end_headers()
2164+
self.wfile.write(data[start:end + 1])
2165+
else:
2166+
SimpleHTTPRequestHandler.do_GET(self)
21352167

21362168
def log_request(code=0, size=0):
21372169
# don't log; too noisy

0 commit comments

Comments
 (0)