| 392 | } |
| 393 | |
| 394 | fn convert_to_text<B>(body: B, content_type: &str) -> BodyFuture |
| 395 | where |
| 396 | B: HttpBody + Unpin + Send + 'static, |
| 397 | B::Data: Send, |
| 398 | B::Error: fmt::Debug, |
| 399 | { |
| 400 | let mime_type = content_type.parse::<Mime>(); |
| 401 | |
| 402 | let encoding = match mime_type.as_ref() { |
| 403 | Ok(mime) => mime.get_param(CHARSET).unwrap_or(mime::UTF_8), |
| 404 | Err(_) => mime::UTF_8, |
| 405 | }; |
| 406 | |
| 407 | let label = encoding.as_ref().as_bytes(); |
| 408 | let encoding = Encoding::for_label(label).unwrap_or(encoding_rs::UTF_8); |
| 409 | |
| 410 | // assumes utf-8 |
| 411 | Box::pin(async move { |
| 412 | let bytes = body.collect().await.expect("unable to read bytes from body").to_bytes(); |
| 413 | let (content, _, _) = encoding.decode(&bytes); |
| 414 | |
| 415 | match content { |
| 416 | Cow::Borrowed(content) => Body::from(content), |
| 417 | Cow::Owned(content) => Body::from(content), |
| 418 | } |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | pub type BodyFuture = Pin<Box<dyn Future<Output = Body> + Send>>; |
| 423 | |