8 changed files with 352 additions and 280 deletions
-
2Cargo.toml
-
319src/client.rs
-
31src/client/data.rs
-
127src/client/download.rs
-
10src/client/error.rs
-
69src/client/message.rs
-
56src/client/util.rs
-
18src/main.rs
@ -0,0 +1,31 @@ |
|||||
|
use http::HeaderMap;
|
||||
|
use tower_http_client::client::BodyReader;
|
||||
|
|
||||
|
use crate::map_dlerror;
|
||||
|
|
||||
|
use super::{util, ClientActorMessageHandle, HttpClient, JoinSetResult};
|
||||
|
|
||||
|
|
||||
|
pub(super) async fn data( mut client: HttpClient
|
||||
|
, mut message: ClientActorMessageHandle ) -> JoinSetResult {
|
||||
|
let uri = message.uri();
|
||||
|
|
||||
|
let mut response = util::request( &mut client
|
||||
|
, "GET"
|
||||
|
, &uri
|
||||
|
, HeaderMap::new() )
|
||||
|
. await
|
||||
|
. map_err(map_dlerror!(message))?;
|
||||
|
|
||||
|
// read body into Vec<u8>
|
||||
|
let body: Vec<u8> = BodyReader::new(response.body_mut())
|
||||
|
. bytes()
|
||||
|
. await
|
||||
|
. map_err(map_dlerror!(message))?
|
||||
|
. to_vec();
|
||||
|
|
||||
|
let buffer = message.buffer_mut();
|
||||
|
*buffer = Some(body);
|
||||
|
|
||||
|
Ok(Some(message))
|
||||
|
}
|
||||
@ -0,0 +1,127 @@ |
|||||
|
use std::{io::ErrorKind, path::Path, time::Duration};
|
||||
|
|
||||
|
use anyhow::anyhow;
|
||||
|
use futures_util::StreamExt as _;
|
||||
|
use http::{header::RANGE, HeaderMap, StatusCode};
|
||||
|
use http_body_util::BodyDataStream;
|
||||
|
use reqwest::Body;
|
||||
|
use tokio::{
|
||||
|
fs::{symlink_metadata, File},
|
||||
|
io::AsyncWriteExt as _,
|
||||
|
time::timeout
|
||||
|
};
|
||||
|
use tower_http::decompression::DecompressionBody;
|
||||
|
|
||||
|
use crate::map_dlerror;
|
||||
|
|
||||
|
use super::{
|
||||
|
util,
|
||||
|
ClientActorMessageHandle,
|
||||
|
DownloadState,
|
||||
|
HttpClient,
|
||||
|
JoinSetResult,
|
||||
|
};
|
||||
|
|
||||
|
|
||||
|
pub(super) async fn download( mut client: HttpClient
|
||||
|
, mut message: ClientActorMessageHandle
|
||||
|
, io_timeout: Duration ) -> JoinSetResult {
|
||||
|
let filename = message.filename();
|
||||
|
let uri = message.uri();
|
||||
|
|
||||
|
// - get all informations to eventually existing file
|
||||
|
let mut from = file_size(&filename).await;
|
||||
|
|
||||
|
// - get infos to uri
|
||||
|
let headers = util::head(&mut client, &uri).await
|
||||
|
. map_err(map_dlerror!(message))?;
|
||||
|
let content_length = util::content_length(&headers).ok();
|
||||
|
let content_type = util::content_type(&headers).ok();
|
||||
|
|
||||
|
message.set_state(DownloadState::GotHead);
|
||||
|
|
||||
|
if let Some(content_length) = content_length {
|
||||
|
if from != 0 && content_length - 1 <= from {
|
||||
|
return Ok(None);
|
||||
|
}
|
||||
|
} else {
|
||||
|
from = 0;
|
||||
|
}
|
||||
|
|
||||
|
// - do the neccessry request.
|
||||
|
let mut headers = HeaderMap::new();
|
||||
|
headers.insert(RANGE, format!("bytes={}-", from).parse().unwrap());
|
||||
|
|
||||
|
let mut response = util::request(&mut client, "GET", &uri, headers).await
|
||||
|
. map_err(map_dlerror!(message))?;
|
||||
|
|
||||
|
// - open or create file
|
||||
|
// - download Data
|
||||
|
store_body( &mut open_outfile(&response.status(), &filename).await
|
||||
|
, response.body_mut()
|
||||
|
, io_timeout )
|
||||
|
. await
|
||||
|
. map_err(map_dlerror!(message))?;
|
||||
|
|
||||
|
message.set_state(DownloadState::Done { content_type });
|
||||
|
|
||||
|
Ok(Some(message))
|
||||
|
}
|
||||
|
|
||||
|
async fn file_size(filename: &Path) -> u64 {
|
||||
|
// - get all informations to eventually existing file
|
||||
|
let metadata = match symlink_metadata(filename).await {
|
||||
|
Ok(metadata) => Some(metadata),
|
||||
|
Err(error) => match error.kind() {
|
||||
|
// If we can't write to a file we need to ... well theres nothing we can do
|
||||
|
ErrorKind::PermissionDenied => panic!("Permission denied on: {:?}", filename),
|
||||
|
_ => None,
|
||||
|
}
|
||||
|
};
|
||||
|
|
||||
|
metadata.map_or(0, |m| m.len())
|
||||
|
}
|
||||
|
|
||||
|
async fn open_outfile(status: &StatusCode, filename: &Path) -> File {
|
||||
|
match status {
|
||||
|
&StatusCode::PARTIAL_CONTENT =>
|
||||
|
// Here we assume that this response only comes if the requested
|
||||
|
// range can be fullfilled and thus is the data range in the
|
||||
|
// response. Thats why I do not check the content-range header.
|
||||
|
// If that assumption does not hold this needs to be fixed.
|
||||
|
File::options() . create(true)
|
||||
|
. append(true)
|
||||
|
. open(filename)
|
||||
|
. await
|
||||
|
. expect("can not create file for writing"),
|
||||
|
|
||||
|
_ =>
|
||||
|
File::create(filename) . await
|
||||
|
. expect("can not create file for writing"),
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
async fn store_body( file: &mut File
|
||||
|
, body: &mut DecompressionBody<Body>
|
||||
|
, io_timeout: Duration ) -> anyhow::Result<()> {
|
||||
|
let mut body = BodyDataStream::new(body);
|
||||
|
|
||||
|
loop {
|
||||
|
// give timeout somehow... probably from client.
|
||||
|
let data = timeout(io_timeout, body.next()).await?;
|
||||
|
match data {
|
||||
|
None => break,
|
||||
|
|
||||
|
Some(Err(e)) => {
|
||||
|
return Err(anyhow!(e));
|
||||
|
}
|
||||
|
|
||||
|
Some(Ok(data)) => {
|
||||
|
file . write_all(data.as_ref()).await?;
|
||||
|
file . flush().await?;
|
||||
|
},
|
||||
|
}
|
||||
|
};
|
||||
|
|
||||
|
Ok(())
|
||||
|
}
|
||||
@ -0,0 +1,69 @@ |
|||||
|
use std::path::PathBuf;
|
||||
|
|
||||
|
use http::Uri;
|
||||
|
use tokio::sync::oneshot;
|
||||
|
|
||||
|
use super::{ActionIndex, DownloadResult, DownloadState};
|
||||
|
|
||||
|
|
||||
|
#[derive(Debug)]
|
||||
|
pub(super) enum ClientActorMessage {
|
||||
|
Download {
|
||||
|
filename: PathBuf,
|
||||
|
uri: Uri,
|
||||
|
respond_to: oneshot::Sender<DownloadResult>,
|
||||
|
},
|
||||
|
GetData {
|
||||
|
uri: Uri,
|
||||
|
respond_to: oneshot::Sender<Option<Vec<u8>>>,
|
||||
|
},
|
||||
|
}
|
||||
|
|
||||
|
#[derive(Clone, Debug)]
|
||||
|
pub(super) enum ClientActorMessageHandle {
|
||||
|
Download {
|
||||
|
filename: PathBuf,
|
||||
|
uri: Uri,
|
||||
|
state: Option<DownloadState>,
|
||||
|
message: ActionIndex,
|
||||
|
},
|
||||
|
GetData {
|
||||
|
uri: Uri,
|
||||
|
buffer: Option<Vec<u8>>,
|
||||
|
message: ActionIndex,
|
||||
|
},
|
||||
|
}
|
||||
|
|
||||
|
|
||||
|
impl ClientActorMessageHandle {
|
||||
|
pub(super) fn set_state(&mut self, new_state: DownloadState) {
|
||||
|
if let Self::Download { ref mut state, .. } = self {
|
||||
|
*state = Some(new_state);
|
||||
|
} else {
|
||||
|
panic!("Called with invalid variant");
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
pub(super) fn filename(&self) -> PathBuf {
|
||||
|
if let Self::Download { ref filename, .. } = self {
|
||||
|
filename.clone()
|
||||
|
} else {
|
||||
|
panic!("called with invalid variant");
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
pub(super) fn uri(&self) -> Uri {
|
||||
|
match self {
|
||||
|
Self::Download { ref uri, .. } => uri.clone(),
|
||||
|
Self::GetData { ref uri, .. } => uri.clone(),
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
pub(super) fn buffer_mut(&mut self) -> &mut Option<Vec<u8>> {
|
||||
|
if let Self::GetData { ref mut buffer, .. } = self {
|
||||
|
buffer
|
||||
|
} else {
|
||||
|
panic!("called with invalid variant");
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
@ -0,0 +1,56 @@ |
|||||
|
use anyhow::anyhow;
|
||||
|
use http::{header::{CONTENT_LENGTH, CONTENT_TYPE}, request::Builder as RequestBuilder, HeaderMap, Response, Uri};
|
||||
|
use reqwest::Body;
|
||||
|
use tower_http::decompression::DecompressionBody;
|
||||
|
use tower_http_client::ServiceExt as _;
|
||||
|
|
||||
|
use super::HttpClient;
|
||||
|
|
||||
|
use log::debug;
|
||||
|
|
||||
|
|
||||
|
pub(super) async fn request( client: &mut HttpClient
|
||||
|
, method: &str |
||||
|
, uri: &Uri
|
||||
|
, headers: HeaderMap ) -> anyhow::Result<Response<DecompressionBody<Body>>> {
|
||||
|
let mut request = RequestBuilder::new()
|
||||
|
. method(method)
|
||||
|
. uri(uri)
|
||||
|
. body(Body::default())?;
|
||||
|
|
||||
|
request.headers_mut().extend(headers);
|
||||
|
|
||||
|
debug!("Request: {:?}", request);
|
||||
|
|
||||
|
let response = client.execute(request).await?;
|
||||
|
|
||||
|
debug!("Response: {:?}", response.headers());
|
||||
|
|
||||
|
anyhow::ensure!( response.status().is_success()
|
||||
|
, "resonse status failed: {}"
|
||||
|
, response.status() );
|
||||
|
|
||||
|
Ok(response)
|
||||
|
}
|
||||
|
|
||||
|
pub(super) async fn head( client: &mut HttpClient
|
||||
|
, uri: &Uri ) -> anyhow::Result<HeaderMap> {
|
||||
|
Ok( request( client
|
||||
|
, "HEAD"
|
||||
|
, uri
|
||||
|
, HeaderMap::new() ).await?.headers().clone() )
|
||||
|
}
|
||||
|
|
||||
|
pub(super) fn content_length(headers: &HeaderMap) -> anyhow::Result<u64> {
|
||||
|
Ok( headers . get(CONTENT_LENGTH)
|
||||
|
. ok_or(anyhow!("unable to get CONTENT-LENGTH value"))?
|
||||
|
. to_str()?
|
||||
|
. parse()? )
|
||||
|
}
|
||||
|
|
||||
|
pub(super) fn content_type(headers: &HeaderMap) -> anyhow::Result<String> {
|
||||
|
Ok( headers . get(CONTENT_TYPE)
|
||||
|
. ok_or(anyhow!("unable to get CONTENT-TYPE value"))?
|
||||
|
. to_str()?
|
||||
|
. to_string() )
|
||||
|
}
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue