Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download: inline errors, rename TLS backend #3888

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ curl-backend = ["download/curl-backend"]
default = [
"curl-backend",
"reqwest-backend",
"reqwest-default-tls",
"reqwest-native-tls",
"reqwest-rustls-tls",
]

reqwest-backend = ["download/reqwest-backend"]
vendored-openssl = ['openssl/vendored']

reqwest-default-tls = ["download/reqwest-default-tls"]
reqwest-native-tls = ["download/reqwest-native-tls"]
reqwest-rustls-tls = ["download/reqwest-rustls-tls"]

# Include in the default set to disable self-update and uninstall.
Expand Down
4 changes: 2 additions & 2 deletions ci/run.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rustc -vV
cargo -vV


FEATURES=('--no-default-features' '--features' 'curl-backend,reqwest-backend,reqwest-default-tls')
FEATURES=('--no-default-features' '--features' 'curl-backend,reqwest-backend,reqwest-native-tls')
case "$(uname -s)" in
*NT* ) ;; # Windows NT
* ) FEATURES+=('--features' 'vendored-openssl') ;;
Expand Down Expand Up @@ -38,7 +38,7 @@ target_cargo() {
target_cargo build

download_pkg_test() {
features=('--no-default-features' '--features' 'curl-backend,reqwest-backend,reqwest-default-tls')
features=('--no-default-features' '--features' 'curl-backend,reqwest-backend,reqwest-native-tls')
case "$TARGET" in
# these platforms aren't supported by ring:
powerpc* ) ;;
Expand Down
6 changes: 2 additions & 4 deletions download/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[features]

default = ["reqwest-backend", "reqwest-rustls-tls", "reqwest-default-tls"]

default = ["reqwest-backend", "reqwest-rustls-tls", "reqwest-native-tls"]
curl-backend = ["curl"]
reqwest-backend = ["reqwest", "env_proxy"]
reqwest-default-tls = ["reqwest/default-tls", "dep:once_cell"]
reqwest-native-tls = ["reqwest/native-tls", "dep:once_cell"]
reqwest-rustls-tls = ["reqwest/rustls-tls-native-roots", "dep:once_cell"]

[dependencies]
Expand Down
21 changes: 0 additions & 21 deletions download/src/errors.rs

This file was deleted.

64 changes: 37 additions & 27 deletions download/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
//! Easy file downloading
#![deny(rust_2018_idioms)]

use std::fs::remove_file;
use std::path::Path;

use anyhow::Context;
pub use anyhow::Result;
use std::fs::remove_file;
use thiserror::Error;
use url::Url;

mod errors;
pub use crate::errors::*;

/// User agent header value for HTTP request.
/// See: https://github.com/rust-lang/rustup/issues/2860.
#[cfg(feature = "curl-backend")]
const CURL_USER_AGENT: &str = concat!("rustup/", env!("CARGO_PKG_VERSION"), " (curl)");

#[cfg(feature = "reqwest-default-tls")]
#[cfg(feature = "reqwest-native-tls")]
const REQWEST_DEFAULT_TLS_USER_AGENT: &str = concat!(
"rustup/",
env!("CARGO_PKG_VERSION"),
Expand All @@ -36,7 +34,7 @@ pub enum Backend {
#[derive(Debug, Copy, Clone)]
pub enum TlsBackend {
Rustls,
Default,
NativeTls,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -185,8 +183,7 @@ pub mod curl {
use curl::easy::Easy;
use url::Url;

use super::Event;
use crate::errors::*;
use super::{DownloadError, Event};

pub fn download(
url: &Url,
Expand Down Expand Up @@ -292,23 +289,21 @@ pub mod curl {
pub mod reqwest_be {
#[cfg(all(
not(feature = "reqwest-rustls-tls"),
not(feature = "reqwest-default-tls")
not(feature = "reqwest-native-tls")
))]
compile_error!("Must select a reqwest TLS backend");

use std::io;
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-default-tls"))]
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
use once_cell::sync::Lazy;
use reqwest::{header, Client, ClientBuilder, Proxy, Response};
use tokio_stream::StreamExt;
use url::Url;

use super::Event;
use super::TlsBackend;
use crate::errors::*;
use super::{DownloadError, Event, TlsBackend};

pub async fn download(
url: &Url,
Expand Down Expand Up @@ -372,7 +367,7 @@ pub mod reqwest_be {
catcher().unwrap()
});

#[cfg(feature = "reqwest-default-tls")]
#[cfg(feature = "reqwest-native-tls")]
static CLIENT_DEFAULT_TLS: Lazy<Client> = Lazy::new(|| {
let catcher = || {
client_generic()
Expand Down Expand Up @@ -405,10 +400,10 @@ pub mod reqwest_be {
TlsBackend::Rustls => {
return Err(DownloadError::BackendUnavailable("reqwest rustls"));
}
#[cfg(feature = "reqwest-default-tls")]
TlsBackend::Default => &CLIENT_DEFAULT_TLS,
#[cfg(not(feature = "reqwest-default-tls"))]
TlsBackend::Default => {
#[cfg(feature = "reqwest-native-tls")]
TlsBackend::NativeTls => &CLIENT_DEFAULT_TLS,
#[cfg(not(feature = "reqwest-native-tls"))]
TlsBackend::NativeTls => {
return Err(DownloadError::BackendUnavailable("reqwest default TLS"));
}
};
Expand Down Expand Up @@ -460,15 +455,33 @@ pub mod reqwest_be {
}
}

#[derive(Debug, Error)]
pub enum DownloadError {
#[error("http request returned an unsuccessful status code: {0}")]
HttpStatus(u32),
#[error("file not found")]
FileNotFound,
#[error("download backend '{0}' unavailable")]
BackendUnavailable(&'static str),
#[error("{0}")]
Message(String),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[cfg(feature = "reqwest-backend")]
#[error(transparent)]
Reqwest(#[from] ::reqwest::Error),
#[cfg(feature = "curl-backend")]
#[error(transparent)]
CurlError(#[from] ::curl::Error),
}

#[cfg(not(feature = "curl-backend"))]
pub mod curl {

use anyhow::{anyhow, Result};

use super::Event;
use crate::errors::*;
use url::Url;

use super::{DownloadError, Event};

pub fn download(
_url: &Url,
_resume_from: u64,
Expand All @@ -480,14 +493,11 @@ pub mod curl {

#[cfg(not(feature = "reqwest-backend"))]
pub mod reqwest_be {

use anyhow::{anyhow, Result};

use super::Event;
use super::TlsBackend;
use crate::errors::*;
use url::Url;

use super::{DownloadError, Event, TlsBackend};

pub fn download(
_url: &Url,
_resume_from: u64,
Expand Down
4 changes: 2 additions & 2 deletions download/tests/download-reqwest-resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn resume_partial_from_file_url() {

let from_url = Url::from_file_path(&from_path).unwrap();
download_to_path_with_backend(
Backend::Reqwest(TlsBackend::Default),
Backend::Reqwest(TlsBackend::NativeTls),
&from_url,
&target_path,
true,
Expand All @@ -48,7 +48,7 @@ async fn callback_gets_all_data_as_if_the_download_happened_all_at_once() {
let received_in_callback = Mutex::new(Vec::new());

download_to_path_with_backend(
Backend::Reqwest(TlsBackend::Default),
Backend::Reqwest(TlsBackend::NativeTls),
&from_url,
&target_path,
true,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ async fn download_file_(
let tls_backend = if use_rustls {
TlsBackend::Rustls
} else {
#[cfg(feature = "reqwest-default-tls")]
#[cfg(feature = "reqwest-native-tls")]
{
TlsBackend::Default
TlsBackend::NativeTls
}
#[cfg(not(feature = "reqwest-default-tls"))]
#[cfg(not(feature = "reqwest-native-tls"))]
{
TlsBackend::Rustls
}
Expand Down
Loading