fix: return 400 instead of 404 for missing client IP header

This commit is contained in:
NetworkCats
2026-03-02 23:27:30 +08:00
parent 31d86d8ab2
commit da2f847faf
3 changed files with 4 additions and 2 deletions
+2
View File
@@ -4,6 +4,7 @@ use axum::response::{IntoResponse, Response};
pub enum AppError {
IpNotFound,
InvalidIp,
MissingClientIp,
DbLookupFailed,
}
@@ -12,6 +13,7 @@ impl IntoResponse for AppError {
let (status, message) = match self {
Self::IpNotFound => (StatusCode::NOT_FOUND, "IP not found in database"),
Self::InvalidIp => (StatusCode::BAD_REQUEST, "Invalid IP address"),
Self::MissingClientIp => (StatusCode::BAD_REQUEST, "Missing client IP address"),
Self::DbLookupFailed => (StatusCode::INTERNAL_SERVER_ERROR, "Database lookup failed"),
};
(status, message).into_response()
+1 -1
View File
@@ -52,7 +52,7 @@ fn extract_client_ip(headers: &HeaderMap) -> Result<IpAddr, AppError> {
.get("CF-Connecting-IP")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.trim().parse().ok())
.ok_or(AppError::IpNotFound)
.ok_or(AppError::MissingClientIp)
}
fn lookup_ip(db: &SharedDb, ip: IpAddr) -> Result<IpInfo, AppError> {
+1 -1
View File
@@ -231,7 +231,7 @@ async fn root_without_cf_header_returns_error() {
)],
)
.await;
assert_eq!(status, StatusCode::NOT_FOUND);
assert_eq!(status, StatusCode::BAD_REQUEST);
}
#[tokio::test]