Skip to content

middleware::log_request: Move OriginalPath code into NormalizePath middleware #4124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/middleware/log_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::util::request_header;
use conduit::{header, RequestExt, StatusCode};
use conduit_cookie::RequestSession;

use crate::middleware::normalize_path::OriginalPath;
use crate::middleware::response_timing::ResponseTime;
use std::fmt::{self, Display, Formatter};

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

struct OriginalPath(String);

impl Middleware for LogRequests {
fn before(&self, req: &mut dyn RequestExt) -> BeforeResult {
let path = OriginalPath(req.path().to_string());
req.mut_extensions().insert(path);

if let Some(request_id) = req
.headers()
.get("x-request-id")
Expand Down Expand Up @@ -166,13 +162,16 @@ struct FullPath<'a>(&'a dyn RequestExt);

impl<'a> Display for FullPath<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// Unwrap shouldn't panic as no other code has access to the private struct to remove it
write!(
f,
"{}",
self.0.extensions().find::<OriginalPath>().unwrap().0
)?;
if let Some(q_string) = self.0.query_string() {
let request = self.0;

let original_path = request.extensions().find::<OriginalPath>();
let path = original_path
.map(|p| p.0.as_str())
.unwrap_or_else(|| request.path());

write!(f, "{}", path)?;

if let Some(q_string) = request.query_string() {
write!(f, "?{}", q_string)?;
}
Ok(())
Expand Down
7 changes: 7 additions & 0 deletions src/middleware/normalize_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use super::prelude::*;

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

pub struct OriginalPath(pub String);

pub struct NormalizePath;

impl Middleware for NormalizePath {
Expand All @@ -14,6 +16,8 @@ impl Middleware for NormalizePath {
return Ok(());
}

let original_path = OriginalPath(path.to_string());

let path = Path::new(path)
.components()
.fold(
Expand All @@ -40,7 +44,10 @@ impl Middleware for NormalizePath {
.to_string(); // non-Unicode is replaced with U+FFFD REPLACEMENT CHARACTER

add_custom_metadata(req, "normalized_path", path.clone());

*req.path_mut() = path;
req.mut_extensions().insert(original_path);

Ok(())
}
}
Expand Down