Procházet zdrojové kódy

Add convenience methods for percent-decoding URL components.

Simon Sapin před 12 roky
rodič
revize
672adcf4c1
1 změnil soubory, kde provedl 61 přidání a 0 odebrání
  1. 61 0
      src/url.rs

+ 61 - 0
src/url.rs

@@ -364,12 +364,31 @@ impl Url {
         self.relative_scheme_data().map(|scheme_data| scheme_data.username.as_slice())
     }
 
+    /// Percent-decode the URL’s username, if any.
+    ///
+    /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
+    /// will be replaced � U+FFFD, the replacement character.
+    #[inline]
+    pub fn lossy_precent_decode_username(&self) -> Option<String> {
+        self.relative_scheme_data().map(|scheme_data| scheme_data.lossy_precent_decode_username())
+    }
+
     #[inline]
     pub fn password<'a>(&'a self) -> Option<&'a str> {
         self.relative_scheme_data().and_then(|scheme_data|
             scheme_data.password.as_ref().map(|password| password.as_slice()))
     }
 
+    /// Percent-decode the URL’s password, if any.
+    ///
+    /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
+    /// will be replaced � U+FFFD, the replacement character.
+    #[inline]
+    pub fn lossy_precent_decode_password(&self) -> Option<String> {
+        self.relative_scheme_data().and_then(|scheme_data|
+            scheme_data.lossy_precent_decode_password())
+    }
+
     #[inline]
     pub fn host<'a>(&'a self) -> Option<&'a Host> {
         self.relative_scheme_data().map(|scheme_data| &scheme_data.host)
@@ -399,6 +418,24 @@ impl Url {
     pub fn serialize_path(&self) -> Option<String> {
         self.relative_scheme_data().map(|scheme_data| scheme_data.serialize_path())
     }
+
+    /// Percent-decode the URL’s query string, if any.
+    ///
+    /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
+    /// will be replaced � U+FFFD, the replacement character.
+    #[inline]
+    pub fn lossy_precent_decode_query(&self) -> Option<String> {
+        self.query.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes()))
+    }
+
+    /// Percent-decode the URL’s fragment identifier, if any.
+    ///
+    /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
+    /// will be replaced � U+FFFD, the replacement character.
+    #[inline]
+    pub fn lossy_precent_decode_fragment(&self) -> Option<String> {
+        self.fragment.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes()))
+    }
 }
 
 
@@ -448,6 +485,24 @@ impl Show for SchemeData {
 
 
 impl RelativeSchemeData {
+    /// Percent-decode the URL’s username.
+    ///
+    /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
+    /// will be replaced � U+FFFD, the replacement character.
+    #[inline]
+    pub fn lossy_precent_decode_username(&self) -> String {
+        lossy_utf8_percent_decode(self.username.as_bytes())
+    }
+
+    /// Percent-decode the URL’s password, if any.
+    ///
+    /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
+    /// will be replaced � U+FFFD, the replacement character.
+    #[inline]
+    pub fn lossy_precent_decode_password(&self) -> Option<String> {
+        self.password.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes()))
+    }
+
     // FIXME: Figure out what to do on Windows.
     #[cfg(unix)]
     pub fn to_file_path(&self) -> Result<Path, ()> {
@@ -989,6 +1044,12 @@ pub fn percent_decode(input: &[u8]) -> Vec<u8> {
 }
 
 
+#[inline]
+pub fn lossy_utf8_percent_decode(input: &[u8]) -> String {
+    String::from_utf8_lossy(percent_decode(input).as_slice()).into_string()
+}
+
+
 // FIXME: Figure out what to do on Windows
 #[cfg(unix)]
 fn encode_file_path(path: &Path) -> Result<Vec<String>, ()> {