use crate::error::{Error, Result};
use crate::url::Url;
pub fn transfer(url_str: &str) -> Result<Vec<u8>> {
let mut url = Url::parse(url_str)?;
url.set_idn(true)?;
transfer_url(&url)
}
pub fn transfer_url(url: &Url) -> Result<Vec<u8>> {
transfer_url_with(url, &crate::net::NetConfig::default())
}
pub(crate) fn transfer_url_with(url: &Url, cfg: &crate::net::NetConfig) -> Result<Vec<u8>> {
match url.scheme.as_str() {
"http" | "https" => crate::Request::get(&format!(
"{}://{}{}{}",
url.scheme,
url.host,
if (url.scheme == "http" && url.port == 80)
|| (url.scheme == "https" && url.port == 443)
{
String::new()
} else {
format!(":{}", url.port)
},
url.path
))?
.connector(cfg.connector.clone())
.verify_tls(cfg.verify)
.send()
.map(|r| r.body),
"ftp" | "ftps" => crate::ftp::fetch_with(url, cfg),
"dict" => crate::dict::fetch_with(url, cfg),
"file" => crate::file::fetch(url),
"gopher" | "gophers" => crate::gopher::fetch_with(url, cfg),
"imap" | "imaps" => crate::imap::fetch_with(url, cfg),
"ldap" | "ldaps" => crate::ldap::fetch_with(url, cfg),
"mqtt" | "mqtts" => crate::mqtt::fetch_with(url, cfg),
"pop3" | "pop3s" => crate::pop3::fetch_with(url, cfg),
"rtsp" => crate::rtsp::fetch_with(url, cfg),
#[cfg(feature = "ssh")]
"sftp" | "scp" => {
let user = crate::ssh::resolve_user(url, None)?;
let (_, password) = crate::ssh::userinfo_password(url);
let opts = crate::ssh::SshOptions {
password,
..Default::default()
};
crate::ssh::fetch(url, &opts, &user)
}
"tftp" => crate::tftp::fetch_with(url, cfg),
"ws" | "wss" => crate::websocket::fetch_with(url, cfg),
other => Err(Error::UnsupportedScheme(other.to_string())),
}
}
pub(crate) fn transfer_url_to_with(
url: &Url,
cfg: &crate::net::NetConfig,
sink: &mut dyn std::io::Write,
) -> Result<u64> {
match url.scheme.as_str() {
"ftp" | "ftps" => crate::ftp::fetch_to_with(url, cfg, sink),
"file" => crate::file::fetch_to(url, sink),
_ => {
let bytes = transfer_url_with(url, cfg)?;
sink.write_all(&bytes)?;
Ok(bytes.len() as u64)
}
}
}