use std::time::Duration;
use anyhow::{Context, Result, anyhow};
use futures::stream::StreamExt;
use reqwest::header::{ACCEPT, HeaderMap, HeaderValue, USER_AGENT};
use url::Url;
use super::SearchResult;
use super::parser::parse_duckduckgo_response;
const MIN_TIMEOUT_SECS: u64 = 1;
const MAX_TIMEOUT_SECS: u64 = 60;
const BROWSER_USER_AGENT: &str =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36";
fn build_client(timeout_secs: u64) -> Result<reqwest::Client> {
let builder = reqwest::Client::builder()
.timeout(Duration::from_secs(effective_timeout_secs(timeout_secs)))
.retry(reqwest::retry::never())
.redirect(reqwest::redirect::Policy::custom(|attempt| {
if attempt.previous().len() >= 3 {
return attempt.stop();
}
if is_allowed_ddg_redirect(attempt.url()) {
attempt.follow()
} else {
attempt.stop()
}
}));
vtcode_commons::http::apply_connect_timeout(builder)
.build()
.context("failed to build HTTP client for web_search")
}
fn effective_timeout_secs(timeout_secs: u64) -> u64 {
timeout_secs.clamp(MIN_TIMEOUT_SECS, MAX_TIMEOUT_SECS)
}
fn is_allowed_ddg_redirect(url: &Url) -> bool {
url.scheme() == "https" && url.host_str() == Some("html.duckduckgo.com")
}
pub(super) async fn duckduckgo_search(query: &str, max_results: usize, timeout_secs: u64) -> Result<Vec<SearchResult>> {
let client = build_client(timeout_secs)?;
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_static(BROWSER_USER_AGENT));
headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml"));
let response = client
.post("https://html.duckduckgo.com/html/")
.headers(headers)
.form(&[("q", query)])
.send()
.await
.context("DuckDuckGo request failed")?;
let status = response.status();
if matches!(status.as_u16(), 202 | 403 | 429) {
return Err(anyhow!(
"DuckDuckGo declined the request (HTTP {status}), likely an anti-bot challenge for this network. Wait a few seconds and retry."
));
}
if !status.is_success() {
return Err(anyhow!("DuckDuckGo returned HTTP {status}"));
}
let body = read_capped_ddg_body(response).await?;
parse_duckduckgo_response(&body, max_results)
}
const MAX_DDG_BODY_BYTES: usize = 1_000_000;
async fn read_capped_ddg_body(response: reqwest::Response) -> Result<String> {
if response
.content_length()
.is_some_and(|length| length > MAX_DDG_BODY_BYTES as u64)
{
return Err(anyhow!("DuckDuckGo response exceeds the {MAX_DDG_BODY_BYTES}-byte limit"));
}
let mut bytes = Vec::with_capacity(MAX_DDG_BODY_BYTES.min(64 * 1024));
let mut stream = response.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk.context("failed to read DuckDuckGo response body")?;
let remaining = MAX_DDG_BODY_BYTES.saturating_sub(bytes.len());
if chunk.len() > remaining {
return Err(anyhow!("DuckDuckGo response body exceeded the {MAX_DDG_BODY_BYTES}-byte limit"));
}
bytes.extend_from_slice(&chunk);
}
Ok(capped_body_text(&bytes))
}
fn capped_body_text(bytes: &[u8]) -> String {
let end = bytes.len().min(MAX_DDG_BODY_BYTES);
String::from_utf8_lossy(&bytes[..end]).into_owned()
}
#[cfg(test)]
mod tests;