diff --git a/crates/archivr-core/src/downloader/http.rs b/crates/archivr-core/src/downloader/http.rs index 24847d7..0979485 100644 --- a/crates/archivr-core/src/downloader/http.rs +++ b/crates/archivr-core/src/downloader/http.rs @@ -12,7 +12,8 @@ pub enum UrlKind { } /// 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. +/// 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. /// /// Returns `Err` if the probe fails (network error, non-2xx/405 status). /// Redirects (3xx) are followed automatically by reqwest; only the final @@ -35,40 +36,42 @@ pub fn probe_url_kind(url: &str, cookies: &HashMap) -> Result break stream, + Err(e) + if e.kind() == std::io::ErrorKind::WouldBlock + && Instant::now() < deadline => + { + std::thread::sleep(Duration::from_millis(10)); + } + Err(e) => panic!("expected {method} request: {e}"), + } + }; + let mut request_line = String::new(); + BufReader::new(&stream).read_line(&mut request_line).unwrap(); + assert!( + request_line.starts_with(method), + "unexpected request: {request_line}" + ); + write!( + stream, + "HTTP/1.1 200 OK\r\n{content_type}Content-Length: 0\r\nConnection: close\r\n\r\n" + ) + .unwrap(); + } + }); + + let kind = probe_url_kind( + &format!("http://{addr}/w/the-map-is-not-the-territory"), + &HashMap::new(), + ); + server.join().unwrap(); + assert_eq!(kind.unwrap(), UrlKind::Html); + } }