44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package http
|
|
|
|
import (
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
// QueryToLua converts HTTP query parameters to a map that can be used with LuaJIT.
|
|
// Single value parameters are stored as strings.
|
|
// Multi-value parameters are converted to []any arrays.
|
|
func QueryToLua(ctx *fasthttp.RequestCtx) map[string]any {
|
|
result := make(map[string]any)
|
|
|
|
// Use a map to track keys that have multiple values
|
|
multiValueKeys := make(map[string]bool)
|
|
|
|
// Process all query args
|
|
ctx.QueryArgs().VisitAll(func(key, value []byte) {
|
|
keyStr := string(key)
|
|
valStr := string(value)
|
|
|
|
if _, exists := result[keyStr]; exists {
|
|
// This key already exists, convert to array if not already
|
|
if !multiValueKeys[keyStr] {
|
|
// First duplicate, convert existing value to array
|
|
multiValueKeys[keyStr] = true
|
|
result[keyStr] = []any{result[keyStr], valStr}
|
|
} else {
|
|
// Already an array, append
|
|
result[keyStr] = append(result[keyStr].([]any), valStr)
|
|
}
|
|
} else {
|
|
// New key
|
|
result[keyStr] = valStr
|
|
}
|
|
})
|
|
|
|
// If we don't have any query parameters, return empty map
|
|
if len(result) == 0 {
|
|
return make(map[string]any)
|
|
}
|
|
|
|
return result
|
|
}
|