1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-01 07:30:05 +01:00
actix-net/actix-connector/src/resolver.rs

129 lines
3.4 KiB
Rust
Raw Normal View History

2018-09-11 04:16:46 +02:00
use std::collections::VecDeque;
use std::marker::PhantomData;
use std::net::IpAddr;
2018-09-11 04:16:46 +02:00
2018-12-09 19:15:49 +01:00
use actix_service::Service;
2018-12-11 03:08:07 +01:00
use futures::{Async, Future, Poll};
2018-09-11 04:16:46 +02:00
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
2018-10-30 04:29:47 +01:00
pub use trust_dns_resolver::error::ResolveError;
2018-09-11 04:16:46 +02:00
use trust_dns_resolver::lookup_ip::LookupIpFuture;
use trust_dns_resolver::system_conf::read_system_conf;
use trust_dns_resolver::{AsyncResolver, Background};
/// Host name of the request
pub trait RequestHost {
2018-09-11 04:16:46 +02:00
fn host(&self) -> &str;
}
impl RequestHost for String {
2018-09-11 04:16:46 +02:00
fn host(&self) -> &str {
self.as_ref()
}
}
pub struct Resolver<T = String> {
resolver: AsyncResolver,
req: PhantomData<T>,
}
impl<T: RequestHost> Default for Resolver<T> {
2018-09-11 04:16:46 +02:00
fn default() -> Self {
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
(cfg, opts)
} else {
(ResolverConfig::default(), ResolverOpts::default())
};
Resolver::new(cfg, opts)
}
}
impl<T: RequestHost> Resolver<T> {
/// Create new resolver instance with custom configuration and options.
2018-09-11 04:16:46 +02:00
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
let (resolver, bg) = AsyncResolver::new(cfg, opts);
2018-12-11 03:08:07 +01:00
actix_rt::Arbiter::spawn(bg);
2018-09-11 04:16:46 +02:00
Resolver {
resolver,
req: PhantomData,
}
}
2018-09-11 04:39:55 +02:00
/// Change type of resolver request.
pub fn into_request<T2: RequestHost>(&self) -> Resolver<T2> {
2018-09-11 04:39:55 +02:00
Resolver {
resolver: self.resolver.clone(),
req: PhantomData,
}
}
2018-09-11 04:16:46 +02:00
}
impl<T> Clone for Resolver<T> {
fn clone(&self) -> Self {
Resolver {
resolver: self.resolver.clone(),
req: PhantomData,
}
}
}
2018-11-30 03:56:15 +01:00
impl<T: RequestHost> Service<T> for Resolver<T> {
type Response = (T, VecDeque<IpAddr>);
2018-10-30 04:29:47 +01:00
type Error = ResolveError;
2018-09-11 04:16:46 +02:00
type Future = ResolverFuture<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2018-11-30 03:56:15 +01:00
fn call(&mut self, req: T) -> Self::Future {
2018-12-11 03:08:07 +01:00
if let Ok(ip) = req.host().parse() {
let mut addrs = VecDeque::new();
addrs.push_back(ip);
ResolverFuture::new(req, &self.resolver, Some(addrs))
} else {
ResolverFuture::new(req, &self.resolver, None)
}
2018-09-11 04:16:46 +02:00
}
}
#[doc(hidden)]
/// Resolver future
pub struct ResolverFuture<T> {
2018-09-11 04:39:55 +02:00
req: Option<T>,
2018-09-11 04:16:46 +02:00
lookup: Option<Background<LookupIpFuture>>,
addrs: Option<VecDeque<IpAddr>>,
2018-09-11 04:16:46 +02:00
}
impl<T: RequestHost> ResolverFuture<T> {
2018-12-11 03:08:07 +01:00
pub fn new(addr: T, resolver: &AsyncResolver, addrs: Option<VecDeque<IpAddr>>) -> Self {
2018-09-11 04:16:46 +02:00
// we need to do dns resolution
2018-10-30 04:29:47 +01:00
let lookup = Some(resolver.lookup_ip(addr.host()));
ResolverFuture {
lookup,
2018-12-11 03:08:07 +01:00
addrs,
2018-10-30 04:29:47 +01:00
req: Some(addr),
2018-09-11 04:16:46 +02:00
}
}
}
impl<T: RequestHost> Future for ResolverFuture<T> {
type Item = (T, VecDeque<IpAddr>);
2018-10-30 04:29:47 +01:00
type Error = ResolveError;
2018-09-11 04:16:46 +02:00
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
2018-10-30 04:29:47 +01:00
if let Some(addrs) = self.addrs.take() {
Ok(Async::Ready((self.req.take().unwrap(), addrs)))
2018-09-11 04:16:46 +02:00
} else {
match self.lookup.as_mut().unwrap().poll() {
Ok(Async::NotReady) => Ok(Async::NotReady),
Ok(Async::Ready(ips)) => Ok(Async::Ready((
self.req.take().unwrap(),
ips.iter().collect(),
))),
2018-10-30 04:29:47 +01:00
Err(err) => Err(err),
2018-09-11 04:16:46 +02:00
}
}
}
}