This commit is contained in:
cdle
2023-06-11 14:09:34 +08:00
parent e985e9db8b
commit d408524fd5
4 changed files with 87 additions and 33 deletions
+20
View File
@@ -1,7 +1,9 @@
package core
import (
"fmt"
"net/http"
"regexp"
"github.com/dop251/goja"
)
@@ -12,6 +14,24 @@ func MakeHeadersObject(vm *goja.Runtime, header http.Header) *goja.Object {
return header.Get(name)
})
obj.Set("gets", func(name string) []string {
return header.Values(name)
})
obj.Set("values", func(name string) []string {
return header.Values(name)
})
obj.Set("cookie", func(name string) string {
for _, v := range header.Values("Set-Cookie") {
res := regexp.MustCompile(fmt.Sprintf("%s=([^;]+)", name)).FindStringSubmatch(v)
if len(res) > 0 {
return res[1]
}
}
return ""
})
obj.Set("has", func(name string) bool {
return header.Get(name) != ""
})