@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "path"
6
7
"strings"
7
8
8
9
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/config"
@@ -32,15 +33,18 @@ func parseConfig(json gjson.Result, grayConfig *config.GrayConfig, log wrapper.L
32
33
}
33
34
34
35
func onHttpRequestHeaders (ctx wrapper.HttpContext , grayConfig config.GrayConfig , log wrapper.Log ) types.Action {
35
- if ! util .IsGrayEnabled (grayConfig ) {
36
+ requestPath , _ := proxywasm .GetHttpRequestHeader (":path" )
37
+ requestPath = path .Clean (requestPath )
38
+ enabledGray := util .IsGrayEnabled (grayConfig , requestPath )
39
+ ctx .SetContext (config .EnabledGray , enabledGray )
40
+
41
+ if ! enabledGray {
42
+ ctx .DontReadRequestBody ()
36
43
return types .ActionContinue
37
44
}
38
45
39
46
cookies , _ := proxywasm .GetHttpRequestHeader ("cookie" )
40
- path , _ := proxywasm .GetHttpRequestHeader (":path" )
41
- fetchMode , _ := proxywasm .GetHttpRequestHeader ("sec-fetch-mode" )
42
-
43
- isPageRequest := util .IsPageRequest (fetchMode , path )
47
+ isPageRequest := util .IsPageRequest (requestPath )
44
48
hasRewrite := len (grayConfig .Rewrite .File ) > 0 || len (grayConfig .Rewrite .Index ) > 0
45
49
grayKeyValueByCookie := util .ExtractCookieValueByKey (cookies , grayConfig .GrayKey )
46
50
grayKeyValueByHeader , _ := proxywasm .GetHttpRequestHeader (grayConfig .GrayKey )
@@ -73,7 +77,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
73
77
} else {
74
78
deployment = util .FilterGrayRule (& grayConfig , grayKeyValue )
75
79
}
76
- log .Infof ("index deployment: %v, path: %v, backend: %v, xPreHigressVersion: %s,%s" , deployment , path , deployment .BackendVersion , preVersion , preUniqueClientId )
80
+ log .Infof ("index deployment: %v, path: %v, backend: %v, xPreHigressVersion: %s,%s" , deployment , requestPath , deployment .BackendVersion , preVersion , preUniqueClientId )
77
81
} else {
78
82
grayDeployment := util .FilterGrayRule (& grayConfig , grayKeyValue )
79
83
deployment = util .GetVersion (grayConfig , grayDeployment , preVersion , isPageRequest )
@@ -91,22 +95,23 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
91
95
}
92
96
93
97
if hasRewrite {
94
- rewritePath := path
98
+ rewritePath := requestPath
95
99
if isPageRequest {
96
- rewritePath = util .IndexRewrite (path , deployment .Version , grayConfig .Rewrite .Index )
100
+ rewritePath = util .IndexRewrite (requestPath , deployment .Version , grayConfig .Rewrite .Index )
97
101
} else {
98
- rewritePath = util .PrefixFileRewrite (path , deployment .Version , grayConfig .Rewrite .File )
102
+ rewritePath = util .PrefixFileRewrite (requestPath , deployment .Version , grayConfig .Rewrite .File )
99
103
}
100
- if path != rewritePath {
101
- log .Infof ("rewrite path:%s, rewritePath:%s, Version:%v" , path , rewritePath , deployment .Version )
104
+ if requestPath != rewritePath {
105
+ log .Infof ("rewrite path:%s, rewritePath:%s, Version:%v" , requestPath , rewritePath , deployment .Version )
102
106
proxywasm .ReplaceHttpRequestHeader (":path" , rewritePath )
103
107
}
104
108
}
105
109
return types .ActionContinue
106
110
}
107
111
108
112
func onHttpResponseHeader (ctx wrapper.HttpContext , grayConfig config.GrayConfig , log wrapper.Log ) types.Action {
109
- if ! util .IsGrayEnabled (grayConfig ) {
113
+ enabledGray , _ := ctx .GetContext (config .EnabledGray ).(bool )
114
+ if ! enabledGray {
110
115
ctx .DontReadResponseBody ()
111
116
return types .ActionContinue
112
117
}
@@ -179,7 +184,8 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
179
184
}
180
185
181
186
func onHttpResponseBody (ctx wrapper.HttpContext , grayConfig config.GrayConfig , body []byte , log wrapper.Log ) types.Action {
182
- if ! util .IsGrayEnabled (grayConfig ) {
187
+ enabledGray , _ := ctx .GetContext (config .EnabledGray ).(bool )
188
+ if ! enabledGray {
183
189
return types .ActionContinue
184
190
}
185
191
isPageRequest , ok := ctx .GetContext (config .IsPageRequest ).(bool )
0 commit comments