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,22 +4,21 @@ use http::request::Parts;
use mlua::prelude::*; use mlua::prelude::*;
use crate::lune::util::TableBuilder;
pub(super) struct LuaRequest { pub(super) struct LuaRequest {
pub(super) _remote_addr: SocketAddr, pub(super) _remote_addr: SocketAddr,
pub(super) head: Parts, pub(super) head: Parts,
pub(super) body: Vec<u8>, pub(super) body: Vec<u8>,
} }
impl LuaUserData for LuaRequest { impl LuaRequest {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { pub fn into_lua_table(self, lua: &Lua) -> LuaResult<LuaTable> {
fields.add_field_method_get("method", |_, this| { let method = self.head.method.as_str().to_string();
Ok(this.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
fields.add_field_method_get("query", |_, this| {
let query: HashMap<String, String> = this
.head .head
.uri .uri
.query() .query()
@ -28,19 +27,19 @@ impl LuaUserData for LuaRequest {
.filter_map(|q| q.split_once('=')) .filter_map(|q| q.split_once('='))
.map(|(k, v)| (k.to_string(), v.to_string())) .map(|(k, v)| (k.to_string(), v.to_string()))
.collect(); .collect();
Ok(query) let headers: HashMap<String, Vec<u8>> = self
});
fields.add_field_method_get("headers", |_, this| {
let headers: HashMap<String, Vec<u8>> = this
.head .head
.headers .headers
.iter() .iter()
.map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec())) .map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec()))
.collect(); .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, head,
body, 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.track_thread(thread_id);
lua.wait_for_thread(thread_id).await; lua.wait_for_thread(thread_id).await;
let thread_res = lua let thread_res = lua