mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 12:47:28 +08:00
style: status change to std http status (#758)
This commit is contained in:
@@ -19,6 +19,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
@@ -292,19 +293,19 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config BasicAuthConfig, log w
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deniedNoBasicAuthData() types.Action {
|
func deniedNoBasicAuthData() types.Action {
|
||||||
_ = proxywasm.SendHttpResponse(401, WWWAuthenticateHeader(protectionSpace),
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, WWWAuthenticateHeader(protectionSpace),
|
||||||
[]byte("Request denied by Basic Auth check. No Basic Authentication information found."), -1)
|
[]byte("Request denied by Basic Auth check. No Basic Authentication information found."), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func deniedInvalidCredentials() types.Action {
|
func deniedInvalidCredentials() types.Action {
|
||||||
_ = proxywasm.SendHttpResponse(401, WWWAuthenticateHeader(protectionSpace),
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, WWWAuthenticateHeader(protectionSpace),
|
||||||
[]byte("Request denied by Basic Auth check. Invalid username and/or password."), -1)
|
[]byte("Request denied by Basic Auth check. Invalid username and/or password."), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func deniedUnauthorizedConsumer() types.Action {
|
func deniedUnauthorizedConsumer() types.Action {
|
||||||
_ = proxywasm.SendHttpResponse(403, WWWAuthenticateHeader(protectionSpace),
|
_ = proxywasm.SendHttpResponse(http.StatusForbidden, WWWAuthenticateHeader(protectionSpace),
|
||||||
[]byte("Request denied by Basic Auth check. Unauthorized consumer."), -1)
|
[]byte("Request denied by Basic Auth check. Unauthorized consumer."), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,20 +92,20 @@ const bodyTemplate string = `
|
|||||||
|
|
||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.Log) types.Action {
|
||||||
pairs := strings.SplitN(ctx.Path(), "?", 2)
|
pairs := strings.SplitN(ctx.Path(), "?", 2)
|
||||||
|
|
||||||
if len(pairs) < 2 {
|
if len(pairs) < 2 {
|
||||||
proxywasm.SendHttpResponse(400, nil, []byte("1-need prompt param"), -1)
|
proxywasm.SendHttpResponse(http.StatusBadRequest, nil, []byte("1-need prompt param"), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
querys, err := url.ParseQuery(pairs[1])
|
querys, err := url.ParseQuery(pairs[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
proxywasm.SendHttpResponse(400, nil, []byte("2-need prompt param"), -1)
|
proxywasm.SendHttpResponse(http.StatusBadRequest, nil, []byte("2-need prompt param"), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
var prompt []string
|
var prompt []string
|
||||||
var ok bool
|
var ok bool
|
||||||
if prompt, ok = querys[config.PromptParam]; !ok || len(prompt) == 0 {
|
if prompt, ok = querys[config.PromptParam]; !ok || len(prompt) == 0 {
|
||||||
proxywasm.SendHttpResponse(400, nil, []byte("3-need prompt param"), -1)
|
proxywasm.SendHttpResponse(http.StatusBadRequest, nil, []byte("3-need prompt param"), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
body := fmt.Sprintf(bodyTemplate, config.Model, prompt[0], config.HumainId, config.AIId)
|
body := fmt.Sprintf(bodyTemplate, config.Model, prompt[0], config.HumainId, config.AIId)
|
||||||
@@ -121,7 +121,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.
|
|||||||
proxywasm.SendHttpResponse(uint32(statusCode), headers, responseBody, -1)
|
proxywasm.SendHttpResponse(uint32(statusCode), headers, responseBody, -1)
|
||||||
}, 10000)
|
}, 10000)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
proxywasm.SendHttpResponse(500, nil, []byte("Internel Error: "+err.Error()), -1)
|
proxywasm.SendHttpResponse(http.StatusInternalServerError, nil, []byte("Internel Error: "+err.Error()), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"cors/config"
|
"cors/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
||||||
@@ -93,7 +94,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig,
|
|||||||
if !httpCorsContext.IsValid {
|
if !httpCorsContext.IsValid {
|
||||||
headers := make([][2]string, 0)
|
headers := make([][2]string, 0)
|
||||||
headers = append(headers, [2]string{config.HeaderPluginTrace, "trace"})
|
headers = append(headers, [2]string{config.HeaderPluginTrace, "trace"})
|
||||||
proxywasm.SendHttpResponse(403, headers, []byte("Invalid CORS request"), -1)
|
proxywasm.SendHttpResponse(http.StatusForbidden, headers, []byte("Invalid CORS request"), -1)
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +102,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig,
|
|||||||
if httpCorsContext.IsPreFlight {
|
if httpCorsContext.IsPreFlight {
|
||||||
headers := make([][2]string, 0)
|
headers := make([][2]string, 0)
|
||||||
headers = append(headers, [2]string{config.HeaderPluginTrace, "trace"})
|
headers = append(headers, [2]string{config.HeaderPluginTrace, "trace"})
|
||||||
proxywasm.SendHttpResponse(200, headers, nil, -1)
|
proxywasm.SendHttpResponse(http.StatusOK, headers, nil, -1)
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
. "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
. "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
@@ -34,6 +35,6 @@ func onHttpRequestHeaders(ctx HttpContext, config MyConfig, log Log) types.Actio
|
|||||||
log.Infof("alloc success, point address: %p", b)
|
log.Infof("alloc success, point address: %p", b)
|
||||||
memstats := fmt.Sprintf(`{"Sys": %d,"HeapSys": %d,"HeapIdle": %d,"HeapInuse": %d,"HeapReleased": %d}`, m.Sys, m.HeapSys, m.HeapIdle, m.HeapInuse, m.HeapReleased)
|
memstats := fmt.Sprintf(`{"Sys": %d,"HeapSys": %d,"HeapIdle": %d,"HeapInuse": %d,"HeapReleased": %d}`, m.Sys, m.HeapSys, m.HeapIdle, m.HeapInuse, m.HeapReleased)
|
||||||
log.Info(memstats)
|
log.Info(memstats)
|
||||||
proxywasm.SendHttpResponse(200, [][2]string{{"Content-Type", "application/json"}}, []byte(memstats), -1)
|
proxywasm.SendHttpResponse(http.StatusOK, [][2]string{{"Content-Type", "application/json"}}, []byte(memstats), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
|
|
||||||
@@ -36,6 +38,6 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config HelloWorldConfig, log
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Critical("failed to set request header")
|
log.Critical("failed to set request header")
|
||||||
}
|
}
|
||||||
proxywasm.SendHttpResponse(200, nil, []byte("hello world"), -1)
|
proxywasm.SendHttpResponse(http.StatusOK, nil, []byte("hello world"), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
@@ -74,9 +75,11 @@ type Consumer struct {
|
|||||||
// credential: token1
|
// credential: token1
|
||||||
// - name: consumer2
|
// - name: consumer2
|
||||||
// credential: token2
|
// credential: token2
|
||||||
|
//
|
||||||
// keys:
|
// keys:
|
||||||
// - x-api-key
|
// - x-api-key
|
||||||
// - token
|
// - token
|
||||||
|
//
|
||||||
// in_query: true
|
// in_query: true
|
||||||
// @End
|
// @End
|
||||||
type KeyAuthConfig struct {
|
type KeyAuthConfig struct {
|
||||||
@@ -319,19 +322,19 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config KeyAuthConfig, log wra
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deniedMutiKeyAuthData() types.Action {
|
func deniedMutiKeyAuthData() types.Action {
|
||||||
_ = proxywasm.SendHttpResponse(401, WWWAuthenticateHeader(protectionSpace),
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, WWWAuthenticateHeader(protectionSpace),
|
||||||
[]byte("Request denied by Key Auth check. Muti Key Authentication information found."), -1)
|
[]byte("Request denied by Key Auth check. Muti Key Authentication information found."), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func deniedNoKeyAuthData() types.Action {
|
func deniedNoKeyAuthData() types.Action {
|
||||||
_ = proxywasm.SendHttpResponse(401, WWWAuthenticateHeader(protectionSpace),
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, WWWAuthenticateHeader(protectionSpace),
|
||||||
[]byte("Request denied by Key Auth check. No Key Authentication information found."), -1)
|
[]byte("Request denied by Key Auth check. No Key Authentication information found."), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func deniedUnauthorizedConsumer() types.Action {
|
func deniedUnauthorizedConsumer() types.Action {
|
||||||
_ = proxywasm.SendHttpResponse(403, WWWAuthenticateHeader(protectionSpace),
|
_ = proxywasm.SendHttpResponse(http.StatusForbidden, WWWAuthenticateHeader(protectionSpace),
|
||||||
[]byte("Request denied by Key Auth check. Unauthorized consumer."), -1)
|
[]byte("Request denied by Key Auth check. Unauthorized consumer."), -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
||||||
@@ -39,19 +41,19 @@ func parseConfig(json gjson.Result, config *Config, log wrapper.Log) error {
|
|||||||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Log) types.Action {
|
||||||
var res Res
|
var res Res
|
||||||
if config.TokenHeaders == "" || config.TokenSecretKey == "" {
|
if config.TokenHeaders == "" || config.TokenSecretKey == "" {
|
||||||
res.Code = 401
|
res.Code = http.StatusBadRequest
|
||||||
res.Msg = "参数不足"
|
res.Msg = "token or secret 不允许为空"
|
||||||
data, _ := json.Marshal(res)
|
data, _ := json.Marshal(res)
|
||||||
_ = proxywasm.SendHttpResponse(401, nil, data, -1)
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, nil, data, -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := proxywasm.GetHttpRequestHeader(config.TokenHeaders)
|
token, err := proxywasm.GetHttpRequestHeader(config.TokenHeaders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.Code = 401
|
res.Code = http.StatusUnauthorized
|
||||||
res.Msg = "认证失败"
|
res.Msg = "认证失败"
|
||||||
data, _ := json.Marshal(res)
|
data, _ := json.Marshal(res)
|
||||||
_ = proxywasm.SendHttpResponse(401, nil, data, -1)
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, nil, data, -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
valid := ParseTokenValid(token, config.TokenSecretKey)
|
valid := ParseTokenValid(token, config.TokenSecretKey)
|
||||||
@@ -59,10 +61,10 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Lo
|
|||||||
_ = proxywasm.ResumeHttpRequest()
|
_ = proxywasm.ResumeHttpRequest()
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
} else {
|
} else {
|
||||||
res.Code = 401
|
res.Code = http.StatusUnauthorized
|
||||||
res.Msg = "认证失败"
|
res.Msg = "认证失败"
|
||||||
data, _ := json.Marshal(res)
|
data, _ := json.Marshal(res)
|
||||||
_ = proxywasm.SendHttpResponse(401, nil, data, -1)
|
_ = proxywasm.SendHttpResponse(http.StatusUnauthorized, nil, data, -1)
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
@@ -59,11 +60,11 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Lo
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(string(sni), "*.") {
|
if !strings.HasPrefix(string(sni), "*.") {
|
||||||
proxywasm.SendHttpResponse(421, nil, []byte("Misdirected Request"), -1)
|
proxywasm.SendHttpResponse(http.StatusMisdirectedRequest, nil, []byte("Misdirected Request"), -1)
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
if !strings.Contains(host, string(sni)[1:]) {
|
if !strings.Contains(host, string(sni)[1:]) {
|
||||||
proxywasm.SendHttpResponse(421, nil, []byte("Misdirected Request"), -1)
|
proxywasm.SendHttpResponse(http.StatusMisdirectedRequest, nil, []byte("Misdirected Request"), -1)
|
||||||
return types.ActionPause
|
return types.ActionPause
|
||||||
}
|
}
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
|
|||||||
Reference in New Issue
Block a user