Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/huggingface_hub/utils/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from dataclasses import dataclass
from shlex import quote
from typing import Any, TypeVar
from urllib.parse import urlparse
from urllib.parse import urljoin, urlparse

import httpx

Expand Down Expand Up @@ -731,8 +731,10 @@ def _httpx_follow_relative_redirects_with_backoff(
if 300 <= response.status_code <= 399:
parsed_target = urlparse(response.headers["Location"])
if parsed_target.netloc == "":
# Relative redirect -> update URL and retry
url = urlparse(url)._replace(path=parsed_target.path).geturl()
# Relative redirect -> resolve it against the current URL and retry.
# `urljoin` follows RFC 3986 reference resolution, so the target's query string
# is preserved and dot-segments (e.g. `../`) are resolved.
url = urljoin(url, response.headers["Location"])
continue

# Break if no relative redirect
Expand Down
42 changes: 42 additions & 0 deletions tests/test_utils_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
_WARNED_TOPICS,
RateLimitInfo,
_adjust_range_header,
_httpx_follow_relative_redirects_with_backoff,
_parse_bucket_id_from_url,
_parse_repo_info_from_url,
_parse_retry_after,
Expand Down Expand Up @@ -783,3 +784,44 @@ def test_redacts_form_urlencoded_body(self):
assert "refresh_token=<REDACTED>" in redacted
assert "device_code=<REDACTED>" in redacted
assert "client_id=abc" in redacted


class TestFollowRelativeRedirects:
"""Tests for `_httpx_follow_relative_redirects_with_backoff`."""

BASE_URL = "https://hf.co/repo/resolve/main/config.json?base=old"

@pytest.fixture(autouse=True)
def setup(self) -> Generator[None, None, None]:
patcher = patch("huggingface_hub.utils._http.http_backoff")
self.mock_backoff = patcher.start()
yield
patcher.stop()

def _follow(self, location: str) -> str:
"""Follow a single relative redirect to `location` and return the URL requested next."""
self.mock_backoff.side_effect = [
httpx.Response(
308,
headers={"Location": location},
request=httpx.Request("HEAD", self.BASE_URL),
),
httpx.Response(200, request=httpx.Request("HEAD", self.BASE_URL)),
]
_httpx_follow_relative_redirects_with_backoff("HEAD", self.BASE_URL)
return self.mock_backoff.call_args_list[-1].kwargs["url"]

def test_target_query_is_preserved(self) -> None:
"""The query string of the redirect target must be kept (e.g. xet `?etag=...`)."""
assert (
self._follow("/api/resolve-cache/config.json?etag=abc123")
== "https://hf.co/api/resolve-cache/config.json?etag=abc123"
)

def test_base_query_is_not_inherited(self) -> None:
"""When the target has no query, the base query must not be carried over (RFC 3986)."""
assert self._follow("/api/resolve-cache/config.json") == "https://hf.co/api/resolve-cache/config.json"

def test_dot_segments_are_resolved(self) -> None:
"""Relative targets with dot-segments must be resolved, not kept verbatim."""
assert self._follow("../other/config.json?etag=xyz") == "https://hf.co/repo/resolve/other/config.json?etag=xyz"