mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-25 00:00:23 +02:00
Change nested routes to see the URI with prefix stripped (#197)
This commit is contained in:
+9
-14
@@ -6,7 +6,7 @@ use crate::{
|
||||
buffer::MpscBuffer,
|
||||
extract::{
|
||||
connect_info::{Connected, IntoMakeServiceWithConnectInfo},
|
||||
NestedUri,
|
||||
OriginalUri,
|
||||
},
|
||||
service::{HandleError, HandleErrorFromRouter},
|
||||
util::ByteStr,
|
||||
@@ -690,11 +690,7 @@ impl PathPattern {
|
||||
}
|
||||
|
||||
fn do_match<'a, B>(&self, req: &'a Request<B>) -> Option<Match<'a>> {
|
||||
let path = if let Some(nested_uri) = req.extensions().get::<NestedUri>() {
|
||||
nested_uri.0.path()
|
||||
} else {
|
||||
req.uri().path()
|
||||
};
|
||||
let path = req.uri().path();
|
||||
|
||||
self.0.full_path_regex.captures(path).map(|captures| {
|
||||
let matched = captures.get(0).unwrap();
|
||||
@@ -948,15 +944,14 @@ where
|
||||
}
|
||||
|
||||
fn call(&mut self, mut req: Request<B>) -> Self::Future {
|
||||
let f = if let Some((prefix, captures)) = self.pattern.prefix_match(&req) {
|
||||
let uri = if let Some(nested_uri) = req.extensions().get::<NestedUri>() {
|
||||
&nested_uri.0
|
||||
} else {
|
||||
req.uri()
|
||||
};
|
||||
if req.extensions().get::<OriginalUri>().is_none() {
|
||||
let original_uri = OriginalUri(req.uri().clone());
|
||||
req.extensions_mut().insert(original_uri);
|
||||
}
|
||||
|
||||
let without_prefix = strip_prefix(uri, prefix);
|
||||
req.extensions_mut().insert(NestedUri(without_prefix));
|
||||
let f = if let Some((prefix, captures)) = self.pattern.prefix_match(&req) {
|
||||
let without_prefix = strip_prefix(req.uri(), prefix);
|
||||
*req.uri_mut() = without_prefix;
|
||||
|
||||
insert_url_params(&mut req, captures);
|
||||
let fut = self.svc.clone().oneshot(req);
|
||||
|
||||
@@ -47,6 +47,8 @@ where
|
||||
}
|
||||
|
||||
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
|
||||
let original_uri = req.uri().clone();
|
||||
|
||||
if let Some(count) = req.extensions_mut().get_mut::<OrDepth>() {
|
||||
count.increment();
|
||||
} else {
|
||||
@@ -58,6 +60,7 @@ where
|
||||
f: self.first.clone().oneshot(req),
|
||||
},
|
||||
second: Some(self.second.clone()),
|
||||
original_uri: Some(original_uri),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,6 +75,9 @@ pin_project! {
|
||||
#[pin]
|
||||
state: State<A, B, ReqBody>,
|
||||
second: Option<B>,
|
||||
// Some services, namely `Nested`, mutates the request URI so we must
|
||||
// restore it to its original state before calling `second`
|
||||
original_uri: Option<http::Uri>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +121,8 @@ where
|
||||
return Poll::Ready(Ok(response));
|
||||
};
|
||||
|
||||
*req.uri_mut() = this.original_uri.take().unwrap();
|
||||
|
||||
let mut leaving_outermost_or = false;
|
||||
if let Some(depth) = req.extensions_mut().get_mut::<OrDepth>() {
|
||||
if depth == 1 {
|
||||
|
||||
Reference in New Issue
Block a user