finish cookie migration

This commit is contained in:
Sky Johnson 2025-04-05 22:36:26 -05:00
parent 45ae6a9d31
commit b9d59bf57c
4 changed files with 32 additions and 69 deletions

View File

@ -268,25 +268,7 @@ func writeResponse(ctx *fasthttp.RequestCtx, result any) {
// Set cookies // Set cookies
for _, cookie := range httpResp.Cookies { for _, cookie := range httpResp.Cookies {
// Convert net/http cookie to fasthttp cookie ctx.Response.Header.SetCookie(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)
} }
// Set status code // Set status code

View File

@ -1,45 +1,46 @@
package runner package runner
import ( import (
"net/http"
"time" "time"
luajit "git.sharkk.net/Sky/LuaJIT-to-Go" luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
"github.com/valyala/fasthttp"
) )
// extractCookie grabs cookies from the Lua state // extractCookie grabs cookies from the Lua state
func extractCookie(state *luajit.State) *http.Cookie { func extractCookie(state *luajit.State) *fasthttp.Cookie {
cookie := &http.Cookie{ cookie := fasthttp.AcquireCookie()
Path: "/", // Default path
}
// Get name // Get name
state.GetField(-1, "name") state.GetField(-1, "name")
if !state.IsString(-1) { if !state.IsString(-1) {
state.Pop(1) state.Pop(1)
fasthttp.ReleaseCookie(cookie)
return nil // Name is required return nil // Name is required
} }
cookie.Name = state.ToString(-1) cookie.SetKey(state.ToString(-1))
state.Pop(1) state.Pop(1)
// Get value // Get value
state.GetField(-1, "value") state.GetField(-1, "value")
if state.IsString(-1) { if state.IsString(-1) {
cookie.Value = state.ToString(-1) cookie.SetValue(state.ToString(-1))
} }
state.Pop(1) state.Pop(1)
// Get path // Get path
state.GetField(-1, "path") state.GetField(-1, "path")
if state.IsString(-1) { if state.IsString(-1) {
cookie.Path = state.ToString(-1) cookie.SetPath(state.ToString(-1))
} else {
cookie.SetPath("/") // Default path
} }
state.Pop(1) state.Pop(1)
// Get domain // Get domain
state.GetField(-1, "domain") state.GetField(-1, "domain")
if state.IsString(-1) { if state.IsString(-1) {
cookie.Domain = state.ToString(-1) cookie.SetDomain(state.ToString(-1))
} }
state.Pop(1) state.Pop(1)
@ -47,28 +48,28 @@ func extractCookie(state *luajit.State) *http.Cookie {
state.GetField(-1, "expires") state.GetField(-1, "expires")
if state.IsNumber(-1) { if state.IsNumber(-1) {
expiry := int64(state.ToNumber(-1)) expiry := int64(state.ToNumber(-1))
cookie.Expires = time.Unix(expiry, 0) cookie.SetExpire(time.Unix(expiry, 0))
} }
state.Pop(1) state.Pop(1)
// Get max age // Get max age
state.GetField(-1, "max_age") state.GetField(-1, "max_age")
if state.IsNumber(-1) { if state.IsNumber(-1) {
cookie.MaxAge = int(state.ToNumber(-1)) cookie.SetMaxAge(int(state.ToNumber(-1)))
} }
state.Pop(1) state.Pop(1)
// Get secure // Get secure
state.GetField(-1, "secure") state.GetField(-1, "secure")
if state.IsBoolean(-1) { if state.IsBoolean(-1) {
cookie.Secure = state.ToBoolean(-1) cookie.SetSecure(state.ToBoolean(-1))
} }
state.Pop(1) state.Pop(1)
// Get http only // Get http only
state.GetField(-1, "http_only") state.GetField(-1, "http_only")
if state.IsBoolean(-1) { if state.IsBoolean(-1) {
cookie.HttpOnly = state.ToBoolean(-1) cookie.SetHTTPOnly(state.ToBoolean(-1))
} }
state.Pop(1) state.Pop(1)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"strings" "strings"
"sync" "sync"
@ -21,10 +20,10 @@ import (
// HTTPResponse represents an HTTP response from Lua // HTTPResponse represents an HTTP response from Lua
type HTTPResponse struct { type HTTPResponse struct {
Status int `json:"status"` Status int `json:"status"`
Headers map[string]string `json:"headers"` Headers map[string]string `json:"headers"`
Body any `json:"body"` Body any `json:"body"`
Cookies []*http.Cookie `json:"-"` Cookies []*fasthttp.Cookie `json:"-"`
} }
// Response pool to reduce allocations // Response pool to reduce allocations
@ -32,8 +31,8 @@ var responsePool = sync.Pool{
New: func() interface{} { New: func() interface{} {
return &HTTPResponse{ return &HTTPResponse{
Status: 200, Status: 200,
Headers: make(map[string]string, 8), // Pre-allocate with reasonable capacity Headers: make(map[string]string, 8), // Pre-allocate with reasonable capacity
Cookies: make([]*http.Cookie, 0, 4), // 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 // Set cookies
for _, cookie := range httpResp.Cookies { for _, cookie := range httpResp.Cookies {
// Convert net/http cookie to fasthttp cookie ctx.Response.Header.SetCookie(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)
} }
// Process the body based on its type // Process the body based on its type

View File

@ -1,7 +1,7 @@
package runner package runner
import ( import (
"net/http" "github.com/valyala/fasthttp"
"Moonshark/core/sessions" "Moonshark/core/sessions"
"Moonshark/core/utils/logger" "Moonshark/core/utils/logger"
@ -157,7 +157,7 @@ func (h *SessionHandler) addSessionCookie(resp *HTTPResponse, sessionID string)
// Check if session cookie is already set // Check if session cookie is already set
cookieName := opts["name"].(string) cookieName := opts["name"].(string)
for _, cookie := range resp.Cookies { for _, cookie := range resp.Cookies {
if cookie.Name == cookieName { if string(cookie.Key()) == cookieName {
h.debug("Session cookie already set in response") h.debug("Session cookie already set in response")
return return
} }
@ -166,21 +166,20 @@ func (h *SessionHandler) addSessionCookie(resp *HTTPResponse, sessionID string)
h.debug("Adding session cookie to response") h.debug("Adding session cookie to response")
// Create and add cookie // Create and add cookie
cookie := &http.Cookie{ cookie := fasthttp.AcquireCookie()
Name: cookieName, cookie.SetKey(cookieName)
Value: sessionID, cookie.SetValue(sessionID)
Path: opts["path"].(string), cookie.SetPath(opts["path"].(string))
HttpOnly: opts["http_only"].(bool), cookie.SetHTTPOnly(opts["http_only"].(bool))
MaxAge: opts["max_age"].(int), cookie.SetMaxAge(opts["max_age"].(int))
}
// Optional cookie parameters // Optional cookie parameters
if domain, ok := opts["domain"].(string); ok && domain != "" { if domain, ok := opts["domain"].(string); ok && domain != "" {
cookie.Domain = domain cookie.SetDomain(domain)
} }
if secure, ok := opts["secure"].(bool); ok { if secure, ok := opts["secure"].(bool); ok {
cookie.Secure = secure cookie.SetSecure(secure)
} }
resp.Cookies = append(resp.Cookies, cookie) resp.Cookies = append(resp.Cookies, cookie)