@@ -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
1111use 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
1824macro_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 ) ]
229239pub 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 {
244255impl 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 {}
261273impl 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