From 195800082a16d067f611313ea92527bcd9cd4e2c Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 10 Apr 2025 10:44:52 -0500 Subject: [PATCH] start sessions.... again --- core/http/Server.go | 13 +++++++++---- core/runner/Sandbox.go | 12 ++---------- core/sessions/Session.go | 5 ++--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/core/http/Server.go b/core/http/Server.go index a308b3a..9c3e514 100644 --- a/core/http/Server.go +++ b/core/http/Server.go @@ -135,7 +135,7 @@ func (s *Server) processRequest(ctx *fasthttp.RequestCtx) { return } else if found { logger.Debug("Found Lua route match for %s %s with %d params", method, path, params.Count) - s.handleLuaRoute(ctx, bytecode, scriptPath, params) + s.handleLuaRoute(ctx, bytecode, scriptPath, params, method, path) return } @@ -152,13 +152,11 @@ func (s *Server) processRequest(ctx *fasthttp.RequestCtx) { } // handleLuaRoute executes a Lua route -func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scriptPath string, params *routers.Params) { +func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scriptPath string, params *routers.Params, method string, path string) { // Create context for Lua execution luaCtx := runner.NewHTTPContext(ctx) defer luaCtx.Release() - method := string(ctx.Method()) - path := string(ctx.Path()) host := string(ctx.Host()) // Set up additional context values @@ -166,6 +164,13 @@ func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scrip luaCtx.Set("path", path) luaCtx.Set("host", host) + // Add session data + session := s.sessionManager.GetSessionFromRequest(ctx) + luaCtx.Set("session", map[string]any{ + "id": session.ID, + "data": session.Data, + }) + // URL parameters if params.Count > 0 { paramMap := make(map[string]any, params.Count) diff --git a/core/runner/Sandbox.go b/core/runner/Sandbox.go index a33a97a..436d9df 100644 --- a/core/runner/Sandbox.go +++ b/core/runner/Sandbox.go @@ -139,19 +139,12 @@ func (s *Sandbox) Execute(state *luajit.State, bytecode []byte, ctx *Context) (* // extractResponseData pulls response info from the Lua state func extractHTTPResponseData(state *luajit.State, response *Response) { - state.GetGlobal("__http_responses") + state.GetGlobal("__http_response") if !state.IsTable(-1) { state.Pop(1) return } - state.PushNumber(1) - state.GetTable(-2) - if !state.IsTable(-1) { - state.Pop(2) - return - } - // Extract status state.GetField(-1, "status") if state.IsNumber(-1) { @@ -200,8 +193,7 @@ func extractHTTPResponseData(state *luajit.State, response *Response) { } state.Pop(1) - // Clean up - state.Pop(2) + state.Pop(1) // Pop __http_response } // extractCookie pulls cookie data from the current table on the stack diff --git a/core/sessions/Session.go b/core/sessions/Session.go index 5bab42e..3581741 100644 --- a/core/sessions/Session.go +++ b/core/sessions/Session.go @@ -1,6 +1,7 @@ package sessions import ( + "maps" "sync" "time" ) @@ -66,9 +67,7 @@ func (s *Session) GetAll() map[string]any { defer s.mu.RUnlock() copy := make(map[string]any, len(s.Data)) - for k, v := range s.Data { - copy[k] = v - } + maps.Copy(copy, s.Data) return copy }