2020-03-02 01:53:41 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
|
|
|
// Copyright (C) 2016 The Moonfire NVR Authors
|
2016-11-25 17:34:00 -05:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// In addition, as a special exception, the copyright holders give
|
|
|
|
// permission to link the code of portions of this program with the
|
|
|
|
// OpenSSL library under certain conditions as described in each
|
|
|
|
// individual source file, and distribute linked combinations including
|
|
|
|
// the two.
|
|
|
|
//
|
|
|
|
// You must obey the GNU General Public License in all respects for all
|
|
|
|
// of the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
// exception, you may extend this exception to your version of the
|
|
|
|
// file(s), but you are not obligated to do so. If you do not wish to do
|
|
|
|
// so, delete this exception statement from your version. If you delete
|
|
|
|
// this exception statement from all source files in the program, then
|
|
|
|
// also delete it here.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-01-23 14:08:21 -05:00
|
|
|
//! Tools for implementing a `http_serve::Entity` body composed from many "slices".
|
2016-12-17 02:11:08 -05:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
use crate::body::{wrap_error, BoxedError};
|
2018-12-28 22:53:29 -05:00
|
|
|
use base::format_err_t;
|
2021-02-17 01:15:54 -05:00
|
|
|
use failure::{bail, Error};
|
|
|
|
use futures::{stream, stream::StreamExt, Stream};
|
2016-11-25 17:34:00 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops::Range;
|
2020-01-09 02:04:36 -05:00
|
|
|
use std::pin::Pin;
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2018-12-28 11:04:47 -05:00
|
|
|
/// Gets a byte range given a context argument.
|
|
|
|
/// Each `Slice` instance belongs to a single `Slices`.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub trait Slice: fmt::Debug + Sized + Sync + 'static {
|
2020-01-09 02:04:36 -05:00
|
|
|
type Ctx: Send + Sync + Clone;
|
|
|
|
type Chunk: Send + Sync;
|
2017-02-25 21:54:52 -05:00
|
|
|
|
2018-12-28 11:04:47 -05:00
|
|
|
/// The byte position (relative to the start of the `Slices`) of the end of this slice,
|
|
|
|
/// exclusive. Note the starting position (and thus length) are inferred from the previous
|
|
|
|
/// slice. Must remain the same for the lifetime of the `Slice`.
|
2017-02-21 22:37:36 -05:00
|
|
|
fn end(&self) -> u64;
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2018-12-28 11:04:47 -05:00
|
|
|
/// Gets the body bytes indicated by `r`, which is relative to this slice's start.
|
2016-12-17 02:11:08 -05:00
|
|
|
/// The additional argument `ctx` is as supplied to the `Slices`.
|
|
|
|
/// The additional argument `l` is the length of this slice, as determined by the `Slices`.
|
2021-02-17 01:15:54 -05:00
|
|
|
fn get_range(
|
|
|
|
&self,
|
|
|
|
ctx: &Self::Ctx,
|
|
|
|
r: Range<u64>,
|
|
|
|
len: u64,
|
|
|
|
) -> Box<dyn Stream<Item = Result<Self::Chunk, BoxedError>> + Sync + Send>;
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2017-03-02 22:29:28 -05:00
|
|
|
fn get_slices(ctx: &Self::Ctx) -> &Slices<Self>;
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2016-12-17 02:11:08 -05:00
|
|
|
/// Helper to serve byte ranges from a body which is broken down into many "slices".
|
2018-12-28 11:04:47 -05:00
|
|
|
/// This is used to implement `.mp4` serving in `mp4::File` from `mp4::Slice` enums.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub struct Slices<S>
|
|
|
|
where
|
|
|
|
S: Slice,
|
|
|
|
{
|
2016-12-17 02:11:08 -05:00
|
|
|
/// The total byte length of the `Slices`.
|
2017-02-21 22:37:36 -05:00
|
|
|
/// Equivalent to `self.slices.back().map(|s| s.end()).unwrap_or(0)`; kept for convenience and
|
|
|
|
/// to avoid a branch.
|
2016-11-25 17:34:00 -05:00
|
|
|
len: u64,
|
2016-12-17 02:11:08 -05:00
|
|
|
|
|
|
|
/// 0 or more slices of this file.
|
2017-02-21 22:37:36 -05:00
|
|
|
slices: Vec<S>,
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
impl<S> fmt::Debug for Slices<S>
|
|
|
|
where
|
|
|
|
S: Slice,
|
|
|
|
{
|
2016-11-25 17:34:00 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-02-17 01:15:54 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{} slices with overall length {}:",
|
|
|
|
self.slices.len(),
|
|
|
|
self.len
|
|
|
|
)?;
|
2016-11-25 17:34:00 -05:00
|
|
|
let mut start = 0;
|
|
|
|
for (i, s) in self.slices.iter().enumerate() {
|
2017-02-21 22:37:36 -05:00
|
|
|
let end = s.end();
|
2021-02-17 01:15:54 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"\ni {:7}: range [{:12}, {:12}) len {:12}: {:?}",
|
|
|
|
i,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
end - start,
|
|
|
|
s
|
|
|
|
)?;
|
2017-02-21 22:37:36 -05:00
|
|
|
start = end;
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
impl<S> Slices<S>
|
|
|
|
where
|
|
|
|
S: Slice,
|
|
|
|
{
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Slices {
|
|
|
|
len: 0,
|
|
|
|
slices: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2016-12-17 02:11:08 -05:00
|
|
|
/// Reserves space for at least `additional` more slices to be appended.
|
2016-11-25 17:34:00 -05:00
|
|
|
pub fn reserve(&mut self, additional: usize) {
|
|
|
|
self.slices.reserve(additional)
|
|
|
|
}
|
|
|
|
|
2018-12-28 11:04:47 -05:00
|
|
|
/// Appends the given slice, which must have end > the Slices's current len.
|
2017-10-17 11:55:21 -04:00
|
|
|
pub fn append(&mut self, slice: S) -> Result<(), Error> {
|
|
|
|
if slice.end() <= self.len {
|
2021-02-17 01:15:54 -05:00
|
|
|
bail!(
|
|
|
|
"end {} <= len {} while adding slice {:?} to slices:\n{:?}",
|
|
|
|
slice.end(),
|
|
|
|
self.len,
|
|
|
|
slice,
|
|
|
|
self
|
|
|
|
);
|
2017-10-17 11:55:21 -04:00
|
|
|
}
|
2017-02-21 22:37:36 -05:00
|
|
|
self.len = slice.end();
|
|
|
|
self.slices.push(slice);
|
2017-10-17 11:55:21 -04:00
|
|
|
Ok(())
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the total byte length of all slices.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub fn len(&self) -> u64 {
|
|
|
|
self.len
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
|
|
|
|
/// Returns the number of slices.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub fn num(&self) -> usize {
|
|
|
|
self.slices.len()
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2016-12-17 02:11:08 -05:00
|
|
|
/// Writes `range` to `out`.
|
2018-01-23 14:08:21 -05:00
|
|
|
/// This interface mirrors `http_serve::Entity::write_to`, with the additional `ctx` argument.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub fn get_range(
|
|
|
|
&self,
|
|
|
|
ctx: &S::Ctx,
|
|
|
|
range: Range<u64>,
|
|
|
|
) -> Box<dyn Stream<Item = Result<S::Chunk, BoxedError>> + Sync + Send> {
|
2016-11-25 17:34:00 -05:00
|
|
|
if range.start > range.end || range.end > self.len {
|
2021-02-17 01:15:54 -05:00
|
|
|
return Box::new(stream::once(futures::future::err(wrap_error(
|
|
|
|
format_err_t!(
|
|
|
|
Internal,
|
|
|
|
"Bad range {:?} for slice of length {}",
|
|
|
|
range,
|
|
|
|
self.len
|
|
|
|
),
|
|
|
|
))));
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Binary search for the first slice of the range to write, determining its index and
|
|
|
|
// (from the preceding slice) the start of its range.
|
2017-03-02 22:29:28 -05:00
|
|
|
let (i, slice_start) = match self.slices.binary_search_by_key(&range.start, |s| s.end()) {
|
2021-02-17 01:15:54 -05:00
|
|
|
Ok(i) => (i + 1, self.slices[i].end()), // desired start == slice i's end; first is i+1!
|
2017-02-21 22:37:36 -05:00
|
|
|
Err(i) if i == 0 => (0, 0), // desired start < slice 0's end; first is 0.
|
2021-02-17 01:15:54 -05:00
|
|
|
Err(i) => (i, self.slices[i - 1].end()), // desired start < slice i's end; first is i.
|
2016-11-25 17:34:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate through and write each slice until the end.
|
2017-03-02 22:29:28 -05:00
|
|
|
|
|
|
|
let start_pos = range.start - slice_start;
|
|
|
|
let bodies = stream::unfold(
|
2021-02-17 01:15:54 -05:00
|
|
|
(ctx.clone(), i, start_pos, slice_start),
|
|
|
|
move |(c, i, start_pos, slice_start)| {
|
|
|
|
let (body, min_end);
|
|
|
|
{
|
|
|
|
let self_ = S::get_slices(&c);
|
|
|
|
if i == self_.slices.len() {
|
|
|
|
return futures::future::ready(None);
|
|
|
|
}
|
|
|
|
let s = &self_.slices[i];
|
|
|
|
if range.end == slice_start + start_pos {
|
|
|
|
return futures::future::ready(None);
|
|
|
|
}
|
|
|
|
let s_end = s.end();
|
|
|
|
min_end = ::std::cmp::min(range.end, s_end);
|
|
|
|
let l = s_end - slice_start;
|
|
|
|
body = s.get_range(&c, start_pos..min_end - slice_start, l);
|
|
|
|
};
|
|
|
|
futures::future::ready(Some((Pin::from(body), (c, i + 1, 0, min_end))))
|
|
|
|
},
|
|
|
|
);
|
2017-09-22 00:51:58 -04:00
|
|
|
Box::new(bodies.flatten())
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-02-17 01:15:54 -05:00
|
|
|
use super::{Slice, Slices};
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::body::BoxedError;
|
2018-12-28 22:53:29 -05:00
|
|
|
use db::testutil;
|
2020-01-09 02:04:36 -05:00
|
|
|
use futures::stream::{self, Stream, TryStreamExt};
|
2018-12-28 22:53:29 -05:00
|
|
|
use lazy_static::lazy_static;
|
2016-11-25 17:34:00 -05:00
|
|
|
use std::ops::Range;
|
2020-01-09 02:04:36 -05:00
|
|
|
use std::pin::Pin;
|
2016-11-25 17:34:00 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2017-03-02 22:29:28 -05:00
|
|
|
pub struct FakeChunk {
|
|
|
|
slice: &'static str,
|
2016-11-25 17:34:00 -05:00
|
|
|
range: Range<u64>,
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:14:47 -04:00
|
|
|
#[derive(Debug)]
|
2017-02-21 22:37:36 -05:00
|
|
|
pub struct FakeSlice {
|
|
|
|
end: u64,
|
2016-11-25 17:34:00 -05:00
|
|
|
name: &'static str,
|
|
|
|
}
|
|
|
|
|
2017-02-25 21:54:52 -05:00
|
|
|
impl Slice for FakeSlice {
|
2017-03-02 22:29:28 -05:00
|
|
|
type Ctx = &'static Slices<FakeSlice>;
|
|
|
|
type Chunk = FakeChunk;
|
2017-02-25 21:54:52 -05:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
fn end(&self) -> u64 {
|
|
|
|
self.end
|
|
|
|
}
|
2017-02-21 22:37:36 -05:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
fn get_range(
|
|
|
|
&self,
|
|
|
|
_ctx: &&'static Slices<FakeSlice>,
|
|
|
|
r: Range<u64>,
|
|
|
|
_l: u64,
|
|
|
|
) -> Box<dyn Stream<Item = Result<FakeChunk, BoxedError>> + Send + Sync> {
|
|
|
|
Box::new(stream::once(futures::future::ok(FakeChunk {
|
|
|
|
slice: self.name,
|
|
|
|
range: r,
|
|
|
|
})))
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
2017-03-02 22:29:28 -05:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
fn get_slices(ctx: &&'static Slices<FakeSlice>) -> &'static Slices<Self> {
|
|
|
|
*ctx
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2017-03-02 22:29:28 -05:00
|
|
|
lazy_static! {
|
2021-02-17 01:15:54 -05:00
|
|
|
#[rustfmt::skip]
|
2017-03-02 22:29:28 -05:00
|
|
|
static ref SLICES: Slices<FakeSlice> = {
|
|
|
|
let mut s = Slices::new();
|
2021-02-17 01:15:54 -05:00
|
|
|
s.append(FakeSlice { end: 5, name: "a" }).unwrap();
|
|
|
|
s.append(FakeSlice { end: 5 + 13, name: "b" }).unwrap();
|
|
|
|
s.append(FakeSlice { end: 5 + 13 + 7, name: "c" }).unwrap();
|
|
|
|
s.append(FakeSlice { end: 5 + 13 + 7 + 17, name: "d" }).unwrap();
|
|
|
|
s.append(FakeSlice { end: 5 + 13 + 7 + 17 + 19, name: "e" }).unwrap();
|
2017-03-02 22:29:28 -05:00
|
|
|
s
|
|
|
|
};
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:04:36 -05:00
|
|
|
async fn get_range(r: Range<u64>) -> Vec<FakeChunk> {
|
2021-02-17 01:15:54 -05:00
|
|
|
Pin::from(SLICES.get_range(&&*SLICES, r))
|
|
|
|
.try_collect()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-01-09 02:04:36 -05:00
|
|
|
}
|
|
|
|
|
2016-11-25 17:34:00 -05:00
|
|
|
#[test]
|
|
|
|
pub fn size() {
|
2017-03-02 22:29:28 -05:00
|
|
|
testutil::init();
|
|
|
|
assert_eq!(5 + 13 + 7 + 17 + 19, SLICES.len());
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:04:36 -05:00
|
|
|
#[tokio::test]
|
|
|
|
pub async fn exact_slice() {
|
2016-11-25 17:34:00 -05:00
|
|
|
// Test writing exactly slice b.
|
2017-03-02 22:29:28 -05:00
|
|
|
testutil::init();
|
2021-02-17 01:15:54 -05:00
|
|
|
let out = get_range(5..18).await;
|
|
|
|
assert_eq!(
|
|
|
|
&[FakeChunk {
|
|
|
|
slice: "b",
|
|
|
|
range: 0..13
|
|
|
|
}],
|
|
|
|
&out[..]
|
|
|
|
);
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:04:36 -05:00
|
|
|
#[tokio::test]
|
|
|
|
pub async fn offset_first() {
|
2016-11-25 17:34:00 -05:00
|
|
|
// Test writing part of slice a.
|
2017-03-02 22:29:28 -05:00
|
|
|
testutil::init();
|
2021-02-17 01:15:54 -05:00
|
|
|
let out = get_range(1..3).await;
|
|
|
|
assert_eq!(
|
|
|
|
&[FakeChunk {
|
|
|
|
slice: "a",
|
|
|
|
range: 1..3
|
|
|
|
}],
|
|
|
|
&out[..]
|
|
|
|
);
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:04:36 -05:00
|
|
|
#[tokio::test]
|
|
|
|
pub async fn offset_mid() {
|
2016-11-25 17:34:00 -05:00
|
|
|
// Test writing part of slice b, all of slice c, and part of slice d.
|
2017-03-02 22:29:28 -05:00
|
|
|
testutil::init();
|
2021-02-17 01:15:54 -05:00
|
|
|
let out = get_range(17..26).await;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
assert_eq!(
|
|
|
|
&[
|
|
|
|
FakeChunk { slice: "b", range: 12..13 },
|
|
|
|
FakeChunk { slice: "c", range: 0..7 },
|
|
|
|
FakeChunk { slice: "d", range: 0..1 },
|
|
|
|
],
|
|
|
|
&out[..]
|
|
|
|
);
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:04:36 -05:00
|
|
|
#[tokio::test]
|
|
|
|
pub async fn everything() {
|
2016-11-25 17:34:00 -05:00
|
|
|
// Test writing the whole Slices.
|
2017-03-02 22:29:28 -05:00
|
|
|
testutil::init();
|
2021-02-17 01:15:54 -05:00
|
|
|
let out = get_range(0..61).await;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
assert_eq!(
|
|
|
|
&[
|
|
|
|
FakeChunk { slice: "a", range: 0..5 },
|
|
|
|
FakeChunk { slice: "b", range: 0..13 },
|
|
|
|
FakeChunk { slice: "c", range: 0..7 },
|
|
|
|
FakeChunk { slice: "d", range: 0..17 },
|
|
|
|
FakeChunk { slice: "e", range: 0..19 },
|
|
|
|
],
|
|
|
|
&out[..]
|
|
|
|
);
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:04:36 -05:00
|
|
|
#[tokio::test]
|
|
|
|
pub async fn at_end() {
|
2017-03-02 22:29:28 -05:00
|
|
|
testutil::init();
|
2021-02-17 01:15:54 -05:00
|
|
|
let out = get_range(61..61).await;
|
2017-03-02 22:29:28 -05:00
|
|
|
let empty: &[FakeChunk] = &[];
|
|
|
|
assert_eq!(empty, &out[..]);
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
}
|