Skip to content

Commit

Permalink
download: check for failure status codes
Browse files Browse the repository at this point in the history
Check for HTTP status codes for immediate failures in download_and_hash,
for now only 403 Forbidden and 404 Not found.
  • Loading branch information
dongsupark committed Nov 14, 2023
1 parent 13537d5 commit d608308
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::error::Error;
use std::io::Write;
use std::io;
use log::warn;

use reqwest::StatusCode;

use sha2::{Sha256, Digest};

Expand All @@ -19,6 +22,23 @@ where
.send()
.await?;

// Return immediately on download failure on the client side.
let status = res.status();

// TODO: handle redirect with retrying with a new URL or Attempt follow.
if status.is_redirection() {
warn!("redirect with status code {:?}", status);
}

if !status.is_success() {
match status {
StatusCode::FORBIDDEN | StatusCode::NOT_FOUND => {
return Err(format!("cannnot fetch remotely with status code {:?}", status).into());
}
_ => return Err(format!("general failure with status code {:?}", status).into()),
}
}

let mut hasher = Sha256::new();

let mut bytes_read = 0usize;
Expand Down

0 comments on commit d608308

Please sign in to comment.