-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Partial Content Delivery #95
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ pub use static_handler::Cache; | |
|
||
mod requested_path; | ||
mod static_handler; | ||
mod partial_file; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use std::fs::File; | ||
use iron::headers::{ByteRangeSpec, ContentLength, ContentRange, ContentRangeSpec}; | ||
use iron::response::{WriteBody, Response}; | ||
use iron::modifier::Modifier; | ||
use iron::status::Status; | ||
use std::io::{self, SeekFrom, Seek, Read, Write}; | ||
use std::path::Path; | ||
|
||
pub enum PartialFileRange { | ||
AllFrom(u64), | ||
FromTo(u64,u64), | ||
Last(u64), | ||
} | ||
|
||
pub struct PartialFile { | ||
file: File, | ||
range: PartialFileRange, | ||
} | ||
|
||
struct PartialContentBody { | ||
pub file: File, | ||
pub offset: u64, | ||
pub len: u64, | ||
} | ||
|
||
impl PartialFile { | ||
pub fn new<Range>(file: File, range: Range) -> PartialFile | ||
where Range: Into<PartialFileRange> { | ||
let range = range.into(); | ||
PartialFile { | ||
file: file, | ||
range: range, | ||
} | ||
} | ||
|
||
/// Panics if the file doesn't exist | ||
pub fn from_path<P: AsRef<Path>, Range>(path: P, range: Range) -> PartialFile | ||
where Range: Into<PartialFileRange> { | ||
let file = File::open(path.as_ref()) | ||
.expect(&format!("No such file: {}", path.as_ref().display())); | ||
Self::new(file, range) | ||
} | ||
} | ||
|
||
impl From<ByteRangeSpec> for PartialFileRange { | ||
fn from(b: ByteRangeSpec) -> PartialFileRange { | ||
match b { | ||
ByteRangeSpec::AllFrom(from) => PartialFileRange::AllFrom(from), | ||
ByteRangeSpec::FromTo(from, to) => PartialFileRange::FromTo(from, to), | ||
ByteRangeSpec::Last(last) => PartialFileRange::Last(last), | ||
} | ||
} | ||
} | ||
|
||
impl From<Vec<ByteRangeSpec>> for PartialFileRange { | ||
fn from(v: Vec<ByteRangeSpec>) -> PartialFileRange { | ||
match v.into_iter().next() { | ||
// in the case no value is in "Range", return | ||
// the whole file instead of panicking | ||
// Note that an empty vec should never happen, | ||
// but we can never be too sure | ||
None => PartialFileRange::AllFrom(0), | ||
Some(byte_range) => PartialFileRange::from(byte_range), | ||
} | ||
} | ||
} | ||
|
||
impl Modifier<Response> for PartialFile { | ||
#[inline] | ||
fn modify(self, res: &mut Response) { | ||
use self::PartialFileRange::*; | ||
let metadata : Option<_> = self.file.metadata().ok(); | ||
let file_length : Option<u64> = metadata.map(|m| m.len()); | ||
let range : Option<(u64, u64)> = match (self.range, file_length) { | ||
(FromTo(from, to), Some(file_length)) => { | ||
if from <= to && to < file_length { | ||
Some((from, to)) | ||
} else { | ||
None | ||
} | ||
}, | ||
(AllFrom(from), Some(file_length)) => { | ||
if from < file_length { | ||
Some((from, file_length - 1)) | ||
} else { | ||
None | ||
} | ||
}, | ||
(Last(last), Some(file_length)) => { | ||
if last < file_length { | ||
Some((file_length - last, file_length - 1)) | ||
} else { | ||
Some((0, file_length - 1)) | ||
} | ||
}, | ||
(_, None) => None, | ||
|
||
}; | ||
if let Some(range) = range { | ||
let content_range = ContentRange(ContentRangeSpec::Bytes { | ||
range: Some(range), | ||
instance_length: file_length, | ||
}); | ||
let content_len = range.1 - range.0 + 1; | ||
res.headers.set(ContentLength(content_len)); | ||
res.headers.set(content_range); | ||
let partial_content = PartialContentBody { | ||
file: self.file, | ||
offset: range.0, | ||
len: content_len, | ||
}; | ||
res.status = Some(Status::PartialContent); | ||
res.body = Some(Box::new(partial_content)); | ||
} else { | ||
if let Some(file_length) = file_length { | ||
res.headers.set(ContentRange(ContentRangeSpec::Bytes { | ||
range: None, | ||
instance_length: Some(file_length), | ||
})); | ||
}; | ||
res.status = Some(Status::RangeNotSatisfiable); | ||
} | ||
} | ||
} | ||
|
||
impl WriteBody for PartialContentBody { | ||
fn write_body(&mut self, res: &mut Write) -> io::Result<()> { | ||
self.file.seek(SeekFrom::Start(self.offset))?; | ||
let mut limiter = <File as Read>::by_ref(&mut self.file).take(self.len); | ||
io::copy(&mut limiter, res).map(|_| ()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting content range can be done at the very beginning to deduplicate the code here. If the file length is
None
we can then setRangeNotSatisfiable
and return early.