Source src/lint/fetch.rs
1//! Bounded blocking HTTPS retrieval; automatic redirects are intentionally disabled.2use std::{io::Read, time::Duration};34pub(super) const MAX_BYTES: usize = 8 * 1024 * 1024;56pub(super) fn read_bounded(reader: impl Read) -> Result<Vec<u8>, String> {7 let mut bytes = Vec::new();8 reader9 .take((MAX_BYTES + 1) as u64)10 .read_to_end(&mut bytes)11 .map_err(|e| e.to_string())?;12 if bytes.len() > MAX_BYTES {13 return Err("Category document exceeds 8 MiB".into());14 }15 Ok(bytes)16}1718pub(super) fn download(url: &str) -> Result<Vec<u8>, String> {19 let agent: ureq::Agent = ureq::Agent::config_builder()20 .https_only(true)21 .max_redirects(0)22 .timeout_global(Some(Duration::from_secs(10)))23 .build()24 .into();25 let mut response = agent.get(url).call().map_err(|e| e.to_string())?;26 if response.status() != 200 {27 return Err(format!(28 "Category document HTTP status: {}",29 response.status()30 ));31 }32 read_bounded(response.body_mut().as_reader())33}3435#[cfg(test)]36mod tests {37 use super::*;38 #[test]39 fn body_limit_and_io_errors_are_explicit() {40 assert_eq!(read_bounded(&b"abc"[..]).unwrap(), b"abc");41 assert!(read_bounded(std::io::repeat(0).take(MAX_BYTES as u64 + 1)).is_err());42 struct Broken; warning: clippy::items_after_statements43 impl Read for Broken { warning: clippy::items_after_statements44 fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> { warning: clippy::items_after_statements45 Err(std::io::Error::other("body failed")) warning: clippy::items_after_statements46 } warning: clippy::items_after_statements47 } warning: clippy::items_after_statements48 assert!(read_bounded(Broken).unwrap_err().contains("body failed"));49 }50}51Findings in this file§
warning clippy::items_after_statements §
warning clippy::items_after_statements §
Targets (1)
cargo-clippy-html (bin)
Primary: src/lint/fetch.rs:42:9
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#items_after_statements
note: `-W clippy::items-after-statements` implied by `-W clippy::pedantic`
help: to override `-W clippy::pedantic` add `#[allow(clippy::items_after_statements)]`
Rendered diagnostic (text)
warning: adding items after statements is confusing, since items exist from the start of the scope --> src/lint/fetch.rs:42:9 | 42 | struct Broken; | ^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#items_after_statements = note: `-W clippy::items-after-statements` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::items_after_statements)]`
warning clippy::items_after_statements §
warning clippy::items_after_statements §
Targets (1)
cargo-clippy-html (bin)
Primary: src/lint/fetch.rs:43:9
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#items_after_statements
Rendered diagnostic (text)
warning: adding items after statements is confusing, since items exist from the start of the scope
--> src/lint/fetch.rs:43:9
|
43 | / impl Read for Broken {
44 | | fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
45 | | Err(std::io::Error::other("body failed"))
46 | | }
47 | | }
| |_________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#items_after_statements