From c835d972a5e7b949f0966f0fdb6d4f990a798382 Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Wed, 14 Oct 2026 17:25:16 +0200 Subject: [PATCH] fix: reuse probe GET for single-use file URLs --- crates/archivr-core/src/capture.rs | 14 ++- crates/archivr-core/src/downloader/http.rs | 104 +++++++++++++++++++-- 2 files changed, 107 insertions(+), 11 deletions(-) diff --git a/crates/archivr-core/src/capture.rs b/crates/archivr-core/src/capture.rs index 2430a60..b438723 100644 --- a/crates/archivr-core/src/capture.rs +++ b/crates/archivr-core/src/capture.rs @@ -1277,10 +1277,11 @@ pub fn perform_capture( // For generic http/https URLs, probe Content-Type to decide whether to // treat the URL as a raw file download or an HTML page for SingleFile. + let mut probed_file_response = None; if source == Source::Url { - match downloader::http::probe_url_kind(locator, &cookies) { - Ok(downloader::http::UrlKind::Html) => source = Source::WebPage, - Ok(downloader::http::UrlKind::File) => {} + match downloader::http::probe_url(locator, &cookies) { + Ok(probe) if probe.kind == downloader::http::UrlKind::Html => source = Source::WebPage, + Ok(probe) => probed_file_response = probe.file_response, Err(e) => { // Probe failed (e.g. Cloudflare JS challenge, 403, network // issue). Fall back to treating the URL as an HTML page so @@ -1651,7 +1652,12 @@ pub fn perform_capture( // Source: generic HTTP/S file URL if source == Source::Url { - match downloader::http::download(locator, store_path, ×tamp, &cookies) { + let download = if let Some(response) = probed_file_response { + downloader::http::download_response(response, locator, store_path, ×tamp) + } else { + downloader::http::download(locator, store_path, ×tamp, &cookies) + }; + match download { Ok((hash, file_extension, title_hint)) => { let temp_file = store_path .join("temp") diff --git a/crates/archivr-core/src/downloader/http.rs b/crates/archivr-core/src/downloader/http.rs index 0979485..8d9ad5b 100644 --- a/crates/archivr-core/src/downloader/http.rs +++ b/crates/archivr-core/src/downloader/http.rs @@ -5,12 +5,19 @@ use crate::downloader::cookies::cookies_to_header; use crate::hash::hash_file; /// Whether a URL resolves to an HTML document or a downloadable file. -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum UrlKind { Html, File, } +/// A GET used to classify a file can also supply its body to the downloader. +/// Keeping that response matters for URLs that allow only one GET. +pub(crate) struct UrlProbe { + pub kind: UrlKind, + pub file_response: Option, +} + /// Probes `url` with a HEAD request and inspects the `Content-Type` header. /// Falls back to a GET request (body not read) if the server returns 405 or /// omits the header on HEAD. Some sites provide a type only for GET. @@ -19,6 +26,10 @@ pub enum UrlKind { /// Redirects (3xx) are followed automatically by reqwest; only the final /// response status is checked. pub fn probe_url_kind(url: &str, cookies: &HashMap) -> Result { + Ok(probe_url(url, cookies)?.kind) +} + +pub(crate) fn probe_url(url: &str, cookies: &HashMap) -> Result { let client = reqwest::blocking::Client::builder() .redirect(reqwest::redirect::Policy::limited(10)) .user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 archivr/0.1") @@ -46,10 +57,13 @@ pub fn probe_url_kind(url: &str, cookies: &HashMap) -> Result) -> Result Result<(String, String, Option)> { if !response.status().is_success() { bail!("HTTP {} fetching {url}", response.status()); } @@ -465,4 +494,65 @@ mod tests { server.join().unwrap(); assert_eq!(kind.unwrap(), UrlKind::Html); } + + #[test] + fn capture_reuses_probe_get_for_single_use_file() { + use std::io::{BufRead, BufReader, Write}; + use std::net::TcpListener; + use std::sync::{Arc, atomic::{AtomicBool, AtomicUsize, Ordering}}; + use std::time::{Duration, Instant}; + + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + listener.set_nonblocking(true).unwrap(); + let stop = Arc::new(AtomicBool::new(false)); + let get_count = Arc::new(AtomicUsize::new(0)); + let server_stop = Arc::clone(&stop); + let server_get_count = Arc::clone(&get_count); + let server = std::thread::spawn(move || { + let deadline = Instant::now() + Duration::from_secs(10); + while !server_stop.load(Ordering::SeqCst) && Instant::now() < deadline { + let mut stream = match listener.accept() { + Ok((stream, _)) => stream, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + std::thread::sleep(Duration::from_millis(10)); + continue; + } + Err(e) => panic!("accept failed: {e}"), + }; + let mut request_line = String::new(); + BufReader::new(&stream).read_line(&mut request_line).unwrap(); + if request_line.starts_with("HEAD ") { + write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").unwrap(); + } else if request_line.starts_with("GET ") { + if server_get_count.fetch_add(1, Ordering::SeqCst) == 0 { + write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: application/pdf\r\nContent-Length: 9\r\nConnection: close\r\n\r\n%PDF-1.7\n").unwrap(); + } else { + write!(stream, "HTTP/1.1 410 Gone\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").unwrap(); + } + } else { + panic!("unexpected request: {request_line}"); + } + } + }); + + let temp = tempfile::tempdir().unwrap(); + let paths = crate::archive::initialize_archive( + temp.path(), + &temp.path().join("store"), + "single-use-file", + false, + ).unwrap(); + let result = crate::capture::perform_capture( + &paths, + &format!("http://{addr}/one-time.pdf"), + None, + None, + &crate::capture::CaptureConfig::default(), + ); + stop.store(true, Ordering::SeqCst); + server.join().unwrap(); + assert_eq!(result.unwrap().status, "completed"); + assert_eq!(get_count.load(Ordering::SeqCst), 1); + } }