http response fix
This commit is contained in:
parent
8e511c5dc9
commit
b2c1d1cb9f
|
@ -172,7 +172,7 @@ func (s *Moonshark) initRunner() error {
|
||||||
|
|
||||||
// Configure session cookies
|
// Configure session cookies
|
||||||
sessionManager.SetCookieOptions(
|
sessionManager.SetCookieOptions(
|
||||||
"MSSESSID", // name
|
"MoonsharkID", // name
|
||||||
"/", // path
|
"/", // path
|
||||||
"", // domain
|
"", // domain
|
||||||
false, // secure
|
false, // secure
|
||||||
|
|
|
@ -3,6 +3,7 @@ package http
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"Moonshark/core/metadata"
|
"Moonshark/core/metadata"
|
||||||
|
@ -160,7 +161,7 @@ func HandleMethodNotAllowed(ctx *fasthttp.RequestCtx, errorConfig utils.ErrorPag
|
||||||
|
|
||||||
// handleLuaRoute executes a Lua route
|
// 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) {
|
||||||
luaCtx := runner.NewContext()
|
luaCtx := runner.NewHTTPContext(ctx) // Use NewHTTPContext instead of NewContext
|
||||||
defer luaCtx.Release()
|
defer luaCtx.Release()
|
||||||
|
|
||||||
method := string(ctx.Method())
|
method := string(ctx.Method())
|
||||||
|
@ -242,7 +243,10 @@ func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scrip
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we got a non-nil result, write it to the response
|
||||||
|
if result != nil {
|
||||||
writeResponse(ctx, result)
|
writeResponse(ctx, result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Content types for responses
|
// Content types for responses
|
||||||
|
@ -258,8 +262,14 @@ func writeResponse(ctx *fasthttp.RequestCtx, result any) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for HTTPResponse type
|
// First check the raw type of the result for strong type identification
|
||||||
if httpResp, ok := result.(*sandbox.HTTPResponse); ok {
|
// Sometimes type assertions don't work as expected with interface values
|
||||||
|
resultType := fmt.Sprintf("%T", result)
|
||||||
|
|
||||||
|
// Strong check for HTTP response
|
||||||
|
if strings.Contains(resultType, "HTTPResponse") || strings.Contains(resultType, "sandbox.HTTPResponse") {
|
||||||
|
httpResp, ok := result.(*sandbox.HTTPResponse)
|
||||||
|
if ok {
|
||||||
defer sandbox.ReleaseResponse(httpResp)
|
defer sandbox.ReleaseResponse(httpResp)
|
||||||
|
|
||||||
// Set response headers
|
// Set response headers
|
||||||
|
@ -280,12 +290,19 @@ func writeResponse(ctx *fasthttp.RequestCtx, result any) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result = httpResp.Body // Set result to body for processing below
|
// Continue with the body only
|
||||||
|
result = httpResp.Body
|
||||||
|
} else {
|
||||||
|
// We identified it as HTTPResponse but couldn't convert it
|
||||||
|
// This is a programming error
|
||||||
|
logger.Error("Found HTTPResponse type but failed to convert: %v", resultType)
|
||||||
|
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's a map (table) or array - return as JSON
|
// Check if it's a map (table) or array - return as JSON
|
||||||
isJSON := false
|
isJSON := false
|
||||||
|
|
||||||
switch result.(type) {
|
switch result.(type) {
|
||||||
case map[string]any, []any, []float64, []string, []int:
|
case map[string]any, []any, []float64, []string, []int:
|
||||||
isJSON = true
|
isJSON = true
|
||||||
|
@ -303,18 +320,21 @@ func writeResponse(ctx *fasthttp.RequestCtx, result any) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// All other types - convert to plain text
|
// Handle string and byte slice cases directly
|
||||||
setContentTypeIfMissing(ctx, contentTypePlain)
|
|
||||||
|
|
||||||
switch r := result.(type) {
|
switch r := result.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
setContentTypeIfMissing(ctx, contentTypePlain)
|
||||||
ctx.SetBodyString(r)
|
ctx.SetBodyString(r)
|
||||||
|
return
|
||||||
case []byte:
|
case []byte:
|
||||||
|
setContentTypeIfMissing(ctx, contentTypePlain)
|
||||||
ctx.SetBody(r)
|
ctx.SetBody(r)
|
||||||
default:
|
return
|
||||||
// Convert any other type to string
|
|
||||||
ctx.SetBodyString(fmt.Sprintf("%v", r))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we reach here, it's an unexpected type - convert to string as a last resort
|
||||||
|
setContentTypeIfMissing(ctx, contentTypePlain)
|
||||||
|
ctx.SetBodyString(fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
func setContentTypeIfMissing(ctx *fasthttp.RequestCtx, contentType string) {
|
func setContentTypeIfMissing(ctx *fasthttp.RequestCtx, contentType string) {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package runner
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -277,8 +276,21 @@ func (r *Runner) Execute(ctx context.Context, bytecode []byte, execCtx *Context,
|
||||||
ctxValues = execCtx.Values
|
ctxValues = execCtx.Values
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute in sandbox
|
// Execute in sandbox with optimized context handling
|
||||||
result, err := state.sandbox.Execute(state.L, bytecode, ctxValues)
|
var result any
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if execCtx != nil && execCtx.RequestCtx != nil {
|
||||||
|
// Use OptimizedExecute directly with the full context if we have RequestCtx
|
||||||
|
result, err = state.sandbox.OptimizedExecute(state.L, bytecode, &sandbox.Context{
|
||||||
|
Values: ctxValues,
|
||||||
|
RequestCtx: execCtx.RequestCtx,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Otherwise use standard Execute with just values
|
||||||
|
result, err = state.sandbox.Execute(state.L, bytecode, ctxValues)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -290,7 +302,8 @@ func (r *Runner) Execute(ctx context.Context, bytecode []byte, execCtx *Context,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for HTTP response
|
// Check for HTTP response if we don't have a RequestCtx or if we still have a result
|
||||||
|
if execCtx == nil || execCtx.RequestCtx == nil || result != nil {
|
||||||
httpResp, hasResponse := sandbox.GetHTTPResponse(state.L)
|
httpResp, hasResponse := sandbox.GetHTTPResponse(state.L)
|
||||||
if hasResponse {
|
if hasResponse {
|
||||||
// Set result as body if not already set
|
// Set result as body if not already set
|
||||||
|
@ -307,18 +320,6 @@ func (r *Runner) Execute(ctx context.Context, bytecode []byte, execCtx *Context,
|
||||||
|
|
||||||
return httpResp, nil
|
return httpResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle direct result if FastHTTP context is available
|
|
||||||
if execCtx != nil && execCtx.RequestCtx != nil && result != nil {
|
|
||||||
switch r := result.(type) {
|
|
||||||
case string:
|
|
||||||
execCtx.RequestCtx.SetBodyString(r)
|
|
||||||
case []byte:
|
|
||||||
execCtx.RequestCtx.SetBody(r)
|
|
||||||
default:
|
|
||||||
execCtx.RequestCtx.SetBodyString(fmt.Sprintf("%v", r))
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, err
|
return result, err
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
const (
|
const (
|
||||||
// Default settings
|
// Default settings
|
||||||
DefaultMaxSize = 100 * 1024 * 1024 // 100MB default cache size
|
DefaultMaxSize = 100 * 1024 * 1024 // 100MB default cache size
|
||||||
DefaultCookieName = "MSESSID"
|
DefaultCookieName = "MoonsharkID"
|
||||||
DefaultCookiePath = "/"
|
DefaultCookiePath = "/"
|
||||||
DefaultMaxAge = 86400 // 1 day in seconds
|
DefaultMaxAge = 86400 // 1 day in seconds
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user