1.4.5.2
This commit is contained in:
203
EdgeNode/internal/js/lib_net_http_response.go
Normal file
203
EdgeNode/internal/js/lib_net_http_response.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build script
|
||||
// +build script
|
||||
|
||||
package js
|
||||
|
||||
import (
|
||||
"errors"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
SharedLibraryManager.Register(&JSNetHTTPResponseLibrary{})
|
||||
}
|
||||
|
||||
type JSNetHTTPResponseLibrary struct {
|
||||
JSBaseLibrary
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) JSNamespace() string {
|
||||
return "gojs.net.http.JSNetHTTPResponseLibrary"
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) JSPrototype() string {
|
||||
return `gojs.net.http.Response = class {
|
||||
#goObjectId
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
setGoObject(objectId) {
|
||||
this.#goObjectId = objectId
|
||||
}
|
||||
|
||||
goObjectId() {
|
||||
return this.#goObjectId
|
||||
}
|
||||
|
||||
setHeader(name, values) {
|
||||
$this.SetHeader(this.#goObjectId, name, values)
|
||||
}
|
||||
|
||||
deleteHeader(name) {
|
||||
$this.DeleteHeader(this.#goObjectId, name)
|
||||
}
|
||||
|
||||
get header() {
|
||||
let header = $this.Header(this.#goObjectId)
|
||||
if (header == null) {
|
||||
header = {}
|
||||
}
|
||||
return header
|
||||
}
|
||||
|
||||
send(status, body) {
|
||||
if (body != null && typeof(body) == "object" && (body instanceof gojs.net.http.client.Response)) {
|
||||
$this.SendResp(this.#goObjectId, status, body.goObjectId)
|
||||
return
|
||||
}
|
||||
$this.Send(this.#goObjectId, status, body)
|
||||
}
|
||||
|
||||
sendFile(status, path) {
|
||||
$this.SendFile(this.#goObjectId, status, path)
|
||||
}
|
||||
|
||||
redirect(status, url) {
|
||||
$this.Redirect(this.#goObjectId, status, url)
|
||||
}
|
||||
}`
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) Header(arguments *FunctionArguments) (http.Header, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return http.Header{}, ErrGoObjectNotFound
|
||||
}
|
||||
return obj.(ResponseInterface).Header(), nil
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) SetHeader(arguments *FunctionArguments) (any, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return nil, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
argName, ok := arguments.FormatStringAt(1)
|
||||
if !ok || len(argName) == 0 {
|
||||
return nil, errors.New("header name should not be empty")
|
||||
}
|
||||
var name = argName
|
||||
|
||||
argValues, ok := arguments.FormatStringsAt(2)
|
||||
if !ok {
|
||||
argValueString, ok := arguments.FormatStringAt(2)
|
||||
if !ok {
|
||||
obj.(ResponseInterface).SetHeader(name, []string{""})
|
||||
return this.JSSuccess()
|
||||
}
|
||||
|
||||
obj.(ResponseInterface).SetHeader(name, []string{argValueString})
|
||||
return this.JSSuccess()
|
||||
}
|
||||
|
||||
obj.(ResponseInterface).SetHeader(name, argValues)
|
||||
|
||||
return this.JSSuccess()
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) DeleteHeader(arguments *FunctionArguments) (any, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return nil, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
name, ok := arguments.FormatStringAt(1)
|
||||
if !ok {
|
||||
return this.JSSuccess()
|
||||
}
|
||||
|
||||
obj.(ResponseInterface).DeleteHeader(name)
|
||||
return this.JSSuccess()
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) Send(arguments *FunctionArguments) (any, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return nil, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
status, ok := arguments.IntAt(1)
|
||||
if !ok {
|
||||
status = 200
|
||||
}
|
||||
|
||||
body, _ := arguments.FormatStringAt(2)
|
||||
|
||||
obj.(ResponseInterface).Send(status, body)
|
||||
|
||||
return this.JSSuccess()
|
||||
}
|
||||
|
||||
// SendResp 发送从 net.http.client.Client 获取的响应数据
|
||||
func (this *JSNetHTTPResponseLibrary) SendResp(arguments *FunctionArguments) (int64, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return 0, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
status, ok := arguments.IntAt(1)
|
||||
if !ok {
|
||||
status = 200
|
||||
}
|
||||
|
||||
respObj, ok := arguments.GoObjectAt(2)
|
||||
if !ok {
|
||||
return 0, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
var resp = respObj.(*JSNetHTTPClientLibraryResponse).resp
|
||||
resp.StatusCode = status
|
||||
return obj.(ResponseInterface).SendResp(resp)
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) SendFile(arguments *FunctionArguments) (written int64, err error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return 0, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
status, ok := arguments.IntAt(1)
|
||||
if !ok {
|
||||
status = 200
|
||||
}
|
||||
path, _ := arguments.FormatStringAt(2)
|
||||
return obj.(ResponseInterface).SendFile(status, path)
|
||||
}
|
||||
|
||||
func (this *JSNetHTTPResponseLibrary) Redirect(arguments *FunctionArguments) (any, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return 0, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
status, ok := arguments.IntAt(1)
|
||||
if !ok {
|
||||
status = http.StatusFound
|
||||
}
|
||||
|
||||
url, ok := arguments.StringAt(2)
|
||||
if !ok || len(url) == 0 {
|
||||
return nil, errors.New("redirect() function require 'url' parameter")
|
||||
}
|
||||
|
||||
obj.(ResponseInterface).Redirect(status, url)
|
||||
return this.JSSuccess()
|
||||
}
|
||||
Reference in New Issue
Block a user