Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download scripts/utils/http_url_normalize.py from CommunityOne/open-navigator: direct link, hf CLI and curl.
- Browser
- Download file 817 Bytes
-
https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/scripts/utils/http_url_normalize.py
- Command line
-
hf download hf://spaces/CommunityOne/open-navigator/scripts/utils/http_url_normalize.py
-
curl -L -o http_url_normalize.py https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/scripts/utils/http_url_normalize.py
817 Bytes
| """RFC-safe HTTP(S) URL path encoding for scrapers and loaders.""" | |
| from __future__ import annotations | |
| from urllib.parse import quote, unquote, urlparse, urlunparse | |
| def normalize_http_url_path_encoding(url: str) -> str: | |
| """ | |
| Percent-encode characters that are illegal in URL paths (e.g. raw spaces) without | |
| double-encoding existing ``%xx`` sequences. Query string is left unchanged; fragment stripped. | |
| """ | |
| raw = (url or "").strip() | |
| if not raw: | |
| return raw | |
| try: | |
| p = urlparse(raw) | |
| except Exception: | |
| return raw | |
| if p.scheme not in ("http", "https") or not p.netloc: | |
| return raw | |
| try: | |
| path = quote(unquote(p.path), safe="/") | |
| return urlunparse((p.scheme, p.netloc, path, p.params, p.query, "")) | |
| except Exception: | |
| return raw | |