Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make encoding_rs an optional feature #2175

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ features = [
]

[features]
default = ["default-tls", "http2", "macos-system-configuration"]
default = ["default-tls", "encoding_rs", "http2", "macos-system-configuration"]

# Note: this doesn't enable the 'native-tls' feature, which adds specific
# functionality for it.
Expand Down Expand Up @@ -60,7 +60,7 @@ json = ["dep:serde_json"]
multipart = ["dep:mime_guess"]

# Deprecated, remove this feature while bumping minor versions.
trust-dns = ["dep:trust-dns-resolver"]
trust-dns = []
hickory-dns = ["dep:hickory-resolver"]

stream = ["tokio/fs", "dep:tokio-util", "dep:wasm-streams"]
Expand Down Expand Up @@ -107,7 +107,7 @@ serde_json = { version = "1.0", optional = true }
mime_guess = { version = "2.0", default-features = false, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
encoding_rs = "0.8"
encoding_rs = { version = "0.8", optional = true }
http-body = "1"
http-body-util = "0.1"
hyper = { version = "1", features = ["http1", "client"] }
Expand Down
30 changes: 27 additions & 3 deletions src/async_impl/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use std::net::SocketAddr;
use std::pin::Pin;

use bytes::Bytes;
use encoding_rs::{Encoding, UTF_8};
use http_body_util::BodyExt;
use hyper::{HeaderMap, StatusCode, Version};
use hyper_util::client::legacy::connect::HttpInfo;
use mime::Mime;
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;
#[cfg(feature = "json")]
Expand All @@ -21,6 +19,11 @@ use crate::async_impl::body::ResponseBody;
#[cfg(feature = "cookies")]
use crate::cookie;

#[cfg(feature = "encoding_rs")]
use encoding_rs::{Encoding, UTF_8};
#[cfg(feature = "encoding_rs")]
use mime::Mime;

/// A Response to a submitted `Request`.
pub struct Response {
pub(super) res: hyper::Response<Decoder>,
Expand Down Expand Up @@ -135,6 +138,11 @@ impl Response {
///
/// Note that the BOM is stripped from the returned String.
///
/// # Note
///
/// If the `encoding_rs` feature is disabled the method will only attempt to decode the
/// response as UTF-8, regardless of the given `Content-Type`
///
/// # Example
///
/// ```
Expand All @@ -149,7 +157,17 @@ impl Response {
/// # }
/// ```
pub async fn text(self) -> crate::Result<String> {
self.text_with_charset("utf-8").await
#[cfg(feature = "encoding_rs")]
{
self.text_with_charset("utf-8").await
}

#[cfg(not(feature = "encoding_rs"))]
{
let full = self.bytes().await?;
let text = String::from_utf8_lossy(&full);
Ok(text.into_owned())
}
}

/// Get the full response text given a specific encoding.
Expand All @@ -164,6 +182,10 @@ impl Response {
///
/// [`encoding_rs`]: https://docs.rs/encoding_rs/0.8/encoding_rs/#relationship-with-windows-code-pages
///
/// # Optional
///
/// This requires the optional `encoding_rs` feature enabled.
///
/// # Example
///
/// ```
Expand All @@ -177,6 +199,8 @@ impl Response {
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "encoding_rs")]
#[cfg_attr(docsrs, doc(cfg(feature = "encoding_rs")))]
pub async fn text_with_charset(self, default_encoding: &str) -> crate::Result<String> {
let content_type = self
.headers()
Expand Down
16 changes: 15 additions & 1 deletion src/blocking/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ impl Response {
/// Encoding is determined from the `charset` parameter of `Content-Type` header,
/// and defaults to `utf-8` if not presented.
///
/// # Note
///
/// If the `encoding_rs` feature is disabled the method will only attempt to decode the
/// response as UTF-8, regardless of the given `Content-Type`
///
/// # Example
///
/// ```rust
Expand All @@ -280,7 +285,10 @@ impl Response {
/// # }
/// ```
pub fn text(self) -> crate::Result<String> {
self.text_with_charset("utf-8")
wait::timeout(self.inner.text(), self.timeout).map_err(|e| match e {
wait::Waited::TimedOut(e) => crate::error::decode(e),
wait::Waited::Inner(e) => e,
})
}

/// Get the response text given a specific encoding.
Expand All @@ -293,6 +301,10 @@ impl Response {
///
/// [`encoding_rs`]: https://docs.rs/encoding_rs/0.8/encoding_rs/#relationship-with-windows-code-pages
///
/// # Optional
///
/// This requires the optional `encoding_rs` feature enabled.
///
/// # Example
///
/// ```rust
Expand All @@ -303,6 +315,8 @@ impl Response {
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "encoding_rs")]
#[cfg_attr(docsrs, doc(cfg(feature = "encoding_rs")))]
pub fn text_with_charset(self, default_encoding: &str) -> crate::Result<String> {
wait::timeout(self.inner.text_with_charset(default_encoding), self.timeout).map_err(|e| {
match e {
Expand Down