diff --git a/core/http/Server.go b/core/http/Server.go index 902c67a..6b87c0a 100644 --- a/core/http/Server.go +++ b/core/http/Server.go @@ -268,25 +268,7 @@ func writeResponse(ctx *fasthttp.RequestCtx, result any) { // Set cookies for _, cookie := range httpResp.Cookies { - // Convert net/http cookie to fasthttp cookie - var c fasthttp.Cookie - c.SetKey(cookie.Name) - c.SetValue(cookie.Value) - if cookie.Path != "" { - c.SetPath(cookie.Path) - } - if cookie.Domain != "" { - c.SetDomain(cookie.Domain) - } - if cookie.MaxAge > 0 { - c.SetMaxAge(cookie.MaxAge) - } - if cookie.Expires.After(time.Time{}) { - c.SetExpire(cookie.Expires) - } - c.SetSecure(cookie.Secure) - c.SetHTTPOnly(cookie.HttpOnly) - ctx.Response.Header.SetCookie(&c) + ctx.Response.Header.SetCookie(cookie) } // Set status code diff --git a/core/runner/CookieModule.go b/core/runner/CookieModule.go index 7047952..356217b 100644 --- a/core/runner/CookieModule.go +++ b/core/runner/CookieModule.go @@ -1,45 +1,46 @@ package runner import ( - "net/http" "time" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" + "github.com/valyala/fasthttp" ) // extractCookie grabs cookies from the Lua state -func extractCookie(state *luajit.State) *http.Cookie { - cookie := &http.Cookie{ - Path: "/", // Default path - } +func extractCookie(state *luajit.State) *fasthttp.Cookie { + cookie := fasthttp.AcquireCookie() // Get name state.GetField(-1, "name") if !state.IsString(-1) { state.Pop(1) + fasthttp.ReleaseCookie(cookie) return nil // Name is required } - cookie.Name = state.ToString(-1) + cookie.SetKey(state.ToString(-1)) state.Pop(1) // Get value state.GetField(-1, "value") if state.IsString(-1) { - cookie.Value = state.ToString(-1) + cookie.SetValue(state.ToString(-1)) } state.Pop(1) // Get path state.GetField(-1, "path") if state.IsString(-1) { - cookie.Path = state.ToString(-1) + cookie.SetPath(state.ToString(-1)) + } else { + cookie.SetPath("/") // Default path } state.Pop(1) // Get domain state.GetField(-1, "domain") if state.IsString(-1) { - cookie.Domain = state.ToString(-1) + cookie.SetDomain(state.ToString(-1)) } state.Pop(1) @@ -47,28 +48,28 @@ func extractCookie(state *luajit.State) *http.Cookie { state.GetField(-1, "expires") if state.IsNumber(-1) { expiry := int64(state.ToNumber(-1)) - cookie.Expires = time.Unix(expiry, 0) + cookie.SetExpire(time.Unix(expiry, 0)) } state.Pop(1) // Get max age state.GetField(-1, "max_age") if state.IsNumber(-1) { - cookie.MaxAge = int(state.ToNumber(-1)) + cookie.SetMaxAge(int(state.ToNumber(-1))) } state.Pop(1) // Get secure state.GetField(-1, "secure") if state.IsBoolean(-1) { - cookie.Secure = state.ToBoolean(-1) + cookie.SetSecure(state.ToBoolean(-1)) } state.Pop(1) // Get http only state.GetField(-1, "http_only") if state.IsBoolean(-1) { - cookie.HttpOnly = state.ToBoolean(-1) + cookie.SetHTTPOnly(state.ToBoolean(-1)) } state.Pop(1) diff --git a/core/runner/HttpModule.go b/core/runner/HttpModule.go index 6f69b75..ea28e9f 100644 --- a/core/runner/HttpModule.go +++ b/core/runner/HttpModule.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "net/http" "net/url" "strings" "sync" @@ -21,10 +20,10 @@ import ( // HTTPResponse represents an HTTP response from Lua type HTTPResponse struct { - Status int `json:"status"` - Headers map[string]string `json:"headers"` - Body any `json:"body"` - Cookies []*http.Cookie `json:"-"` + Status int `json:"status"` + Headers map[string]string `json:"headers"` + Body any `json:"body"` + Cookies []*fasthttp.Cookie `json:"-"` } // Response pool to reduce allocations @@ -32,8 +31,8 @@ var responsePool = sync.Pool{ New: func() interface{} { return &HTTPResponse{ Status: 200, - Headers: make(map[string]string, 8), // Pre-allocate with reasonable capacity - Cookies: make([]*http.Cookie, 0, 4), // Pre-allocate with reasonable capacity + Headers: make(map[string]string, 8), // Pre-allocate with reasonable capacity + Cookies: make([]*fasthttp.Cookie, 0, 4), // Pre-allocate with reasonable capacity } }, } @@ -492,25 +491,7 @@ func ApplyHTTPResponse(httpResp *HTTPResponse, ctx *fasthttp.RequestCtx) { // Set cookies for _, cookie := range httpResp.Cookies { - // Convert net/http cookie to fasthttp cookie - var c fasthttp.Cookie - c.SetKey(cookie.Name) - c.SetValue(cookie.Value) - if cookie.Path != "" { - c.SetPath(cookie.Path) - } - if cookie.Domain != "" { - c.SetDomain(cookie.Domain) - } - if cookie.MaxAge > 0 { - c.SetMaxAge(cookie.MaxAge) - } - if cookie.Expires.After(time.Time{}) { - c.SetExpire(cookie.Expires) - } - c.SetSecure(cookie.Secure) - c.SetHTTPOnly(cookie.HttpOnly) - ctx.Response.Header.SetCookie(&c) + ctx.Response.Header.SetCookie(cookie) } // Process the body based on its type diff --git a/core/runner/SessionHandler.go b/core/runner/SessionHandler.go index 92d1a5c..b423b30 100644 --- a/core/runner/SessionHandler.go +++ b/core/runner/SessionHandler.go @@ -1,7 +1,7 @@ package runner import ( - "net/http" + "github.com/valyala/fasthttp" "Moonshark/core/sessions" "Moonshark/core/utils/logger" @@ -157,7 +157,7 @@ func (h *SessionHandler) addSessionCookie(resp *HTTPResponse, sessionID string) // Check if session cookie is already set cookieName := opts["name"].(string) for _, cookie := range resp.Cookies { - if cookie.Name == cookieName { + if string(cookie.Key()) == cookieName { h.debug("Session cookie already set in response") return } @@ -166,21 +166,20 @@ func (h *SessionHandler) addSessionCookie(resp *HTTPResponse, sessionID string) h.debug("Adding session cookie to response") // Create and add cookie - cookie := &http.Cookie{ - Name: cookieName, - Value: sessionID, - Path: opts["path"].(string), - HttpOnly: opts["http_only"].(bool), - MaxAge: opts["max_age"].(int), - } + cookie := fasthttp.AcquireCookie() + cookie.SetKey(cookieName) + cookie.SetValue(sessionID) + cookie.SetPath(opts["path"].(string)) + cookie.SetHTTPOnly(opts["http_only"].(bool)) + cookie.SetMaxAge(opts["max_age"].(int)) // Optional cookie parameters if domain, ok := opts["domain"].(string); ok && domain != "" { - cookie.Domain = domain + cookie.SetDomain(domain) } if secure, ok := opts["secure"].(bool); ok { - cookie.Secure = secure + cookie.SetSecure(secure) } resp.Cookies = append(resp.Cookies, cookie)