diff --git a/plugins/wasm-go/extensions/frontend-gray/README.md b/plugins/wasm-go/extensions/frontend-gray/README.md index af92008377..dce90802c6 100644 --- a/plugins/wasm-go/extensions/frontend-gray/README.md +++ b/plugins/wasm-go/extensions/frontend-gray/README.md @@ -31,8 +31,8 @@ |------------|--------------|------|-----|------------------------------| | `host` | string | 非必填 | - | host地址,如果是OSS则设置为 VPC 内网访问地址 | | `notFoundUri` | string | 非必填 | - | 404 页面配置 | -| `indexRouting` | object | 非必填 | - | 首页重写配置 | -| `fileRouting` | object | 非必填 | - | 文件重写配置 | +| `indexRouting` | map of string to string | 非必填 | - | 用于定义首页重写路由规则。每个键 (Key) 表示首页的路由路径,值 (Value) 则指向重定向的目标文件。例如,键为 `/app1` 对应的值为 `/mfe/app1/{version}/index.html`。生效version为`0.0.1`, 访问路径为 `/app1`,则重定向到 `/mfe/app1/0.0.1/index.html`。 | +| `fileRouting` | map of string to string | 非必填 | - | 用于定义资源文件重写路由规则。每个键 (Key) 表示资源访问路径,值 (Value) 则指向重定向的目标文件。例如,键为 `/app1/` 对应的值为 `/mfe/app1/{version}`。生效version为`0.0.1`,访问路径为 `/app1/js/a.js`,则重定向到 `/mfe/app1/0.0.1/js/a.js`。 | `baseDeployment`字段配置说明: diff --git a/plugins/wasm-go/extensions/frontend-gray/main.go b/plugins/wasm-go/extensions/frontend-gray/main.go index 17e03922b4..365c200f95 100644 --- a/plugins/wasm-go/extensions/frontend-gray/main.go +++ b/plugins/wasm-go/extensions/frontend-gray/main.go @@ -38,10 +38,9 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig, cookies, _ := proxywasm.GetHttpRequestHeader("cookie") path, _ := proxywasm.GetHttpRequestHeader(":path") - accept, _ := proxywasm.GetHttpRequestHeader("accept") fetchMode, _ := proxywasm.GetHttpRequestHeader("sec-fetch-mode") - isIndex := util.IsIndexRequest(fetchMode, accept, path) + isIndex := util.IsIndexRequest(fetchMode, path) hasRewrite := len(grayConfig.Rewrite.File) > 0 || len(grayConfig.Rewrite.Index) > 0 grayKeyValue := util.GetGrayKey(util.ExtractCookieValueByKey(cookies, grayConfig.GrayKey), grayConfig.GraySubKey) diff --git a/plugins/wasm-go/extensions/frontend-gray/util/utils.go b/plugins/wasm-go/extensions/frontend-gray/util/utils.go index 45d30da677..9b0cb52080 100644 --- a/plugins/wasm-go/extensions/frontend-gray/util/utils.go +++ b/plugins/wasm-go/extensions/frontend-gray/util/utils.go @@ -99,18 +99,12 @@ var indexSuffixes = []string{ } // IsIndexRequest determines if the request is an index request -func IsIndexRequest(fetchMode string, accept string, p string) bool { +func IsIndexRequest(fetchMode string, p string) bool { if fetchMode == "cors" { return false } - - for _, suffix := range indexSuffixes { - if strings.HasSuffix(p, suffix) { - return true - } - } - - return path.Ext(p) == "" + ext := path.Ext(p) + return ext == "" || ContainsValue(indexSuffixes, ext) } // 首页Rewrite diff --git a/plugins/wasm-go/extensions/frontend-gray/util/utils_test.go b/plugins/wasm-go/extensions/frontend-gray/util/utils_test.go index 4ab7154923..147c32311b 100644 --- a/plugins/wasm-go/extensions/frontend-gray/util/utils_test.go +++ b/plugins/wasm-go/extensions/frontend-gray/util/utils_test.go @@ -79,3 +79,26 @@ func TestPrefixFileRewrite(t *testing.T) { }) } } + +func TestIsIndexRequest(t *testing.T) { + var tests = []struct { + fetchMode string + p string + output bool + }{ + {"cors", "/js/a.js", false}, + {"no-cors", "/js/a.js", false}, + {"no-cors", "/images/a.png", false}, + {"no-cors", "/index", true}, + {"cors", "/inde", false}, + {"no-cors", "/index.html", true}, + {"no-cors", "/demo.php", true}, + } + for _, test := range tests { + testPath := test.p + t.Run(testPath, func(t *testing.T) { + output := IsIndexRequest(test.fetchMode, testPath) + assert.Equal(t, test.output, output) + }) + } +}