2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-09-27 16:27:06 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-09-27 16:27:06 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
use futures::future::{self, Loop};
|
|
|
|
use futures::sync::{mpsc, oneshot};
|
|
|
|
use futures::{self, Future, Async, Sink, Stream};
|
2018-10-22 09:40:50 +02:00
|
|
|
use hyper::header::{self, HeaderMap, HeaderValue, IntoHeaderName};
|
2018-04-10 13:51:29 +02:00
|
|
|
use hyper::{self, Method, StatusCode};
|
2018-03-14 13:40:54 +01:00
|
|
|
use hyper_rustls;
|
|
|
|
use std;
|
|
|
|
use std::cmp::min;
|
2016-12-22 18:26:39 +01:00
|
|
|
use std::sync::Arc;
|
2018-03-14 13:40:54 +01:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
|
|
use std::sync::mpsc::RecvTimeoutError;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::{io, fmt};
|
2018-10-22 09:40:50 +02:00
|
|
|
use tokio::{self, util::FutureExt};
|
2018-03-14 13:40:54 +01:00
|
|
|
use url::{self, Url};
|
2018-04-10 13:51:29 +02:00
|
|
|
use bytes::Bytes;
|
2018-03-14 13:40:54 +01:00
|
|
|
|
|
|
|
const MAX_SIZE: usize = 64 * 1024 * 1024;
|
2018-04-02 10:47:56 +02:00
|
|
|
const MAX_SECS: Duration = Duration::from_secs(5);
|
2018-03-14 13:40:54 +01:00
|
|
|
const MAX_REDR: usize = 5;
|
|
|
|
|
|
|
|
/// A handle to abort requests.
|
|
|
|
///
|
|
|
|
/// Requests are either aborted based on reaching thresholds such as
|
|
|
|
/// maximum response size, timeouts or too many redirects, or else
|
|
|
|
/// they can be aborted explicitly by the calling code.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Abort {
|
|
|
|
abort: Arc<AtomicBool>,
|
|
|
|
size: usize,
|
|
|
|
time: Duration,
|
|
|
|
redir: usize,
|
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl Default for Abort {
|
|
|
|
fn default() -> Abort {
|
|
|
|
Abort {
|
|
|
|
abort: Arc::new(AtomicBool::new(false)),
|
|
|
|
size: MAX_SIZE,
|
2018-04-02 10:47:56 +02:00
|
|
|
time: MAX_SECS,
|
2018-03-14 13:40:54 +01:00
|
|
|
redir: MAX_REDR,
|
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
impl From<Arc<AtomicBool>> for Abort {
|
2018-03-14 13:40:54 +01:00
|
|
|
fn from(a: Arc<AtomicBool>) -> Abort {
|
|
|
|
Abort {
|
|
|
|
abort: a,
|
|
|
|
size: MAX_SIZE,
|
2018-04-02 10:47:56 +02:00
|
|
|
time: MAX_SECS,
|
2018-03-14 13:40:54 +01:00
|
|
|
redir: MAX_REDR,
|
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl Abort {
|
|
|
|
/// True if `abort` has been invoked.
|
|
|
|
pub fn is_aborted(&self) -> bool {
|
|
|
|
self.abort.load(Ordering::SeqCst)
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// The maximum response body size.
|
|
|
|
pub fn max_size(&self) -> usize {
|
|
|
|
self.size
|
|
|
|
}
|
2016-12-27 16:38:55 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// The maximum total time, including redirects.
|
|
|
|
pub fn max_duration(&self) -> Duration {
|
|
|
|
self.time
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// The maximum number of redirects to allow.
|
|
|
|
pub fn max_redirects(&self) -> usize {
|
|
|
|
self.redir
|
2017-10-05 12:35:01 +02:00
|
|
|
}
|
2017-07-16 18:22:45 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Mark as aborted.
|
|
|
|
pub fn abort(&self) {
|
|
|
|
self.abort.store(true, Ordering::SeqCst)
|
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Set the maximum reponse body size.
|
|
|
|
pub fn with_max_size(self, n: usize) -> Abort {
|
|
|
|
Abort { size: n, .. self }
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Set the maximum duration (including redirects).
|
|
|
|
pub fn with_max_duration(self, d: Duration) -> Abort {
|
|
|
|
Abort { time: d, .. self }
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Set the maximum number of redirects to follow.
|
|
|
|
pub fn with_max_redirects(self, n: usize) -> Abort {
|
|
|
|
Abort { redir: n, .. self }
|
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Types which retrieve content from some URL.
|
|
|
|
pub trait Fetch: Clone + Send + Sync + 'static {
|
|
|
|
/// The result future.
|
2018-10-22 09:40:50 +02:00
|
|
|
type Result: Future<Item = Response, Error = Error> + Send + 'static;
|
2018-03-14 13:40:54 +01:00
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
/// Make a request to given URL
|
2018-04-10 13:51:29 +02:00
|
|
|
fn fetch(&self, request: Request, abort: Abort) -> Self::Result;
|
2018-04-09 16:14:33 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Get content from some URL.
|
2018-04-10 13:51:29 +02:00
|
|
|
fn get(&self, url: &str, abort: Abort) -> Self::Result;
|
2018-04-09 16:14:33 +02:00
|
|
|
|
|
|
|
/// Post content to some URL.
|
2018-04-10 13:51:29 +02:00
|
|
|
fn post(&self, url: &str, abort: Abort) -> Self::Result;
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2017-02-02 19:11:28 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
type TxResponse = oneshot::Sender<Result<Response, Error>>;
|
2018-10-22 09:40:50 +02:00
|
|
|
type TxStartup = std::sync::mpsc::SyncSender<Result<(), tokio::io::Error>>;
|
2018-04-10 13:51:29 +02:00
|
|
|
type ChanItem = Option<(Request, Abort, TxResponse)>;
|
2018-03-14 13:40:54 +01:00
|
|
|
|
|
|
|
/// An implementation of `Fetch` using a `hyper` client.
|
|
|
|
// Due to the `Send` bound of `Fetch` we spawn a background thread for
|
|
|
|
// actual request/response processing as `hyper::Client` itself does
|
|
|
|
// not implement `Send` currently.
|
|
|
|
#[derive(Debug)]
|
2016-09-27 16:27:06 +02:00
|
|
|
pub struct Client {
|
2018-10-22 09:40:50 +02:00
|
|
|
runtime: mpsc::Sender<ChanItem>,
|
2018-03-14 13:40:54 +01:00
|
|
|
refs: Arc<AtomicUsize>,
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
// When cloning a client we increment the internal reference counter.
|
2017-02-02 19:11:28 +01:00
|
|
|
impl Clone for Client {
|
2018-03-14 13:40:54 +01:00
|
|
|
fn clone(&self) -> Client {
|
|
|
|
self.refs.fetch_add(1, Ordering::SeqCst);
|
2017-02-02 19:11:28 +01:00
|
|
|
Client {
|
2018-10-22 09:40:50 +02:00
|
|
|
runtime: self.runtime.clone(),
|
2018-03-14 13:40:54 +01:00
|
|
|
refs: self.refs.clone(),
|
2017-02-02 19:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
// When dropping a client, we decrement the reference counter.
|
|
|
|
// Once it reaches 0 we terminate the background thread.
|
|
|
|
impl Drop for Client {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.refs.fetch_sub(1, Ordering::SeqCst) == 1 {
|
|
|
|
// ignore send error as it means the background thread is gone already
|
2018-10-22 09:40:50 +02:00
|
|
|
let _ = self.runtime.clone().send(None).wait();
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2017-02-02 19:11:28 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl Client {
|
|
|
|
/// Create a new fetch client.
|
2018-09-28 15:26:38 +02:00
|
|
|
pub fn new(num_dns_threads: usize) -> Result<Self, Error> {
|
2018-03-14 13:40:54 +01:00
|
|
|
let (tx_start, rx_start) = std::sync::mpsc::sync_channel(1);
|
|
|
|
let (tx_proto, rx_proto) = mpsc::channel(64);
|
2017-02-02 19:11:28 +01:00
|
|
|
|
2018-09-28 15:26:38 +02:00
|
|
|
Client::background_thread(tx_start, rx_proto, num_dns_threads)?;
|
2018-02-22 11:22:25 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
match rx_start.recv_timeout(Duration::from_secs(10)) {
|
|
|
|
Err(RecvTimeoutError::Timeout) => {
|
|
|
|
error!(target: "fetch", "timeout starting background thread");
|
|
|
|
return Err(Error::BackgroundThreadDead)
|
2017-02-02 19:11:28 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
Err(RecvTimeoutError::Disconnected) => {
|
|
|
|
error!(target: "fetch", "background thread gone");
|
|
|
|
return Err(Error::BackgroundThreadDead)
|
|
|
|
}
|
|
|
|
Ok(Err(e)) => {
|
|
|
|
error!(target: "fetch", "error starting background thread: {}", e);
|
|
|
|
return Err(e.into())
|
|
|
|
}
|
|
|
|
Ok(Ok(())) => {}
|
2017-02-02 19:11:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
Ok(Client {
|
2018-10-22 09:40:50 +02:00
|
|
|
runtime: tx_proto,
|
2018-03-14 13:40:54 +01:00
|
|
|
refs: Arc::new(AtomicUsize::new(1)),
|
|
|
|
})
|
2017-02-02 19:11:28 +01:00
|
|
|
}
|
2017-07-11 12:23:46 +02:00
|
|
|
|
2018-09-28 15:26:38 +02:00
|
|
|
fn background_thread(tx_start: TxStartup, rx_proto: mpsc::Receiver<ChanItem>, num_dns_threads: usize) -> io::Result<thread::JoinHandle<()>> {
|
2018-03-14 13:40:54 +01:00
|
|
|
thread::Builder::new().name("fetch".into()).spawn(move || {
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = match tokio::runtime::current_thread::Runtime::new() {
|
2018-03-14 13:40:54 +01:00
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return tx_start.send(Err(e)).unwrap_or(())
|
|
|
|
};
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
let hyper = hyper::Client::builder()
|
|
|
|
.build(hyper_rustls::HttpsConnector::new(num_dns_threads));
|
2018-03-14 13:40:54 +01:00
|
|
|
|
|
|
|
let future = rx_proto.take_while(|item| Ok(item.is_some()))
|
|
|
|
.map(|item| item.expect("`take_while` is only passing on channel items != None; qed"))
|
2018-04-10 13:51:29 +02:00
|
|
|
.for_each(|(request, abort, sender)|
|
2018-03-14 13:40:54 +01:00
|
|
|
{
|
2018-04-10 13:51:29 +02:00
|
|
|
trace!(target: "fetch", "new request to {}", request.url());
|
2018-03-14 13:40:54 +01:00
|
|
|
if abort.is_aborted() {
|
|
|
|
return future::ok(sender.send(Err(Error::Aborted)).unwrap_or(()))
|
|
|
|
}
|
2018-04-10 13:51:29 +02:00
|
|
|
let ini = (hyper.clone(), request, abort, 0);
|
|
|
|
let fut = future::loop_fn(ini, |(client, request, abort, redirects)| {
|
|
|
|
let request2 = request.clone();
|
|
|
|
let url2 = request2.url().clone();
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort2 = abort.clone();
|
2018-04-10 13:51:29 +02:00
|
|
|
client.request(request.into())
|
2018-03-14 13:40:54 +01:00
|
|
|
.map(move |resp| Response::new(url2, resp, abort2))
|
|
|
|
.from_err()
|
|
|
|
.and_then(move |resp| {
|
|
|
|
if abort.is_aborted() {
|
2018-04-10 13:51:29 +02:00
|
|
|
debug!(target: "fetch", "fetch of {} aborted", request2.url());
|
2018-03-14 13:40:54 +01:00
|
|
|
return Err(Error::Aborted)
|
|
|
|
}
|
2018-04-10 13:51:29 +02:00
|
|
|
if let Some((next_url, preserve_method)) = redirect_location(request2.url().clone(), &resp) {
|
2018-03-14 13:40:54 +01:00
|
|
|
if redirects >= abort.max_redirects() {
|
|
|
|
return Err(Error::TooManyRedirects)
|
|
|
|
}
|
2018-04-10 13:51:29 +02:00
|
|
|
let request = if preserve_method {
|
|
|
|
let mut request2 = request2.clone();
|
|
|
|
request2.set_url(next_url);
|
|
|
|
request2
|
|
|
|
} else {
|
2018-10-22 09:40:50 +02:00
|
|
|
Request::new(next_url, Method::GET)
|
2018-04-10 13:51:29 +02:00
|
|
|
};
|
|
|
|
Ok(Loop::Continue((client, request, abort, redirects + 1)))
|
2018-03-14 13:40:54 +01:00
|
|
|
} else {
|
2018-10-22 09:40:50 +02:00
|
|
|
if let Some(ref h_val) = resp.headers.get(header::CONTENT_LENGTH) {
|
|
|
|
let content_len = h_val
|
|
|
|
.to_str()?
|
|
|
|
.parse::<u64>()?;
|
|
|
|
|
|
|
|
if content_len > abort.max_size() as u64 {
|
|
|
|
return Err(Error::SizeLimit)
|
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
Ok(Loop::Break(resp))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(|result| {
|
|
|
|
future::ok(sender.send(result).unwrap_or(()))
|
|
|
|
});
|
2018-10-22 09:40:50 +02:00
|
|
|
tokio::spawn(fut);
|
2018-03-14 13:40:54 +01:00
|
|
|
trace!(target: "fetch", "waiting for next request ...");
|
|
|
|
future::ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
tx_start.send(Ok(())).unwrap_or(());
|
|
|
|
|
|
|
|
debug!(target: "fetch", "processing requests ...");
|
2018-10-22 09:40:50 +02:00
|
|
|
if let Err(()) = runtime.block_on(future) {
|
2018-03-14 13:40:54 +01:00
|
|
|
error!(target: "fetch", "error while executing future")
|
|
|
|
}
|
|
|
|
debug!(target: "fetch", "fetch background thread finished")
|
|
|
|
})
|
2017-07-11 12:23:46 +02:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Fetch for Client {
|
2018-10-22 09:40:50 +02:00
|
|
|
type Result = Box<Future<Item=Response, Error=Error> + Send + 'static>;
|
2018-03-14 13:40:54 +01:00
|
|
|
|
2018-04-10 13:51:29 +02:00
|
|
|
fn fetch(&self, request: Request, abort: Abort) -> Self::Result {
|
|
|
|
debug!(target: "fetch", "fetching: {:?}", request.url());
|
2018-03-14 13:40:54 +01:00
|
|
|
if abort.is_aborted() {
|
|
|
|
return Box::new(future::err(Error::Aborted))
|
2017-02-02 19:11:28 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
let (tx_res, rx_res) = oneshot::channel();
|
|
|
|
let maxdur = abort.max_duration();
|
2018-10-22 09:40:50 +02:00
|
|
|
let sender = self.runtime.clone();
|
2018-04-10 13:51:29 +02:00
|
|
|
let future = sender.send(Some((request, abort, tx_res)))
|
2018-03-14 13:40:54 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
error!(target: "fetch", "failed to schedule request: {}", e);
|
|
|
|
Error::BackgroundThreadDead
|
|
|
|
})
|
|
|
|
.and_then(|_| rx_res.map_err(|oneshot::Canceled| Error::BackgroundThreadDead))
|
2018-07-09 10:59:05 +02:00
|
|
|
.and_then(future::result);
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
Box::new(future.timeout(maxdur)
|
|
|
|
.map_err(|err| {
|
|
|
|
if err.is_inner() {
|
|
|
|
Error::from(err.into_inner().unwrap())
|
|
|
|
} else {
|
|
|
|
Error::from(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
2018-04-10 13:51:29 +02:00
|
|
|
|
|
|
|
/// Get content from some URL.
|
|
|
|
fn get(&self, url: &str, abort: Abort) -> Self::Result {
|
|
|
|
let url: Url = match url.parse() {
|
|
|
|
Ok(u) => u,
|
|
|
|
Err(e) => return Box::new(future::err(e.into()))
|
|
|
|
};
|
|
|
|
self.fetch(Request::get(url), abort)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Post content to some URL.
|
|
|
|
fn post(&self, url: &str, abort: Abort) -> Self::Result {
|
|
|
|
let url: Url = match url.parse() {
|
|
|
|
Ok(u) => u,
|
|
|
|
Err(e) => return Box::new(future::err(e.into()))
|
|
|
|
};
|
|
|
|
self.fetch(Request::post(url), abort)
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-04-10 13:51:29 +02:00
|
|
|
// Extract redirect location from response. The second return value indicate whether the original method should be preserved.
|
|
|
|
fn redirect_location(u: Url, r: &Response) -> Option<(Url, bool)> {
|
|
|
|
let preserve_method = match r.status() {
|
2018-10-22 09:40:50 +02:00
|
|
|
StatusCode::TEMPORARY_REDIRECT | StatusCode::PERMANENT_REDIRECT => true,
|
2018-04-10 13:51:29 +02:00
|
|
|
_ => false,
|
|
|
|
};
|
2018-03-14 13:40:54 +01:00
|
|
|
match r.status() {
|
2018-10-22 09:40:50 +02:00
|
|
|
StatusCode::MOVED_PERMANENTLY
|
|
|
|
| StatusCode::PERMANENT_REDIRECT
|
|
|
|
| StatusCode::TEMPORARY_REDIRECT
|
|
|
|
| StatusCode::FOUND
|
|
|
|
| StatusCode::SEE_OTHER => {
|
|
|
|
r.headers.get(header::LOCATION).and_then(|loc| {
|
|
|
|
loc.to_str().ok().and_then(|loc_s| {
|
|
|
|
u.join(loc_s).ok().map(|url| (url, preserve_method))
|
|
|
|
})
|
|
|
|
})
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 13:51:29 +02:00
|
|
|
/// A wrapper for hyper::Request using Url and with methods.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Request {
|
|
|
|
url: Url,
|
|
|
|
method: Method,
|
2018-10-22 09:40:50 +02:00
|
|
|
headers: HeaderMap,
|
2018-04-10 13:51:29 +02:00
|
|
|
body: Bytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Request {
|
|
|
|
/// Create a new request, with given url and method.
|
|
|
|
pub fn new(url: Url, method: Method) -> Request {
|
|
|
|
Request {
|
|
|
|
url, method,
|
2018-10-22 09:40:50 +02:00
|
|
|
headers: HeaderMap::new(),
|
2018-04-10 13:51:29 +02:00
|
|
|
body: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new GET request.
|
|
|
|
pub fn get(url: Url) -> Request {
|
2018-10-22 09:40:50 +02:00
|
|
|
Request::new(url, Method::GET)
|
2018-04-10 13:51:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new empty POST request.
|
|
|
|
pub fn post(url: Url) -> Request {
|
2018-10-22 09:40:50 +02:00
|
|
|
Request::new(url, Method::POST)
|
2018-04-10 13:51:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the url.
|
|
|
|
pub fn url(&self) -> &Url {
|
|
|
|
&self.url
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the request headers.
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2018-04-10 13:51:29 +02:00
|
|
|
&self.headers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable reference to the headers.
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2018-04-10 13:51:29 +02:00
|
|
|
&mut self.headers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the body of the request.
|
|
|
|
pub fn set_body<T: Into<Bytes>>(&mut self, body: T) {
|
|
|
|
self.body = body.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the url of the request.
|
|
|
|
pub fn set_url(&mut self, url: Url) {
|
|
|
|
self.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume self, and return it with the added given header.
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn with_header<K>(mut self, key: K, val: HeaderValue) -> Self
|
|
|
|
where K: IntoHeaderName,
|
|
|
|
{
|
|
|
|
self.headers_mut().append(key, val);
|
2018-04-10 13:51:29 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume self, and return it with the body.
|
|
|
|
pub fn with_body<T: Into<Bytes>>(mut self, body: T) -> Self {
|
|
|
|
self.set_body(body);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
impl From<Request> for hyper::Request<hyper::Body> {
|
|
|
|
fn from(req: Request) -> hyper::Request<hyper::Body> {
|
|
|
|
let uri: hyper::Uri = req.url.as_ref().parse().expect("Every valid URLis also a URI.");
|
|
|
|
hyper::Request::builder()
|
|
|
|
.method(req.method)
|
|
|
|
.uri(uri)
|
|
|
|
.header(header::USER_AGENT, HeaderValue::from_static("Parity Fetch Neo"))
|
|
|
|
.body(req.body.into())
|
|
|
|
.expect("Header, uri, method, and body are already valid and can not fail to parse; qed")
|
2018-04-10 13:51:29 +02:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An HTTP response.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Response {
|
|
|
|
url: Url,
|
|
|
|
status: StatusCode,
|
2018-10-22 09:40:50 +02:00
|
|
|
headers: HeaderMap,
|
2018-03-14 13:40:54 +01:00
|
|
|
body: hyper::Body,
|
2016-12-22 18:26:39 +01:00
|
|
|
abort: Abort,
|
2018-03-14 13:40:54 +01:00
|
|
|
nread: usize,
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl Response {
|
|
|
|
/// Create a new response, wrapping a hyper response.
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn new(u: Url, r: hyper::Response<hyper::Body>, a: Abort) -> Response {
|
2018-03-14 13:40:54 +01:00
|
|
|
Response {
|
|
|
|
url: u,
|
|
|
|
status: r.status(),
|
|
|
|
headers: r.headers().clone(),
|
2018-10-22 09:40:50 +02:00
|
|
|
body: r.into_body(),
|
2018-03-14 13:40:54 +01:00
|
|
|
abort: a,
|
|
|
|
nread: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The response status.
|
|
|
|
pub fn status(&self) -> StatusCode {
|
|
|
|
self.status
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Status code == OK (200)?
|
|
|
|
pub fn is_success(&self) -> bool {
|
2018-10-22 09:40:50 +02:00
|
|
|
self.status() == StatusCode::OK
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
|
2018-04-09 16:14:33 +02:00
|
|
|
/// Status code == 404.
|
|
|
|
pub fn is_not_found(&self) -> bool {
|
2018-10-22 09:40:50 +02:00
|
|
|
self.status() == StatusCode::NOT_FOUND
|
2018-04-09 16:14:33 +02:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Is the content-type text/html?
|
|
|
|
pub fn is_html(&self) -> bool {
|
2018-10-22 09:40:50 +02:00
|
|
|
self.headers.get(header::CONTENT_TYPE).and_then(|ct_val| {
|
|
|
|
ct_val.to_str().ok().map(|ct_str| {
|
|
|
|
ct_str.contains("text") && ct_str.contains("html")
|
|
|
|
})
|
|
|
|
}).unwrap_or(false)
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Response {
|
|
|
|
type Item = hyper::Chunk;
|
2016-12-22 18:26:39 +01:00
|
|
|
type Error = Error;
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
|
2016-12-22 18:26:39 +01:00
|
|
|
if self.abort.is_aborted() {
|
2018-03-14 13:40:54 +01:00
|
|
|
debug!(target: "fetch", "fetch of {} aborted", self.url);
|
|
|
|
return Err(Error::Aborted)
|
|
|
|
}
|
|
|
|
match try_ready!(self.body.poll()) {
|
|
|
|
None => Ok(Async::Ready(None)),
|
|
|
|
Some(c) => {
|
|
|
|
if self.nread + c.len() > self.abort.max_size() {
|
|
|
|
debug!(target: "fetch", "size limit {:?} for {} exceeded", self.abort.max_size(), self.url);
|
|
|
|
return Err(Error::SizeLimit)
|
|
|
|
}
|
|
|
|
self.nread += c.len();
|
|
|
|
Ok(Async::Ready(Some(c)))
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
/// `BodyReader` serves as an adapter from async to sync I/O.
|
|
|
|
///
|
|
|
|
/// It implements `io::Read` by repedately waiting for the next `Chunk`
|
|
|
|
/// of hyper's response `Body` which blocks the current thread.
|
|
|
|
pub struct BodyReader {
|
|
|
|
chunk: hyper::Chunk,
|
|
|
|
body: Option<hyper::Body>,
|
|
|
|
abort: Abort,
|
|
|
|
offset: usize,
|
|
|
|
count: usize,
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl BodyReader {
|
|
|
|
/// Create a new body reader for the given response.
|
|
|
|
pub fn new(r: Response) -> BodyReader {
|
|
|
|
BodyReader {
|
|
|
|
body: Some(r.body),
|
|
|
|
chunk: Default::default(),
|
|
|
|
abort: r.abort,
|
|
|
|
offset: 0,
|
|
|
|
count: 0,
|
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl io::Read for BodyReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
let mut n = 0;
|
|
|
|
while self.body.is_some() {
|
|
|
|
// Can we still read from the current chunk?
|
|
|
|
if self.offset < self.chunk.len() {
|
|
|
|
let k = min(self.chunk.len() - self.offset, buf.len() - n);
|
|
|
|
if self.count + k > self.abort.max_size() {
|
|
|
|
debug!(target: "fetch", "size limit {:?} exceeded", self.abort.max_size());
|
|
|
|
return Err(io::Error::new(io::ErrorKind::PermissionDenied, "size limit exceeded"))
|
|
|
|
}
|
|
|
|
let c = &self.chunk[self.offset .. self.offset + k];
|
|
|
|
(&mut buf[n .. n + k]).copy_from_slice(c);
|
|
|
|
self.offset += k;
|
|
|
|
self.count += k;
|
|
|
|
n += k;
|
|
|
|
if n == buf.len() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let body = self.body.take().expect("loop condition ensures `self.body` is always defined; qed");
|
|
|
|
match body.into_future().wait() { // wait for next chunk
|
|
|
|
Err((e, _)) => {
|
|
|
|
error!(target: "fetch", "failed to read chunk: {}", e);
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "failed to read body chunk"))
|
|
|
|
}
|
|
|
|
Ok((None, _)) => break, // body is exhausted, break out of the loop
|
|
|
|
Ok((Some(c), b)) => {
|
|
|
|
self.body = Some(b);
|
|
|
|
self.chunk = c;
|
|
|
|
self.offset = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch error cases.
|
2016-12-22 18:26:39 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Hyper gave us an error.
|
|
|
|
Hyper(hyper::Error),
|
2018-10-22 09:40:50 +02:00
|
|
|
/// A hyper header conversion error.
|
|
|
|
HyperHeaderToStrError(hyper::header::ToStrError),
|
|
|
|
/// An integer parsing error.
|
|
|
|
ParseInt(std::num::ParseIntError),
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Some I/O error occured.
|
|
|
|
Io(io::Error),
|
|
|
|
/// Invalid URLs where attempted to parse.
|
|
|
|
Url(url::ParseError),
|
|
|
|
/// Calling code invoked `Abort::abort`.
|
2016-12-22 18:26:39 +01:00
|
|
|
Aborted,
|
2018-03-14 13:40:54 +01:00
|
|
|
/// Too many redirects have been encountered.
|
|
|
|
TooManyRedirects,
|
2018-10-22 09:40:50 +02:00
|
|
|
/// tokio-timer inner future gave us an error.
|
|
|
|
TokioTimeoutInnerVal(String),
|
2018-07-09 10:59:05 +02:00
|
|
|
/// tokio-timer gave us an error.
|
2018-10-22 09:40:50 +02:00
|
|
|
TokioTimer(Option<tokio::timer::Error>),
|
2018-03-14 13:40:54 +01:00
|
|
|
/// The maximum duration was reached.
|
|
|
|
Timeout,
|
|
|
|
/// The response body is too large.
|
|
|
|
SizeLimit,
|
|
|
|
/// The background processing thread does not run.
|
|
|
|
BackgroundThreadDead,
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2017-07-11 12:23:46 +02:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Error::Aborted => write!(fmt, "The request has been aborted."),
|
2018-03-14 13:40:54 +01:00
|
|
|
Error::Hyper(ref e) => write!(fmt, "{}", e),
|
2018-10-22 09:40:50 +02:00
|
|
|
Error::HyperHeaderToStrError(ref e) => write!(fmt, "{}", e),
|
|
|
|
Error::ParseInt(ref e) => write!(fmt, "{}", e),
|
2018-03-14 13:40:54 +01:00
|
|
|
Error::Url(ref e) => write!(fmt, "{}", e),
|
|
|
|
Error::Io(ref e) => write!(fmt, "{}", e),
|
|
|
|
Error::BackgroundThreadDead => write!(fmt, "background thread gond"),
|
|
|
|
Error::TooManyRedirects => write!(fmt, "too many redirects"),
|
2018-10-22 09:40:50 +02:00
|
|
|
Error::TokioTimeoutInnerVal(ref s) => write!(fmt, "tokio timer inner value error: {:?}", s),
|
|
|
|
Error::TokioTimer(ref e) => write!(fmt, "tokio timer error: {:?}", e),
|
2018-03-14 13:40:54 +01:00
|
|
|
Error::Timeout => write!(fmt, "request timed out"),
|
|
|
|
Error::SizeLimit => write!(fmt, "size limit reached"),
|
2017-07-11 12:23:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
impl ::std::error::Error for Error {
|
|
|
|
fn description(&self) -> &str { "Fetch client error" }
|
|
|
|
fn cause(&self) -> Option<&::std::error::Error> { None }
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl From<hyper::Error> for Error {
|
|
|
|
fn from(e: hyper::Error) -> Self {
|
|
|
|
Error::Hyper(e)
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
impl From<hyper::header::ToStrError> for Error {
|
|
|
|
fn from(e: hyper::header::ToStrError) -> Self {
|
|
|
|
Error::HyperHeaderToStrError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::num::ParseIntError> for Error {
|
|
|
|
fn from(e: std::num::ParseIntError) -> Self {
|
|
|
|
Error::ParseInt(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(e: io::Error) -> Self {
|
|
|
|
Error::Io(e)
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl From<url::ParseError> for Error {
|
|
|
|
fn from(e: url::ParseError) -> Self {
|
|
|
|
Error::Url(e)
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
impl<T: std::fmt::Debug> From<tokio::timer::timeout::Error<T>> for Error {
|
|
|
|
fn from(e: tokio::timer::timeout::Error<T>) -> Self {
|
|
|
|
if e.is_inner() {
|
|
|
|
Error::TokioTimeoutInnerVal(format!("{:?}", e.into_inner().unwrap()))
|
|
|
|
} else if e.is_elapsed() {
|
|
|
|
Error::Timeout
|
|
|
|
} else {
|
|
|
|
Error::TokioTimer(e.into_timer())
|
2018-07-09 10:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
impl From<tokio::timer::Error> for Error {
|
|
|
|
fn from(e: tokio::timer::Error) -> Self {
|
|
|
|
Error::TokioTimer(Some(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use futures::future;
|
2018-10-22 09:40:50 +02:00
|
|
|
use futures::sync::oneshot;
|
|
|
|
use hyper::{
|
|
|
|
StatusCode,
|
|
|
|
service::Service,
|
|
|
|
};
|
|
|
|
use tokio::timer::Delay;
|
|
|
|
use tokio::runtime::current_thread::Runtime;
|
2018-03-14 13:40:54 +01:00
|
|
|
use std::io::Read;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
|
|
|
const ADDRESS: &str = "127.0.0.1:0";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_should_fetch() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
|
|
|
let future = client.get(&format!("http://{}?123", server.addr()), Abort::default())
|
|
|
|
.map(|resp| {
|
|
|
|
assert!(resp.is_success());
|
|
|
|
resp
|
|
|
|
})
|
|
|
|
.map(|resp| resp.concat2())
|
|
|
|
.flatten()
|
|
|
|
.map(|body| assert_eq!(&body[..], b"123"))
|
|
|
|
.map_err(|err| panic!(err));
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2018-09-28 15:26:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_should_fetch_in_light_mode() {
|
|
|
|
let server = TestServer::run();
|
|
|
|
let client = Client::new(1).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
|
|
|
let future = client.get(&format!("http://{}?123", server.addr()), Abort::default())
|
|
|
|
.map(|resp| {
|
|
|
|
assert!(resp.is_success());
|
|
|
|
resp
|
|
|
|
})
|
|
|
|
.map(|resp| resp.concat2())
|
|
|
|
.flatten()
|
|
|
|
.map(|body| assert_eq!(&body[..], b"123"))
|
|
|
|
.map_err(|err| panic!(err));
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[test]
|
|
|
|
fn it_should_timeout() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default().with_max_duration(Duration::from_secs(1));
|
2018-10-22 09:40:50 +02:00
|
|
|
|
|
|
|
let future = client.get(&format!("http://{}/delay?3", server.addr()), abort)
|
|
|
|
.then(|res| {
|
|
|
|
match res {
|
|
|
|
Err(Error::Timeout) => Ok::<_, ()>(()),
|
|
|
|
other => panic!("expected timeout, got {:?}", other),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[test]
|
|
|
|
fn it_should_follow_redirects() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default();
|
2018-10-22 09:40:50 +02:00
|
|
|
|
|
|
|
let future = client.get(&format!("http://{}/redirect?http://{}/", server.addr(), server.addr()), abort)
|
|
|
|
.and_then(|resp| {
|
|
|
|
if resp.is_success() { Ok(()) } else { panic!("Response unsuccessful") }
|
|
|
|
});
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2017-02-20 16:30:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[test]
|
|
|
|
fn it_should_follow_relative_redirects() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default().with_max_redirects(4);
|
2018-10-22 09:40:50 +02:00
|
|
|
let future = client.get(&format!("http://{}/redirect?/", server.addr()), abort)
|
|
|
|
.and_then(|resp| {
|
|
|
|
if resp.is_success() { Ok(()) } else { panic!("Response unsuccessful") }
|
|
|
|
});
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_should_not_follow_too_many_redirects() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default().with_max_redirects(3);
|
2018-10-22 09:40:50 +02:00
|
|
|
let future = client.get(&format!("http://{}/loop", server.addr()), abort)
|
|
|
|
.then(|res| {
|
|
|
|
match res {
|
|
|
|
Err(Error::TooManyRedirects) => Ok::<_, ()>(()),
|
|
|
|
other => panic!("expected too many redirects error, got {:?}", other)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[test]
|
|
|
|
fn it_should_read_data() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default();
|
2018-10-22 09:40:50 +02:00
|
|
|
let future = client.get(&format!("http://{}?abcdefghijklmnopqrstuvwxyz", server.addr()), abort)
|
|
|
|
.and_then(|resp| {
|
|
|
|
if resp.is_success() { Ok(resp) } else { panic!("Response unsuccessful") }
|
|
|
|
})
|
|
|
|
.map(|resp| resp.concat2())
|
|
|
|
.flatten()
|
|
|
|
.map(|body| assert_eq!(&body[..], b"abcdefghijklmnopqrstuvwxyz"));
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2017-02-20 16:30:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[test]
|
|
|
|
fn it_should_not_read_too_much_data() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default().with_max_size(3);
|
2018-10-22 09:40:50 +02:00
|
|
|
let future = client.get(&format!("http://{}/?1234", server.addr()), abort)
|
|
|
|
.and_then(|resp| {
|
|
|
|
if resp.is_success() { Ok(resp) } else { panic!("Response unsuccessful") }
|
|
|
|
})
|
|
|
|
.map(|resp| resp.concat2())
|
|
|
|
.flatten()
|
|
|
|
.then(|body| {
|
|
|
|
match body {
|
|
|
|
Err(Error::SizeLimit) => Ok::<_, ()>(()),
|
|
|
|
other => panic!("expected size limit error, got {:?}", other),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
runtime.block_on(future).unwrap();
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
#[test]
|
|
|
|
fn it_should_not_read_too_much_data_sync() {
|
|
|
|
let server = TestServer::run();
|
2018-09-28 15:26:38 +02:00
|
|
|
let client = Client::new(4).unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
let mut runtime = Runtime::new().unwrap();
|
|
|
|
|
|
|
|
// let abort = Abort::default().with_max_size(3);
|
|
|
|
// let resp = client.get(&format!("http://{}/?1234", server.addr()), abort).wait().unwrap();
|
|
|
|
// assert!(resp.is_success());
|
|
|
|
// let mut buffer = Vec::new();
|
|
|
|
// let mut reader = BodyReader::new(resp);
|
|
|
|
// match reader.read_to_end(&mut buffer) {
|
|
|
|
// Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => {}
|
|
|
|
// other => panic!("expected size limit error, got {:?}", other)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// FIXME (c0gent): The prior version of this test (pre-hyper-0.12,
|
|
|
|
// commented out above) is not possible to recreate. It relied on an
|
|
|
|
// apparent bug in `Client::background_thread` which suppressed the
|
|
|
|
// `SizeLimit` error from occurring. This is due to the headers
|
|
|
|
// collection not returning a value for content length when queried.
|
|
|
|
// The precise reason why this was happening is unclear.
|
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
let abort = Abort::default().with_max_size(3);
|
2018-10-22 09:40:50 +02:00
|
|
|
let future = client.get(&format!("http://{}/?1234", server.addr()), abort)
|
|
|
|
.and_then(|resp| {
|
|
|
|
assert_eq!(true, false, "Unreachable. (see FIXME note)");
|
|
|
|
assert!(resp.is_success());
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let mut reader = BodyReader::new(resp);
|
|
|
|
match reader.read_to_end(&mut buffer) {
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => Ok(()),
|
|
|
|
other => panic!("expected size limit error, got {:?}", other)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME: This simply demonstrates the above point.
|
|
|
|
match runtime.block_on(future) {
|
|
|
|
Err(Error::SizeLimit) => {},
|
|
|
|
other => panic!("Expected `Error::SizeLimit`, got: {:?}", other),
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2016-09-27 16:27:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
struct TestServer;
|
2018-03-14 13:40:54 +01:00
|
|
|
|
|
|
|
impl Service for TestServer {
|
2018-10-22 09:40:50 +02:00
|
|
|
type ReqBody = hyper::Body;
|
|
|
|
type ResBody = hyper::Body;
|
|
|
|
type Error = Error;
|
|
|
|
type Future = Box<Future<Item=hyper::Response<Self::ResBody>, Error=Self::Error> + Send + 'static>;
|
2018-03-14 13:40:54 +01:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
fn call(&mut self, req: hyper::Request<hyper::Body>) -> Self::Future {
|
2018-03-14 13:40:54 +01:00
|
|
|
match req.uri().path() {
|
|
|
|
"/" => {
|
|
|
|
let body = req.uri().query().unwrap_or("").to_string();
|
2018-10-22 09:40:50 +02:00
|
|
|
let res = hyper::Response::new(body.into());
|
|
|
|
Box::new(future::ok(res))
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
"/redirect" => {
|
2018-10-22 09:40:50 +02:00
|
|
|
let loc = req.uri().query().unwrap_or("/").to_string();
|
|
|
|
let res = hyper::Response::builder()
|
|
|
|
.status(StatusCode::MOVED_PERMANENTLY)
|
|
|
|
.header(hyper::header::LOCATION, loc)
|
|
|
|
.body(hyper::Body::empty())
|
|
|
|
.expect("Unable to create response");
|
|
|
|
Box::new(future::ok(res))
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
"/loop" => {
|
2018-10-22 09:40:50 +02:00
|
|
|
let res = hyper::Response::builder()
|
|
|
|
.status(StatusCode::MOVED_PERMANENTLY)
|
|
|
|
.header(hyper::header::LOCATION, "/loop")
|
|
|
|
.body(hyper::Body::empty())
|
|
|
|
.expect("Unable to create response");
|
|
|
|
Box::new(future::ok(res))
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
"/delay" => {
|
2018-10-22 09:40:50 +02:00
|
|
|
let dur = Duration::from_secs(req.uri().query().unwrap_or("0").parse().unwrap());
|
|
|
|
let delayed_res = Delay::new(std::time::Instant::now() + dur)
|
|
|
|
.and_then(|_| Ok::<_, _>(hyper::Response::new(hyper::Body::empty())))
|
|
|
|
.from_err();
|
|
|
|
Box::new(delayed_res)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let res = hyper::Response::builder()
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
.body(hyper::Body::empty())
|
|
|
|
.expect("Unable to create response");
|
|
|
|
Box::new(future::ok(res))
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl TestServer {
|
|
|
|
fn run() -> Handle {
|
|
|
|
let (tx_start, rx_start) = std::sync::mpsc::sync_channel(1);
|
2018-10-22 09:40:50 +02:00
|
|
|
let (tx_end, rx_end) = oneshot::channel();
|
|
|
|
let rx_end_fut = rx_end.map(|_| ()).map_err(|_| ());
|
2018-03-14 13:40:54 +01:00
|
|
|
thread::spawn(move || {
|
|
|
|
let addr = ADDRESS.parse().unwrap();
|
2018-10-22 09:40:50 +02:00
|
|
|
|
|
|
|
let server = hyper::server::Server::bind(&addr)
|
|
|
|
.serve(|| future::ok::<_, hyper::Error>(TestServer));
|
|
|
|
|
|
|
|
tx_start.send(server.local_addr()).unwrap_or(());
|
|
|
|
|
|
|
|
tokio::run(
|
|
|
|
server.with_graceful_shutdown(rx_end_fut)
|
|
|
|
.map_err(|e| panic!("server error: {}", e))
|
|
|
|
);
|
2018-03-14 13:40:54 +01:00
|
|
|
});
|
2018-10-22 09:40:50 +02:00
|
|
|
|
|
|
|
Handle(rx_start.recv().unwrap(), Some(tx_end))
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
struct Handle(SocketAddr, Option<oneshot::Sender<()>>);
|
2018-03-14 13:40:54 +01:00
|
|
|
|
|
|
|
impl Handle {
|
|
|
|
fn addr(&self) -> SocketAddr {
|
|
|
|
self.0
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
2018-03-14 13:40:54 +01:00
|
|
|
impl Drop for Handle {
|
|
|
|
fn drop(&mut self) {
|
2018-10-22 09:40:50 +02:00
|
|
|
self.1.take().unwrap().send(()).unwrap();
|
2018-03-14 13:40:54 +01:00
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
}
|