feat: improve scripts and write tests
This commit is contained in:
32 files changed
+3817
-723
No files matched your search
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tiny static file server for tests: supports `Range: bytes=N-` and can inject faults.
|
||||
|
||||
usage: range_server.py DIR PORT_FILE [FAULT_FILE]
|
||||
|
||||
FAULT_FILE holds one fault per line; each GET of a non-.sha256 file consumes the first line:
|
||||
truncate N send only N body bytes, then drop the connection
|
||||
status CODE reply with that HTTP status and no body
|
||||
norange ignore the Range header and send the whole file with 200
|
||||
Every request is appended to DIR/../requests.log as "<path> <range-header-or-->".
|
||||
"""
|
||||
|
||||
import http.server
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
root = Path(sys.argv[1]).resolve()
|
||||
port_file = Path(sys.argv[2])
|
||||
fault_file = Path(sys.argv[3]) if len(sys.argv) > 3 else None
|
||||
log_file = root.parent / "requests.log"
|
||||
|
||||
|
||||
def pop_fault():
|
||||
if fault_file is None or not fault_file.exists():
|
||||
return None
|
||||
lines = [ln for ln in fault_file.read_text().splitlines() if ln.strip()]
|
||||
if not lines:
|
||||
return None
|
||||
fault_file.write_text("\n".join(lines[1:]) + ("\n" if lines[1:] else ""))
|
||||
return lines[0].split()
|
||||
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
protocol_version = "HTTP/1.1"
|
||||
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
rng = self.headers.get("Range")
|
||||
with log_file.open("a") as f:
|
||||
f.write(f"{self.path} {rng or '-'}\n")
|
||||
target = (root / self.path.lstrip("/")).resolve()
|
||||
if root not in target.parents or not target.is_file():
|
||||
self.send_response(404)
|
||||
self.send_header("Content-Length", "0")
|
||||
self.end_headers()
|
||||
return
|
||||
data = target.read_bytes()
|
||||
fault = pop_fault() if not target.name.endswith(".sha256") else None
|
||||
|
||||
if fault and fault[0] == "status":
|
||||
self.send_response(int(fault[1]))
|
||||
self.send_header("Content-Length", "0")
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
start = 0
|
||||
if rng and not (fault and fault[0] == "norange"):
|
||||
start = int(rng.split("=")[1].split("-")[0])
|
||||
if start >= len(data):
|
||||
self.send_response(416)
|
||||
self.send_header("Content-Range", f"bytes */{len(data)}")
|
||||
self.send_header("Content-Length", "0")
|
||||
self.end_headers()
|
||||
return
|
||||
self.send_response(206)
|
||||
self.send_header(
|
||||
"Content-Range", f"bytes {start}-{len(data) - 1}/{len(data)}"
|
||||
)
|
||||
else:
|
||||
self.send_response(200)
|
||||
body = data[start:]
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.send_header("Accept-Ranges", "bytes")
|
||||
self.end_headers()
|
||||
if fault and fault[0] == "truncate":
|
||||
self.wfile.write(body[: int(fault[1])])
|
||||
self.wfile.flush()
|
||||
self.close_connection = True
|
||||
self.connection.shutdown(2)
|
||||
return
|
||||
self.wfile.write(body)
|
||||
|
||||
|
||||
class QuietServer(http.server.ThreadingHTTPServer):
|
||||
def handle_error(
|
||||
self, request, client_address
|
||||
): # clients drop connections on purpose
|
||||
pass
|
||||
|
||||
|
||||
srv = QuietServer(("127.0.0.1", 0), Handler)
|
||||
port_file.write_text(str(srv.server_address[1]))
|
||||
srv.serve_forever()
|
||||
Reference in new issue
Block a user