Skip to content

Commit

Permalink
refactor: 💡 优化 IndexRequest 逻辑,以及加上test case
Browse files Browse the repository at this point in the history
  • Loading branch information
heimanba committed Aug 14, 2024
1 parent 5513f95 commit 3f8ef30
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions plugins/wasm-go/extensions/frontend-gray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`字段配置说明:

Expand Down
3 changes: 1 addition & 2 deletions plugins/wasm-go/extensions/frontend-gray/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 3 additions & 9 deletions plugins/wasm-go/extensions/frontend-gray/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions plugins/wasm-go/extensions/frontend-gray/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 3f8ef30

Please sign in to comment.