Simplify response body decoding when there is no content encoding.

This commit is contained in:
Jean-Christophe Amiel 2024-07-04 13:20:48 +02:00
parent 1ed0b4e29c
commit 3141e344cd
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC

View File

@ -67,12 +67,17 @@ impl ContentEncoding {
impl Response {
/// Returns response body as text.
pub fn text(&self) -> Result<String, HttpError> {
let encoding = self.headers.character_encoding()?;
let body = &self.uncompress_body()?;
match encoding.decode(body, DecoderTrap::Strict) {
let content_encodings = self.headers.content_encoding()?;
let body = if content_encodings.is_empty() {
&self.body
} else {
&self.uncompress_body()?
};
let character_encoding = self.headers.character_encoding()?;
match character_encoding.decode(body, DecoderTrap::Strict) {
Ok(s) => Ok(s),
Err(_) => Err(HttpError::InvalidDecoding {
charset: encoding.name().to_string(),
charset: character_encoding.name().to_string(),
}),
}
}