use crate::util::{curl, percent_encode};
pub fn fetch_srcinfo(name: &str, timeout_seconds: Option<u64>) -> Result<String, String> {
let url = format!(
"https://aur.archlinux.org/cgit/aur.git/plain/.SRCINFO?h={}",
percent_encode(name)
);
tracing::debug!("Fetching .SRCINFO from: {}", url);
let text = if let Some(timeout) = timeout_seconds {
let timeout_str = timeout.to_string();
curl::curl_text_with_args(&url, &["--max-time", &timeout_str])
.map_err(|e| format!("curl failed: {e}"))?
} else {
curl::curl_text(&url).map_err(|e| format!("curl failed: {e}"))?
};
if text.trim().is_empty() {
return Err("Empty .SRCINFO content".to_string());
}
if text.trim_start().starts_with("<html") || text.trim_start().starts_with("<!DOCTYPE") {
return Err("Received HTML error page instead of .SRCINFO".to_string());
}
if !text.contains("pkgbase =") && !text.contains("pkgname =") {
return Err("Response does not appear to be valid .SRCINFO format".to_string());
}
Ok(text)
}
pub async fn fetch_srcinfo_async(client: &reqwest::Client, name: &str) -> Result<String, String> {
let url = format!(
"https://aur.archlinux.org/cgit/aur.git/plain/.SRCINFO?h={}",
percent_encode(name)
);
tracing::debug!("Fetching .SRCINFO from: {}", url);
let response = client
.get(&url)
.send()
.await
.map_err(|e| format!("HTTP request failed: {e}"))?;
if !response.status().is_success() {
return Err(format!(
"HTTP request failed with status: {}",
response.status()
));
}
let text = response
.text()
.await
.map_err(|e| format!("Failed to read response body: {e}"))?;
if text.trim().is_empty() {
return Err("Empty .SRCINFO content".to_string());
}
if text.trim_start().starts_with("<html") || text.trim_start().starts_with("<!DOCTYPE") {
return Err("Received HTML error page instead of .SRCINFO".to_string());
}
Ok(text)
}