Skip to content

Commit

Permalink
chore: add niklas suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
oleonardolima committed Jul 1, 2024
1 parent db13afa commit 5756e62
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 62 deletions.
1 change: 0 additions & 1 deletion client/transport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ futures-channel = { version = "0.3.14", default-features = false, optional = tru
[features]
tls = ["rustls", "tokio-rustls", "rustls-pki-types"]
tls-rustls-platform-verifier = ["tls", "rustls-platform-verifier"]
default = ["tls-rustls-platform-verifier"]

ws = [
"base64",
Expand Down
22 changes: 8 additions & 14 deletions client/transport/src/ws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub type CustomCertStore = rustls::ClientConfig;
#[cfg(feature = "tls")]
#[derive(Debug, Clone)]
pub enum CertificateStore {
#[cfg(feature = "tls-rustls-platform-verifier")]
/// Native.
Native,
/// Custom certificate store.
Expand Down Expand Up @@ -102,9 +101,9 @@ pub struct WsTransportClientBuilder {
}

impl Default for WsTransportClientBuilder {
#[cfg(feature = "tls-rustls-platform-verifier")]
fn default() -> Self {
Self {
#[cfg(feature = "tls")]
certificate_store: CertificateStore::Native,
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
Expand All @@ -114,18 +113,6 @@ impl Default for WsTransportClientBuilder {
tcp_no_delay: true,
}
}

#[cfg(not(feature = "tls"))]
fn default() -> Self {
Self {
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
connection_timeout: Duration::from_secs(10),
headers: http::HeaderMap::new(),
max_redirections: 5,
tcp_no_delay: true,
}
}
}

impl WsTransportClientBuilder {
Expand Down Expand Up @@ -629,6 +616,13 @@ fn build_tls_config(cert_store: &CertificateStore) -> Result<tokio_rustls::TlsCo
let config = match cert_store {
#[cfg(feature = "tls-rustls-platform-verifier")]
CertificateStore::Native => rustls_platform_verifier::tls_config(),
#[cfg(not(feature = "tls-rustls-platform-verifier"))]
CertificateStore::Native => {
return Err(WsHandshakeError::CertificateStore(io::Error::new(
io::ErrorKind::Other,
"Native certificate store not supported, either call `Builder::with_custom_cert_store` or enable the `tls-rustls-platform-verifier` feature.",
)))
}
CertificateStore::Custom(cfg) => cfg.clone(),
};

Expand Down
4 changes: 2 additions & 2 deletions client/ws-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish = true
[dependencies]
http = "1"
jsonrpsee-types = { workspace = true }
jsonrpsee-client-transport = { workspace = true, features = ["ws"], default-features = false }
jsonrpsee-client-transport = { workspace = true, features = ["ws"] }
jsonrpsee-core = { workspace = true, features = ["async-client"] }
url = "2.4.0"

Expand All @@ -30,7 +30,7 @@ rustls = { version = "0.23.7", default-features = false, features = ["logging",

[features]
tls = ["jsonrpsee-client-transport/tls"]
tls-rustls-platform-verifier = ["jsonrpsee-client-transport/tls-rustls-platform-verifier"]
tls-rustls-platform-verifier = ["jsonrpsee-client-transport/tls-rustls-platform-verifier", "tls"]
default = ["tls-rustls-platform-verifier"]

[package.metadata.docs.rs]
Expand Down
49 changes: 5 additions & 44 deletions client/ws-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use url::Url;
#[cfg(feature = "tls")]
pub use jsonrpsee_client_transport::ws::CustomCertStore;

#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
#[cfg(feature = "tls")]
use jsonrpsee_client_transport::ws::CertificateStore;

/// Builder for [`WsClient`].
Expand Down Expand Up @@ -83,7 +83,7 @@ use jsonrpsee_client_transport::ws::CertificateStore;
/// ```
#[derive(Clone, Debug)]
pub struct WsClientBuilder {
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
#[cfg(feature = "tls")]
certificate_store: CertificateStore,
max_request_size: u32,
max_response_size: u32,
Expand All @@ -100,9 +100,9 @@ pub struct WsClientBuilder {
}

impl Default for WsClientBuilder {
#[cfg(feature = "tls-rustls-platform-verifier")]
fn default() -> Self {
Self {
#[cfg(feature = "tls")]
certificate_store: CertificateStore::Native,
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
Expand All @@ -118,53 +118,14 @@ impl Default for WsClientBuilder {
tcp_no_delay: true,
}
}

#[cfg(not(any(feature = "tls", feature = "tls-rustls-platform-verifier")))]
fn default() -> Self {
Self {
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
request_timeout: Duration::from_secs(60),
connection_timeout: Duration::from_secs(10),
ping_config: None,
headers: HeaderMap::new(),
max_concurrent_requests: 256,
max_buffer_capacity_per_subscription: 1024,
max_redirections: 5,
id_kind: IdKind::Number,
max_log_length: 4096,
tcp_no_delay: true,
}
}
}

impl WsClientBuilder {
/// Create a new WebSocket client builder.
#[cfg(any(not(feature = "tls"), feature = "tls-rustls-platform-verifier"))]
pub fn new() -> WsClientBuilder {
WsClientBuilder::default()
}

/// Create a new WebSocket client builder.
#[cfg(feature = "tls")]
pub fn new(cfg: CustomCertStore) -> WsClientBuilder {
WsClientBuilder {
certificate_store: CertificateStore::Custom(cfg),
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
request_timeout: Duration::from_secs(60),
connection_timeout: Duration::from_secs(10),
ping_config: None,
headers: HeaderMap::new(),
max_concurrent_requests: 256,
max_buffer_capacity_per_subscription: 1024,
max_redirections: 5,
id_kind: IdKind::Number,
max_log_length: 4096,
tcp_no_delay: true,
}
}

/// Force to use a custom certificate store.
///
/// # Optional
Expand Down Expand Up @@ -359,7 +320,7 @@ impl WsClientBuilder {
T: AsyncRead + AsyncWrite + Unpin + MaybeSend + 'static,
{
let transport_builder = WsTransportClientBuilder {
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
#[cfg(feature = "tls")]
certificate_store: self.certificate_store.clone(),
connection_timeout: self.connection_timeout,
headers: self.headers.clone(),
Expand All @@ -385,7 +346,7 @@ impl WsClientBuilder {
/// Panics if being called outside of `tokio` runtime context.
pub async fn build(self, url: impl AsRef<str>) -> Result<WsClient, Error> {
let transport_builder = WsTransportClientBuilder {
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
#[cfg(feature = "tls")]
certificate_store: self.certificate_store.clone(),
connection_timeout: self.connection_timeout,
headers: self.headers.clone(),
Expand Down
2 changes: 1 addition & 1 deletion jsonrpsee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tracing = { version = "0.1.34", optional = true }
tokio = { version = "1.23.1", optional = true }

[features]
client-ws-transport-tls = ["jsonrpsee-client-transport/ws", "jsonrpsee-client-transport/tls"]
client-ws-transport-tls = ["jsonrpsee-client-transport/ws", "jsonrpsee-client-transport/tls-rustls-platform-verifier"]
client-ws-transport-no-tls = ["jsonrpsee-client-transport/ws"]
client-web-transport = ["jsonrpsee-client-transport/web"]
async-client = ["jsonrpsee-core/async-client"]
Expand Down

0 comments on commit 5756e62

Please sign in to comment.