Skip to content

Commit 39af37e

Browse files
committed
add range request support for test web server
1 parent 0545af9 commit 39af37e

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
@@ -2049,23 +2049,39 @@ class TestServerHandler(SimpleHTTPRequestHandler):
20492049
# Request header handler for default do_GET() path in
20502050
# SimpleHTTPRequestHandler.do_GET(self) below.
20512051
def send_head(self):
2052+
path = self.translate_path(self.path)
2053+
try:
2054+
f = open(path, 'rb')
2055+
fs = os.fstat(f.fileno())
2056+
except IOError:
2057+
self.send_error(404, "File not found: " + path)
2058+
return None
20522059
if self.path.endswith('.js'):
2053-
path = self.translate_path(self.path)
2054-
try:
2055-
f = open(path, 'rb')
2056-
except IOError:
2057-
self.send_error(404, "File not found: " + path)
2058-
return None
20592060
self.send_response(200)
20602061
self.send_header('Content-type', 'application/javascript')
20612062
self.send_header('Connection', 'close')
2063+
self.send_header('Content-length', fs[6])
2064+
self.end_headers()
2065+
return f
2066+
elif self.headers.get("Range"):
2067+
self.send_response(206)
2068+
ctype = self.guess_type(path)
2069+
self.send_header('Content-Type', ctype)
2070+
pieces = self.headers.get("Range").split("=")[1].split("-")
2071+
start = int(pieces[0]) if pieces[0] != '' else 0
2072+
end = int(pieces[1]) if pieces[1] != '' else fs[6] - 1
2073+
end = min(fs[6] - 1, end)
2074+
length = end - start + 1
2075+
self.send_header('Content-Range', "bytes " + str(start) + "-" + str(end) + "/" + str(fs[6]))
2076+
self.send_header('Content-Length', str(length))
20622077
self.end_headers()
20632078
return f
20642079
else:
20652080
return SimpleHTTPRequestHandler.send_head(self)
20662081

20672082
# Add COOP, COEP, CORP, and no-caching headers
20682083
def end_headers(self):
2084+
self.send_header('Accept-Ranges', 'bytes')
20692085
self.send_header('Access-Control-Allow-Origin', '*')
20702086
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
20712087
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
@@ -2160,7 +2176,23 @@ def do_GET(self):
21602176
# Use SimpleHTTPServer default file serving operation for GET.
21612177
if DEBUG:
21622178
print('[simple HTTP serving:', unquote_plus(self.path), ']')
2163-
SimpleHTTPRequestHandler.do_GET(self)
2179+
if self.headers.get("Range"):
2180+
self.send_response(206)
2181+
path = self.translate_path(self.path)
2182+
data = read_binary(path)
2183+
ctype = self.guess_type(path)
2184+
self.send_header('Content-type', ctype)
2185+
pieces = self.headers.get("range").split("=")[1].split("-")
2186+
start = int(pieces[0]) if pieces[0] != '' else 0
2187+
end = int(pieces[1]) if pieces[1] != '' else len(data) - 1
2188+
end = min(len(data) - 1, end)
2189+
length = end - start + 1
2190+
self.send_header('Content-Length', str(length))
2191+
self.send_header('Content-Range', "bytes " + str(start) + "-" + str(end) + "/" + str(len(data)))
2192+
self.end_headers()
2193+
self.wfile.write(data[start:end + 1])
2194+
else:
2195+
SimpleHTTPRequestHandler.do_GET(self)
21642196

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

0 commit comments

Comments
 (0)