Skip to content

Commit daeb9fb

Browse files
committed
Auto merge of #4124 - Turbo87:original-path, r=JohnTitor
middleware::log_request: Move `OriginalPath` code into `NormalizePath` middleware The `NormalizePath` middleware is the one that mutates the `path`, so it should also be responsible for saving the original one, if necessary. This PR slightly changes the logic in that `OriginalPath` is only added to the request extensions if the path has been changed. The `NormalizePath` middleware now defaults to `request.path()`, if `OriginalPath` is not found in the request extensions.
2 parents 8eefe79 + 362f80d commit daeb9fb

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/middleware/log_request.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::util::request_header;
77
use conduit::{header, RequestExt, StatusCode};
88
use conduit_cookie::RequestSession;
99

10+
use crate::middleware::normalize_path::OriginalPath;
1011
use crate::middleware::response_timing::ResponseTime;
1112
use std::fmt::{self, Display, Formatter};
1213

@@ -15,13 +16,8 @@ const SLOW_REQUEST_THRESHOLD_MS: u64 = 1000;
1516
#[derive(Default)]
1617
pub(super) struct LogRequests();
1718

18-
struct OriginalPath(String);
19-
2019
impl Middleware for LogRequests {
2120
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
22-
let path = OriginalPath(req.path().to_string());
23-
req.mut_extensions().insert(path);
24-
2521
if let Some(request_id) = req
2622
.headers()
2723
.get("x-request-id")
@@ -166,13 +162,16 @@ struct FullPath<'a>(&'a dyn RequestExt);
166162

167163
impl<'a> Display for FullPath<'a> {
168164
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
169-
// Unwrap shouldn't panic as no other code has access to the private struct to remove it
170-
write!(
171-
f,
172-
"{}",
173-
self.0.extensions().find::<OriginalPath>().unwrap().0
174-
)?;
175-
if let Some(q_string) = self.0.query_string() {
165+
let request = self.0;
166+
167+
let original_path = request.extensions().find::<OriginalPath>();
168+
let path = original_path
169+
.map(|p| p.0.as_str())
170+
.unwrap_or_else(|| request.path());
171+
172+
write!(f, "{}", path)?;
173+
174+
if let Some(q_string) = request.query_string() {
176175
write!(f, "?{}", q_string)?;
177176
}
178177
Ok(())

src/middleware/normalize_path.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use super::prelude::*;
44

55
use std::path::{Component, Path, PathBuf};
66

7+
pub struct OriginalPath(pub String);
8+
79
pub struct NormalizePath;
810

911
impl Middleware for NormalizePath {
@@ -14,6 +16,8 @@ impl Middleware for NormalizePath {
1416
return Ok(());
1517
}
1618

19+
let original_path = OriginalPath(path.to_string());
20+
1721
let path = Path::new(path)
1822
.components()
1923
.fold(
@@ -40,7 +44,10 @@ impl Middleware for NormalizePath {
4044
.to_string(); // non-Unicode is replaced with U+FFFD REPLACEMENT CHARACTER
4145

4246
add_custom_metadata(req, "normalized_path", path.clone());
47+
4348
*req.path_mut() = path;
49+
req.mut_extensions().insert(original_path);
50+
4451
Ok(())
4552
}
4653
}

0 commit comments

Comments
 (0)