Share incoming body implementation between both client and server

This commit is contained in:
Filip Tibell 2025-04-27 13:05:00 +02:00
parent 85ba4e66fe
commit af46cbbe1b
4 changed files with 49 additions and 37 deletions

View file

@ -0,0 +1,31 @@
use futures_lite::prelude::*;
use http_body_util::BodyStream;
use hyper::{
body::{Body as _, Bytes, Incoming},
HeaderMap,
};
use mlua::prelude::*;
pub async fn handle_incoming_body(
_headers: &HeaderMap,
body: Incoming,
_decompress: bool,
) -> LuaResult<(Bytes, bool)> {
let size = body.size_hint().lower() as usize;
let buffer = Vec::<u8>::with_capacity(size);
let body = BodyStream::new(body)
.try_fold(buffer, |mut body, chunk| {
if let Some(chunk) = chunk.data_ref() {
body.extend_from_slice(chunk);
}
Ok(body)
})
.await
.into_lua_err()?;
// TODO: Decompress the body if necessary
Ok((Bytes::from(body), false))
}

View file

@ -1,4 +1,5 @@
pub mod headers;
pub mod hyper;
pub mod incoming;
pub mod request;
pub mod response;

View file

@ -1,11 +1,10 @@
use std::collections::HashMap;
use futures_lite::prelude::*;
use http_body_util::{BodyStream, Full};
use http_body_util::Full;
use url::Url;
use hyper::{
body::{Body as _, Bytes, Incoming},
body::{Bytes, Incoming},
header::{HeaderName, HeaderValue},
HeaderMap, Method, Request as HyperRequest,
};
@ -14,7 +13,10 @@ use mlua::prelude::*;
use crate::{
client::config::RequestConfig,
shared::headers::{hash_map_to_table, header_map_to_table},
shared::{
headers::{hash_map_to_table, header_map_to_table},
incoming::handle_incoming_body,
},
};
#[derive(Debug, Clone)]
@ -36,22 +38,10 @@ impl Request {
) -> LuaResult<Self> {
let (parts, body) = incoming.into_parts();
let size = body.size_hint().lower() as usize;
let buffer = Vec::<u8>::with_capacity(size);
let body = BodyStream::new(body)
.try_fold(buffer, |mut body, chunk| {
if let Some(chunk) = chunk.data_ref() {
body.extend_from_slice(chunk);
}
Ok(body)
})
.await
.into_lua_err()?;
// TODO: Decompress body if decompress is true and headers are present
let (body, decompress) = handle_incoming_body(&parts.headers, body, decompress).await?;
Ok(Self {
inner: HyperRequest::from_parts(parts, Bytes::from(body)),
inner: HyperRequest::from_parts(parts, body),
redirects: 0,
decompress,
})

View file

@ -1,15 +1,17 @@
use futures_lite::prelude::*;
use http_body_util::{BodyStream, Full};
use http_body_util::Full;
use hyper::{
body::{Body, Bytes, Incoming},
body::{Bytes, Incoming},
header::{HeaderName, HeaderValue},
HeaderMap, Response as HyperResponse,
};
use mlua::prelude::*;
use crate::{server::config::ResponseConfig, shared::headers::header_map_to_table};
use crate::{
server::config::ResponseConfig,
shared::{headers::header_map_to_table, incoming::handle_incoming_body},
};
#[derive(Debug, Clone)]
pub struct Response {
@ -29,23 +31,11 @@ impl Response {
) -> LuaResult<Self> {
let (parts, body) = incoming.into_parts();
let size = body.size_hint().lower() as usize;
let buffer = Vec::<u8>::with_capacity(size);
let body = BodyStream::new(body)
.try_fold(buffer, |mut body, chunk| {
if let Some(chunk) = chunk.data_ref() {
body.extend_from_slice(chunk);
}
Ok(body)
})
.await
.into_lua_err()?;
// TODO: Decompress body if decompress is true and headers are present
let (body, decompressed) = handle_incoming_body(&parts.headers, body, decompress).await?;
Ok(Self {
inner: HyperResponse::from_parts(parts, Bytes::from(body)),
decompressed: decompress,
inner: HyperResponse::from_parts(parts, body),
decompressed,
})
}