2021-10-23 04:48:56 +00:00
|
|
|
use crate::{
|
|
|
|
error::{Error, UploadError},
|
|
|
|
store::Store,
|
|
|
|
};
|
2021-01-18 23:11:32 +00:00
|
|
|
use actix_web::{
|
|
|
|
dev::Payload,
|
2021-12-12 20:48:37 +00:00
|
|
|
http::header::{ContentRange, ContentRangeSpec, HeaderValue},
|
2021-01-18 23:11:32 +00:00
|
|
|
web::Bytes,
|
|
|
|
FromRequest, HttpRequest,
|
|
|
|
};
|
2021-10-14 00:06:53 +00:00
|
|
|
use futures_util::stream::{once, Stream};
|
|
|
|
use std::future::ready;
|
2021-01-18 23:11:32 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum Range {
|
2021-09-12 15:42:44 +00:00
|
|
|
Start(u64),
|
2021-01-18 23:11:32 +00:00
|
|
|
SuffixLength(u64),
|
|
|
|
Segment(u64, u64),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct RangeHeader {
|
|
|
|
unit: String,
|
|
|
|
ranges: Vec<Range>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Range {
|
2021-11-03 17:04:59 +00:00
|
|
|
pub(crate) fn to_content_range(&self, instance_length: u64) -> Option<ContentRange> {
|
2021-01-18 23:11:32 +00:00
|
|
|
match self {
|
2021-11-03 17:04:59 +00:00
|
|
|
Range::Start(start) => {
|
|
|
|
if *start >= instance_length {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
|
|
|
range: Some((*start, instance_length - *start)),
|
|
|
|
instance_length: Some(instance_length),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
Range::SuffixLength(from_start) => {
|
|
|
|
if *from_start > instance_length {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
|
|
|
range: Some((0, *from_start)),
|
|
|
|
instance_length: Some(instance_length),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
Range::Segment(start, end) => {
|
|
|
|
if *start >= instance_length || *end > instance_length {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
|
|
|
range: Some((*start, *end)),
|
|
|
|
instance_length: Some(instance_length),
|
|
|
|
}))
|
|
|
|
}
|
2021-01-18 23:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:02:33 +00:00
|
|
|
pub(crate) fn chop_bytes(&self, bytes: Bytes) -> impl Stream<Item = Result<Bytes, Error>> {
|
2021-09-09 21:35:07 +00:00
|
|
|
match self {
|
2021-09-12 15:42:44 +00:00
|
|
|
Range::Start(start) => once(ready(Ok(bytes.slice(*start as usize..)))),
|
2021-09-11 21:35:38 +00:00
|
|
|
Range::SuffixLength(from_start) => once(ready(Ok(bytes.slice(..*from_start as usize)))),
|
2021-09-09 21:35:07 +00:00
|
|
|
Range::Segment(start, end) => {
|
|
|
|
once(ready(Ok(bytes.slice(*start as usize..*end as usize))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
pub(crate) async fn chop_store<S: Store>(
|
2021-01-18 23:11:32 +00:00
|
|
|
&self,
|
2021-11-01 02:11:35 +00:00
|
|
|
store: &S,
|
2021-10-23 04:48:56 +00:00
|
|
|
identifier: S::Identifier,
|
|
|
|
) -> Result<impl Stream<Item = std::io::Result<Bytes>>, Error>
|
|
|
|
where
|
|
|
|
Error: From<S::Error>,
|
|
|
|
{
|
2021-01-18 23:11:32 +00:00
|
|
|
match self {
|
2021-10-23 04:48:56 +00:00
|
|
|
Range::Start(start) => Ok(store.to_stream(&identifier, Some(*start), None).await?),
|
|
|
|
Range::SuffixLength(from_start) => Ok(store
|
|
|
|
.to_stream(&identifier, None, Some(*from_start))
|
|
|
|
.await?),
|
|
|
|
Range::Segment(start, end) => Ok(store
|
|
|
|
.to_stream(&identifier, Some(*start), Some(end.saturating_sub(*start)))
|
|
|
|
.await?),
|
2021-01-18 23:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RangeHeader {
|
2021-11-03 17:04:59 +00:00
|
|
|
pub(crate) fn single_bytes_range(&self) -> Option<&'_ Range> {
|
|
|
|
if self.ranges.len() == 1 && self.unit == "bytes" {
|
2021-11-23 22:31:15 +00:00
|
|
|
self.ranges.get(0)
|
2021-11-03 17:04:59 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-01-18 23:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromRequest for RangeHeader {
|
2021-09-14 01:22:42 +00:00
|
|
|
type Error = Error;
|
2021-01-18 23:11:32 +00:00
|
|
|
type Future = std::future::Ready<Result<Self, Self::Error>>;
|
|
|
|
|
|
|
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
|
|
|
if let Some(range_head) = req.headers().get("Range") {
|
|
|
|
ready(parse_range_header(range_head).map_err(|e| {
|
|
|
|
tracing::warn!("Failed to parse range header: {}", e);
|
2021-09-14 01:22:42 +00:00
|
|
|
e
|
2021-01-18 23:11:32 +00:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
ready(Err(UploadError::ParseReq(
|
|
|
|
"Range header missing".to_string(),
|
|
|
|
)
|
|
|
|
.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 01:22:42 +00:00
|
|
|
fn parse_range_header(range_head: &HeaderValue) -> Result<RangeHeader, Error> {
|
2021-01-18 23:11:32 +00:00
|
|
|
let range_head_str = range_head.to_str().map_err(|_| {
|
|
|
|
UploadError::ParseReq("Range header contains non-utf8 characters".to_string())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let eq_pos = range_head_str
|
|
|
|
.find('=')
|
|
|
|
.ok_or_else(|| UploadError::ParseReq("Malformed Range Header".to_string()))?;
|
|
|
|
|
|
|
|
let (unit, ranges) = range_head_str.split_at(eq_pos);
|
|
|
|
let ranges = ranges.trim_start_matches('=');
|
|
|
|
|
|
|
|
let ranges = ranges
|
|
|
|
.split(',')
|
|
|
|
.map(parse_range)
|
2021-09-14 01:22:42 +00:00
|
|
|
.collect::<Result<Vec<Range>, Error>>()?;
|
2021-01-18 23:11:32 +00:00
|
|
|
|
|
|
|
Ok(RangeHeader {
|
|
|
|
unit: unit.to_owned(),
|
|
|
|
ranges,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-14 01:22:42 +00:00
|
|
|
fn parse_range(s: &str) -> Result<Range, Error> {
|
2021-01-18 23:11:32 +00:00
|
|
|
let dash_pos = s
|
|
|
|
.find('-')
|
|
|
|
.ok_or_else(|| UploadError::ParseReq("Mailformed Range Bound".to_string()))?;
|
|
|
|
|
|
|
|
let (start, end) = s.split_at(dash_pos);
|
|
|
|
let start = start.trim();
|
|
|
|
let end = end.trim_start_matches('-').trim();
|
|
|
|
|
|
|
|
if start.is_empty() && end.is_empty() {
|
2021-09-14 01:22:42 +00:00
|
|
|
Err(UploadError::ParseReq("Malformed content range".to_string()).into())
|
2021-01-18 23:11:32 +00:00
|
|
|
} else if start.is_empty() {
|
|
|
|
let suffix_length = end.parse().map_err(|_| {
|
|
|
|
UploadError::ParseReq("Cannot parse suffix length for range header".to_string())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Range::SuffixLength(suffix_length))
|
|
|
|
} else if end.is_empty() {
|
|
|
|
let range_start = start.parse().map_err(|_| {
|
|
|
|
UploadError::ParseReq("Cannot parse range start for range header".to_string())
|
|
|
|
})?;
|
|
|
|
|
2021-09-12 15:42:44 +00:00
|
|
|
Ok(Range::Start(range_start))
|
2021-01-18 23:11:32 +00:00
|
|
|
} else {
|
|
|
|
let range_start = start.parse().map_err(|_| {
|
|
|
|
UploadError::ParseReq("Cannot parse range start for range header".to_string())
|
|
|
|
})?;
|
|
|
|
let range_end = end.parse().map_err(|_| {
|
|
|
|
UploadError::ParseReq("Cannot parse range end for range header".to_string())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if range_start > range_end {
|
2021-09-14 01:22:42 +00:00
|
|
|
return Err(UploadError::Range.into());
|
2021-01-18 23:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Range::Segment(range_start, range_end))
|
|
|
|
}
|
|
|
|
}
|