Skip to content

Commit

Permalink
Merge pull request #1 from dalloriam/support-axum-v0.5
Browse files Browse the repository at this point in the history
Adapt for axum 0.5
  • Loading branch information
imbolc authored Apr 3, 2022
2 parents 9af225e + 37ed9f2 commit 1b312e9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repository = "https://github.com/imbolc/axum-client-ip"
version = "0.1.0"

[dependencies]
axum = "0.4"
axum = "0.5"
forwarded-header-value = "0.1"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() {
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(
// Don't forget to add `ConnetInfo` if you aren't behind a proxy
app.into_make_service_with_connect_info::<SocketAddr, _>()
app.into_make_service_with_connect_info::<SocketAddr>()
)
.await
.unwrap()
Expand Down
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
//! .serve(
//! // Don't forget to add `ConnetInfo` if you aren't behind a proxy
//! app.into_make_service_with_connect_info::<SocketAddr, _>()
//! app.into_make_service_with_connect_info::<SocketAddr>()
//! )
//! .await
//! .unwrap()
Expand Down Expand Up @@ -60,10 +60,7 @@ where
type Rejection = (StatusCode, &'static str);

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let headers = req.headers().ok_or((
StatusCode::INTERNAL_SERVER_ERROR,
"Can't extract client IP: headers has been taken by another extractor",
))?;
let headers = req.headers();

maybe_x_forwarded_for(headers)
.or_else(|| maybe_x_real_ip(headers))
Expand Down Expand Up @@ -114,7 +111,7 @@ fn maybe_forwarded(headers: &HeaderMap) -> Option<IpAddr> {
/// Looks in `ConnectInfo` extension
fn maybe_connect_info<B: Send>(req: &RequestParts<B>) -> Option<IpAddr> {
req.extensions()
.and_then(|e| e.get::<ConnectInfo<SocketAddr>>())
.get::<ConnectInfo<SocketAddr>>()
.map(|ConnectInfo(addr)| addr.ip())
}

Expand Down

0 comments on commit 1b312e9

Please sign in to comment.