|
|
|
@ -2,9 +2,8 @@ use std::{path::Path, time::Duration}; |
|
|
|
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use futures_util::StreamExt as _;
|
|
|
|
use http::{header::CONTENT_TYPE, uri::{Authority, Scheme}, Request, Response, Uri};
|
|
|
|
use http::{header::{CONTENT_TYPE, RANGE}, uri::{Authority, Scheme}, Request, Response, Uri, request::Builder as RequestBuilder};
|
|
|
|
use http_body_util::BodyDataStream;
|
|
|
|
use log::{log, Level};
|
|
|
|
use m3u8_rs::{MediaPlaylist, MediaSegment, Playlist};
|
|
|
|
use reqwest::{redirect::Policy, Body};
|
|
|
|
use tokio::{fs::File, io::AsyncWriteExt as _, time::timeout};
|
|
|
|
@ -14,6 +13,8 @@ use tower_reqwest::HttpClientLayer; |
|
|
|
|
|
|
|
use crate::download_error::DownloadError;
|
|
|
|
|
|
|
|
use log::{log, Level};
|
|
|
|
|
|
|
|
|
|
|
|
type HttpClient = tower::util::BoxCloneService<Request<Body>, Response<Body>, anyhow::Error>;
|
|
|
|
|
|
|
|
@ -73,7 +74,7 @@ impl State { |
|
|
|
. path_and_query(path_and_query)
|
|
|
|
. build()?;
|
|
|
|
|
|
|
|
let mut response = self.request(&uri).await?;
|
|
|
|
let mut response = self.request(&uri, 0).await?;
|
|
|
|
|
|
|
|
// read body into Vec<u8>
|
|
|
|
let body: Vec<u8> = BodyReader::new(response.body_mut())
|
|
|
|
@ -95,7 +96,7 @@ impl State { |
|
|
|
pub(super) async fn get_m3u8_segment(&mut self, uri: &Uri)
|
|
|
|
-> Result<(), DownloadError>
|
|
|
|
{
|
|
|
|
let mut response = self.request(uri).await
|
|
|
|
let mut response = self.request(uri, 0).await
|
|
|
|
. map_err(|e| DownloadError::new(uri.clone(), Some(e)))?;
|
|
|
|
|
|
|
|
// We always need the content-type to be able to decide
|
|
|
|
@ -178,15 +179,26 @@ impl State { |
|
|
|
Ok(filename.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn request(&mut self, uri: &Uri) -> anyhow::Result<Response<Body>>
|
|
|
|
async fn request(&mut self, uri: &Uri, from: usize) -> anyhow::Result<Response<Body>>
|
|
|
|
{
|
|
|
|
let request = RequestBuilder::new()
|
|
|
|
. uri(uri)
|
|
|
|
. header(RANGE, format!("{}-", from))
|
|
|
|
. body(Body::default())?;
|
|
|
|
|
|
|
|
log!(Level::Debug, "{:?}", request);
|
|
|
|
|
|
|
|
// Send get request with timeout.
|
|
|
|
let send_fut = self.client.get(uri).send()?;
|
|
|
|
// let send_fut = self.client.get(uri).send()?;
|
|
|
|
let send_fut = self.client.execute(request);
|
|
|
|
let response = timeout(self.timeout, send_fut).await??;
|
|
|
|
|
|
|
|
anyhow::ensure!( response.status().is_success()
|
|
|
|
, "resonse status failed: {}"
|
|
|
|
, response.status() );
|
|
|
|
|
|
|
|
log!(Level::Debug, "{:?}", response);
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
|