Unbreak accidental breaking change in net.serve

This commit is contained in:
Filip Tibell 2024-03-12 23:29:19 +01:00
parent 0116d405b2
commit 59aa780654
No known key found for this signature in database
2 changed files with 31 additions and 31 deletions

View file

@ -4,43 +4,42 @@ use http::request::Parts;
use mlua::prelude::*;
use crate::lune::util::TableBuilder;
pub(super) struct LuaRequest {
pub(super) _remote_addr: SocketAddr,
pub(super) head: Parts,
pub(super) body: Vec<u8>,
}
impl LuaUserData for LuaRequest {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("method", |_, this| {
Ok(this.head.method.as_str().to_string())
});
impl LuaRequest {
pub fn into_lua_table(self, lua: &Lua) -> LuaResult<LuaTable> {
let method = self.head.method.as_str().to_string();
let path = self.head.uri.path().to_string();
let body = lua.create_string(&self.body)?;
fields.add_field_method_get("path", |_, this| Ok(this.head.uri.path().to_string()));
let query: HashMap<String, String> = self
.head
.uri
.query()
.unwrap_or_default()
.split('&')
.filter_map(|q| q.split_once('='))
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
let headers: HashMap<String, Vec<u8>> = self
.head
.headers
.iter()
.map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec()))
.collect();
fields.add_field_method_get("query", |_, this| {
let query: HashMap<String, String> = this
.head
.uri
.query()
.unwrap_or_default()
.split('&')
.filter_map(|q| q.split_once('='))
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
Ok(query)
});
fields.add_field_method_get("headers", |_, this| {
let headers: HashMap<String, Vec<u8>> = this
.head
.headers
.iter()
.map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec()))
.collect();
Ok(headers)
});
fields.add_field_method_get("body", |lua, this| lua.create_string(&this.body));
TableBuilder::new(lua)?
.with_value("method", method)?
.with_value("path", path)?
.with_value("query", query)?
.with_value("headers", headers)?
.with_value("body", body)?
.build()
}
}

View file

@ -66,8 +66,9 @@ impl Service<Request<Incoming>> for Svc {
head,
body,
};
let lua_req_table = lua_req.into_lua_table(&lua)?;
let thread_id = lua.push_thread_back(handler_request, lua_req)?;
let thread_id = lua.push_thread_back(handler_request, lua_req_table)?;
lua.track_thread(thread_id);
lua.wait_for_thread(thread_id).await;
let thread_res = lua