2024-10-17 13:39:35 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"apigo.cc/gojs"
|
|
|
|
"apigo.cc/gojs/goja"
|
|
|
|
"github.com/ssgo/s"
|
|
|
|
"github.com/ssgo/standard"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
req *s.Request
|
2024-10-18 17:54:37 +08:00
|
|
|
Id string
|
2024-10-17 13:39:35 +08:00
|
|
|
Proto string
|
|
|
|
Scheme string
|
|
|
|
Host string
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
RemoteAddr string
|
|
|
|
RealIP string
|
|
|
|
Referer string
|
|
|
|
UserAgent string
|
|
|
|
Url string
|
|
|
|
ContentLength int64
|
|
|
|
Cookies map[string]string
|
|
|
|
Headers map[string]string
|
|
|
|
Args map[string]any
|
|
|
|
Files map[string]map[string]any
|
|
|
|
MultiFiles map[string][]map[string]any
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeRequest(req *s.Request, args map[string]any, headers map[string]string) map[string]any {
|
|
|
|
cookies := map[string]string{}
|
|
|
|
for _, ck := range req.Cookies() {
|
|
|
|
cookies[ck.Name] = ck.Value
|
|
|
|
}
|
|
|
|
files := map[string]map[string]any{}
|
|
|
|
multiFiles := map[string][]map[string]any{}
|
|
|
|
for k, v := range args {
|
|
|
|
if v1, ok := v.(s.UploadFile); ok {
|
|
|
|
if data, err := v1.Content(); err == nil {
|
|
|
|
files[k] = map[string]any{
|
|
|
|
"name": v1.Filename,
|
|
|
|
"size": v1.Size,
|
|
|
|
"data": data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(args, k)
|
|
|
|
} else if v2, ok := v.([]s.UploadFile); ok {
|
|
|
|
multiFiles[k] = make([]map[string]any, len(v2))
|
|
|
|
for i, v1 := range v2 {
|
|
|
|
if data, err := v1.Content(); err == nil {
|
|
|
|
multiFiles[k][i] = map[string]any{
|
|
|
|
"name": v1.Filename,
|
|
|
|
"size": v1.Size,
|
|
|
|
"data": data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(args, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gojs.MakeMap(&Request{
|
|
|
|
req: req,
|
2024-10-18 17:54:37 +08:00
|
|
|
Id: req.Id,
|
2024-10-17 13:39:35 +08:00
|
|
|
Proto: req.Proto,
|
|
|
|
Scheme: req.Header.Get(standard.DiscoverHeaderScheme),
|
|
|
|
Host: req.Header.Get(standard.DiscoverHeaderHost),
|
|
|
|
Method: req.Method,
|
|
|
|
Path: req.RequestURI,
|
|
|
|
RemoteAddr: req.RemoteAddr,
|
|
|
|
RealIP: req.GetRealIp(),
|
|
|
|
Referer: req.Referer(),
|
|
|
|
UserAgent: req.UserAgent(),
|
|
|
|
Url: req.URL.String(),
|
|
|
|
ContentLength: req.ContentLength,
|
|
|
|
Cookies: cookies,
|
|
|
|
Headers: headers,
|
|
|
|
Args: args,
|
|
|
|
Files: files,
|
|
|
|
MultiFiles: multiFiles,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) MakeURL(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
return vm.ToValue(r.req.MakeUrl(args.Str(0)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) ReadAll(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
data, err := io.ReadAll(r.req.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(vm.NewGoError(err))
|
|
|
|
}
|
|
|
|
return vm.ToValue(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) Read(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
size := args.Int(0)
|
|
|
|
data := make([]byte, size)
|
|
|
|
n, err := r.req.Body.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(vm.NewGoError(err))
|
|
|
|
}
|
|
|
|
return vm.ToValue(data[0:n])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) Close(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
err := r.req.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(vm.NewGoError(err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) Get(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
return vm.ToValue(r.req.Get(args.Str(0)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) Set(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
|
|
r.req.Set(args.Str(0), args.Any(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetHeader(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
return vm.ToValue(r.req.Header.Get(args.Str(0)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) SetHeader(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
|
|
r.req.Header.Set(args.Str(0), args.Str(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) SetUserID(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
r.req.SetUserId(args.Str(0))
|
|
|
|
return nil
|
|
|
|
}
|