mirror of
https://github.com/aykhans/sarin.git
synced 2026-02-28 06:49:13 +00:00
Build request data through RequestData struct instead of fasthttp directly
Refactor request generators to populate a script.RequestData intermediate struct, then apply it to fasthttp.Request in one step. This eliminates the round-trip conversion (fasthttp → RequestData → fasthttp) when scripts are enabled. Remove the URL field from RequestData and the now-unused fasthttp conversion functions from chain.go.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"github.com/valyala/fasthttp"
|
||||
"go.aykhans.me/sarin/internal/types"
|
||||
)
|
||||
|
||||
@@ -106,83 +105,3 @@ func (t *Transformer) Close() {
|
||||
func (t *Transformer) IsEmpty() bool {
|
||||
return len(t.luaEngines) == 0 && len(t.jsEngines) == 0
|
||||
}
|
||||
|
||||
// RequestDataFromFastHTTP extracts RequestData from a fasthttp.Request.
|
||||
func RequestDataFromFastHTTP(req *fasthttp.Request) *RequestData {
|
||||
data := &RequestData{
|
||||
Method: string(req.Header.Method()),
|
||||
URL: string(req.URI().FullURI()),
|
||||
Path: string(req.URI().Path()),
|
||||
Body: string(req.Body()),
|
||||
Headers: make(map[string][]string),
|
||||
Params: make(map[string][]string),
|
||||
Cookies: make(map[string][]string),
|
||||
}
|
||||
|
||||
// Extract headers (supports multiple values per key)
|
||||
req.Header.All()(func(key, value []byte) bool {
|
||||
k := string(key)
|
||||
data.Headers[k] = append(data.Headers[k], string(value))
|
||||
return true
|
||||
})
|
||||
|
||||
// Extract query params (supports multiple values per key)
|
||||
req.URI().QueryArgs().All()(func(key, value []byte) bool {
|
||||
k := string(key)
|
||||
data.Params[k] = append(data.Params[k], string(value))
|
||||
return true
|
||||
})
|
||||
|
||||
// Extract cookies (supports multiple values per key)
|
||||
req.Header.Cookies()(func(key, value []byte) bool {
|
||||
k := string(key)
|
||||
data.Cookies[k] = append(data.Cookies[k], string(value))
|
||||
return true
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// ApplyToFastHTTP applies the modified RequestData back to a fasthttp.Request.
|
||||
func ApplyToFastHTTP(data *RequestData, req *fasthttp.Request) {
|
||||
// Method
|
||||
req.Header.SetMethod(data.Method)
|
||||
|
||||
// Path (preserve scheme and host)
|
||||
req.URI().SetPath(data.Path)
|
||||
|
||||
// Body
|
||||
req.SetBody([]byte(data.Body))
|
||||
|
||||
// Clear and set headers (supports multiple values per key)
|
||||
req.Header.All()(func(key, _ []byte) bool {
|
||||
keyStr := string(key)
|
||||
if keyStr != "Host" {
|
||||
req.Header.Del(keyStr)
|
||||
}
|
||||
return true
|
||||
})
|
||||
for k, values := range data.Headers {
|
||||
if k != "Host" { // Don't overwrite Host
|
||||
for _, v := range values {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear and set query params (supports multiple values per key)
|
||||
req.URI().QueryArgs().Reset()
|
||||
for k, values := range data.Params {
|
||||
for _, v := range values {
|
||||
req.URI().QueryArgs().Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// Clear and set cookies (supports multiple values per key)
|
||||
req.Header.DelAllCookies()
|
||||
for k, values := range data.Cookies {
|
||||
for _, v := range values {
|
||||
req.Header.SetCookie(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,6 @@ func (e *JsEngine) requestDataToObject(req *RequestData) goja.Value {
|
||||
obj := e.runtime.NewObject()
|
||||
|
||||
_ = obj.Set("method", req.Method)
|
||||
_ = obj.Set("url", req.URL)
|
||||
_ = obj.Set("path", req.Path)
|
||||
_ = obj.Set("body", req.Body)
|
||||
|
||||
@@ -130,11 +129,6 @@ func (e *JsEngine) objectToRequestData(val goja.Value, req *RequestData) error {
|
||||
req.Method = v.String()
|
||||
}
|
||||
|
||||
// URL
|
||||
if v := obj.Get("url"); v != nil && !goja.IsUndefined(v) {
|
||||
req.URL = v.String()
|
||||
}
|
||||
|
||||
// Path
|
||||
if v := obj.Get("path"); v != nil && !goja.IsUndefined(v) {
|
||||
req.Path = v.String()
|
||||
|
||||
@@ -90,7 +90,6 @@ func (e *LuaEngine) requestDataToTable(req *RequestData) *lua.LTable {
|
||||
t := L.NewTable()
|
||||
|
||||
t.RawSetString("method", lua.LString(req.Method))
|
||||
t.RawSetString("url", lua.LString(req.URL))
|
||||
t.RawSetString("path", lua.LString(req.Path))
|
||||
t.RawSetString("body", lua.LString(req.Body))
|
||||
|
||||
@@ -137,11 +136,6 @@ func (e *LuaEngine) tableToRequestData(t *lua.LTable, req *RequestData) {
|
||||
req.Method = string(v.(lua.LString))
|
||||
}
|
||||
|
||||
// URL
|
||||
if v := t.RawGetString("url"); v.Type() == lua.LTString {
|
||||
req.URL = string(v.(lua.LString))
|
||||
}
|
||||
|
||||
// Path
|
||||
if v := t.RawGetString("path"); v.Type() == lua.LTString {
|
||||
req.Path = string(v.(lua.LString))
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
// Headers, Params, and Cookies use []string values to support multiple values per key.
|
||||
type RequestData struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Path string `json:"path"`
|
||||
Headers map[string][]string `json:"headers"`
|
||||
Params map[string][]string `json:"params"`
|
||||
|
||||
Reference in New Issue
Block a user