From d35c87d228420f4289ec250af1fa726bdf95bfbd Mon Sep 17 00:00:00 2001
From: Nikolay Kim <fafhrd91@gmail.com>
Date: Mon, 10 Dec 2018 16:16:40 -0800
Subject: [PATCH] move helper services to separate package

---
 Cargo.toml                            |  57 +-------------
 actix-connector/src/lib.rs            |   1 +
 actix-connector/src/ssl/mod.rs        |   6 ++
 actix-connector/src/ssl/openssl.rs    | 106 ++++++++++++++++++++++++++
 actix-utils/CHANGES.md                |   5 ++
 actix-utils/Cargo.toml                |  34 +++++++++
 {src => actix-utils/src}/cell.rs      |   0
 {src => actix-utils/src}/cloneable.rs |   0
 {src => actix-utils/src}/counter.rs   |   0
 {src => actix-utils/src}/either.rs    |   0
 {src => actix-utils/src}/framed.rs    |   3 +-
 {src => actix-utils/src}/inflight.rs  |   0
 {src => actix-utils/src}/keepalive.rs |   0
 {src => actix-utils/src}/lib.rs       |   0
 {src => actix-utils/src}/stream.rs    |   0
 {src => actix-utils/src}/time.rs      |   0
 {src => actix-utils/src}/timeout.rs   |   0
 17 files changed, 154 insertions(+), 58 deletions(-)
 create mode 100644 actix-connector/src/ssl/mod.rs
 create mode 100644 actix-connector/src/ssl/openssl.rs
 create mode 100644 actix-utils/CHANGES.md
 create mode 100644 actix-utils/Cargo.toml
 rename {src => actix-utils/src}/cell.rs (100%)
 rename {src => actix-utils/src}/cloneable.rs (100%)
 rename {src => actix-utils/src}/counter.rs (100%)
 rename {src => actix-utils/src}/either.rs (100%)
 rename {src => actix-utils/src}/framed.rs (99%)
 rename {src => actix-utils/src}/inflight.rs (100%)
 rename {src => actix-utils/src}/keepalive.rs (100%)
 rename {src => actix-utils/src}/lib.rs (100%)
 rename {src => actix-utils/src}/stream.rs (100%)
 rename {src => actix-utils/src}/time.rs (100%)
 rename {src => actix-utils/src}/timeout.rs (100%)

diff --git a/Cargo.toml b/Cargo.toml
index 1f8bf448..a327a7a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,65 +15,10 @@ edition = "2018"
 
 [workspace]
 members = [
-  "./",
   "actix-codec",
   "actix-connector",
   "actix-service",
   "actix-server",
   "actix-rt",
+  "actix-utils",
 ]
-
-[package.metadata.docs.rs]
-features = ["ssl"]
-
-[badges]
-travis-ci = { repository = "actix/actix-net", branch = "master" }
-# appveyor = { repository = "fafhrd91/actix-web-hdy9d" }
-codecov = { repository = "actix/actix-net", branch = "master", service = "github" }
-
-[lib]
-name = "actix_net"
-path = "src/lib.rs"
-
-[features]
-default = []
-
-# openssl
-ssl = ["openssl", "tokio-openssl"]
-
-cell = []
-
-[dependencies]
-actix-service = "0.1.1"
-actix-codec = { path = "actix-codec" }
-actix-rt = { path = "actix-rt" }
-
-log = "0.4"
-num_cpus = "1.0"
-
-# io
-mio = "^0.6.13"
-net2 = "0.2"
-bytes = "0.4"
-futures = "0.1"
-slab = "0.4"
-tokio-io = "0.1"
-tokio-tcp = "0.1"
-tokio-timer = "0.2"
-tokio-reactor = "0.1"
-tokio-signal = "0.2"
-
-trust-dns-proto = "^0.5.0"
-trust-dns-resolver = "^0.10.0"
-
-# openssl
-openssl = { version="0.10", optional = true }
-tokio-openssl = { version="0.3", optional = true }
-
-[dev-dependencies]
-env_logger = "0.5"
-
-[profile.release]
-lto = true
-opt-level = 3
-codegen-units = 1
diff --git a/actix-connector/src/lib.rs b/actix-connector/src/lib.rs
index a364dfcf..be92d5e7 100644
--- a/actix-connector/src/lib.rs
+++ b/actix-connector/src/lib.rs
@@ -8,6 +8,7 @@
 
 mod connector;
 mod resolver;
