Przeglądaj źródła

Remove _charset_ support

CC https://github.com/whatwg/url/commit/3fe969679f78c92c353047661b0c4b6797f099f6
Simon Sapin 7 lat temu
rodzic
commit
47e2286ff3
2 zmienionych plików z 6 dodań i 85 usunięć
  1. 1 31
      src/encoding.rs
  2. 5 54
      src/form_urlencoded.rs

+ 1 - 31
src/encoding.rs

@@ -17,11 +17,9 @@ use std::borrow::Cow;
 use std::fmt::{self, Debug, Formatter};
 use std::fmt::{self, Debug, Formatter};
 
 
 #[cfg(feature = "query_encoding")]
 #[cfg(feature = "query_encoding")]
-use self::encoding::label::encoding_from_whatwg_label;
+use self::encoding::types::EncoderTrap;
 #[cfg(feature = "query_encoding")]
 #[cfg(feature = "query_encoding")]
 pub use self::encoding::types::EncodingRef;
 pub use self::encoding::types::EncodingRef;
-#[cfg(feature = "query_encoding")]
-use self::encoding::types::{DecoderTrap, EncoderTrap};
 
 
 #[cfg(feature = "query_encoding")]
 #[cfg(feature = "query_encoding")]
 #[derive(Copy, Clone)]
 #[derive(Copy, Clone)]
@@ -51,15 +49,6 @@ impl EncodingOverride {
         EncodingOverride { encoding: None }
         EncodingOverride { encoding: None }
     }
     }
 
 
-    pub fn lookup(label: &[u8]) -> Option<Self> {
-        // Don't use String::from_utf8_lossy since no encoding label contains U+FFFD
-        // https://encoding.spec.whatwg.org/#names-and-labels
-        ::std::str::from_utf8(label)
-            .ok()
-            .and_then(encoding_from_whatwg_label)
-            .map(Self::from_encoding)
-    }
-
     /// https://encoding.spec.whatwg.org/#get-an-output-encoding
     /// https://encoding.spec.whatwg.org/#get-an-output-encoding
     pub fn to_output_encoding(self) -> Self {
     pub fn to_output_encoding(self) -> Self {
         if let Some(encoding) = self.encoding {
         if let Some(encoding) = self.encoding {
@@ -70,10 +59,6 @@ impl EncodingOverride {
         self
         self
     }
     }
 
 
-    pub fn is_utf8(&self) -> bool {
-        self.encoding.is_none()
-    }
-
     pub fn name(&self) -> &'static str {
     pub fn name(&self) -> &'static str {
         match self.encoding {
         match self.encoding {
             Some(encoding) => encoding.name(),
             Some(encoding) => encoding.name(),
@@ -81,17 +66,6 @@ impl EncodingOverride {
         }
         }
     }
     }
 
 
-    pub fn decode<'a>(&self, input: Cow<'a, [u8]>) -> Cow<'a, str> {
-        match self.encoding {
-            // `encoding.decode` never returns `Err` when called with `DecoderTrap::Replace`
-            Some(encoding) => encoding
-                .decode(&input, DecoderTrap::Replace)
-                .unwrap()
-                .into(),
-            None => decode_utf8_lossy(input),
-        }
-    }
-
     pub fn encode<'a>(&self, input: Cow<'a, str>) -> Cow<'a, [u8]> {
     pub fn encode<'a>(&self, input: Cow<'a, str>) -> Cow<'a, [u8]> {
         match self.encoding {
         match self.encoding {
             // `encoding.encode` never returns `Err` when called with `EncoderTrap::NcrEscape`
             // `encoding.encode` never returns `Err` when called with `EncoderTrap::NcrEscape`
@@ -123,10 +97,6 @@ impl EncodingOverride {
         EncodingOverride
         EncodingOverride
     }
     }
 
 
-    pub fn decode<'a>(&self, input: Cow<'a, [u8]>) -> Cow<'a, str> {
-        decode_utf8_lossy(input)
-    }
-
     pub fn encode<'a>(&self, input: Cow<'a, str>) -> Cow<'a, [u8]> {
     pub fn encode<'a>(&self, input: Cow<'a, str>) -> Cow<'a, [u8]> {
         encode_utf8(input)
         encode_utf8(input)
     }
     }

+ 5 - 54
src/form_urlencoded.rs

