Skip to content

Commit

Permalink
Migrate to ntex-service 1.2 (#209)
Browse files Browse the repository at this point in the history
* Migrate to ntex-service 1.2
  • Loading branch information
fafhrd91 committed Jun 15, 2023
1 parent 627b08e commit ea28b26
Show file tree
Hide file tree
Showing 84 changed files with 678 additions and 823 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ ntex-io = { path = "ntex-io" }
ntex-http = { path = "ntex-http" }
ntex-router = { path = "ntex-router" }
ntex-rt = { path = "ntex-rt" }
#ntex-service = { path = "ntex-service" }
ntex-service = { path = "ntex-service" }
ntex-tls = { path = "ntex-tls" }
ntex-macros = { path = "ntex-macros" }
ntex-util = { path = "ntex-util" }

ntex-glommio = { path = "ntex-glommio" }
ntex-tokio = { path = "ntex-tokio" }
ntex-async-std = { path = "ntex-async-std" }

ntex-h2 = { git = "https://github.com/ntex-rs/ntex-h2.git", branch = "0-3" }
6 changes: 3 additions & 3 deletions ntex-async-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-async-std"
version = "0.2.2"
version = "0.3.0"
authors = ["ntex contributors <team@ntex.rs>"]
description = "async-std intergration for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -17,8 +17,8 @@ path = "src/lib.rs"

[dependencies]
ntex-bytes = "0.1.19"
ntex-io = "0.2.4"
ntex-util = "0.2.0"
ntex-io = "0.3.0"
ntex-util = "0.3.0"
async-oneshot = "0.5.0"
log = "0.4"
pin-project-lite = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion ntex-bytes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ simdutf8 = { version = "0.1.4", optional = true }
[dev-dependencies]
serde_test = "1.0"
serde_json = "1.0"
ntex = { version = "0.6.0", features = ["tokio"] }
ntex = { version = "0.7.0", features = ["tokio"] }
22 changes: 11 additions & 11 deletions ntex-connect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-connect"
version = "0.2.1"
version = "0.3.0"
authors = ["ntex contributors <team@ntex.rs>"]
description = "ntexwork connect utils for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand Down Expand Up @@ -34,28 +34,28 @@ glommio = ["ntex-rt/glommio", "ntex-glommio"]
async-std = ["ntex-rt/async-std", "ntex-async-std"]

[dependencies]
ntex-service = "1.0.0"
ntex-service = "1.2.0"
ntex-bytes = "0.1.19"
ntex-http = "0.1.8"
ntex-io = "0.2.1"
ntex-io = "0.3.0"
ntex-rt = "0.4.7"
ntex-tls = "0.2.1"
ntex-util = "0.2.0"
ntex-tls = "0.3.0"
ntex-util = "0.3.0"
log = "0.4"
thiserror = "1.0"

ntex-tokio = { version = "0.2.3", optional = true }
ntex-glommio = { version = "0.2.4", optional = true }
ntex-async-std = { version = "0.2.2", optional = true }
ntex-tokio = { version = "0.3.0", optional = true }
ntex-glommio = { version = "0.3.0", optional = true }
ntex-async-std = { version = "0.3.0", optional = true }

# openssl
tls-openssl = { version="0.10", package = "openssl", optional = true }

# rustls
tls-rustls = { version = "0.20", package = "rustls", optional = true }
webpki-roots = { version = "0.22", optional = true }
tls-rustls = { version = "0.21", package = "rustls", optional = true }
webpki-roots = { version = "0.23", optional = true }

[dev-dependencies]
rand = "0.8"
env_logger = "0.10"
ntex = { version = "0.6.0", features = ["tokio"] }
ntex = { version = "0.7.0", features = ["tokio"] }
4 changes: 2 additions & 2 deletions ntex-connect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ pub use self::resolve::Resolver;
pub use self::service::Connector;

use ntex_io::Io;
use ntex_service::Service;

/// Resolve and connect to remote host
pub async fn connect<T, U>(message: U) -> Result<Io, ConnectError>
where
T: Address,
Connect<T>: From<U>,
{
service::ConnectServiceResponse::new(Resolver::new().call(message.into())).await
service::ConnectServiceResponse::new(Box::pin(Resolver::new().lookup(message.into())))
.await
}

#[allow(unused_imports)]
Expand Down
25 changes: 16 additions & 9 deletions ntex-connect/src/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ pub use tls_openssl::ssl::{Error as SslError, HandshakeError, SslConnector, SslM

use ntex_bytes::PoolId;
use ntex_io::{FilterFactory, Io, Layer};
use ntex_service::{Service, ServiceFactory};
use ntex_service::{Container, Ctx, Service, ServiceFactory};
use ntex_tls::openssl::SslConnector as IoSslConnector;
use ntex_util::future::{BoxFuture, Ready};

use super::{Address, Connect, ConnectError, Connector as BaseConnector};

pub struct Connector<T> {
connector: BaseConnector<T>,
connector: Container<BaseConnector<T>>,
openssl: SslConnector,
}

impl<T> Connector<T> {
impl<T: Address> Connector<T> {
/// Construct new OpensslConnectService factory
pub fn new(connector: SslConnector) -> Self {
Connector {
connector: BaseConnector::default(),
connector: BaseConnector::default().into(),
openssl: connector,
}
}
Expand All @@ -30,8 +30,15 @@ impl<T> Connector<T> {
/// Use specified memory pool for memory allocations. By default P0
/// memory pool is used.
pub fn memory_pool(self, id: PoolId) -> Self {
let connector = self
.connector
.into_service()
.expect("Connector has been cloned")
.memory_pool(id)
.into();

Self {
connector: self.connector.memory_pool(id),
connector,
openssl: self.openssl,
}
}
Expand Down Expand Up @@ -100,15 +107,15 @@ impl<T: Address> Service<Connect<T>> for Connector<T> {
type Future<'f> = BoxFuture<'f, Result<Self::Response, Self::Error>>;

#[inline]
fn call(&self, req: Connect<T>) -> Self::Future<'_> {
fn call<'a>(&'a self, req: Connect<T>, _: Ctx<'a, Self>) -> Self::Future<'a> {
Box::pin(self.connect(req))
}
}

#[cfg(test)]
mod tests {
use super::*;
use ntex_service::{Service, ServiceFactory};
use ntex_service::ServiceFactory;

#[ntex::test]
async fn test_openssl_connect() {
Expand All @@ -117,9 +124,9 @@ mod tests {
});

let ssl = SslConnector::builder(SslMethod::tls()).unwrap();
let factory = Connector::new(ssl.build()).clone().memory_pool(PoolId::P5);
let factory = Connector::new(ssl.build()).memory_pool(PoolId::P5).clone();

let srv = factory.create(&()).await.unwrap();
let srv = factory.container(&()).await.unwrap();
let result = srv
.call(Connect::new("").set_addr(Some(server.addr())))
.await;
Expand Down
6 changes: 3 additions & 3 deletions ntex-connect/src/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{fmt, io, marker, net};

use ntex_rt::spawn_blocking;
use ntex_service::{Service, ServiceFactory};
use ntex_service::{Ctx, Service, ServiceFactory};
use ntex_util::future::{BoxFuture, Either, Ready};

use crate::{Address, Connect, ConnectError};
Expand Down Expand Up @@ -115,7 +115,7 @@ impl<T: Address> Service<Connect<T>> for Resolver<T> {
type Future<'f> = BoxFuture<'f, Result<Connect<T>, Self::Error>>;

#[inline]
fn call(&self, req: Connect<T>) -> Self::Future<'_> {
fn call<'a>(&'a self, req: Connect<T>, _: Ctx<'a, Self>) -> Self::Future<'_> {
Box::pin(self.lookup(req))
}
}
Expand All @@ -129,7 +129,7 @@ mod tests {
async fn resolver() {
let resolver = Resolver::default().clone();
assert!(format!("{:?}", resolver).contains("Resolver"));
let srv = resolver.create(()).await.unwrap();
let srv = resolver.container(()).await.unwrap();
assert!(lazy(|cx| srv.poll_ready(cx)).await.is_ready());

let res = srv.call(Connect::new("www.rust-lang.org")).await;
Expand Down
30 changes: 19 additions & 11 deletions ntex-connect/src/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ pub use tls_rustls::{ClientConfig, ServerName};

use ntex_bytes::PoolId;
use ntex_io::{FilterFactory, Io, Layer};
use ntex_service::{Service, ServiceFactory};
use ntex_service::{Container, Ctx, Service, ServiceFactory};
use ntex_tls::rustls::TlsConnector;
use ntex_util::future::{BoxFuture, Ready};

use super::{Address, Connect, ConnectError, Connector as BaseConnector};

/// Rustls connector factory
pub struct Connector<T> {
connector: BaseConnector<T>,
connector: Container<BaseConnector<T>>,
inner: TlsConnector,
}

impl<T> From<std::sync::Arc<ClientConfig>> for Connector<T> {
impl<T: Address> From<std::sync::Arc<ClientConfig>> for Connector<T> {
fn from(cfg: std::sync::Arc<ClientConfig>) -> Self {
Connector {
inner: TlsConnector::new(cfg),
connector: BaseConnector::default(),
connector: BaseConnector::default().into(),
}
}
}

impl<T> Connector<T> {
impl<T: Address> Connector<T> {
pub fn new(config: ClientConfig) -> Self {
Connector {
inner: TlsConnector::new(std::sync::Arc::new(config)),
connector: BaseConnector::default(),
connector: BaseConnector::default().into(),
}
}

Expand All @@ -39,8 +39,14 @@ impl<T> Connector<T> {
/// Use specified memory pool for memory allocations. By default P0
/// memory pool is used.
pub fn memory_pool(self, id: PoolId) -> Self {
let connector = self
.connector
.into_service()
.unwrap()
.memory_pool(id)
.into();
Self {
connector: self.connector.memory_pool(id),
connector,
inner: self.inner,
}
}
Expand Down Expand Up @@ -104,7 +110,7 @@ impl<T: Address> Service<Connect<T>> for Connector<T> {
type Error = ConnectError;
type Future<'f> = BoxFuture<'f, Result<Self::Response, Self::Error>>;

fn call(&self, req: Connect<T>) -> Self::Future<'_> {
fn call<'a>(&'a self, req: Connect<T>, _: Ctx<'a, Self>) -> Self::Future<'a> {
Box::pin(self.connect(req))
}
}
Expand All @@ -115,7 +121,7 @@ mod tests {
use tls_rustls::{OwnedTrustAnchor, RootCertStore};

use super::*;
use ntex_service::{Service, ServiceFactory};
use ntex_service::ServiceFactory;
use ntex_util::future::lazy;

#[ntex::test]
Expand All @@ -139,9 +145,11 @@ mod tests {
.with_root_certificates(cert_store)
.with_no_client_auth();
let _ = Connector::<&'static str>::new(config.clone()).clone();
let factory = Connector::from(Arc::new(config)).memory_pool(PoolId::P5);
let factory = Connector::from(Arc::new(config))
.memory_pool(PoolId::P5)
.clone();

let srv = factory.create(&()).await.unwrap();
let srv = factory.container(&()).await.unwrap();
// always ready
assert!(lazy(|cx| srv.poll_ready(cx)).await.is_ready());
let result = srv
Expand Down
12 changes: 6 additions & 6 deletions ntex-connect/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::VecDeque, future::Future, io, net::SocketAddr, pin::Pin};

use ntex_bytes::{PoolId, PoolRef};
use ntex_io::{types, Io};
use ntex_service::{Service, ServiceFactory};
use ntex_service::{Ctx, Service, ServiceFactory};
use ntex_util::future::{BoxFuture, Either, Ready};

use crate::{net::tcp_connect_in, Address, Connect, ConnectError, Resolver};
Expand Down Expand Up @@ -39,7 +39,7 @@ impl<T: Address> Connector<T> {
Connect<T>: From<U>,
{
ConnectServiceResponse {
state: ConnectState::Resolve(self.resolver.call(message.into())),
state: ConnectState::Resolve(Box::pin(self.resolver.lookup(message.into()))),
pool: self.pool,
}
.await
Expand Down Expand Up @@ -80,13 +80,13 @@ impl<T: Address> Service<Connect<T>> for Connector<T> {
type Future<'f> = ConnectServiceResponse<'f, T>;

#[inline]
fn call(&self, req: Connect<T>) -> Self::Future<'_> {
ConnectServiceResponse::new(self.resolver.call(req))
fn call<'a>(&'a self, req: Connect<T>, _: Ctx<'a, Self>) -> Self::Future<'a> {
ConnectServiceResponse::new(Box::pin(self.resolver.lookup(req)))
}
}

enum ConnectState<'f, T: Address> {
Resolve(<Resolver<T> as Service<Connect<T>>>::Future<'f>),
Resolve(BoxFuture<'f, Result<Connect<T>, ConnectError>>),
Connect(TcpConnectorResponse<T>),
}

Expand All @@ -97,7 +97,7 @@ pub struct ConnectServiceResponse<'f, T: Address> {
}

impl<'f, T: Address> ConnectServiceResponse<'f, T> {
pub(super) fn new(fut: <Resolver<T> as Service<Connect<T>>>::Future<'f>) -> Self {
pub(super) fn new(fut: BoxFuture<'f, Result<Connect<T>, ConnectError>>) -> Self {
Self {
state: ConnectState::Resolve(fut),
pool: PoolId::P0.pool_ref(),
Expand Down
6 changes: 3 additions & 3 deletions ntex-glommio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-glommio"
version = "0.2.4"
version = "0.3.0"
authors = ["ntex contributors <team@ntex.rs>"]
description = "glommio intergration for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand All @@ -17,8 +17,8 @@ path = "src/lib.rs"

[dependencies]
ntex-bytes = "0.1.19"
ntex-io = "0.2.4"
ntex-util = "0.2.0"
ntex-io = "0.3.0"
ntex-util = "0.3.0"
async-oneshot = "0.5.0"
futures-lite = "1.12"
log = "0.4"
Expand Down
6 changes: 2 additions & 4 deletions ntex-http/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(
clippy::derive_hash_xor_eq,
clippy::should_implement_trait,
clippy::no_effect,
clippy::missing_safety_doc
Expand All @@ -8,6 +7,7 @@ use std::{cmp, error::Error, fmt, str, str::FromStr};

use ntex_bytes::{ByteString, Bytes};

#[allow(clippy::derived_hash_with_manual_eq)]
/// Represents an HTTP header field value.
///
/// In practice, HTTP header field values are usually valid ASCII. However, the
Expand All @@ -17,7 +17,7 @@ use ntex_bytes::{ByteString, Bytes};
/// To handle this, the `HeaderValue` is useable as a type and can be compared
/// with strings and implements `Debug`. A `to_str` fn is provided that returns
/// an `Err` if the header value contains non visible ascii characters.
#[derive(Clone, Hash)]
#[derive(Clone, Hash, Eq)]
pub struct HeaderValue {
inner: Bytes,
is_sensitive: bool,
Expand Down Expand Up @@ -542,8 +542,6 @@ impl PartialEq for HeaderValue {
}
}

impl Eq for HeaderValue {}

impl PartialOrd for HeaderValue {
#[inline]
fn partial_cmp(&self, other: &HeaderValue) -> Option<cmp::Ordering> {
Expand Down
Loading

0 comments on commit ea28b26

Please sign in to comment.