Skip to content

Commit 6f0aaba

Browse files
committed
add range request support for test web server
1 parent 0b6e63e commit 6f0aaba

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
@@ -2008,23 +2008,39 @@ class TestServerHandler(SimpleHTTPRequestHandler):
20082008
# Request header handler for default do_GET() path in
20092009
# SimpleHTTPRequestHandler.do_GET(self) below.
20102010
def send_head(self):
2011+
path = self.translate_path(self.path)
2012+
try:
2013+
f = open(path, 'rb')
2014+
fs = os.fstat(f.fileno())
2015+
except IOError:
2016+
self.send_error(404, "File not found: " + path)
2017+
return None
20112018
if self.path.endswith('.js'):
2012-
path = self.translate_path(self.path)
2013-
try:
2014-
f = open(path, 'rb')
2015-
except IOError:
2016-
self.send_error(404, "File not found: " + path)
2017-
return None
20182019
self.send_response(200)
20192020
self.send_header('Content-type', 'application/javascript')
20202021
self.send_header('Connection', 'close')
2022+
self.send_header('Content-length', fs[6])
2023+
self.end_headers()
2024+
return f
2025+
elif self.headers.get("Range"):
2026+
self.send_response(206)
2027+
ctype = self.guess_type(path)
2028+
self.send_header('Content-Type', ctype)
2029+
pieces = self.headers.get("Range").split("=")[1].split("-")
2030+
start = int(pieces[0]) if pieces[0] != '' else 0
2031+
end = int(pieces[1]) if pieces[1] != '' else fs[6] - 1
2032+
end = min(fs[6] - 1, end)
2033+
length = end - start + 1
2034+
self.send_header('Content-Range', "bytes " + str(start) + "-" + str(end) + "/" + str(fs[6]))
2035+
self.send_header('Content-Length', str(length))
20212036
self.end_headers()
20222037
return f
20232038
else:
20242039
return SimpleHTTPRequestHandler.send_head(self)
20252040

20262041
# Add COOP, COEP, CORP, and no-caching headers
20272042
def end_headers(self):
2043+
self.send_header('Accept-Ranges', 'bytes')
20282044
self.send_header('Access-Control-Allow-Origin', '*')
20292045
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
20302046
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
@@ -2119,7 +2135,23 @@ def do_GET(self):
21192135
# Use SimpleHTTPServer default file serving operation for GET.
21202136
if DEBUG:
21212137
print('[simple HTTP serving:', unquote_plus(self.path), ']')
2122-
SimpleHTTPRequestHandler.do_GET(self)
2138+
if self.headers.get("Range"):
2139+
self.send_response(206)
2140+
path = self.translate_path(self.path)
2141+
data = read_binary(path)
2142+
ctype = self.guess_type(path)
2143+
self.send_header('Content-type', ctype)
2144+
pieces = self.headers.get("range").split("=")[1].split("-")
2145+
start = int(pieces[0]) if pieces[0] != '' else 0
2146+
end = int(pieces[1]) if pieces[1] != '' else len(data) - 1
2147+
end = min(len(data) - 1, end)
2148+
length = end - start + 1
2149+
self.send_header('Content-Length', str(length))
2150+
self.send_header('Content-Range', "bytes " + str(start) + "-" + str(end) + "/" + str(len(data)))
2151+
self.end_headers()
2152+
self.wfile.write(data[start:end + 1])
2153+
else:
2154+
SimpleHTTPRequestHandler.do_GET(self)
21232155

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

0 commit comments

Comments
 (0)