1use crate::types::RequestMeta;
2use crate::Error;
3use http::Extensions;
4use percent_encoding::percent_decode_str;
5
6pub(crate) fn update_req_meta_in_extensions(ext: &mut Extensions, new_req_meta: RequestMeta) {
7 if let Some(existing_req_meta) = ext.get_mut::<RequestMeta>() {
8 existing_req_meta.extend(new_req_meta);
9 } else {
10 ext.insert(new_req_meta);
11 }
12}
13
14pub(crate) fn percent_decode_request_path(val: &str) -> crate::Result<String> {
15 percent_decode_str(val)
16 .decode_utf8()
17 .map_err(|e| Error::new(format!("Couldn't decode the request path as UTF8: {}", e)).into())
18 .map(|val| val.to_string())
19}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24
25 #[test]
26 fn test_percent_decode_request_path() {
27 let val = "/Alice%20John/do something";
28 assert_eq!(
29 percent_decode_request_path(val).unwrap(),
30 "/Alice John/do something".to_owned()
31 );
32
33 let val = "Alice%20John";
34 assert_eq!(percent_decode_request_path(val).unwrap(), "Alice John".to_owned());
35
36 let val = "Go<>crazy";
37 assert_eq!(percent_decode_request_path(val).unwrap(), "Go<>crazy".to_owned());
38
39 let val = "go%crazy";
40 assert_eq!(percent_decode_request_path(val).unwrap(), "go%crazy".to_owned());
41 }
42}