@@ -13,7 +13,7 @@
 //! Converts between a string (such as an URL’s query string)
 //! Converts between a string (such as an URL’s query string)
 //! and a sequence of (name, value) pairs.
 //! and a sequence of (name, value) pairs.
 
 
-use encoding::EncodingOverride;
+use encoding::{decode_utf8_lossy, EncodingOverride};
 use percent_encoding::{percent_decode, percent_encode_byte};
 use percent_encoding::{percent_decode, percent_encode_byte};
 use std::borrow::{Borrow, Cow};
 use std::borrow::{Borrow, Cow};
 use std::fmt;
 use std::fmt;
@@ -28,61 +28,12 @@ use std::str;
 /// converted to `[("#first", "%try%")]`.
 /// converted to `[("#first", "%try%")]`.
 #[inline]
 #[inline]
 pub fn parse(input: &[u8]) -> Parse {
 pub fn parse(input: &[u8]) -> Parse {
-    Parse {
-        input: input,
-        encoding: EncodingOverride::utf8(),
-    }
+    Parse { input: input }
 }
 }
-
-/// Convert a byte string in the `application/x-www-form-urlencoded` syntax
-/// into a iterator of (name, value) pairs.
-///
-/// Use `parse(input.as_bytes())` to parse a `&str` string.
-///
-/// This function is only available if the `query_encoding`
-/// [feature](http://doc.crates.io/manifest.html#the-features-section]) is enabled.
-///
-/// Arguments:
-///
-/// * `encoding_override`: The character encoding each name and values is decoded as
-///    after percent-decoding. Defaults to UTF-8.
-///    `EncodingRef` is defined in [rust-encoding](https://github.com/lifthrasiir/rust-encoding).
-/// * `use_charset`: The *use _charset_ flag*. If in doubt, set to `false`.
-#[cfg(feature = "query_encoding")]
-pub fn parse_with_encoding<'a>(
-    input: &'a [u8],
-    encoding_override: Option<::encoding::EncodingRef>,
-    use_charset: bool,
-) -> Result<Parse<'a>, ()> {
-    let mut encoding = EncodingOverride::from_opt_encoding(encoding_override);
-    if !(encoding.is_utf8() || input.is_ascii()) {
-        return Err(());
-    }
-    if use_charset {
-        for sequence in input.split(|&b| b == b'&') {
-            // No '+' in "_charset_" to replace with ' '.
-            if sequence.starts_with(b"_charset_=") {
-                let value = &sequence[b"_charset_=".len()..];
-                // Skip replacing '+' with ' ' in value since no encoding label contains either:
-                // https://encoding.spec.whatwg.org/#names-and-labels
-                if let Some(e) = EncodingOverride::lookup(value) {
-                    encoding = e;
-                    break;
-                }
-            }
-        }
-    }
-    Ok(Parse {
-        input: input,
-        encoding: encoding,
-    })
-}
-
 /// The return type of `parse()`.
 /// The return type of `parse()`.
 #[derive(Copy, Clone, Debug)]
 #[derive(Copy, Clone, Debug)]
 pub struct Parse<'a> {
 pub struct Parse<'a> {
     input: &'a [u8],
     input: &'a [u8],
-    encoding: EncodingOverride,
 }
 }
 
 
 impl<'a> Iterator for Parse<'a> {
 impl<'a> Iterator for Parse<'a> {
@@ -102,14 +53,14 @@ impl<'a> Iterator for Parse<'a> {
             let mut split2 = sequence.splitn(2, |&b| b == b'=');
             let mut split2 = sequence.splitn(2, |&b| b == b'=');
             let name = split2.next().unwrap();
             let name = split2.next().unwrap();
             let value = split2.next().unwrap_or(&[][..]);
             let value = split2.next().unwrap_or(&[][..]);
-            return Some((decode(name, self.encoding), decode(value, self.encoding)));
+            return Some((decode(name), decode(value)));
         }
         }
     }
     }
 }
 }
 
 
-fn decode(input: &[u8], encoding: EncodingOverride) -> Cow<str> {
+fn decode(input: &[u8]) -> Cow<str> {
     let replaced = replace_plus(input);
     let replaced = replace_plus(input);
-    encoding.decode(match percent_decode(&replaced).if_any() {
+    decode_utf8_lossy(match percent_decode(&replaced).if_any() {
         Some(vec) => Cow::Owned(vec),
         Some(vec) => Cow::Owned(vec),
         None => replaced,
         None => replaced,
     })
     })