Replace unmaintained encoding crate with encoding_rs

This commit is contained in:
messense
2020-11-24 14:22:59 +08:00
committed by Martin Algesten
parent a0b88926fa
commit df1f4bd0e1
3 changed files with 11 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ features = [ "tls", "json", "charset", "cookies", "socks-proxy" ]
[features]
default = ["tls"]
json = ["serde", "serde_json"]
charset = ["encoding"]
charset = ["encoding_rs"]
tls = ["rustls", "webpki", "webpki-roots"]
native-certs = ["rustls-native-certs"]
cookies = ["cookie", "cookie_store"]
@@ -36,7 +36,7 @@ webpki-roots = { version = "0.21", optional = true }
rustls-native-certs = { version = "0.5", optional = true }
serde = { version = "1", optional = true }
serde_json = { version = "1", optional = true }
encoding = { version = "0.2", optional = true }
encoding_rs = { version = "0.8", optional = true }
cookie_store = { version = "0.12.0", optional = true }
log = "0.4.11"

View File

@@ -5,9 +5,7 @@ use std::io::{self, copy, empty, Cursor, Read, Write};
#[cfg(feature = "charset")]
use crate::response::DEFAULT_CHARACTER_SET;
#[cfg(feature = "charset")]
use encoding::label::encoding_from_whatwg_label;
#[cfg(feature = "charset")]
use encoding::EncoderTrap;
use encoding_rs::Encoding;
#[cfg(feature = "json")]
use super::SerdeValue;
@@ -80,10 +78,10 @@ impl<'a> Payload<'a> {
Payload::Text(text, _charset) => {
#[cfg(feature = "charset")]
let bytes = {
let encoding = encoding_from_whatwg_label(&_charset)
.or_else(|| encoding_from_whatwg_label(DEFAULT_CHARACTER_SET))
let encoding = Encoding::for_label(_charset.as_bytes())
.or_else(|| Encoding::for_label(DEFAULT_CHARACTER_SET.as_bytes()))
.unwrap();
encoding.encode(&text, EncoderTrap::Replace).unwrap()
encoding.encode(&text).0
};
#[cfg(not(feature = "charset"))]
let bytes = text.as_bytes();

View File

@@ -14,9 +14,7 @@ use crate::unit::Unit;
use serde::de::DeserializeOwned;
#[cfg(feature = "charset")]
use encoding::label::encoding_from_whatwg_label;
#[cfg(feature = "charset")]
use encoding::DecoderTrap;
use encoding_rs::Encoding;
pub const DEFAULT_CONTENT_TYPE: &str = "text/plain";
pub const DEFAULT_CHARACTER_SET: &str = "utf-8";
@@ -327,12 +325,13 @@ impl Response {
pub fn into_string(self) -> io::Result<String> {
#[cfg(feature = "charset")]
{
let encoding = encoding_from_whatwg_label(self.charset())
.or_else(|| encoding_from_whatwg_label(DEFAULT_CHARACTER_SET))
let encoding = Encoding::for_label(self.charset().as_bytes())
.or_else(|| Encoding::for_label(DEFAULT_CHARACTER_SET.as_bytes()))
.unwrap();
let mut buf: Vec<u8> = vec![];
self.into_reader().read_to_end(&mut buf)?;
Ok(encoding.decode(&buf, DecoderTrap::Replace).unwrap())
let (text, _, _) = encoding.decode(&buf);
Ok(text.into_owned())
}
#[cfg(not(feature = "charset"))]
{