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 Oct 31, 2023
1 parent 13537d5 commit 27b6fc9
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::error::Error;
use std::io::Write;
use std::io;

use reqwest::StatusCode;

use sha2::{Sha256, Digest};

pub struct DownloadResult<W: std::io::Write> {
Expand All @@ -14,11 +16,23 @@ where
U: reqwest::IntoUrl,
W: io::Write,
{
// All HTTP status codes that must result in an immediate failure.
let failure_status = [
StatusCode::FORBIDDEN, // 403
StatusCode::NOT_FOUND, // 404
];

#[rustfmt::skip]
let mut res = client.get(url)
.send()
.await?;

// Return immediately on download failure on the client side.
let status = res.status();
if status.is_client_error() && failure_status.contains(&status) {
return Err(format!("cannnot fetch remotely with status code {:?}", status).into());
}

let mut hasher = Sha256::new();

let mut bytes_read = 0usize;
Expand Down

0 comments on commit 27b6fc9

Please sign in to comment.