mirror of
https://github.com/actix/actix-web.git
synced 2025-04-24 18:54:04 +00:00
Use encoding_rs crate instead of unmaintained encoding crate
This commit is contained in:
parent
d7780d53c9
commit
0c58fe3c2c
6 changed files with 39 additions and 39 deletions
|
@ -83,7 +83,7 @@ awc = { version = "0.2.1", optional = true }
|
||||||
|
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
derive_more = "0.15.0"
|
derive_more = "0.15.0"
|
||||||
encoding = "0.2"
|
encoding_rs = "0.8"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
hashbrown = "0.5.0"
|
hashbrown = "0.5.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
|
@ -58,7 +58,7 @@ byteorder = "1.2"
|
||||||
copyless = "0.1.2"
|
copyless = "0.1.2"
|
||||||
derive_more = "0.15.0"
|
derive_more = "0.15.0"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
encoding = "0.2"
|
encoding_rs = "0.8"
|
||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
hashbrown = "0.5.0"
|
hashbrown = "0.5.0"
|
||||||
h2 = "0.1.16"
|
h2 = "0.1.16"
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
use std::cell::{Ref, RefMut};
|
use std::cell::{Ref, RefMut};
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use encoding::label::encoding_from_whatwg_label;
|
|
||||||
use encoding::EncodingRef;
|
|
||||||
use http::header;
|
use http::header;
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
|
||||||
|
@ -59,10 +57,12 @@ pub trait HttpMessage: Sized {
|
||||||
/// Get content type encoding
|
/// Get content type encoding
|
||||||
///
|
///
|
||||||
/// UTF-8 is used by default, If request charset is not set.
|
/// UTF-8 is used by default, If request charset is not set.
|
||||||
fn encoding(&self) -> Result<EncodingRef, ContentTypeError> {
|
fn encoding(&self) -> Result<&'static Encoding, ContentTypeError> {
|
||||||
if let Some(mime_type) = self.mime_type()? {
|
if let Some(mime_type) = self.mime_type()? {
|
||||||
if let Some(charset) = mime_type.get_param("charset") {
|
if let Some(charset) = mime_type.get_param("charset") {
|
||||||
if let Some(enc) = encoding_from_whatwg_label(charset.as_str()) {
|
if let Some(enc) =
|
||||||
|
Encoding::for_label_no_replacement(charset.as_str().as_bytes())
|
||||||
|
{
|
||||||
Ok(enc)
|
Ok(enc)
|
||||||
} else {
|
} else {
|
||||||
Err(ContentTypeError::UnknownEncoding)
|
Err(ContentTypeError::UnknownEncoding)
|
||||||
|
@ -166,8 +166,7 @@ where
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use encoding::all::ISO_8859_2;
|
use encoding_rs::ISO_8859_2;
|
||||||
use encoding::Encoding;
|
|
||||||
use mime;
|
use mime;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -223,7 +222,7 @@ mod tests {
|
||||||
"application/json; charset=ISO-8859-2",
|
"application/json; charset=ISO-8859-2",
|
||||||
)
|
)
|
||||||
.finish();
|
.finish();
|
||||||
assert_eq!(ISO_8859_2.name(), req.encoding().unwrap().name());
|
assert_eq!(ISO_8859_2, req.encoding().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -5,9 +5,7 @@ use std::{fmt, ops};
|
||||||
|
|
||||||
use actix_http::{Error, HttpMessage, Payload};
|
use actix_http::{Error, HttpMessage, Payload};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use encoding::types::{DecoderTrap, Encoding};
|
|
||||||
use encoding::EncodingRef;
|
|
||||||
use futures::{Future, Poll, Stream};
|
use futures::{Future, Poll, Stream};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
|
@ -187,7 +185,7 @@ pub struct UrlEncoded<U> {
|
||||||
stream: Option<Decompress<Payload>>,
|
stream: Option<Decompress<Payload>>,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
length: Option<usize>,
|
length: Option<usize>,
|
||||||
encoding: EncodingRef,
|
encoding: &'static Encoding,
|
||||||
err: Option<UrlencodedError>,
|
err: Option<UrlencodedError>,
|
||||||
fut: Option<Box<Future<Item = U, Error = UrlencodedError>>>,
|
fut: Option<Box<Future<Item = U, Error = UrlencodedError>>>,
|
||||||
}
|
}
|
||||||
|
@ -286,13 +284,14 @@ where
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.and_then(move |body| {
|
.and_then(move |body| {
|
||||||
if (encoding as *const Encoding) == UTF_8 {
|
if encoding == UTF_8 {
|
||||||
serde_urlencoded::from_bytes::<U>(&body)
|
serde_urlencoded::from_bytes::<U>(&body)
|
||||||
.map_err(|_| UrlencodedError::Parse)
|
.map_err(|_| UrlencodedError::Parse)
|
||||||
} else {
|
} else {
|
||||||
let body = encoding
|
let body = encoding
|
||||||
.decode(&body, DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map_err(|_| UrlencodedError::Parse)?;
|
.map(|s| s.into_owned())
|
||||||
|
.ok_or(UrlencodedError::Parse)?;
|
||||||
serde_urlencoded::from_str::<U>(&body)
|
serde_urlencoded::from_str::<U>(&body)
|
||||||
.map_err(|_| UrlencodedError::Parse)
|
.map_err(|_| UrlencodedError::Parse)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,7 @@ use std::str;
|
||||||
use actix_http::error::{Error, ErrorBadRequest, PayloadError};
|
use actix_http::error::{Error, ErrorBadRequest, PayloadError};
|
||||||
use actix_http::HttpMessage;
|
use actix_http::HttpMessage;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::UTF_8;
|
||||||
use encoding::types::{DecoderTrap, Encoding};
|
|
||||||
use futures::future::{err, Either, FutureResult};
|
use futures::future::{err, Either, FutureResult};
|
||||||
use futures::{Future, Poll, Stream};
|
use futures::{Future, Poll, Stream};
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
@ -208,15 +207,15 @@ impl FromRequest for String {
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(move |body| {
|
.and_then(move |body| {
|
||||||
let enc: *const Encoding = encoding as *const Encoding;
|
if encoding == UTF_8 {
|
||||||
if enc == UTF_8 {
|
|
||||||
Ok(str::from_utf8(body.as_ref())
|
Ok(str::from_utf8(body.as_ref())
|
||||||
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
||||||
.to_owned())
|
.to_owned())
|
||||||
} else {
|
} else {
|
||||||
Ok(encoding
|
Ok(encoding
|
||||||
.decode(&body, DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(&body)
|
||||||
.map_err(|_| ErrorBadRequest("Can not decode body"))?)
|
.map(|s| s.into_owned())
|
||||||
|
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
))
|
))
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use encoding::all::UTF_8;
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use encoding::types::{DecoderTrap, Encoding};
|
|
||||||
use encoding::EncodingRef;
|
|
||||||
use futures::{Async, Poll, Stream};
|
use futures::{Async, Poll, Stream};
|
||||||
|
|
||||||
use crate::dev::Payload;
|
use crate::dev::Payload;
|
||||||
|
@ -16,7 +15,7 @@ pub struct Readlines<T: HttpMessage> {
|
||||||
buff: BytesMut,
|
buff: BytesMut,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
checked_buff: bool,
|
checked_buff: bool,
|
||||||
encoding: EncodingRef,
|
encoding: &'static Encoding,
|
||||||
err: Option<ReadlinesError>,
|
err: Option<ReadlinesError>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,15 +86,17 @@ where
|
||||||
if ind + 1 > self.limit {
|
if ind + 1 > self.limit {
|
||||||
return Err(ReadlinesError::LimitOverflow);
|
return Err(ReadlinesError::LimitOverflow);
|
||||||
}
|
}
|
||||||
let enc: *const Encoding = self.encoding as *const Encoding;
|
let line = if self.encoding == UTF_8 {
|
||||||
let line = if enc == UTF_8 {
|
|
||||||
str::from_utf8(&self.buff.split_to(ind + 1))
|
str::from_utf8(&self.buff.split_to(ind + 1))
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map_err(|_| ReadlinesError::EncodingError)?
|
||||||
.to_owned()
|
.to_owned()
|
||||||
} else {
|
} else {
|
||||||
self.encoding
|
self.encoding
|
||||||
.decode(&self.buff.split_to(ind + 1), DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
&self.buff.split_to(ind + 1),
|
||||||
|
)
|
||||||
|
.map(Cow::into_owned)
|
||||||
|
.ok_or(ReadlinesError::EncodingError)?
|
||||||
};
|
};
|
||||||
return Ok(Async::Ready(Some(line)));
|
return Ok(Async::Ready(Some(line)));
|
||||||
}
|
}
|
||||||
|
@ -117,15 +118,17 @@ where
|
||||||
if ind + 1 > self.limit {
|
if ind + 1 > self.limit {
|
||||||
return Err(ReadlinesError::LimitOverflow);
|
return Err(ReadlinesError::LimitOverflow);
|
||||||
}
|
}
|
||||||
let enc: *const Encoding = self.encoding as *const Encoding;
|
let line = if self.encoding == UTF_8 {
|
||||||
let line = if enc == UTF_8 {
|
|
||||||
str::from_utf8(&bytes.split_to(ind + 1))
|
str::from_utf8(&bytes.split_to(ind + 1))
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map_err(|_| ReadlinesError::EncodingError)?
|
||||||
.to_owned()
|
.to_owned()
|
||||||
} else {
|
} else {
|
||||||
self.encoding
|
self.encoding
|
||||||
.decode(&bytes.split_to(ind + 1), DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
&bytes.split_to(ind + 1),
|
||||||
|
)
|
||||||
|
.map(Cow::into_owned)
|
||||||
|
.ok_or(ReadlinesError::EncodingError)?
|
||||||
};
|
};
|
||||||
// extend buffer with rest of the bytes;
|
// extend buffer with rest of the bytes;
|
||||||
self.buff.extend_from_slice(&bytes);
|
self.buff.extend_from_slice(&bytes);
|
||||||
|
@ -143,15 +146,15 @@ where
|
||||||
if self.buff.len() > self.limit {
|
if self.buff.len() > self.limit {
|
||||||
return Err(ReadlinesError::LimitOverflow);
|
return Err(ReadlinesError::LimitOverflow);
|
||||||
}
|
}
|
||||||
let enc: *const Encoding = self.encoding as *const Encoding;
|
let line = if self.encoding == UTF_8 {
|
||||||
let line = if enc == UTF_8 {
|
|
||||||
str::from_utf8(&self.buff)
|
str::from_utf8(&self.buff)
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map_err(|_| ReadlinesError::EncodingError)?
|
||||||
.to_owned()
|
.to_owned()
|
||||||
} else {
|
} else {
|
||||||
self.encoding
|
self.encoding
|
||||||
.decode(&self.buff, DecoderTrap::Strict)
|
.decode_without_bom_handling_and_without_replacement(&self.buff)
|
||||||
.map_err(|_| ReadlinesError::EncodingError)?
|
.map(Cow::into_owned)
|
||||||
|
.ok_or(ReadlinesError::EncodingError)?
|
||||||
};
|
};
|
||||||
self.buff.clear();
|
self.buff.clear();
|
||||||
Ok(Async::Ready(Some(line)))
|
Ok(Async::Ready(Some(line)))
|
||||||
|
|
Loading…
Reference in a new issue