Skip to content

Commit dbcda19

Browse files
committed
middleware::log_request: Move OriginalPath code into NormalizePath middleware
1 parent 5437c24 commit dbcda19

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/middleware/log_request.rs

Lines changed: 1 addition & 8 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::request_timing::ResponseTime;
1112
use std::fmt::{self, Display, Formatter};
1213

@@ -15,15 +16,7 @@ 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 {
21-
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
22-
let path = OriginalPath(req.path().to_string());
23-
req.mut_extensions().insert(path);
24-
Ok(())
25-
}
26-
2720
fn after(&self, req: &mut dyn RequestExt, res: AfterResult) -> AfterResult {
2821
let response_time = req.extensions().find::<ResponseTime>().unwrap();
2922

src/middleware/normalize_path.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ 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 {
1012
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
1113
let path = req.path();
14+
let original_path = OriginalPath(path.to_string());
15+
1216
if !(path.contains("//") || path.contains("/.")) {
17+
req.mut_extensions().insert(original_path);
18+
1319
// Avoid allocations if rewriting is unnecessary
1420
return Ok(());
1521
}
@@ -40,6 +46,7 @@ impl Middleware for NormalizePath {
4046
.to_string(); // non-Unicode is replaced with U+FFFD REPLACEMENT CHARACTER
4147

4248
add_custom_metadata(req, "normalized_path", path.clone());
49+
req.mut_extensions().insert(original_path);
4350
*req.path_mut() = path;
4451
Ok(())
4552
}

0 commit comments

Comments
 (0)