Skip to content

Commit ac5c4a6

Browse files
committed
middleware::log_request: Move OriginalPath code into NormalizePath middleware
1 parent 768851b commit ac5c4a6

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
@@ -8,6 +8,7 @@ use conduit::{header, Host, RequestExt, Scheme, StatusCode};
88
use conduit_cookie::RequestSession;
99
use sentry::Level;
1010

11+
use crate::middleware::normalize_path::OriginalPath;
1112
use crate::middleware::request_timing::ResponseTime;
1213
use std::fmt::{self, Display, Formatter};
1314

@@ -18,15 +19,7 @@ const FILTERED_HEADERS: &[&str] = &["Authorization", "Cookie", "X-Real-Ip", "X-F
1819
#[derive(Default)]
1920
pub(super) struct LogRequests();
2021

21-
struct OriginalPath(String);
22-
2322
impl Middleware for LogRequests {
24-
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
25-
let path = OriginalPath(req.path().to_string());
26-
req.mut_extensions().insert(path);
27-
Ok(())
28-
}
29-
3023
fn after(&self, req: &mut dyn RequestExt, res: AfterResult) -> AfterResult {
3124
let response_time = req.extensions().find::<ResponseTime>().unwrap();
3225

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)