Skip to main content

utz_build/
download.rs

1//! The conditional-GET download cache stores `ETag` / `Last-Modified`
2//! next to each cached file and revalidates with `If-None-Match` /
3//! `If-Modified-Since`; a 304 reuses the cache untouched.
4
5use std::io::Read as _;
6use std::path::{Path, PathBuf};
7
8/// Fetches `url` into `cache_dir`, revalidating any cached copy, and
9/// returns the cached file path. When offline with a cached copy present,
10/// it warns and reuses the copy.
11///
12/// # Errors
13/// Returns an error on a URL without a filename component, an HTTP
14/// failure with no cached copy to fall back on, or an I/O failure writing
15/// the cache.
16pub fn fetch(url: &str, cache_dir: &Path) -> crate::Result<PathBuf> {
17    std::fs::create_dir_all(cache_dir)?;
18    let name = url
19        .rsplit('/')
20        .next()
21        .filter(|segment| !segment.is_empty())
22        .ok_or_else(|| crate::Error::NoFilename { url: url.into() })?;
23    let file = cache_dir.join(name);
24    let headers_path = cache_dir.join(format!("{name}.headers"));
25
26    let mut request = ureq::get(url);
27    if file.exists()
28        && let Ok(cached_headers) = std::fs::read_to_string(&headers_path)
29    {
30        for line in cached_headers.lines() {
31            if let Some(value) = line.strip_prefix("etag: ") {
32                request = request.header("If-None-Match", value);
33            } else if let Some(value) = line.strip_prefix("last-modified: ") {
34                request = request.header("If-Modified-Since", value);
35            }
36        }
37    }
38
39    match request.call() {
40        Ok(response) if response.status() == 304 => Ok(file),
41        Ok(response) => {
42            use std::fmt::Write as _;
43            let header = |name: &str| {
44                response
45                    .headers()
46                    .get(name)
47                    .and_then(|value| value.to_str().ok())
48                    .map(str::to_owned)
49            };
50            let mut headers = String::new();
51            if let Some(value) = header("etag") {
52                let _ = writeln!(headers, "etag: {value}");
53            }
54            if let Some(value) = header("last-modified") {
55                let _ = writeln!(headers, "last-modified: {value}");
56            }
57            let mut bytes = Vec::new();
58            response.into_body().into_reader().read_to_end(&mut bytes)?;
59            let partial_path = file.with_extension("part");
60            std::fs::write(&partial_path, &bytes)?;
61            std::fs::rename(&partial_path, &file)?;
62            std::fs::write(&headers_path, headers)?;
63            Ok(file)
64        }
65        Err(error) if file.exists() => {
66            eprintln!("warning: revalidation of {url} failed ({error}); using cached copy");
67            Ok(file)
68        }
69        Err(error) => Err(error.into()),
70    }
71}