Skip to content

Commit db117ee

Browse files
committed
add range request support for test web server
1 parent dd04ca9 commit db117ee

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
@@ -1930,23 +1930,39 @@ class TestServerHandler(SimpleHTTPRequestHandler):
19301930
# Request header handler for default do_GET() path in
19311931
# SimpleHTTPRequestHandler.do_GET(self) below.
19321932
def send_head(self):
1933+
path = self.translate_path(self.path)
1934+
try:
1935+
f = open(path, 'rb')
1936+
fs = os.fstat(f.fileno())
1937+
except IOError:
1938+
self.send_error(404, "File not found: " + path)
1939+
return None
19331940
if self.path.endswith('.js'):
1934-
path = self.translate_path(self.path)
1935-
try:
1936-
f = open(path, 'rb')
1937-
except IOError:
1938-
self.send_error(404, "File not found: " + path)
1939-
return None
19401941
self.send_response(200)
19411942
self.send_header('Content-type', 'application/javascript')
19421943
self.send_header('Connection', 'close')
1944+
self.send_header('Content-length', fs[6])
1945+
self.end_headers()
1946+
return f
1947+
elif self.headers.get("Range"):
1948+
self.send_response(206)
1949+
ctype = self.guess_type(path)
1950+
self.send_header('Content-Type', ctype)
1951+
pieces = self.headers.get("Range").split("=")[1].split("-")
1952+
start = int(pieces[0]) if pieces[0] != '' else 0
1953+
end = int(pieces[1]) if pieces[1] != '' else fs[6]-1
1954+
end = min(fs[6] - 1, end)
1955+
length = end - start + 1
1956+
self.send_header('Content-Range', "bytes "+str(start)+"-"+str(end)+"/"+str(fs[6]));
1957+
self.send_header('Content-Length', str(length))
19431958
self.end_headers()
19441959
return f
19451960
else:
19461961
return SimpleHTTPRequestHandler.send_head(self)
19471962

19481963
# Add COOP, COEP, CORP, and no-caching headers
19491964
def end_headers(self):
1965+
self.send_header('Accept-Ranges', 'bytes')
19501966
self.send_header('Access-Control-Allow-Origin', '*')
19511967
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
19521968
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
@@ -2041,7 +2057,24 @@ def do_GET(self):
20412057
# Use SimpleHTTPServer default file serving operation for GET.
20422058
if DEBUG:
20432059
print('[simple HTTP serving:', unquote_plus(self.path), ']')
2044-
SimpleHTTPRequestHandler.do_GET(self)
2060+
if self.headers.get("Range"):
2061+
self.send_response(206)
2062+
path = self.translate_path(self.path)
2063+
data = read_binary(path)
2064+
ctype = self.guess_type(path)
2065+
self.send_header('Content-type', 'text/html')
2066+
pieces = self.headers.get("range").split("=")[1].split("-")
2067+
start = int(pieces[0]) if pieces[0] != '' else 0
2068+
end = int(pieces[1]) if pieces[1] != '' else len(data)-1
2069+
end = min(len(data) - 1, end)
2070+
length = end - start + 1
2071+
self.send_header('Content-Length', str(length))
2072+
self.send_header('Content-Range', "bytes "+str(start)+"-"+str(end)+"/"+str(len(data)));
2073+
self.end_headers()
2074+
self.wfile.write(data[start:end + 1])
2075+
else:
2076+
SimpleHTTPRequestHandler.do_GET(self)
2077+
20452078

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

0 commit comments

Comments
 (0)