1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
#![doc = include_str!(concat!(env!("OUT_DIR"), "/README-rustdocified.md"))]
#![deny(missing_docs)]
#![forbid(unsafe_code)]
use std::{
error::Error as StdError,
fmt, thread,
time::{Duration, Instant},
};
use bytes::Bytes;
use digest::DynDigest;
use reqwest::{
blocking::{Client, ClientBuilder},
Error as ReqwestError, IntoUrl, StatusCode,
};
// ======================================================================
// Error - PUBLIC
/// Represents all possible errors that can occur in this library.
#[derive(Debug)]
pub enum Error {
/// Got error from [reqwest](https://crates.io/crates/reqwest).
Reqwest(
/// The error.
ReqwestError,
),
/// HTTP response status is not `OK` (200).
StatusNotOk(
/// HTTP response status.
StatusCode,
),
/// Hash of downloaded file doesn't match.
HashMismatch {
/// Hash of downloaded file, lowercase hexadecimal.
got: String,
/// Hash given to [`RequestBuilder::hash`], lowercase hexadecimal.
expected: String,
},
/// Download failed.
DownloadFailed(
/// Errors, one error for each (re)try.
Vec<Error>,
),
}
// ======================================================================
// Error - IMPL DISPLAY
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Reqwest(inner) => inner.fmt(f),
Error::StatusNotOk(status) => status.fmt(f),
Error::HashMismatch { got, expected } => {
write!(f, "hash mismatch\nGot :{}\nExpected:{}", got, expected)
}
Error::DownloadFailed(errors) => {
write!(f, "download failed:")?;
for (index, error) in errors.iter().enumerate() {
write!(f, "\n[{}]: {}", index, error)?;
}
Ok(())
}
}
}
}
// ======================================================================
// Error - IMPL ERROR
impl StdError for Error {}
// ======================================================================
// Error - IMPL FROM
impl From<ReqwestError> for Error {
fn from(error: ReqwestError) -> Self {
Self::Reqwest(error)
}
}
// ======================================================================
// Downloader - PUBLIC
/// Simple blocking downloader.
///
/// See [crate index](crate#examples) for examples.
pub struct Downloader {
client: Client,
min_interval: Duration,
max_interval: Duration,
retry_delays: Vec<(Duration, Duration)>,
prev_download_start: Option<Instant>,
}
impl Downloader {
/// Creates [`DownloaderBuilder`] to configure [`Downloader`].
///
/// This is same as [`DownloaderBuilder::new`].
///
/// See [custom configuration] for an example.
///
/// [custom configuration]: crate#custom-configuration
pub fn builder() -> DownloaderBuilder {
DownloaderBuilder::new()
}
/// Begins building a request to download file from given `url`.
///
/// See [simple usage] and [`RequestBuilder::hash`] for examples.
///
/// [simple usage]: crate#simple-usage
pub fn get<U: IntoUrl>(&mut self, url: U) -> RequestBuilder {
RequestBuilder::new(self, self.client.get(url))
}
/// Creates new [`Downloader`] with default configuration.
pub fn new() -> Result<Self, Error> {
DownloaderBuilder::new().build()
}
/// Sleeps until ready for next download.
///
/// After this the next [`RequestBuilder::send`] will start
/// download immediately without sleep.
///
/// See [`DownloaderBuilder::interval`].
///
/// # Examples
///
/// ```no_run
/// use ml_downloader::Downloader;
///
/// let mut downloader = Downloader::builder()
/// .interval(1.0, 1.0)
/// .build()?;
///
/// println!("First download");
/// let bytes1 = downloader.get("https://example.com/first").send()?;
/// downloader.sleep_until_ready();
/// println!("Second download");
/// let bytes2 = downloader.get("https://example.com/second").send()?;
///
/// # Ok::<(), ml_downloader::Error>(())
/// ```
pub fn sleep_until_ready(&mut self) {
if let Some(prev_download_start) = self.prev_download_start {
let interval = random_duration(self.min_interval, self.max_interval);
let elapsed = Instant::now() - prev_download_start;
if elapsed < interval {
std::thread::sleep(interval - elapsed);
}
self.prev_download_start = None;
}
}
}
// ======================================================================
// DownloaderBuilder - PUBLIC
/// A builder to create [`Downloader`] with custom configuration.
///
/// See [custom configuration] for an example.
///
/// [custom configuration]: crate#custom-configuration
pub struct DownloaderBuilder {
client_builder: ClientBuilder,
min_interval: Duration,
max_interval: Duration,
retry_delays: Vec<(Duration, Duration)>,
}
impl Default for DownloaderBuilder {
fn default() -> Self {
Self::new()
}
}
impl DownloaderBuilder {
/// Creates [`Downloader`] using configuration of this [`DownloaderBuilder`].
///
/// See [custom configuration] for an example.
///
/// [custom configuration]: crate#custom-configuration
pub fn build(self) -> Result<Downloader, Error> {
Ok(Downloader {
client: self.client_builder.build()?,
min_interval: self.min_interval,
max_interval: self.max_interval,
retry_delays: self.retry_delays,
prev_download_start: None,
})
}
/// Sets interval between successful downloads in seconds, default is 0.
///
/// A random interval between given `min` and `max` is generated
/// for each download. If elapsed time since previous download started
/// is less than this interval then [`RequestBuilder::send`] will sleep
/// for the remaining duration before starting download.
///
/// # Panics
///
/// If `min > max`.
///
/// # Examples
///
/// Configure `1.0 - 1.1` seconds interval between successful downloads.
///
/// ```rust
/// use ml_downloader::Downloader;
///
/// let mut downloader = Downloader::builder()
/// .interval(1.0, 1.1)
/// .build()?;
///
/// # Ok::<(), ml_downloader::Error>(())
/// ```
pub fn interval(self, min: f32, max: f32) -> Self {
assert!(min <= max);
DownloaderBuilder {
min_interval: Duration::from_secs_f32(min),
max_interval: Duration::from_secs_f32(max),
..self
}
}
/// Creates [`DownloaderBuilder`] to configure [`Downloader`].
///
/// This is same as [`Downloader::builder`].
pub fn new() -> Self {
Self {
client_builder: Client::builder(),
min_interval: Duration::ZERO,
max_interval: Duration::ZERO,
retry_delays: Vec::new(),
}
}
/// Configures underlying [`ClientBuilder`].
///
/// # Examples
///
/// ```rust
/// use ml_downloader::Downloader;
///
/// let mut downloader = Downloader::builder()
/// .reqwest(|cb| cb.user_agent("foobar/1.0"))
/// .build()?;
///
/// # Ok::<(), ml_downloader::Error>(())
/// ```
///
/// [`ClientBuilder`]: reqwest::blocking::ClientBuilder
pub fn reqwest<F>(self, f: F) -> Self
where
F: FnOnce(ClientBuilder) -> ClientBuilder,
{
DownloaderBuilder {
client_builder: f(self.client_builder),
..self
}
}
/// Sets retry delays in seconds, default is none.
///
/// Each item is a pair of `min` and `max` delays
/// and the number of items defines the number of retries.
///
/// A random delay between given `min` and `max` is generated for each retry.
///
/// # Panics
///
/// If any item has `min > max`.
///
/// # Examples
///
/// Configure two retries after failed download with
/// `2.0 - 2.2` seconds delay after initial failure and
/// `5.0 - 5.5` seconds delay after 2nd failure.
///
/// ```rust
/// use ml_downloader::Downloader;
///
/// let mut downloader = Downloader::builder()
/// .retry_delays(&[(2.0, 2.2), (5.0, 5.5)])
/// .build()?;
///
/// # Ok::<(), ml_downloader::Error>(())
/// ```
pub fn retry_delays(self, retry_delays: &[(f32, f32)]) -> Self {
let mut vec = Vec::with_capacity(retry_delays.len());
for (min, max) in retry_delays {
assert!(min <= max);
vec.push((Duration::from_secs_f32(*min), Duration::from_secs_f32(*max)));
}
DownloaderBuilder {
retry_delays: vec,
..self
}
}
}
// ======================================================================
// RequestBuilder - PUBLIC
/// A builder to configure download request.
///
/// See [custom configuration] for an example.
///
/// [custom configuration]: crate#custom-configuration
pub struct RequestBuilder<'a> {
downloader: &'a mut Downloader,
inner: reqwest::blocking::RequestBuilder,
hash: Option<(String, Box<dyn DynDigest>)>,
}
impl<'a> RequestBuilder<'a> {
/// Sets expected file hash and digest used to calculate it.
///
/// Hash is given in hexadecimal, uppercase or lowercase.
///
/// # Examples
///
/// ```no_run
/// use ml_downloader::Downloader;
/// use sha2::{Digest, Sha256};
///
/// let mut downloader = Downloader::new()?;
/// let bytes = downloader
/// .get("https://example.com/")
/// .hash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Sha256::new())
/// .send()?;
///
/// # Ok::<(), ml_downloader::Error>(())
/// ```
pub fn hash<D: DynDigest + 'static>(self, expected: &str, digest: D) -> Self {
RequestBuilder {
hash: Some((expected.to_lowercase(), Box::new(digest))),
..self
}
}
/// Creates download request and sends it to target URL, with retries.
///
/// - Sleeps before starting download if needed.
/// - See [`DownloaderBuilder::interval`] and [`Downloader::sleep_until_ready`].
/// - Number of retries and the delays inbetween them is configured with
/// [`DownloaderBuilder::retry_delays`].
///
/// See [simple usage] and [`RequestBuilder::hash`] for examples.
///
/// [simple usage]: crate#simple-usage
pub fn send(mut self) -> Result<Bytes, Error> {
let mut errors = Vec::with_capacity(self.downloader.retry_delays.len());
self.downloader.sleep_until_ready();
let mut retry_count = 0;
loop {
self.downloader.prev_download_start = Some(Instant::now());
match self.send_once() {
Ok(bytes) => return Ok(bytes),
Err(error) => errors.push(error),
}
if retry_count == self.downloader.retry_delays.len() {
return Err(Error::DownloadFailed(errors));
}
let (min, max) = self.downloader.retry_delays[retry_count];
thread::sleep(random_duration(min, max));
retry_count += 1;
}
}
}
// ======================================================================
// RequestBuilder - PRIVATE
impl<'a> RequestBuilder<'a> {
fn new(downloader: &'a mut Downloader, inner: reqwest::blocking::RequestBuilder) -> Self {
Self {
downloader,
inner,
hash: None,
}
}
fn send_once(&mut self) -> Result<Bytes, Error> {
let response = self.inner.try_clone().unwrap().send()?;
let status = response.status();
if status != StatusCode::OK {
Err(Error::StatusNotOk(status))
} else {
let bytes = response.bytes()?;
if let Some((expected, digest)) = &mut self.hash {
digest.reset();
digest.update(&bytes);
let mut got = vec![0; digest.output_size()];
digest.finalize_into_reset(got.as_mut()).unwrap();
let got = hex::encode(got);
if &got != expected {
return Err(Error::HashMismatch {
got,
expected: expected.clone(),
});
}
}
Ok(bytes)
}
}
}
// ======================================================================
// FUNCTIONS - PRIVATE
fn random_duration(min: Duration, max: Duration) -> Duration {
Duration::from_micros(fastrand::u64(
min.as_micros() as u64..=max.as_micros() as u64,
))
}