Skip to content

Commit 1f5d04b

Browse files
feat: make tokio optional (#40)
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
1 parent 6d970ed commit 1f5d04b

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ all-features = true
1111
rustdoc-args = ["--cfg", "docsrs"]
1212

1313
[features]
14-
default = ["serde"]
14+
default = ["serde", "connect-info"]
15+
# Enables `ConnectInfo` extractor
16+
connect-info = ["axum/tokio"]
1517
# Enables `RightmostForwarded` extractor
1618
forwarded-header = ["client-ip/forwarded-header"]
1719
# Enables `ClientIpSource` serde compatibility
1820
serde = ["dep:serde"]
1921

2022
[dependencies]
21-
axum = { version = "0.8", default-features = false, features = ["tokio"] }
23+
axum = { version = "0.8", default-features = false }
2224
client-ip = "0.1"
2325
serde = { version = "1", features = ["derive"], optional = true }
2426

src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ use std::{
44
error::Error,
55
fmt,
66
marker::Sync,
7-
net::{IpAddr, SocketAddr},
7+
net::IpAddr,
88
str::FromStr,
99
};
1010

1111
use axum::{
12-
extract::{ConnectInfo, Extension, FromRequestParts},
12+
extract::{Extension, FromRequestParts},
1313
http::{StatusCode, request::Parts},
1414
response::{IntoResponse, Response},
1515
};
1616

17+
#[cfg(feature = "connect-info")]
18+
use std::net::SocketAddr;
19+
20+
#[cfg(feature = "connect-info")]
21+
use axum::extract::ConnectInfo;
22+
1723
/// Defines an extractor
1824
macro_rules! define_extractor {
1925
(
@@ -116,6 +122,7 @@ pub enum ClientIpSource {
116122
CfConnectingIp,
117123
/// IP from the `CloudFront-Viewer-Address` header
118124
CloudFrontViewerAddress,
125+
#[cfg(feature = "connect-info")]
119126
/// IP from the [`axum::extract::ConnectInfo`]
120127
ConnectInfo,
121128
/// IP from the `Fly-Client-IP` header
@@ -158,6 +165,7 @@ impl FromStr for ClientIpSource {
158165
Ok(match s {
159166
"CfConnectingIp" => Self::CfConnectingIp,
160167
"CloudFrontViewerAddress" => Self::CloudFrontViewerAddress,
168+
#[cfg(feature = "connect-info")]
161169
"ConnectInfo" => Self::ConnectInfo,
162170
"FlyClientIp" => Self::FlyClientIp,
163171
#[cfg(feature = "forwarded-header")]
@@ -176,6 +184,7 @@ impl fmt::Display for ClientIpSource {
176184
f.write_str(match self {
177185
ClientIpSource::CfConnectingIp => "CfConnectingIp",
178186
ClientIpSource::CloudFrontViewerAddress => "CloudFrontViewerAddress",
187+
#[cfg(feature = "connect-info")]
179188
ClientIpSource::ConnectInfo => "ConnectInfo",
180189
ClientIpSource::FlyClientIp => "FlyClientIp",
181190
#[cfg(feature = "forwarded-header")]
@@ -203,6 +212,7 @@ where
203212
ClientIpSource::CloudFrontViewerAddress => {
204213
CloudFrontViewerAddress::ip_from_headers(&parts.headers)
205214
}
215+
#[cfg(feature = "connect-info")]
206216
ClientIpSource::ConnectInfo => parts
207217
.extensions
208218
.get::<ConnectInfo<SocketAddr>>()
@@ -227,6 +237,7 @@ where
227237
#[non_exhaustive]
228238
#[derive(Debug, PartialEq)]
229239
pub enum Rejection {
240+
#[cfg(feature = "connect-info")]
230241
/// No [`axum::extract::ConnectInfo`] in extensions
231242
NoConnectInfo,
232243
/// No [`ClientIpSource`] in extensions
@@ -244,6 +255,7 @@ impl From<client_ip::Error> for Rejection {
244255
impl fmt::Display for Rejection {
245256
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246257
match self {
258+
#[cfg(feature = "connect-info")]
247259
Rejection::NoConnectInfo => {
248260
write!(f, "Add `axum::extract::ConnectInfo` to request extensions")
249261
}
@@ -261,7 +273,9 @@ impl std::error::Error for Rejection {}
261273
impl IntoResponse for Rejection {
262274
fn into_response(self) -> Response {
263275
let title = match self {
264-
Self::NoConnectInfo | Self::NoClientIpSource => "500 Axum Misconfiguration",
276+
#[cfg(feature = "connect-info")]
277+
Self::NoConnectInfo => "500 Axum Misconfiguration",
278+
Self::NoClientIpSource => "500 Axum Misconfiguration",
265279
Self::ClientIp { .. } => "500 Proxy Server Misconfiguration",
266280
};
267281
let footer = "(the request is rejected by axum-client-ip)";
@@ -526,6 +540,7 @@ mod tests {
526540

527541
assert_match(ClientIpSource::CfConnectingIp);
528542
assert_match(ClientIpSource::CloudFrontViewerAddress);
543+
#[cfg(feature = "connect-info")]
529544
assert_match(ClientIpSource::ConnectInfo);
530545
assert_match(ClientIpSource::FlyClientIp);
531546
#[cfg(feature = "forwarded-header")]

0 commit comments

Comments
 (0)