+pub mod ssl;
 
 pub use self::connector::{
     Connect, Connector, ConnectorError, DefaultConnector, RequestPort, TcpConnector,
diff --git a/actix-connector/src/ssl/mod.rs b/actix-connector/src/ssl/mod.rs
new file mode 100644
index 00000000..ecbd0a88
--- /dev/null
+++ b/actix-connector/src/ssl/mod.rs
@@ -0,0 +1,6 @@
+//! SSL Services
+
+#[cfg(feature = "ssl")]
+mod openssl;
+#[cfg(feature = "ssl")]
+pub use self::openssl::OpensslConnector;
diff --git a/actix-connector/src/ssl/openssl.rs b/actix-connector/src/ssl/openssl.rs
new file mode 100644
index 00000000..71292d0e
--- /dev/null
+++ b/actix-connector/src/ssl/openssl.rs
@@ -0,0 +1,106 @@
+use std::marker::PhantomData;
+
+use actix_codec::{AsyncRead, AsyncWrite};
+use actix_service::{NewService, Service};
+use futures::{future::ok, future::FutureResult, Async, Future, Poll};
+use openssl::ssl::{HandshakeError, SslConnector};
+use tokio_openssl::{ConnectAsync, SslConnectorExt, SslStream};
+
+use crate::resolver::RequestHost;
+
+/// Openssl connector factory
+pub struct OpensslConnector<R, T, E> {
+    connector: SslConnector,
+    _t: PhantomData<(R, T, E)>,
+}
+
+impl<R, T, E> OpensslConnector<R, T, E> {
+    pub fn new(connector: SslConnector) -> Self {
+        OpensslConnector {
+            connector,
+            _t: PhantomData,
+        }
+    }
+}
+
+impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
+    pub fn service(
+        connector: SslConnector,
+    ) -> impl Service<(R, T), Response = (R, SslStream<T>), Error = HandshakeError<T>> {
+        OpensslConnectorService {
+            connector: connector,
+            _t: PhantomData,
+        }
+    }
+}
+
+impl<R, T, E> Clone for OpensslConnector<R, T, E> {
+    fn clone(&self) -> Self {
+        Self {
+            connector: self.connector.clone(),
+            _t: PhantomData,
+        }
+    }
+}
+
+impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService<(R, T)>
+    for OpensslConnector<R, T, E>
+{
+    type Response = (R, SslStream<T>);
+    type Error = HandshakeError<T>;
+    type Service = OpensslConnectorService<R, T>;
+    type InitError = E;
+    type Future = FutureResult<Self::Service, Self::InitError>;
+
+    fn new_service(&self) -> Self::Future {
+        ok(OpensslConnectorService {
+            connector: self.connector.clone(),
+            _t: PhantomData,
+        })
+    }
+}
+
+pub struct OpensslConnectorService<R, T> {
+    connector: SslConnector,
+    _t: PhantomData<(R, T)>,
+}
+
+impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service<(R, T)>
+    for OpensslConnectorService<R, T>
+{
+    type Response = (R, SslStream<T>);
+    type Error = HandshakeError<T>;
+    type Future = ConnectAsyncExt<R, T>;
+
+    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
+        Ok(Async::Ready(()))
+    }
+
+    fn call(&mut self, (req, stream): (R, T)) -> Self::Future {
+        ConnectAsyncExt {
+            fut: SslConnectorExt::connect_async(&self.connector, req.host(), stream),
+            req: Some(req),
+        }
+    }
+}
+
+pub struct ConnectAsyncExt<R, T> {
+    req: Option<R>,
+    fut: ConnectAsync<T>,
+}
+
+impl<R, T> Future for ConnectAsyncExt<R, T>
+where
+    R: RequestHost,
+    T: AsyncRead + AsyncWrite,
+{
+    type Item = (R, SslStream<T>);
+    type Error = HandshakeError<T>;
+
+    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
+        match self.fut.poll()? {
+            Async::Ready(stream) => Ok(Async::Ready((self.req.take().unwrap(), stream))),
+            Async::NotReady => Ok(Async::NotReady),
+        }
+    }
+}
diff --git a/actix-utils/CHANGES.md b/actix-utils/CHANGES.md
new file mode 100644
index 00000000..b859a7dc
--- /dev/null
+++ b/actix-utils/CHANGES.md
@@ -0,0 +1,5 @@
+# Changes
+
+## [0.1.0] - 2018-12-09
+
+* Move utils services to separate crate
diff --git a/actix-utils/Cargo.toml b/actix-utils/Cargo.toml
new file mode 100644
index 00000000..52620579
--- /dev/null
+++ b/actix-utils/Cargo.toml
@@ -0,0 +1,34 @@
+[package]
+name = "actix-utils"
+version = "0.1.0"
+authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
+description = "Actix utils - various actix net related services"
+readme = "README.md"
+keywords = ["network", "framework", "async", "futures"]
+homepage = "https://actix.rs"
+repository = "https://github.com/actix/actix-net.git"
+documentation = "https://docs.rs/actix-utils/"
+categories = ["network-programming", "asynchronous"]
+license = "MIT/Apache-2.0"
+exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
+edition = "2018"
+workspace = "../"
+
+[lib]
+name = "actix_utils"
+path = "src/lib.rs"
+
+[dependencies]
+actix-service = "0.1.1"
+actix-codec = { path = "../actix-codec" }
+actix-rt = { path = "../actix-rt" }
+
+# io
+bytes = "0.4"
+futures = "0.1"
+tokio-timer = "0.2.8"
+
+[profile.release]
+lto = true
+opt-level = 3
+codegen-units = 1
diff --git a/src/cell.rs b/actix-utils/src/cell.rs
similarity index 100%
rename from src/cell.rs
rename to actix-utils/src/cell.rs
diff --git a/src/cloneable.rs b/actix-utils/src/cloneable.rs
similarity index 100%
rename from src/cloneable.rs
rename to actix-utils/src/cloneable.rs
diff --git a/src/counter.rs b/actix-utils/src/counter.rs
similarity index 100%
rename from src/counter.rs
rename to actix-utils/src/counter.rs
diff --git a/src/either.rs b/actix-utils/src/either.rs
similarity index 100%
rename from src/either.rs
rename to actix-utils/src/either.rs
diff --git a/src/framed.rs b/actix-utils/src/framed.rs
similarity index 99%
rename from src/framed.rs
rename to actix-utils/src/framed.rs
index 42e46ed6..bb6e9c13 100644
--- a/src/framed.rs
+++ b/actix-utils/src/framed.rs
@@ -2,13 +2,12 @@
 use std::marker::PhantomData;
 use std::mem;
 
