Feature flag for axum-original-uri

This commit is contained in:
Felix Ableitner 2025-06-05 15:37:37 +02:00
parent ae7910e5f5
commit 4c373c5dc8
2 changed files with 17 additions and 9 deletions

View file

@ -12,6 +12,7 @@ documentation = "https://docs.rs/activitypub_federation/"
default = ["actix-web", "axum"]
actix-web = ["dep:actix-web", "dep:http02"]
axum = ["dep:axum", "dep:tower"]
axum-original-uri = ["dep:axum", "axum/original-uri"]
[lints.rust]
warnings = "deny"
@ -84,8 +85,7 @@ http02 = { package = "http", version = "0.2.12", optional = true }
# Axum
axum = { version = "0.8.4", features = [
"json",
"original-uri",
"json"
], default-features = false, optional = true }
tower = { version = "0.5.2", optional = true }

View file

@ -11,11 +11,11 @@ use crate::{
};
use axum::{
body::Body,
extract::{FromRequest, FromRequestParts, OriginalUri},
extract::FromRequest,
http::{Request, StatusCode},
response::{IntoResponse, Response},
};
use http::{HeaderMap, Method};
use http::{HeaderMap, Method, Uri};
use serde::de::DeserializeOwned;
use tracing::debug;
@ -53,7 +53,7 @@ where
pub struct ActivityData {
headers: HeaderMap,
method: Method,
uri: OriginalUri,
uri: Uri,
body: Vec<u8>,
}
@ -63,14 +63,22 @@ where
{
type Rejection = Response;
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
#[allow(unused_mut)]
let (mut parts, body) = req.into_parts();
// take the full URI to handle nested routers
// OriginalUri::from_request_parts has an Infallible error type
let uri = OriginalUri::from_request_parts(&mut parts, state)
#[cfg(feature = "axum-original-uri")]
let uri = {
use axum::extract::{FromRequestParts, OriginalUri};
OriginalUri::from_request_parts(&mut parts, _state)
.await
.expect("infallible");
.expect("infallible")
.0
};
#[cfg(not(feature = "axum-original-uri"))]
let uri = parts.uri;
// this wont work if the body is an long running stream
let bytes = axum::body::to_bytes(body, usize::MAX)