diff --git a/Cargo.toml b/Cargo.toml index 439c72196..874084a3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ tiny_http = "0.6.0" url = "2" threadpool = "1" num_cpus = "1" +anysocket="0.1" [dev-dependencies] postgres = { version = "0.15.2", default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 78b0cfa93..054dadee2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,6 +72,7 @@ pub extern crate url; pub extern crate percent_encoding; extern crate threadpool; extern crate num_cpus; +extern crate anysocket; // https://github.com/servo/rust-url/blob/e121d8d0aafd50247de5f5310a227ecb1efe6ffe/percent_encoding/lib.rs#L126 pub const DEFAULT_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS @@ -90,8 +91,6 @@ use std::io::Cursor; use std::io::Result as IoResult; use std::io::Read; use std::marker::PhantomData; -use std::net::SocketAddr; -use std::net::ToSocketAddrs; use std::panic; use std::panic::AssertUnwindSafe; use std::slice::Iter as SliceIter; @@ -101,6 +100,8 @@ use std::sync::mpsc; use std::thread; use std::fmt; +use anysocket::AbstractToSocketAddrs; +use anysocket::AbstractAddr; pub mod cgi; pub mod content_encoding; @@ -214,7 +215,7 @@ macro_rules! assert_or_400 { /// /// If you need to handle these situations, please see `Server`. pub fn start_server(addr: A, handler: F) -> ! - where A: ToSocketAddrs, + where A: AbstractToSocketAddrs, F: Send + Sync + 'static + Fn(&Request) -> Response { Server::new(addr, handler).expect("Failed to start server").run(); @@ -227,7 +228,7 @@ pub fn start_server(addr: A, handler: F) -> ! /// When `pool_size` is `None`, the thread pool size will default to `8 * num-cpus`. /// `pool_size` must be greater than zero or this function will panic. pub fn start_server_with_pool(addr: A, pool_size: Option, handler: F) -> ! - where A: ToSocketAddrs, + where A: AbstractToSocketAddrs, F: Send + Sync + 'static + Fn(&Request) -> Response { Server::new(addr, handler).expect("Failed to start server") @@ -237,7 +238,7 @@ pub fn start_server_with_pool(addr: A, pool_size: Option, handler: } -/// Executes a function in either a thread of a thread pool +/// Executes a function in either a thread or a thread pool enum Executor { Threaded, Pooled { @@ -296,7 +297,7 @@ impl Server where F: Send + Sync + 'static + Fn(&Request) -> Response { /// Returns an error if there was an error while creating the listening socket, for example if /// the port is already in use. pub fn new(addr: A, handler: F) -> Result, Box> - where A: ToSocketAddrs + where A: AbstractToSocketAddrs { let server = try!(tiny_http::Server::http(addr)); Ok(Server { @@ -318,7 +319,7 @@ impl Server where F: Send + Sync + 'static + Fn(&Request) -> Response { handler: F, certificate: Vec, private_key: Vec, - ) -> Result, Box> where A: ToSocketAddrs { + ) -> Result, Box> where A: AbstractToSocketAddrs { let ssl_config = tiny_http::SslConfig { certificate, private_key, @@ -341,7 +342,7 @@ impl Server where F: Send + Sync + 'static + Fn(&Request) -> Response { /// Returns the address of the listening socket. #[inline] - pub fn server_addr(&self) -> SocketAddr { + pub fn server_addr(&self) -> AbstractAddr { self.server.server_addr() } @@ -457,7 +458,7 @@ impl Server where F: Send + Sync + 'static + Fn(&Request) -> Response { let url = request.url().to_owned(); let method = request.method().as_str().to_owned(); let headers = request.headers().iter().map(|h| (h.field.to_string(), h.value.clone().into())).collect(); - let remote_addr = *request.remote_addr(); + let remote_addr = request.remote_addr().clone(); tiny_http_request = Arc::new(Mutex::new(Some(request))); let data = Arc::new(Mutex::new(Some(Box::new(RequestRead(tiny_http_request.clone())) as Box<_>))); @@ -549,7 +550,7 @@ pub struct Request { headers: Vec<(String, String)>, https: bool, data: Arc>>>, - remote_addr: SocketAddr, + remote_addr: AbstractAddr, } impl fmt::Debug for Request { @@ -573,7 +574,7 @@ impl Request { -> Request where U: Into, M: Into { let data = Arc::new(Mutex::new(Some(Box::new(Cursor::new(data)) as Box<_>))); - let remote_addr = "127.0.0.1:12345".parse().unwrap(); + let remote_addr = "127.0.0.1:12345".parse::().unwrap().into(); Request { url: url.into(), @@ -586,7 +587,7 @@ impl Request { } /// Builds a fake HTTP request to be used during tests. - pub fn fake_http_from(from: SocketAddr, method: M, url: U, + pub fn fake_http_from(from: AbstractAddr, method: M, url: U, headers: Vec<(String, String)>, data: Vec) -> Request where U: Into, M: Into { @@ -610,7 +611,7 @@ impl Request { -> Request where U: Into, M: Into { let data = Arc::new(Mutex::new(Some(Box::new(Cursor::new(data)) as Box<_>))); - let remote_addr = "127.0.0.1:12345".parse().unwrap(); + let remote_addr = "127.0.0.1:12345".parse::().unwrap().into(); Request { url: url.into(), @@ -623,7 +624,7 @@ impl Request { } /// Builds a fake HTTPS request to be used during tests. - pub fn fake_https_from(from: SocketAddr, method: M, url: U, + pub fn fake_https_from(from: AbstractAddr, method: M, url: U, headers: Vec<(String, String)>, data: Vec) -> Request where U: Into, M: Into { @@ -669,7 +670,7 @@ impl Request { headers: self.headers.clone(), // TODO: expensive https: self.https, data: self.data.clone(), - remote_addr: self.remote_addr, + remote_addr: self.remote_addr.clone(), }) } @@ -867,7 +868,7 @@ impl Request { /// } /// ``` #[inline] - pub fn remote_addr(&self) -> &SocketAddr { + pub fn remote_addr(&self) -> &AbstractAddr { &self.remote_addr } }