Skip to content

Commit e2295ce

Browse files
committed
add range request support for test web server
1 parent 4dc0d40 commit e2295ce

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

test/common.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,23 +1933,39 @@ class TestServerHandler(SimpleHTTPRequestHandler):
19331933
# Request header handler for default do_GET() path in
19341934
# SimpleHTTPRequestHandler.do_GET(self) below.
19351935
def send_head(self):
1936+
path = self.translate_path(self.path)
1937+
try:
1938+
f = open(path, 'rb')
1939+
fs = os.fstat(f.fileno())
1940+
except IOError:
1941+
self.send_error(404, "File not found: " + path)
1942+
return None
19361943
if self.path.endswith('.js'):
1937-
path = self.translate_path(self.path)
1938-
try:
1939-
f = open(path, 'rb')
1940-
except IOError:
1941-
self.send_error(404, "File not found: " + path)
1942-
return None
19431944
self.send_response(200)
19441945
self.send_header('Content-type', 'application/javascript')
19451946
self.send_header('Connection', 'close')
1947+
self.send_header('Content-length', fs[6])
1948+
self.end_headers()
1949+
return f
1950+
elif self.headers.get("Range"):
1951+
self.send_response(206)
1952+
ctype = self.guess_type(path)
1953+
self.send_header('Content-Type', ctype)
1954+
pieces = self.headers.get("Range").split("=")[1].split("-")
1955+
start = int(pieces[0]) if pieces[0] != '' else 0
1956+
end = int(pieces[1]) if pieces[1] != '' else fs[6]-1
1957+
end = min(fs[6] - 1, end)
1958+
length = end - start + 1
1959+
self.send_header('Content-Range', "bytes "+str(start)+"-"+str(end)+"/"+str(fs[6]));
1960+
self.send_header('Content-Length', str(length))
19461961
self.end_headers()
19471962
return f
19481963
else:
19491964
return SimpleHTTPRequestHandler.send_head(self)
19501965

19511966
# Add COOP, COEP, CORP, and no-caching headers
19521967
def end_headers(self):
1968+
self.send_header('Accept-Ranges', 'bytes')
19531969
self.send_header('Access-Control-Allow-Origin', '*')
19541970
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
19551971
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
@@ -2044,7 +2060,24 @@ def do_GET(self):
20442060
# Use SimpleHTTPServer default file serving operation for GET.
20452061
if DEBUG:
20462062
print('[simple HTTP serving:', unquote_plus(self.path), ']')
2047-
SimpleHTTPRequestHandler.do_GET(self)
2063+
if self.headers.get("Range"):
2064+
self.send_response(206)
2065+
path = self.translate_path(self.path)
2066+
data = read_binary(path)
2067+
ctype = self.guess_type(path)
2068+
self.send_header('Content-type', 'text/html')
2069+
pieces = self.headers.get("range").split("=")[1].split("-")
2070+
start = int(pieces[0]) if pieces[0] != '' else 0
2071+
end = int(pieces[1]) if pieces[1] != '' else len(data)-1
2072+
end = min(len(data) - 1, end)
2073+
length = end - start + 1
2074+
self.send_header('Content-Length', str(length))
2075+
self.send_header('Content-Range', "bytes "+str(start)+"-"+str(end)+"/"+str(len(data)));
2076+
self.end_headers()
2077+
self.wfile.write(data[start:end + 1])
2078+
else:
2079+
SimpleHTTPRequestHandler.do_GET(self)
2080+
20482081

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

0 commit comments

Comments
 (0)