]> Untitled Git - lemmy.git/commitdiff
Dont append ? to url when cleaning it (#1716)
authorNutomic <me@nutomic.com>
Sat, 21 Aug 2021 13:36:33 +0000 (13:36 +0000)
committerGitHub <noreply@github.com>
Sat, 21 Aug 2021 13:36:33 +0000 (09:36 -0400)
crates/utils/src/utils.rs

index 1254aff0a9fd016227174ea934235e5e16685669..0fb1f788882727a45f4ddd4fb4d2544b63b1a573 100644 (file)
@@ -149,12 +149,14 @@ pub fn get_ip(conn_info: &ConnectionInfo) -> IpAddr {
 }
 
 pub fn clean_url_params(mut url: Url) -> Url {
-  let new_query = url
-    .query_pairs()
-    .filter(|q| !CLEAN_URL_PARAMS_REGEX.is_match(&q.0))
-    .map(|q| format!("{}={}", q.0, q.1))
-    .join("&");
-  url.set_query(Some(&new_query));
+  if url.query().is_some() {
+    let new_query = url
+      .query_pairs()
+      .filter(|q| !CLEAN_URL_PARAMS_REGEX.is_match(&q.0))
+      .map(|q| format!("{}={}", q.0, q.1))
+      .join("&");
+    url.set_query(Some(&new_query));
+  }
   url
 }
 
@@ -169,5 +171,9 @@ mod tests {
     let cleaned = clean_url_params(url);
     let expected = Url::parse("https://example.com/path/123?username=randomuser&id=123").unwrap();
     assert_eq!(expected.to_string(), cleaned.to_string());
+
+    let url = Url::parse("https://example.com/path/123").unwrap();
+    let cleaned = clean_url_params(url.clone());
+    assert_eq!(url.to_string(), cleaned.to_string());
   }
 }