Skip to content

Commit 3dbbcb0

Browse files
committed
Auto merge of #2616 - jtgeibel:fix-path-rewrite-logging, r=JohnTitor
Fix logging of original request path In the original implementation the logging middleware would log the final rewritten path. The NormalizePath middleware had custom logic to log the original path, but the other 2 uses of path_mut() were not handled consistently. The logging middleware now ensures that the original request path is always logged. r? @JohnTitor
2 parents 40bd8e1 + 29eadd0 commit 3dbbcb0

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/middleware/log_request.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ const SLOW_REQUEST_THRESHOLD_MS: u64 = 1000;
1313
pub(super) struct LogRequests();
1414

1515
struct RequestStart(Instant);
16+
struct OriginalPath(String);
1617

1718
impl Middleware for LogRequests {
1819
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
1920
req.mut_extensions().insert(RequestStart(Instant::now()));
21+
let path = OriginalPath(req.path().to_string());
22+
req.mut_extensions().insert(path);
2023
Ok(())
2124
}
2225

@@ -59,6 +62,7 @@ pub fn add_custom_metadata<V: Display>(req: &mut dyn RequestExt, key: &'static s
5962

6063
#[cfg(test)]
6164
pub(crate) fn get_log_message(req: &dyn RequestExt, key: &'static str) -> String {
65+
// Unwrap shouldn't panic as no other code has access to the private struct to remove it
6266
for (k, v) in &req.extensions().find::<CustomMetadata>().unwrap().entries {
6367
if key == *k {
6468
return v.clone();
@@ -113,7 +117,12 @@ struct FullPath<'a>(&'a dyn RequestExt);
113117

114118
impl<'a> Display for FullPath<'a> {
115119
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
116-
write!(f, "{}", self.0.path())?;
120+
// Unwrap shouldn't panic as no other code has access to the private struct to remove it
121+
write!(
122+
f,
123+
"{}",
124+
self.0.extensions().find::<OriginalPath>().unwrap().0
125+
)?;
117126
if let Some(q_string) = self.0.query_string() {
118127
write!(f, "?{}", q_string)?;
119128
}

src/middleware/normalize_path.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,32 @@ impl Middleware for NormalizePath {
1414
return Ok(());
1515
}
1616

17-
let original = path.to_string();
18-
let length = path.len();
19-
let path = Path::new(path);
20-
21-
let path = path
17+
let path = Path::new(path)
2218
.components()
23-
.fold(PathBuf::with_capacity(length), |mut result, p| match p {
24-
Component::Normal(x) => {
25-
if x != "" {
26-
result.push(x)
27-
};
28-
result
29-
}
30-
Component::ParentDir => {
31-
result.pop();
32-
result
33-
}
34-
Component::RootDir => {
35-
result.push(Component::RootDir);
36-
result
37-
}
38-
_ => result,
39-
})
19+
.fold(
20+
PathBuf::with_capacity(path.len()),
21+
|mut result, p| match p {
22+
Component::Normal(x) => {
23+
if x != "" {
24+
result.push(x)
25+
};
26+
result
27+
}
28+
Component::ParentDir => {
29+
result.pop();
30+
result
31+
}
32+
Component::RootDir => {
33+
result.push(Component::RootDir);
34+
result
35+
}
36+
_ => result,
37+
},
38+
)
4039
.to_string_lossy()
4140
.to_string(); // non-Unicode is replaced with U+FFFD REPLACEMENT CHARACTER
4241

43-
super::log_request::add_custom_metadata(req, "original_path", original);
42+
super::log_request::add_custom_metadata(req, "normalized_path", path.clone());
4443
*req.path_mut() = path;
4544
Ok(())
4645
}

0 commit comments

Comments
 (0)