Files
waf-platform/EdgeNode/internal/js/lib_net_http_request.go
2026-02-04 20:27:13 +08:00

438 lines
9.8 KiB
Go

// 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"
"github.com/iwind/TeaGo/maps"
"net/http"
)
func init() {
if !teaconst.IsMain {
return
}
SharedLibraryManager.Register(&JSNetHTTPRequestLibrary{})
}
type JSNetHTTPRequestLibrary struct {
JSBaseLibrary
}
func (this *JSNetHTTPRequestLibrary) JSNamespace() string {
return "gojs.net.http.JSNetHTTPRequestLibrary"
}
func (this *JSNetHTTPRequestLibrary) JSPrototype() string {
return `gojs.net.http.Request = class {
#goObjectId
constructor() {
}
setGoObject(objectId) {
this.#goObjectId = objectId
}
goObjectId() {
return this.#goObjectId
}
get id() {
return $this.Id(this.#goObjectId)
}
get requestId() {
return this.id
}
get url() {
return $this.URL(this.#goObjectId)
}
get server() {
return $this.Server(this.#goObjectId)
}
get node() {
return $this.Node(this.#goObjectId)
}
get requestURL() {
return this.url
}
get path() {
return $this.Path(this.#goObjectId)
}
get requestPath() {
return this.path
}
get uri() {
return $this.URI(this.#goObjectId)
}
get requestURI() {
return this.uri
}
set uri(uri) {
$this.SetURI(this.#goObjectId, uri)
}
set requestURI(uri) {
this.uri = uri
}
get host() {
return $this.Host(this.#goObjectId)
}
get remoteAddr() {
return $this.RemoteAddr(this.#goObjectId)
}
get rawRemoteAddr() {
return $this.RawRemoteAddr(this.#goObjectId)
}
get remotePort() {
return $this.RemotePort(this.#goObjectId)
}
get method() {
return $this.Method(this.#goObjectId)
}
get contentLength() {
return $this.ContentLength(this.#goObjectId)
}
get transferEncoding() {
return $this.TransferEncoding(this.#goObjectId)
}
get proto() {
return $this.Proto(this.#goObjectId)
}
get protoMajor() {
return $this.ProtoMajor(this.#goObjectId)
}
get protoMinor() {
return $this.ProtoMinor(this.#goObjectId)
}
cookie(name) {
return $this.Cookie(this.#goObjectId, name)
}
get header() {
let header = $this.Header(this.#goObjectId)
if (header == null) {
header = {}
}
return header
}
setHeader(name, values) {
$this.SetHeader(this.#goObjectId, name, values)
}
deleteHeader(name) {
$this.DeleteHeader(this.#goObjectId, name)
}
setAttr(name, value) {
$this.SetAttr(this.#goObjectId, name, value)
}
setVar(name, value) {
$this.SetHeader(this.#goObjectId, name, value)
}
format(s) {
return $this.Format(this.#goObjectId, s)
}
done() {
$this.Done(this.#goObjectId)
}
close() {
$this.Close(this.#goObjectId)
}
allow() {
$this.Allow(this.#goObjectId)
}
}`
}
func (this *JSNetHTTPRequestLibrary) Id(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).Id(), nil
}
func (this *JSNetHTTPRequestLibrary) Server(arguments *FunctionArguments) (maps.Map, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return maps.Map{}, ErrGoObjectNotFound
}
return obj.(RequestInterface).Server(), nil
}
func (this *JSNetHTTPRequestLibrary) Node(arguments *FunctionArguments) (maps.Map, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return maps.Map{}, ErrGoObjectNotFound
}
return obj.(RequestInterface).Node(), nil
}
func (this *JSNetHTTPRequestLibrary) URL(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).URL(), nil
}
func (this *JSNetHTTPRequestLibrary) Path(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).Path(), nil
}
func (this *JSNetHTTPRequestLibrary) URI(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).URI(), nil
}
func (this *JSNetHTTPRequestLibrary) SetURI(arguments *FunctionArguments) (any, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
var uri, _ = arguments.FormatStringAt(1)
obj.(RequestInterface).SetURI(uri)
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) Host(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).Host(), nil
}
func (this *JSNetHTTPRequestLibrary) RemoteAddr(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).RemoteAddr(), nil
}
func (this *JSNetHTTPRequestLibrary) RawRemoteAddr(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).RawRemoteAddr(), nil
}
func (this *JSNetHTTPRequestLibrary) RemotePort(arguments *FunctionArguments) (int, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return 0, ErrGoObjectNotFound
}
return obj.(RequestInterface).RemotePort(), nil
}
func (this *JSNetHTTPRequestLibrary) Method(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).Method(), nil
}
func (this *JSNetHTTPRequestLibrary) ContentLength(arguments *FunctionArguments) (int64, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return 0, ErrGoObjectNotFound
}
return obj.(RequestInterface).ContentLength(), nil
}
func (this *JSNetHTTPRequestLibrary) TransferEncoding(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).TransferEncoding(), nil
}
func (this *JSNetHTTPRequestLibrary) Proto(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
return obj.(RequestInterface).Proto(), nil
}
func (this *JSNetHTTPRequestLibrary) ProtoMajor(arguments *FunctionArguments) (int, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return 0, ErrGoObjectNotFound
}
return obj.(RequestInterface).ProtoMajor(), nil
}
func (this *JSNetHTTPRequestLibrary) ProtoMinor(arguments *FunctionArguments) (int, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return 0, ErrGoObjectNotFound
}
return obj.(RequestInterface).ProtoMinor(), nil
}
func (this *JSNetHTTPRequestLibrary) Cookie(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
var name, _ = arguments.FormatStringAt(1)
if len(name) == 0 {
return "", nil
}
return obj.(RequestInterface).Cookie(name), nil
}
func (this *JSNetHTTPRequestLibrary) Header(arguments *FunctionArguments) (http.Header, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return http.Header{}, ErrGoObjectNotFound
}
return obj.(RequestInterface).Header(), nil
}
func (this *JSNetHTTPRequestLibrary) 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.(RequestInterface).SetHeader(name, []string{""})
return this.JSSuccess()
}
obj.(RequestInterface).SetHeader(name, []string{argValueString})
return this.JSSuccess()
}
obj.(RequestInterface).SetHeader(name, argValues)
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) 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.(RequestInterface).DeleteHeader(name)
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) SetAttr(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
value, _ := arguments.FormatStringAt(2)
obj.(RequestInterface).SetAttr(name, value)
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) SetVar(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
value, _ := arguments.FormatStringAt(2)
obj.(RequestInterface).SetVar(name, value)
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) Format(arguments *FunctionArguments) (string, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return "", ErrGoObjectNotFound
}
s, _ := arguments.FormatStringAt(1)
return obj.(RequestInterface).Format(s), nil
}
func (this *JSNetHTTPRequestLibrary) Done(arguments *FunctionArguments) (any, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return nil, ErrGoObjectNotFound
}
obj.(RequestInterface).Done()
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) Close(arguments *FunctionArguments) (any, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return nil, ErrGoObjectNotFound
}
obj.(RequestInterface).Close()
return this.JSSuccess()
}
func (this *JSNetHTTPRequestLibrary) Allow(arguments *FunctionArguments) (any, error) {
obj, ok := arguments.GoObjectAt(0)
if !ok {
return nil, ErrGoObjectNotFound
}
obj.(RequestInterface).Allow()
return this.JSSuccess()
}