relay_server/middlewares/
normalize_path.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::borrow::Cow;
use std::task::{Context, Poll};

use axum::http::{Request, Response, Uri};
use once_cell::sync::Lazy;
use regex::Regex;
use tower::Service;

/// Normalizes URLs with redundant slashes.
///
/// Any groups of slashes will be collapsed into a single slash. For example, a request with
/// `//foo///` will be changed to `/foo/`.
#[derive(Clone, Copy, Debug)]
pub struct NormalizePath<S> {
    inner: S,
}

impl<S> NormalizePath<S> {
    pub fn new(inner: S) -> Self {
        Self { inner }
    }
}

impl<S, I, O> Service<Request<I>> for NormalizePath<S>
where
    S: Service<Request<I>, Response = Response<O>>,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = S::Future;

    #[inline]
    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, mut req: Request<I>) -> Self::Future {
        fold_duplicate_slashes(req.uri_mut());
        self.inner.call(req)
    }
}

fn fold_duplicate_slashes(uri: &mut Uri) {
    static REPLACE: Lazy<Regex> = Lazy::new(|| Regex::new("/{2,}").unwrap());

    let Cow::Owned(new_path) = REPLACE.replace_all(uri.path(), "/") else {
        return;
    };

    let path_and_query = match uri.query() {
        Some(query) => format!("{new_path}?{query}"),
        None => new_path,
    };

    let mut builder = Uri::builder().path_and_query(path_and_query);
    if let Some(scheme) = uri.scheme() {
        builder = builder.scheme(scheme.clone());
    }
    if let Some(authority) = uri.authority() {
        builder = builder.authority(authority.clone());
    }

    if let Ok(new_uri) = builder.build() {
        *uri = new_uri;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use similar_asserts::assert_eq;

    #[test]
    fn root() {
        let mut uri = "/".parse().unwrap();
        fold_duplicate_slashes(&mut uri);
        assert_eq!("/", uri.to_string());
    }

    #[test]
    fn path() {
        let mut uri = "///hello///world///".parse().unwrap();
        fold_duplicate_slashes(&mut uri);
        assert_eq!("/hello/world/", uri.to_string());
    }

    #[test]
    fn no_trailing_slash() {
        let mut uri = "/hello".parse().unwrap();
        fold_duplicate_slashes(&mut uri);
        assert_eq!("/hello", uri.to_string());
    }

    #[test]
    fn query_and_fragment() {
        let mut uri = "//hello//?world=true///".parse().unwrap();
        fold_duplicate_slashes(&mut uri);
        assert_eq!("/hello/?world=true///", uri.to_string());
    }
}