@@ -3,11 +3,9 @@ use std::net::SocketAddr;
33use std:: pin:: Pin ;
44
55use bytes:: Bytes ;
6- use encoding_rs:: { Encoding , UTF_8 } ;
76use http_body_util:: BodyExt ;
87use hyper:: { HeaderMap , StatusCode , Version } ;
98use hyper_util:: client:: legacy:: connect:: HttpInfo ;
10- use mime:: Mime ;
119#[ cfg( feature = "json" ) ]
1210use serde:: de:: DeserializeOwned ;
1311#[ cfg( feature = "json" ) ]
@@ -21,6 +19,11 @@ use crate::async_impl::body::ResponseBody;
2119#[ cfg( feature = "cookies" ) ]
2220use crate :: cookie;
2321
22+ #[ cfg( feature = "charset" ) ]
23+ use encoding_rs:: { Encoding , UTF_8 } ;
24+ #[ cfg( feature = "charset" ) ]
25+ use mime:: Mime ;
26+
2427/// A Response to a submitted `Request`.
2528pub struct Response {
2629 pub ( super ) res : hyper:: Response < Decoder > ,
@@ -135,6 +138,11 @@ impl Response {
135138 ///
136139 /// Note that the BOM is stripped from the returned String.
137140 ///
141+ /// # Note
142+ ///
143+ /// If the `charset` feature is disabled the method will only attempt to decode the
144+ /// response as UTF-8, regardless of the given `Content-Type`
145+ ///
138146 /// # Example
139147 ///
140148 /// ```
@@ -149,7 +157,17 @@ impl Response {
149157 /// # }
150158 /// ```
151159 pub async fn text ( self ) -> crate :: Result < String > {
152- self . text_with_charset ( "utf-8" ) . await
160+ #[ cfg( feature = "charset" ) ]
161+ {
162+ self . text_with_charset ( "utf-8" ) . await
163+ }
164+
165+ #[ cfg( not( feature = "charset" ) ) ]
166+ {
167+ let full = self . bytes ( ) . await ?;
168+ let text = String :: from_utf8_lossy ( & full) ;
169+ Ok ( text. into_owned ( ) )
170+ }
153171 }
154172
155173 /// Get the full response text given a specific encoding.
@@ -164,6 +182,10 @@ impl Response {
164182 ///
165183 /// [`encoding_rs`]: https://docs.rs/encoding_rs/0.8/encoding_rs/#relationship-with-windows-code-pages
166184 ///
185+ /// # Optional
186+ ///
187+ /// This requires the optional `encoding_rs` feature enabled.
188+ ///
167189 /// # Example
168190 ///
169191 /// ```
@@ -177,6 +199,8 @@ impl Response {
177199 /// # Ok(())
178200 /// # }
179201 /// ```
202+ #[ cfg( feature = "charset" ) ]
203+ #[ cfg_attr( docsrs, doc( cfg( feature = "charset" ) ) ) ]
180204 pub async fn text_with_charset ( self , default_encoding : & str ) -> crate :: Result < String > {
181205 let content_type = self
182206 . headers ( )
0 commit comments