Spaces:
Running
Running
Download deployment_config.py from ZHIWEI666/ComfyUI-Ranking-API: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/spaces/ZHIWEI666/ComfyUI-Ranking-API/resolve/main/deployment_config.py
- Command line
-
hf download hf://spaces/ZHIWEI666/ComfyUI-Ranking-API/deployment_config.py
-
curl -L -o deployment_config.py https://huggingface.co/spaces/ZHIWEI666/ComfyUI-Ranking-API/resolve/main/deployment_config.py
2.35 kB
| """可信部署地址配置;不依据请求 Host 或转发头构造持久链接。""" | |
| import ipaddress | |
| import os | |
| import re | |
| from urllib.parse import urlsplit, urlunsplit | |
| DEFAULT_PUBLIC_API_BASE_URL = "https://zhiwei666-comfyui-ranking-api.hf.space" | |
| PUBLIC_API_BASE_URL_ENV = "RANKING_PUBLIC_API_BASE_URL" | |
| _CONFIG_ERROR = "RANKING_PUBLIC_API_BASE_URL 配置无效:需要 HTTPS 服务基址,HTTP 仅允许本机回环地址" | |
| def public_api_base_url(environ=None): | |
| """读取受运维控制的基址;缺失兼容默认,显式错误拒绝初始化。""" | |
| environ = os.environ if environ is None else environ | |
| value = environ.get(PUBLIC_API_BASE_URL_ENV, DEFAULT_PUBLIC_API_BASE_URL) | |
| if (not isinstance(value, str) or not value or len(value) > 2048 | |
| or any(ord(char) <= 32 or ord(char) >= 127 for char in value) | |
| or "\\" in value): | |
| raise RuntimeError(_CONFIG_ERROR) | |
| try: | |
| parsed = urlsplit(value) | |
| host = parsed.hostname | |
| port = parsed.port | |
| if (parsed.scheme not in {"http", "https"} or not host | |
| or parsed.username is not None or parsed.password is not None | |
| or "?" in value or "#" in value | |
| or (port is not None and not 1 <= port <= 65535) | |
| or parsed.netloc.endswith(":")): | |
| raise ValueError() | |
| try: | |
| address = ipaddress.ip_address(host) | |
| except ValueError: | |
| address = None | |
| if (len(host) > 253 or not all(re.fullmatch(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?", label) | |
| for label in host.split(".")) or re.fullmatch(r"[0-9.]+", host)): | |
| raise ValueError() | |
| if parsed.scheme == "http" and not (host == "localhost" or address and address.is_loopback): | |
| raise ValueError() | |
| path = parsed.path.rstrip("/") | |
| if (path and (not path.startswith("/") or "//" in path | |
| or not re.fullmatch(r"/[A-Za-z0-9._~/-]+", path) | |
| or any(segment in {".", ".."} for segment in path.split("/")))): | |
| raise ValueError() | |
| return urlunsplit((parsed.scheme, parsed.netloc.lower(), path, "", "")) | |
| except ValueError: | |
| raise RuntimeError(_CONFIG_ERROR) from None | |
| PUBLIC_API_BASE_URL = public_api_base_url() | |