2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-14 00:13:59 +00:00
pict-rs/src/range.rs

187 lines
5.5 KiB
Rust
Raw Normal View History

2021-09-14 01:22:42 +00:00
use crate::{
error::{Error, UploadError},
stream::bytes_stream,
};
use actix_web::{
dev::Payload,
http::{
header::{ContentRange, ContentRangeSpec},
HeaderValue,
},
web::Bytes,
FromRequest, HttpRequest,
};
use futures_util::stream::{once, LocalBoxStream, Stream};
2021-09-04 19:20:31 +00:00
use std::{future::ready, io};
use tokio::io::{AsyncReadExt, AsyncSeekExt};
#[derive(Debug)]
pub(crate) enum Range {
2021-09-12 15:42:44 +00:00
Start(u64),
SuffixLength(u64),
Segment(u64, u64),
}
#[derive(Debug)]
pub(crate) struct RangeHeader {
unit: String,
ranges: Vec<Range>,
}
impl Range {
pub(crate) fn to_content_range(&self, instance_length: u64) -> ContentRange {
match self {
2021-09-12 15:42:44 +00:00
Range::Start(start) => ContentRange(ContentRangeSpec::Bytes {
range: Some((*start, instance_length)),
instance_length: Some(instance_length),
}),
Range::SuffixLength(from_start) => ContentRange(ContentRangeSpec::Bytes {
range: Some((0, *from_start)),
instance_length: Some(instance_length),
}),
Range::Segment(start, end) => ContentRange(ContentRangeSpec::Bytes {
range: Some((*start, *end)),
instance_length: Some(instance_length),
}),
}
}
pub(crate) fn chop_bytes(
&self,
bytes: Bytes,
2021-09-14 01:22:42 +00:00
) -> impl Stream<Item = Result<Bytes, Error>> + Unpin {
match self {
2021-09-12 15:42:44 +00:00
Range::Start(start) => once(ready(Ok(bytes.slice(*start as usize..)))),
Range::SuffixLength(from_start) => once(ready(Ok(bytes.slice(..*from_start as usize)))),
Range::Segment(start, end) => {
once(ready(Ok(bytes.slice(*start as usize..*end as usize))))
}
}
}
pub(crate) async fn chop_file(
&self,
2021-09-04 19:20:31 +00:00
mut file: tokio::fs::File,
2021-09-14 01:22:42 +00:00
) -> Result<LocalBoxStream<'static, Result<Bytes, Error>>, Error> {
match self {
2021-09-12 15:42:44 +00:00
Range::Start(start) => {
2021-09-04 19:20:31 +00:00
file.seek(io::SeekFrom::Start(*start)).await?;
Ok(Box::pin(bytes_stream(file)))
}
Range::SuffixLength(from_start) => {
2021-09-04 19:20:31 +00:00
file.seek(io::SeekFrom::Start(0)).await?;
let reader = file.take(*from_start);
2021-09-04 19:20:31 +00:00
Ok(Box::pin(bytes_stream(reader)))
}
Range::Segment(start, end) => {
2021-09-04 19:20:31 +00:00
file.seek(io::SeekFrom::Start(*start)).await?;
let reader = file.take(end.saturating_sub(*start));
2021-09-04 19:20:31 +00:00
Ok(Box::pin(bytes_stream(reader)))
}
}
}
}
impl RangeHeader {
pub(crate) fn is_bytes(&self) -> bool {
self.unit == "bytes"
}
2021-09-12 15:42:44 +00:00
pub(crate) fn ranges(&self) -> impl Iterator<Item = &'_ Range> + '_ {
self.ranges.iter()
}
pub(crate) fn len(&self) -> usize {
self.ranges.len()
}
pub(crate) fn is_empty(&self) -> bool {
self.ranges.is_empty()
}
}
impl FromRequest for RangeHeader {
type Config = ();
2021-09-14 01:22:42 +00:00
type Error = Error;
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
}))
} 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> {
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>>()?;
Ok(RangeHeader {
unit: unit.to_owned(),
ranges,
})
}
2021-09-14 01:22:42 +00:00
fn parse_range(s: &str) -> Result<Range, Error> {
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())
} 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))
} 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());
}
Ok(Range::Segment(range_start, range_end))
}
}