From 112df62a01e482f33b68e1dac190f70085fcec64 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 10:00:50 +0900 Subject: refactor(php-shim): give parse_url a typed UrlComponents result parse_url now returns Option instead of a PhpMixed array, and the component-selecting overload with the PHP_URL_* constants is gone: callers read the field they want. Two call sites change behaviour as a result, both towards PHP: * CurlDownloader::handle_redirect tested scheme and host with is_null(), so an unparsable Location header (PhpMixed::Bool(false)) counted as an absolute URL. PHP's truthiness test sends it to the relative-path branch. * Url::get_origin appended a literal port 0, which PHP treats as falsy and leaves off. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/util/http/curl_downloader.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'crates/shirabe/src/util/http/curl_downloader.rs') diff --git a/crates/shirabe/src/util/http/curl_downloader.rs b/crates/shirabe/src/util/http/curl_downloader.rs index ff59266e..742e0fd3 100644 --- a/crates/shirabe/src/util/http/curl_downloader.rs +++ b/crates/shirabe/src/util/http/curl_downloader.rs @@ -628,22 +628,31 @@ impl CurlDownloader { if let Some(location_header) = response.inner.get_header("location") && !location_header.is_empty() { - if !parse_url(&location_header, shirabe_php_shim::PHP_URL_SCHEME).is_null() { + let location_parsed = parse_url(&location_header); + if location_parsed + .as_ref() + .and_then(|parsed| parsed.scheme.as_deref()) + .is_some_and(|scheme| !scheme.is_empty() && scheme != "0") + { // Absolute URL; e.g. https://example.com/composer target_url = location_header; - } else if !parse_url(&location_header, shirabe_php_shim::PHP_URL_HOST).is_null() { + } else if location_parsed + .as_ref() + .and_then(|parsed| parsed.host.as_deref()) + .is_some_and(|host| !host.is_empty() && host != "0") + { // Scheme relative; e.g. //example.com/foo target_url = format!( "{}:{}", - parse_url(url, shirabe_php_shim::PHP_URL_SCHEME) - .as_string() - .unwrap_or(""), + parse_url(url) + .and_then(|parsed| parsed.scheme) + .unwrap_or_default(), location_header ); } else if location_header.starts_with('/') { // Absolute path; e.g. /foo - let url_host = parse_url(url, shirabe_php_shim::PHP_URL_HOST); - let url_host_str = url_host.as_string().unwrap_or(""); + let url_host = parse_url(url).and_then(|parsed| parsed.host); + let url_host_str = url_host.as_deref().unwrap_or(""); target_url = Preg::replace( format!( r"{{^(.+(?://|@){}(?::\d+)?)(?:[/\?].*)?$}}", -- cgit v1.3.1-4-g156e