Skip to content

Commit 40e188e

Browse files
authored
Merge pull request #69 from taiki-e/feature
Fix h1_client feature
2 parents 0f57f86 + 023768c commit 40e188e

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

.github/workflows/ci.yaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
runs-on: ubuntu-latest
3434
steps:
3535
- uses: actions/checkout@master
36-
36+
3737
- name: check
3838
run: cargo check --no-default-features
3939

@@ -51,7 +51,7 @@ jobs:
5151
override: true
5252
components: clippy, rustfmt
5353

54-
- name: clippy
54+
- name: clippy
5555
run: cargo clippy --all-targets --workspace --features=docs
5656

5757
- name: fmt
@@ -79,3 +79,18 @@ jobs:
7979
with:
8080
command: check
8181
args: --target wasm32-unknown-unknown --no-default-features --features "native_client,wasm_client"
82+
83+
check_features:
84+
name: Check feature combinations
85+
runs-on: ubuntu-latest
86+
steps:
87+
- uses: actions/checkout@master
88+
89+
- name: Install cargo-hack
90+
run: cargo install cargo-hack
91+
92+
- name: Check all feature combinations works properly
93+
# * `--feature-powerset` - run for the feature powerset of the package
94+
# * `--no-dev-deps` - build without dev-dependencies to avoid https://github.com/rust-lang/cargo/issues/4866
95+
# * `--skip docs` - skip `docs` feature
96+
run: cargo hack check --feature-powerset --no-dev-deps --skip docs

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async-trait = "0.1.37"
3737
dashmap = "4.0.2"
3838
http-types = "2.3.0"
3939
log = "0.4.7"
40+
cfg-if = "1.0.0"
4041

4142
# h1_client
4243
async-h1 = { version = "2.0.0", optional = true }
@@ -91,5 +92,4 @@ tide-rustls = { version = "0.1.4" }
9192
tokio = { version = "0.2.21", features = ["macros"] }
9293
serde = "1.0"
9394
serde_json = "1.0"
94-
cfg-if = "0.1.10"
9595
mockito = "0.23.3"

src/h1/mod.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,56 @@ use dashmap::DashMap;
99
use deadpool::managed::Pool;
1010
use http_types::StatusCode;
1111

12-
#[cfg(feature = "native-tls")]
13-
use async_native_tls::TlsStream;
14-
#[cfg(feature = "rustls")]
15-
use async_tls::client::TlsStream;
12+
cfg_if::cfg_if! {
13+
if #[cfg(feature = "rustls")] {
14+
use async_tls::client::TlsStream;
15+
} else if #[cfg(feature = "native-tls")] {
16+
use async_native_tls::TlsStream;
17+
}
18+
}
1619

1720
use super::{async_trait, Error, HttpClient, Request, Response};
1821

1922
mod tcp;
23+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
2024
mod tls;
2125

2226
use tcp::{TcpConnWrapper, TcpConnection};
27+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
2328
use tls::{TlsConnWrapper, TlsConnection};
2429

2530
// This number is based on a few random benchmarks and see whatever gave decent perf vs resource use.
2631
const DEFAULT_MAX_CONCURRENT_CONNECTIONS: usize = 50;
2732

2833
type HttpPool = DashMap<SocketAddr, Pool<TcpStream, std::io::Error>>;
34+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
2935
type HttpsPool = DashMap<SocketAddr, Pool<TlsStream<TcpStream>, Error>>;
3036

3137
/// Async-h1 based HTTP Client, with connecton pooling ("Keep-Alive").
3238
pub struct H1Client {
3339
http_pools: HttpPool,
40+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
3441
https_pools: HttpsPool,
3542
max_concurrent_connections: usize,
3643
}
3744

