mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
35 lines
910 B
Rust
35 lines
910 B
Rust
use http_body_util::BodyExt;
|
|
use hyper::{
|
|
body::{Bytes, Incoming},
|
|
header::CONTENT_ENCODING,
|
|
HeaderMap,
|
|
};
|
|
|
|
use mlua::prelude::*;
|
|
|
|
use lune_std_serde::{decompress, CompressDecompressFormat};
|
|
|
|
pub async fn handle_incoming_body(
|
|
headers: &HeaderMap,
|
|
body: Incoming,
|
|
should_decompress: bool,
|
|
) -> LuaResult<(Bytes, bool)> {
|
|
let mut body = body.collect().await.into_lua_err()?.to_bytes();
|
|
|
|
let was_decompressed = if should_decompress {
|
|
let decompress_format = headers
|
|
.get(CONTENT_ENCODING)
|
|
.and_then(|value| value.to_str().ok())
|
|
.and_then(CompressDecompressFormat::detect_from_header_str);
|
|
if let Some(format) = decompress_format {
|
|
body = Bytes::from(decompress(body, format).await?);
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
} else {
|
|
false
|
|
};
|
|
|
|
Ok((body, was_decompressed))
|
|
}
|