From b1e4cf9492afa1b79dc0f28b13b14de1bff46b0f Mon Sep 17 00:00:00 2001 From: baerwang <52104949+baerwang@users.noreply.github.com> Date: Mon, 8 Jan 2024 11:07:11 +0800 Subject: [PATCH] style: status change to std http status (#758) --- plugins/wasm-go/extensions/basic-auth/main.go | 7 ++++--- plugins/wasm-go/extensions/chatgpt-proxy/main.go | 10 +++++----- plugins/wasm-go/extensions/cors/main.go | 5 +++-- plugins/wasm-go/extensions/gc-test/main.go | 3 ++- plugins/wasm-go/extensions/hello-world/main.go | 4 +++- plugins/wasm-go/extensions/key-auth/main.go | 9 ++++++--- .../wasm-go/extensions/simple-jwt-auth/main.go | 16 +++++++++------- plugins/wasm-go/extensions/sni-misdirect/main.go | 5 +++-- 8 files changed, 35 insertions(+), 24 deletions(-) diff --git a/plugins/wasm-go/extensions/basic-auth/main.go b/plugins/wasm-go/extensions/basic-auth/main.go index e739691bb..5c8ef45ef 100644 --- a/plugins/wasm-go/extensions/basic-auth/main.go +++ b/plugins/wasm-go/extensions/basic-auth/main.go @@ -19,6 +19,7 @@ package main import ( "encoding/base64" "fmt" + "net/http" "strings" "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 { - _ = proxywasm.SendHttpResponse(401, WWWAuthenticateHeader(protectionSpace), + _ = proxywasm.SendHttpResponse(http.StatusUnauthorized, WWWAuthenticateHeader(protectionSpace), []byte("Request denied by Basic Auth check. No Basic Authentication information found."), -1) return types.ActionContinue } 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) return types.ActionContinue } 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) return types.ActionContinue } diff --git a/plugins/wasm-go/extensions/chatgpt-proxy/main.go b/plugins/wasm-go/extensions/chatgpt-proxy/main.go index 15ebb4ba6..8750e35b9 100644 --- a/plugins/wasm-go/extensions/chatgpt-proxy/main.go +++ b/plugins/wasm-go/extensions/chatgpt-proxy/main.go @@ -92,20 +92,20 @@ const bodyTemplate string = ` func onHttpRequestHeaders(ctx wrapper.HttpContext, config MyConfig, log wrapper.Log) types.Action { pairs := strings.SplitN(ctx.Path(), "?", 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 } querys, err := url.ParseQuery(pairs[1]) 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 } var prompt []string var ok bool 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 } 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) }, 10000) 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.ActionPause diff --git a/plugins/wasm-go/extensions/cors/main.go b/plugins/wasm-go/extensions/cors/main.go index e51df7f78..a4f3f2237 100644 --- a/plugins/wasm-go/extensions/cors/main.go +++ b/plugins/wasm-go/extensions/cors/main.go @@ -17,6 +17,7 @@ package main import ( "cors/config" "fmt" + "net/http" "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" @@ -93,7 +94,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig, if !httpCorsContext.IsValid { headers := make([][2]string, 0) 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 } @@ -101,7 +102,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, corsConfig config.CorsConfig, if httpCorsContext.IsPreFlight { headers := make([][2]string, 0) headers = append(headers, [2]string{config.HeaderPluginTrace, "trace"}) - proxywasm.SendHttpResponse(200, headers, nil, -1) + proxywasm.SendHttpResponse(http.StatusOK, headers, nil, -1) return types.ActionPause } diff --git a/plugins/wasm-go/extensions/gc-test/main.go b/plugins/wasm-go/extensions/gc-test/main.go index 66f290d96..8931ed51d 100644 --- a/plugins/wasm-go/extensions/gc-test/main.go +++ b/plugins/wasm-go/extensions/gc-test/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "net/http" "runtime" . "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) 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) - 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 } diff --git a/plugins/wasm-go/extensions/hello-world/main.go b/plugins/wasm-go/extensions/hello-world/main.go index ac201e62b..53c15fc00 100644 --- a/plugins/wasm-go/extensions/hello-world/main.go +++ b/plugins/wasm-go/extensions/hello-world/main.go @@ -15,6 +15,8 @@ package main import ( + "net/http" + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" @@ -36,6 +38,6 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config HelloWorldConfig, log if err != nil { 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 } diff --git a/plugins/wasm-go/extensions/key-auth/main.go b/plugins/wasm-go/extensions/key-auth/main.go index 12a63bac7..4d837ffbd 100644 --- a/plugins/wasm-go/extensions/key-auth/main.go +++ b/plugins/wasm-go/extensions/key-auth/main.go @@ -17,6 +17,7 @@ package main import ( "errors" "fmt" + "net/http" "net/url" "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" @@ -74,9 +75,11 @@ type Consumer struct { // credential: token1 // - name: consumer2 // credential: token2 +// // keys: // - x-api-key // - token +// // in_query: true // @End type KeyAuthConfig struct { @@ -319,19 +322,19 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config KeyAuthConfig, log wra } 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) return types.ActionContinue } 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) return types.ActionContinue } 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) return types.ActionContinue } diff --git a/plugins/wasm-go/extensions/simple-jwt-auth/main.go b/plugins/wasm-go/extensions/simple-jwt-auth/main.go index 4b9e2457e..0cc71d439 100644 --- a/plugins/wasm-go/extensions/simple-jwt-auth/main.go +++ b/plugins/wasm-go/extensions/simple-jwt-auth/main.go @@ -2,6 +2,8 @@ package main import ( "encoding/json" + "net/http" + "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" jwt "github.com/dgrijalva/jwt-go" "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 { var res Res if config.TokenHeaders == "" || config.TokenSecretKey == "" { - res.Code = 401 - res.Msg = "参数不足" + res.Code = http.StatusBadRequest + res.Msg = "token or secret 不允许为空" data, _ := json.Marshal(res) - _ = proxywasm.SendHttpResponse(401, nil, data, -1) + _ = proxywasm.SendHttpResponse(http.StatusUnauthorized, nil, data, -1) return types.ActionContinue } token, err := proxywasm.GetHttpRequestHeader(config.TokenHeaders) if err != nil { - res.Code = 401 + res.Code = http.StatusUnauthorized res.Msg = "认证失败" data, _ := json.Marshal(res) - _ = proxywasm.SendHttpResponse(401, nil, data, -1) + _ = proxywasm.SendHttpResponse(http.StatusUnauthorized, nil, data, -1) return types.ActionContinue } valid := ParseTokenValid(token, config.TokenSecretKey) @@ -59,10 +61,10 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config, log wrapper.Lo _ = proxywasm.ResumeHttpRequest() return types.ActionPause } else { - res.Code = 401 + res.Code = http.StatusUnauthorized res.Msg = "认证失败" data, _ := json.Marshal(res) - _ = proxywasm.SendHttpResponse(401, nil, data, -1) + _ = proxywasm.SendHttpResponse(http.StatusUnauthorized, nil, data, -1) return types.ActionContinue } } diff --git a/plugins/wasm-go/extensions/sni-misdirect/main.go b/plugins/wasm-go/extensions/sni-misdirect/main.go index fff460696..c811d3cce 100644 --- a/plugins/wasm-go/extensions/sni-misdirect/main.go +++ b/plugins/wasm-go/extensions/sni-misdirect/main.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "strings" "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 } 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 } 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.ActionContinue