2016-08-18 12:19:09 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity 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.
|
|
|
|
|
|
|
|
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Hyper Client Handler to Fetch File
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
use std::{io, fs, fmt};
|
2016-08-18 12:19:09 +02:00
|
|
|
use std::path::PathBuf;
|
2016-09-27 16:27:06 +02:00
|
|
|
use std::sync::Arc;
|
2016-08-30 14:04:52 +02:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2016-08-18 12:19:09 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use hyper::status::StatusCode;
|
|
|
|
use hyper::client::{Request, Response, DefaultTransport as HttpStream};
|
|
|
|
use hyper::header::Connection;
|
|
|
|
use hyper::{self, Decoder, Encoder, Next};
|
|
|
|
|
2016-09-03 01:34:32 +02:00
|
|
|
use super::FetchError;
|
|
|
|
|
2016-08-18 12:19:09 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2016-08-30 14:04:52 +02:00
|
|
|
Aborted,
|
2016-08-18 12:19:09 +02:00
|
|
|
NotStarted,
|
2016-09-27 16:27:06 +02:00
|
|
|
SizeLimit,
|
2016-08-18 12:19:09 +02:00
|
|
|
UnexpectedStatus(StatusCode),
|
|
|
|
IoError(io::Error),
|
|
|
|
HyperError(hyper::Error),
|
|
|
|
}
|
|
|
|
|
2016-09-03 01:34:32 +02:00
|
|
|
pub type FetchResult = Result<PathBuf, FetchError>;
|
2016-09-27 16:27:06 +02:00
|
|
|
pub type OnDone = Box<Fn(FetchResult) + Send>;
|
2016-08-18 12:19:09 +02:00
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
pub struct FetchHandler {
|
2016-08-18 12:19:09 +02:00
|
|
|
path: PathBuf,
|
2016-08-30 14:04:52 +02:00
|
|
|
abort: Arc<AtomicBool>,
|
2016-08-18 12:19:09 +02:00
|
|
|
file: Option<fs::File>,
|
|
|
|
result: Option<FetchResult>,
|
|
|
|
on_done: Option<OnDone>,
|
2016-09-27 16:27:06 +02:00
|
|
|
size_limit: Option<u64>,
|
2016-08-18 12:19:09 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
impl fmt::Debug for FetchHandler {
|
2016-08-18 12:19:09 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(f, "Fetch {{ path: {:?}, file: {:?}, result: {:?} }}", self.path, self.file, self.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
impl Drop for FetchHandler {
|
2016-08-18 12:19:09 +02:00
|
|
|
fn drop(&mut self) {
|
2016-09-03 01:34:32 +02:00
|
|
|
let res = self.result.take().unwrap_or(Err(Error::NotStarted.into()));
|
2016-08-18 12:19:09 +02:00
|
|
|
// Remove file if there was an error
|
2016-08-30 14:04:52 +02:00
|
|
|
if res.is_err() || self.is_aborted() {
|
2016-08-18 12:19:09 +02:00
|
|
|
if let Some(file) = self.file.take() {
|
|
|
|
drop(file);
|
|
|
|
// Remove file
|
|
|
|
let _ = fs::remove_file(&self.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send result
|
|
|
|
if let Some(f) = self.on_done.take() {
|
2016-09-27 16:27:06 +02:00
|
|
|
f(res);
|
2016-08-18 12:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
impl FetchHandler {
|
|
|
|
pub fn new(path: PathBuf, abort: Arc<AtomicBool>, on_done: OnDone, size_limit: Option<u64>) -> Self {
|
|
|
|
FetchHandler {
|
|
|
|
path: path,
|
2016-08-30 14:04:52 +02:00
|
|
|
abort: abort,
|
2016-08-18 12:19:09 +02:00
|
|
|
file: None,
|
|
|
|
result: None,
|
|
|
|
on_done: Some(on_done),
|
2016-09-27 16:27:06 +02:00
|
|
|
size_limit: size_limit,
|
2016-08-18 12:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 14:04:52 +02:00
|
|
|
fn is_aborted(&self) -> bool {
|
2016-09-03 01:34:32 +02:00
|
|
|
self.abort.load(Ordering::SeqCst)
|
2016-08-30 14:04:52 +02:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2016-08-30 14:04:52 +02:00
|
|
|
fn mark_aborted(&mut self) -> Next {
|
2016-09-03 01:34:32 +02:00
|
|
|
self.result = Some(Err(Error::Aborted.into()));
|
2016-08-30 14:04:52 +02:00
|
|
|
Next::end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:27:06 +02:00
|
|
|
impl hyper::client::Handler<HttpStream> for FetchHandler {
|
2016-08-18 12:19:09 +02:00
|
|
|
fn on_request(&mut self, req: &mut Request) -> Next {
|
2016-08-30 14:04:52 +02:00
|
|
|
if self.is_aborted() {
|
|
|
|
return self.mark_aborted();
|
|
|
|
}
|
2016-08-18 12:19:09 +02:00
|
|
|
req.headers_mut().set(Connection::close());
|
|
|
|
read()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_request_writable(&mut self, _encoder: &mut Encoder<HttpStream>) -> Next {
|
2016-08-30 14:04:52 +02:00
|
|
|
if self.is_aborted() {
|
|
|
|
return self.mark_aborted();
|
|
|
|
}
|
2016-08-18 12:19:09 +02:00
|
|
|
read()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_response(&mut self, res: Response) -> Next {
|
2016-08-30 14:04:52 +02:00
|
|
|
if self.is_aborted() {
|
|
|
|
return self.mark_aborted();
|
|
|
|
}
|
2016-08-18 12:19:09 +02:00
|
|
|
if *res.status() != StatusCode::Ok {
|
2016-09-03 01:34:32 +02:00
|
|
|
self.result = Some(Err(Error::UnexpectedStatus(*res.status()).into()));
|
2016-08-18 12:19:09 +02:00
|
|
|
return Next::end();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open file to write
|
|
|
|
match fs::File::create(&self.path) {
|
|
|
|
Ok(file) => {
|
|
|
|
self.file = Some(file);
|
|
|
|
self.result = Some(Ok(self.path.clone()));
|
|
|
|
read()
|
|
|
|
},
|
|
|
|
Err(err) => {
|
2016-09-03 01:34:32 +02:00
|
|
|
self.result = Some(Err(Error::IoError(err).into()));
|
2016-08-18 12:19:09 +02:00
|
|
|
Next::end()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_response_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
|
2016-08-30 14:04:52 +02:00
|
|
|
if self.is_aborted() {
|
|
|
|
return self.mark_aborted();
|
|
|
|
}
|
2016-08-18 12:19:09 +02:00
|
|
|
match io::copy(decoder, self.file.as_mut().expect("File is there because on_response has created it.")) {
|
|
|
|
Ok(0) => Next::end(),
|
2016-09-27 16:27:06 +02:00
|
|
|
Ok(bytes_read) => match self.size_limit {
|
|
|
|
None => read(),
|
|
|
|
// Check limit
|
|
|
|
Some(limit) if limit > bytes_read => {
|
|
|
|
self.size_limit = Some(limit - bytes_read);
|
|
|
|
read()
|
|
|
|
},
|
|
|
|
// Size limit reached
|
|
|
|
_ => {
|
|
|
|
self.result = Some(Err(Error::SizeLimit.into()));
|
|
|
|
Next::end()
|
|
|
|
},
|
|
|
|
},
|
2016-08-18 12:19:09 +02:00
|
|
|
Err(e) => match e.kind() {
|
|
|
|
io::ErrorKind::WouldBlock => Next::read(),
|
|
|
|
_ => {
|
2016-09-03 01:34:32 +02:00
|
|
|
self.result = Some(Err(Error::IoError(e).into()));
|
2016-08-18 12:19:09 +02:00
|
|
|
Next::end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_error(&mut self, err: hyper::Error) -> Next {
|
2016-09-03 01:34:32 +02:00
|
|
|
self.result = Some(Err(Error::HyperError(err).into()));
|
2016-08-18 12:19:09 +02:00
|
|
|
Next::remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read() -> Next {
|
|
|
|
Next::read().timeout(Duration::from_secs(15))
|
|
|
|
}
|