-use actix_codec::{Decoder, Encoder, Framed};
+use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
 use actix_rt::Arbiter;
 use actix_service::{IntoNewService, IntoService, NewService, Service};
 use futures::future::{ok, FutureResult};
 use futures::unsync::mpsc;
 use futures::{Async, AsyncSink, Future, Poll, Sink, Stream};
-use tokio_io::{AsyncRead, AsyncWrite};
 
 type Request<U> = <U as Decoder>::Item;
 type Response<U> = <U as Encoder>::Item;
diff --git a/src/inflight.rs b/actix-utils/src/inflight.rs
similarity index 100%
rename from src/inflight.rs
rename to actix-utils/src/inflight.rs
diff --git a/src/keepalive.rs b/actix-utils/src/keepalive.rs
similarity index 100%
rename from src/keepalive.rs
rename to actix-utils/src/keepalive.rs
diff --git a/src/lib.rs b/actix-utils/src/lib.rs
similarity index 100%
rename from src/lib.rs
rename to actix-utils/src/lib.rs
diff --git a/src/stream.rs b/actix-utils/src/stream.rs
similarity index 100%
rename from src/stream.rs
rename to actix-utils/src/stream.rs
diff --git a/src/time.rs b/actix-utils/src/time.rs
similarity index 100%
rename from src/time.rs
rename to actix-utils/src/time.rs
diff --git a/src/timeout.rs b/actix-utils/src/timeout.rs
similarity index 100%
rename from src/timeout.rs
rename to actix-utils/src/timeout.rs