use crate::error::{Error, Result};
use crate::url::Url;
pub fn transfer(url_str: &str) -> Result<Vec<u8>> {
let url = Url::parse(url_str)?;
transfer_url(&url)
}
pub fn transfer_url(url: &Url) -> 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
))?
.send()
.map(|r| r.body),
"ftp" | "ftps" => crate::ftp::fetch(url),
"dict" => crate::dict::fetch(url),
"file" => crate::file::fetch(url),
"gopher" | "gophers" => crate::gopher::fetch(url),
"imap" | "imaps" => crate::imap::fetch(url),
"ldap" | "ldaps" => crate::ldap::fetch(url),
"mqtt" | "mqtts" => crate::mqtt::fetch(url),
"pop3" | "pop3s" => crate::pop3::fetch(url),
"rtsp" => crate::rtsp::fetch(url),
"tftp" => crate::tftp::fetch(url),
"ws" | "wss" => crate::websocket::fetch(url),
other => Err(Error::UnsupportedScheme(other.to_string())),
}
}