mirror of
https://github.com/alibaba/higress.git
synced 2026-05-30 07:37:26 +08:00
feat: implement apiToken failover mechanism (#1256)
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package util
|
||||
|
||||
import "github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||
)
|
||||
|
||||
const (
|
||||
HeaderContentType = "Content-Type"
|
||||
@@ -21,13 +25,6 @@ func CreateHeaders(kvs ...string) [][2]string {
|
||||
return headers
|
||||
}
|
||||
|
||||
func OverwriteRequestHost(host string) error {
|
||||
if originHost, err := proxywasm.GetHttpRequestHeader(":authority"); err == nil {
|
||||
_ = proxywasm.ReplaceHttpRequestHeader("X-ENVOY-ORIGINAL-HOST", originHost)
|
||||
}
|
||||
return proxywasm.ReplaceHttpRequestHeader(":authority", host)
|
||||
}
|
||||
|
||||
func OverwriteRequestPath(path string) error {
|
||||
if originPath, err := proxywasm.GetHttpRequestHeader(":path"); err == nil {
|
||||
_ = proxywasm.ReplaceHttpRequestHeader("X-ENVOY-ORIGINAL-PATH", originPath)
|
||||
@@ -43,3 +40,56 @@ func OverwriteRequestAuthorization(credential string) error {
|
||||
}
|
||||
return proxywasm.ReplaceHttpRequestHeader("Authorization", credential)
|
||||
}
|
||||
|
||||
func OverwriteRequestHostHeader(headers http.Header, host string) {
|
||||
if originHost, err := proxywasm.GetHttpRequestHeader(":authority"); err == nil {
|
||||
headers.Set("X-ENVOY-ORIGINAL-HOST", originHost)
|
||||
}
|
||||
headers.Set(":authority", host)
|
||||
}
|
||||
|
||||
func OverwriteRequestPathHeader(headers http.Header, path string) {
|
||||
if originPath, err := proxywasm.GetHttpRequestHeader(":path"); err == nil {
|
||||
headers.Set("X-ENVOY-ORIGINAL-PATH", originPath)
|
||||
}
|
||||
headers.Set(":path", path)
|
||||
}
|
||||
|
||||
func OverwriteRequestAuthorizationHeader(headers http.Header, credential string) {
|
||||
if exist := headers.Get("X-HI-ORIGINAL-AUTH"); exist == "" {
|
||||
if originAuth := headers.Get("Authorization"); originAuth != "" {
|
||||
headers.Set("X-HI-ORIGINAL-AUTH", originAuth)
|
||||
}
|
||||
}
|
||||
headers.Set("Authorization", credential)
|
||||
}
|
||||
|
||||
func HeaderToSlice(header http.Header) [][2]string {
|
||||
slice := make([][2]string, 0, len(header))
|
||||
for key, values := range header {
|
||||
for _, value := range values {
|
||||
slice = append(slice, [2]string{key, value})
|
||||
}
|
||||
}
|
||||
return slice
|
||||
}
|
||||
|
||||
func SliceToHeader(slice [][2]string) http.Header {
|
||||
header := make(http.Header)
|
||||
for _, pair := range slice {
|
||||
key := pair[0]
|
||||
value := pair[1]
|
||||
header.Add(key, value)
|
||||
}
|
||||
return header
|
||||
}
|
||||
|
||||
func GetOriginalHttpHeaders() http.Header {
|
||||
originalHeaders, _ := proxywasm.GetHttpRequestHeaders()
|
||||
return SliceToHeader(originalHeaders)
|
||||
}
|
||||
|
||||
func ReplaceOriginalHttpHeaders(headers http.Header) {
|
||||
modifiedHeaders := HeaderToSlice(headers)
|
||||
_ = proxywasm.ReplaceHttpRequestHeaders(modifiedHeaders)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user