3845
impl Debug for H1Client {
3946
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47+
let https_pools = if cfg!(any(feature = "native-tls", feature = "rustls")) {
48+
self.http_pools
49+
.iter()
50+
.map(|pool| {
51+
let status = pool.status();
52+
format!(
53+
"Connections: {}, Available: {}, Max: {}",
54+
status.size, status.available, status.max_size
55+
)
56+
})
57+
.collect::<Vec<String>>()
58+
} else {
59+
vec![]
60+
};
61+
4062
f.debug_struct("H1Client")
4163
.field(
4264
"http_pools",
@@ -52,20 +74,7 @@ impl Debug for H1Client {
5274
})
5375
.collect::<Vec<String>>(),
5476
)
55-
.field(
56-
"https_pools",
57-
&self
58-
.http_pools
59-
.iter()
60-
.map(|pool| {
61-
let status = pool.status();
62-
format!(
63-
"Connections: {}, Available: {}, Max: {}",
64-
status.size, status.available, status.max_size
65-
)
66-
})
67-
.collect::<Vec<String>>(),
68-
)
77+
.field("https_pools", &https_pools)
6978
.field(
7079
"max_concurrent_connections",
7180
&self.max_concurrent_connections,
@@ -85,6 +94,7 @@ impl H1Client {
8594
pub fn new() -> Self {
8695
Self {
8796
http_pools: DashMap::new(),
97+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
8898
https_pools: DashMap::new(),
8999
max_concurrent_connections: DEFAULT_MAX_CONCURRENT_CONNECTIONS,
90100
}
@@ -94,6 +104,7 @@ impl H1Client {
94104
pub fn with_max_connections(max: usize) -> Self {
95105
Self {
96106
http_pools: DashMap::new(),
107+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
97108
https_pools: DashMap::new(),
98109
max_concurrent_connections: max,
99110
}
@@ -106,14 +117,17 @@ impl HttpClient for H1Client {
106117
req.insert_header("Connection", "keep-alive");
107118

108119
// Insert host
120+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
109121
let host = req
110122
.url()
111123
.host_str()
112124
.ok_or_else(|| Error::from_str(StatusCode::BadRequest, "missing hostname"))?
113125
.to_string();
114126

115127
let scheme = req.url().scheme();
116-
if scheme != "http" && scheme != "https" {
128+
if scheme != "http"
129+
&& (scheme != "https" || cfg!(not(any(feature = "native-tls", feature = "rustls"))))
130+
{
117131
return Err(Error::from_str(
118132
StatusCode::BadRequest,
119133
format!("invalid url scheme '{}'", scheme),
@@ -124,6 +138,7 @@ impl HttpClient for H1Client {
124138
.url()
125139
.socket_addrs(|| match req.url().scheme() {
126140
"http" => Some(80),
141+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
127142
"https" => Some(443),
128143
_ => None,
129144
})?
@@ -156,6 +171,7 @@ impl HttpClient for H1Client {
156171
req.set_local_addr(stream.local_addr().ok());
157172
client::connect(TcpConnWrapper::new(stream), req).await
158173
}
174+
#[cfg(any(feature = "native-tls", feature = "rustls"))]
159175
"https" => {
160176
let pool_ref = if let Some(pool_ref) = self.https_pools.get(&addr) {
161177
pool_ref

src/h1/tls.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ use deadpool::managed::{Manager, Object, RecycleResult};
88
use futures::io::{AsyncRead, AsyncWrite};
99
use futures::task::{Context, Poll};
1010

11-
#[cfg(feature = "native-tls")]
12-
use async_native_tls::TlsStream;
13-
#[cfg(feature = "rustls")]
14-
use async_tls::client::TlsStream;
11+
cfg_if::cfg_if! {
12+
if #[cfg(feature = "rustls")] {
13+
use async_tls::client::TlsStream;
14+
} else if #[cfg(feature = "native-tls")] {
15+
use async_native_tls::TlsStream;
16+
}
17+
}
1518

1619
use crate::Error;
1720

@@ -76,16 +79,18 @@ impl Manager<TlsStream<TcpStream>, Error> for TlsConnection {
7679
}
7780
}
7881

79-
#[cfg(feature = "native-tls")]
80-
async fn add_tls(
81-
host: &str,
82-
stream: TcpStream,
83-
) -> Result<TlsStream<TcpStream>, async_native_tls::Error> {
84-
async_native_tls::connect(host, stream).await
85-
}
86-
87-
#[cfg(feature = "rustls")]
88-
async fn add_tls(host: &str, stream: TcpStream) -> Result<TlsStream<TcpStream>, std::io::Error> {
89-
let connector = async_tls::TlsConnector::default();
90-
connector.connect(host, stream).await
82+
cfg_if::cfg_if! {
83+
if #[cfg(feature = "rustls")] {
84+
async fn add_tls(host: &str, stream: TcpStream) -> Result<TlsStream<TcpStream>, std::io::Error> {
85+
let connector = async_tls::TlsConnector::default();
86+
connector.connect(host, stream).await
87+
}
88+
} else if #[cfg(feature = "native-tls")] {
89+
async fn add_tls(
90+
host: &str,
91+
stream: TcpStream,
92+
) -> Result<TlsStream<TcpStream>, async_native_tls::Error> {
93+
async_native_tls::connect(host, stream).await
94+
}
95+
}
9196
}

0 commit comments

Comments
 (0)