Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use encoding_rs crate instead of unmaintained encoding crate (#922)
* Use encoding_rs crate instead of unmaintained encoding crate

* Update changelog
  • Loading branch information
messense authored and fafhrd91 committed Jun 18, 2019
1 parent d7780d5 commit 313ac48
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 45 deletions.
16 changes: 11 additions & 5 deletions CHANGES.md
@@ -1,8 +1,14 @@
# Changes

## [1.0.3] - unreleased

### Changed

* Use `encoding_rs` crate instead of unmaintained `encoding` crate

## [1.0.2] - 2019-06-17

### Changes
### Changed

* Move cors middleware to `actix-cors` crate.

Expand All @@ -17,7 +23,7 @@

* Add `middleware::identity::RequestIdentity` trait to `get_identity` from `HttpMessage`.

### Changes
### Changed

* Move cors middleware to `actix-cors` crate.

Expand Down Expand Up @@ -47,7 +53,7 @@

* Add macros for head, options, trace, connect and patch http methods

### Changes
### Changed

* Drop an unnecessary `Option<_>` indirection around `ServerBuilder` from `HttpServer`. #863

Expand All @@ -65,7 +71,7 @@
* Add `Query<T>::from_query()` to extract parameters from a query string. #846
* `QueryConfig`, similar to `JsonConfig` for customizing error handling of query extractors.

### Changes
### Changed

* `JsonConfig` is now `Send + Sync`, this implies that `error_handler` must be `Send + Sync` too.

Expand All @@ -80,7 +86,7 @@

* Allow to set/override app data on scope level

### Changes
### Changed

* `App::configure` take an `FnOnce` instead of `Fn`
* Upgrade actix-net crates
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -83,7 +83,7 @@ awc = { version = "0.2.1", optional = true }

bytes = "0.4"
derive_more = "0.15.0"
encoding = "0.2"
encoding_rs = "0.8"
futures = "0.1.25"
hashbrown = "0.5.0"
log = "0.4"
Expand Down
8 changes: 7 additions & 1 deletion actix-http/CHANGES.md
@@ -1,5 +1,11 @@
# Changes

## [0.2.5] - unreleased

### Changed

* Use `encoding_rs` crate instead of unmaintained `encoding` crate

## [0.2.4] - 2019-06-16

### Fixed
Expand Down Expand Up @@ -83,7 +89,7 @@

## [0.1.1] - 2019-04-19

### Changes
### Changed

* Cookie::max_age() accepts value in seconds

Expand Down
2 changes: 1 addition & 1 deletion actix-http/Cargo.toml
Expand Up @@ -58,7 +58,7 @@ byteorder = "1.2"
copyless = "0.1.2"
derive_more = "0.15.0"
either = "1.5.2"
encoding = "0.2"
encoding_rs = "0.8"
futures = "0.1.25"
hashbrown = "0.5.0"
h2 = "0.1.16"
Expand Down
15 changes: 7 additions & 8 deletions actix-http/src/httpmessage.rs
@@ -1,9 +1,7 @@
use std::cell::{Ref, RefMut};
use std::str;

use encoding::all::UTF_8;
use encoding::label::encoding_from_whatwg_label;
use encoding::EncodingRef;
use encoding_rs::{Encoding, UTF_8};
use http::header;
use mime::Mime;

Expand Down Expand Up @@ -59,10 +57,12 @@ pub trait HttpMessage: Sized {
/// Get content type encoding
///
/// 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(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)
} else {
Err(ContentTypeError::UnknownEncoding)
Expand Down Expand Up @@ -166,8 +166,7 @@ where
#[cfg(test)]
mod tests {
use bytes::Bytes;
use encoding::all::ISO_8859_2;
use encoding::Encoding;
use encoding_rs::ISO_8859_2;
use mime;

use super::*;
Expand Down Expand Up @@ -223,7 +222,7 @@ mod tests {
"application/json; charset=ISO-8859-2",
)
.finish();
assert_eq!(ISO_8859_2.name(), req.encoding().unwrap().name());
assert_eq!(ISO_8859_2, req.encoding().unwrap());
}

#[test]
Expand Down
13 changes: 6 additions & 7 deletions src/types/form.rs
Expand Up @@ -5,9 +5,7 @@ use std::{fmt, ops};

use actix_http::{Error, HttpMessage, Payload};
use bytes::BytesMut;
use encoding::all::UTF_8;
use encoding::types::{DecoderTrap, Encoding};
use encoding::EncodingRef;
use encoding_rs::{Encoding, UTF_8};
use futures::{Future, Poll, Stream};
use serde::de::DeserializeOwned;

Expand Down Expand Up @@ -187,7 +185,7 @@ pub struct UrlEncoded<U> {
stream: Option<Decompress<Payload>>,
limit: usize,
length: Option<usize>,
encoding: EncodingRef,
encoding: &'static Encoding,
err: Option<UrlencodedError>,
fut: Option<Box<Future<Item = U, Error = UrlencodedError>>>,
}
Expand Down Expand Up @@ -286,13 +284,14 @@ where
}
})
.and_then(move |body| {
if (encoding as *const Encoding) == UTF_8 {
if encoding == UTF_8 {
serde_urlencoded::from_bytes::<U>(&body)
.map_err(|_| UrlencodedError::Parse)
} else {
let body = encoding
.decode(&body, DecoderTrap::Strict)
.map_err(|_| UrlencodedError::Parse)?;
.decode_without_bom_handling_and_without_replacement(&body)
.map(|s| s.into_owned())
.ok_or(UrlencodedError::Parse)?;
serde_urlencoded::from_str::<U>(&body)
.map_err(|_| UrlencodedError::Parse)
}
Expand Down
11 changes: 5 additions & 6 deletions src/types/payload.rs
Expand Up @@ -4,8 +4,7 @@ use std::str;
use actix_http::error::{Error, ErrorBadRequest, PayloadError};
use actix_http::HttpMessage;
use bytes::{Bytes, BytesMut};
use encoding::all::UTF_8;
use encoding::types::{DecoderTrap, Encoding};
use encoding_rs::UTF_8;
use futures::future::{err, Either, FutureResult};
use futures::{Future, Poll, Stream};
use mime::Mime;
Expand Down Expand Up @@ -208,15 +207,15 @@ impl FromRequest for String {
.limit(limit)
.from_err()
.and_then(move |body| {
let enc: *const Encoding = encoding as *const Encoding;
if enc == UTF_8 {
if encoding == UTF_8 {
Ok(str::from_utf8(body.as_ref())
.map_err(|_| ErrorBadRequest("Can not decode body"))?
.to_owned())
} else {
Ok(encoding
.decode(&body, DecoderTrap::Strict)
.map_err(|_| ErrorBadRequest("Can not decode body"))?)
.decode_without_bom_handling_and_without_replacement(&body)
.map(|s| s.into_owned())
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
}
}),
))
Expand Down
35 changes: 19 additions & 16 deletions src/types/readlines.rs
@@ -1,9 +1,8 @@
use std::borrow::Cow;
use std::str;

use bytes::{Bytes, BytesMut};
use encoding::all::UTF_8;
use encoding::types::{DecoderTrap, Encoding};
use encoding::EncodingRef;
use encoding_rs::{Encoding, UTF_8};
use futures::{Async, Poll, Stream};

use crate::dev::Payload;
Expand All @@ -16,7 +15,7 @@ pub struct Readlines<T: HttpMessage> {
buff: BytesMut,
limit: usize,
checked_buff: bool,
encoding: EncodingRef,
encoding: &'static Encoding,
err: Option<ReadlinesError>,
}

Expand Down Expand Up @@ -87,15 +86,17 @@ where
if ind + 1 > self.limit {
return Err(ReadlinesError::LimitOverflow);
}
let enc: *const Encoding = self.encoding as *const Encoding;
let line = if enc == UTF_8 {
let line = if self.encoding == UTF_8 {
str::from_utf8(&self.buff.split_to(ind + 1))
.map_err(|_| ReadlinesError::EncodingError)?
.to_owned()
} else {
self.encoding
.decode(&self.buff.split_to(ind + 1), DecoderTrap::Strict)
.map_err(|_| ReadlinesError::EncodingError)?
.decode_without_bom_handling_and_without_replacement(
&self.buff.split_to(ind + 1),
)
.map(Cow::into_owned)
.ok_or(ReadlinesError::EncodingError)?
};
return Ok(Async::Ready(Some(line)));
}
Expand All @@ -117,15 +118,17 @@ where
if ind + 1 > self.limit {
return Err(ReadlinesError::LimitOverflow);
}
let enc: *const Encoding = self.encoding as *const Encoding;
let line = if enc == UTF_8 {
let line = if self.encoding == UTF_8 {
str::from_utf8(&bytes.split_to(ind + 1))
.map_err(|_| ReadlinesError::EncodingError)?
.to_owned()
} else {
self.encoding
.decode(&bytes.split_to(ind + 1), DecoderTrap::Strict)
.map_err(|_| ReadlinesError::EncodingError)?
.decode_without_bom_handling_and_without_replacement(
&bytes.split_to(ind + 1),
)
.map(Cow::into_owned)
.ok_or(ReadlinesError::EncodingError)?
};
// extend buffer with rest of the bytes;
self.buff.extend_from_slice(&bytes);
Expand All @@ -143,15 +146,15 @@ where
if self.buff.len() > self.limit {
return Err(ReadlinesError::LimitOverflow);
}
let enc: *const Encoding = self.encoding as *const Encoding;
let line = if enc == UTF_8 {
let line = if self.encoding == UTF_8 {
str::from_utf8(&self.buff)
.map_err(|_| ReadlinesError::EncodingError)?
.to_owned()
} else {
self.encoding
.decode(&self.buff, DecoderTrap::Strict)
.map_err(|_| ReadlinesError::EncodingError)?
.decode_without_bom_handling_and_without_replacement(&self.buff)
.map(Cow::into_owned)
.ok_or(ReadlinesError::EncodingError)?
};
self.buff.clear();
Ok(Async::Ready(Some(line)))
Expand Down

0 comments on commit 313ac48

Please sign in to comment.