Optimize server startup speed, client update caching, and HTTP 304 conditional downloads
CI Test Suite / Run Component Tests & Pipeline Verification (push) Failing after 1m28s
CI Test Suite / Run Component Tests & Pipeline Verification (push) Failing after 1m28s
This commit is contained in:
+47
-6
@@ -1497,11 +1497,17 @@ def check_client_update(
|
||||
|
||||
|
||||
@app.get("/api/client/update/download")
|
||||
def download_client_update_package(os: Optional[str] = "windows", os_name: Optional[str] = None):
|
||||
def download_client_update_package(
|
||||
request: Request,
|
||||
os: Optional[str] = "windows",
|
||||
os_name: Optional[str] = None
|
||||
):
|
||||
"""
|
||||
Streams the latest client binary or installer package for the specified OS.
|
||||
Supports HTTP ETag and conditional 304 Not Modified requests to minimize network bandwidth.
|
||||
"""
|
||||
import os as _os
|
||||
import email.utils
|
||||
target_os = os_name or os or "windows"
|
||||
mgr: ServerUpdateManager = SERVER_STATE.get("update_manager") or UPDATE_MANAGER
|
||||
pkg_tuple = mgr.get_client_package_path(os_type=target_os)
|
||||
@@ -1512,14 +1518,50 @@ def download_client_update_package(os: Optional[str] = "windows", os_name: Optio
|
||||
if not _os.path.exists(file_path):
|
||||
raise HTTPException(status_code=404, detail="Client package file missing on server disk.")
|
||||
|
||||
etag = f'"{sha256}"'
|
||||
if request is not None:
|
||||
if_none_match = request.headers.get("if-none-match")
|
||||
if if_none_match and (if_none_match == etag or if_none_match.strip('"') == sha256):
|
||||
return Response(
|
||||
status_code=304,
|
||||
headers={
|
||||
"ETag": etag,
|
||||
"Cache-Control": "public, max-age=300, must-revalidate",
|
||||
"X-Checksum-SHA256": sha256
|
||||
}
|
||||
)
|
||||
|
||||
try:
|
||||
mtime = _os.path.getmtime(file_path)
|
||||
last_modified = email.utils.formatdate(mtime, usegmt=True)
|
||||
except Exception:
|
||||
last_modified = None
|
||||
|
||||
if_modified_since = request.headers.get("if-modified-since")
|
||||
if if_modified_since and last_modified and if_modified_since == last_modified:
|
||||
return Response(
|
||||
status_code=304,
|
||||
headers={
|
||||
"ETag": etag,
|
||||
"Cache-Control": "public, max-age=300, must-revalidate",
|
||||
"X-Checksum-SHA256": sha256
|
||||
}
|
||||
)
|
||||
|
||||
headers = {
|
||||
"ETag": etag,
|
||||
"Cache-Control": "public, max-age=300, must-revalidate",
|
||||
"X-Checksum-SHA256": sha256,
|
||||
"Content-Disposition": f'attachment; filename="{filename}"'
|
||||
}
|
||||
if last_modified:
|
||||
headers["Last-Modified"] = last_modified
|
||||
|
||||
return FileResponse(
|
||||
path=file_path,
|
||||
filename=filename,
|
||||
media_type="application/octet-stream",
|
||||
headers={
|
||||
"X-Checksum-SHA256": sha256,
|
||||
"Content-Disposition": f'attachment; filename="{filename}"'
|
||||
}
|
||||
headers=headers
|
||||
)
|
||||
|
||||
|
||||
@@ -2360,7 +2402,6 @@ def main():
|
||||
csr_path = os.path.join(cert_dir, "ca.csr")
|
||||
if not os.path.exists(csr_path):
|
||||
enrollment.generate_ca_csr(cert_dir=cert_dir)
|
||||
export_license_request_lic(config)
|
||||
|
||||
srv_cert, srv_key, srv_pem, srv_key_pem = enrollment.generate_server_cert_if_needed(
|
||||
ca_cert, ca_key,
|
||||
|
||||
Reference in New Issue
Block a user