From 59aa78065428e8d32f82ac232b4c418525177ed7 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Tue, 12 Mar 2024 23:29:19 +0100 Subject: [PATCH] Unbreak accidental breaking change in net.serve --- src/lune/builtins/net/server/request.rs | 59 ++++++++++++------------- src/lune/builtins/net/server/service.rs | 3 +- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/lune/builtins/net/server/request.rs b/src/lune/builtins/net/server/request.rs index 2eea689..dde529f 100644 --- a/src/lune/builtins/net/server/request.rs +++ b/src/lune/builtins/net/server/request.rs @@ -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, } -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 { + 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 = 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> = 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 = 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> = 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() } } diff --git a/src/lune/builtins/net/server/service.rs b/src/lune/builtins/net/server/service.rs index a37d669..7bc7e53 100644 --- a/src/lune/builtins/net/server/service.rs +++ b/src/lune/builtins/net/server/service.rs @@ -66,8 +66,9 @@ impl Service> 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