mirror of
https://github.com/alibaba/higress.git
synced 2026-04-20 11:37:27 +08:00
chore: 🤖 [frontend-gray]优化关于处理index page 处理逻辑 (#1345)
Co-authored-by: Kent Dong <ch3cho@qq.com>
This commit is contained in:
@@ -130,7 +130,7 @@ grayDeployments:
|
|||||||
- name: beta-user
|
- name: beta-user
|
||||||
version: gray
|
version: gray
|
||||||
enabled: true
|
enabled: true
|
||||||
weight: 80
|
weight: 80
|
||||||
```
|
```
|
||||||
总的灰度规则为100%,其中灰度版本的权重为`80%`,基线版本为`20%`。一旦用户命中了灰度规则,会根据IP固定这个用户的灰度版本(否则会在下次请求时随机选择一个灰度版本)。
|
总的灰度规则为100%,其中灰度版本的权重为`80%`,基线版本为`20%`。一旦用户命中了灰度规则,会根据IP固定这个用户的灰度版本(否则会在下次请求时随机选择一个灰度版本)。
|
||||||
|
|
||||||
@@ -229,16 +229,16 @@ grayDeployments:
|
|||||||
- name: beta-user
|
- name: beta-user
|
||||||
version: gray
|
version: gray
|
||||||
enabled: true
|
enabled: true
|
||||||
weight: 80
|
weight: 80
|
||||||
injection:
|
injection:
|
||||||
head:
|
head:
|
||||||
- <script>console.log('Header')</script>
|
- <script>console.log('Header')</script>
|
||||||
body:
|
body:
|
||||||
first:
|
first:
|
||||||
- <script>console.log('hello world before')</script>
|
- <script>console.log('hello world before')</script>
|
||||||
- <script>console.log('hello world before1')</script>
|
- <script>console.log('hello world before1')</script>
|
||||||
last:
|
last:
|
||||||
- <script>console.log('hello world after')</script>
|
- <script>console.log('hello world after')</script>
|
||||||
- <script>console.log('hello world after2')</script>
|
- <script>console.log('hello world after2')</script>
|
||||||
```
|
```
|
||||||
通过 `injection`往HTML首页注入代码,可以在`head`标签注入代码,也可以在`body`标签的`first`和`last`位置注入代码。
|
通过 `injection`往HTML首页注入代码,可以在`head`标签注入代码,也可以在`body`标签的`first`和`last`位置注入代码。
|
||||||
@@ -73,25 +73,24 @@ static_resources:
|
|||||||
],
|
],
|
||||||
"rewrite": {
|
"rewrite": {
|
||||||
"host": "frontend-gray-cn-shanghai.oss-cn-shanghai-internal.aliyuncs.com",
|
"host": "frontend-gray-cn-shanghai.oss-cn-shanghai-internal.aliyuncs.com",
|
||||||
"notFoundUri": "/mfe/app1/{version}/333.html",
|
"notFoundUri": "/cygtapi/{version}/333.html",
|
||||||
"indexRouting": {
|
"indexRouting": {
|
||||||
"/app1": "/mfe/app1/{version}/index.html",
|
"/app1": "/cygtapi/{version}/index.html",
|
||||||
"/": "/mfe/app1/{version}/index.html"
|
"/": "/cygtapi/{version}/index.html"
|
||||||
},
|
},
|
||||||
"fileRouting": {
|
"fileRouting": {
|
||||||
"/": "/mfe/app1/{version}",
|
"/": "/cygtapi/{version}",
|
||||||
"/app1": "/mfe/app1/{version}"
|
"/app1": "/cygtapi/{version}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"baseDeployment": {
|
"baseDeployment": {
|
||||||
"version": "dev"
|
"version": "base"
|
||||||
},
|
},
|
||||||
"grayDeployments": [
|
"grayDeployments": [
|
||||||
{
|
{
|
||||||
"name": "beta-user",
|
"name": "beta-user",
|
||||||
"version": "0.0.1",
|
"version": "gray",
|
||||||
"enabled": true,
|
"enabled": true
|
||||||
"weight": 50
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"injection": {
|
"injection": {
|
||||||
@@ -128,4 +127,4 @@ static_resources:
|
|||||||
address:
|
address:
|
||||||
socket_address:
|
socket_address:
|
||||||
address: frontend-gray-cn-shanghai.oss-cn-shanghai.aliyuncs.com
|
address: frontend-gray-cn-shanghai.oss-cn-shanghai.aliyuncs.com
|
||||||
port_value: 80
|
port_value: 80
|
||||||
|
|||||||
@@ -108,19 +108,26 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
|
|||||||
if !util.IsGrayEnabled(grayConfig) {
|
if !util.IsGrayEnabled(grayConfig) {
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
status, err := proxywasm.GetHttpResponseHeader(":status")
|
isPageRequest, ok := ctx.GetContext(config.IsPageRequest).(bool)
|
||||||
contentType, _ := proxywasm.GetHttpResponseHeader("Content-Type")
|
if !ok {
|
||||||
|
isPageRequest = false // 默认值
|
||||||
|
}
|
||||||
|
// response 不处理非首页的请求
|
||||||
|
if !isPageRequest {
|
||||||
|
ctx.DontReadResponseBody()
|
||||||
|
return types.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
status, err := proxywasm.GetHttpResponseHeader(":status")
|
||||||
if grayConfig.Rewrite != nil && grayConfig.Rewrite.Host != "" {
|
if grayConfig.Rewrite != nil && grayConfig.Rewrite.Host != "" {
|
||||||
// 删除Content-Disposition,避免自动下载文件
|
// 删除Content-Disposition,避免自动下载文件
|
||||||
proxywasm.RemoveHttpResponseHeader("Content-Disposition")
|
proxywasm.RemoveHttpResponseHeader("Content-Disposition")
|
||||||
}
|
}
|
||||||
|
|
||||||
isPageRequest, ok := ctx.GetContext(config.IsPageRequest).(bool)
|
// 删除content-length,可能要修改Response返回值
|
||||||
if !ok {
|
proxywasm.RemoveHttpResponseHeader("Content-Length")
|
||||||
isPageRequest = false // 默认值
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 处理code为 200的情况
|
||||||
if err != nil || status != "200" {
|
if err != nil || status != "200" {
|
||||||
if status == "404" {
|
if status == "404" {
|
||||||
if grayConfig.Rewrite.NotFound != "" && isPageRequest {
|
if grayConfig.Rewrite.NotFound != "" && isPageRequest {
|
||||||
@@ -143,6 +150,7 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
|
|||||||
ctx.BufferResponseBody()
|
ctx.BufferResponseBody()
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
} else {
|
} else {
|
||||||
|
// 直接返回400
|
||||||
ctx.DontReadResponseBody()
|
ctx.DontReadResponseBody()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,25 +158,19 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
|
|||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除content-length,可能要修改Response返回值
|
// 不会进去Streaming 的Body处理
|
||||||
proxywasm.RemoveHttpResponseHeader("Content-Length")
|
ctx.BufferResponseBody()
|
||||||
|
proxywasm.ReplaceHttpResponseHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
|
||||||
|
|
||||||
if strings.HasPrefix(contentType, "text/html") || isPageRequest {
|
frontendVersion := ctx.GetContext(config.XPreHigressTag).(string)
|
||||||
// 不会进去Streaming 的Body处理
|
xUniqueClient := ctx.GetContext(config.XUniqueClientId).(string)
|
||||||
ctx.BufferResponseBody()
|
|
||||||
|
|
||||||
proxywasm.ReplaceHttpResponseHeader("Cache-Control", "no-cache, no-store")
|
// 设置前端的版本
|
||||||
|
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s,%s; Max-Age=%s; Path=/;", config.XPreHigressTag, frontendVersion, xUniqueClient, grayConfig.UserStickyMaxAge))
|
||||||
frontendVersion := ctx.GetContext(config.XPreHigressTag).(string)
|
// 设置后端的版本
|
||||||
xUniqueClient := ctx.GetContext(config.XUniqueClientId).(string)
|
if util.IsBackendGrayEnabled(grayConfig) {
|
||||||
|
backendVersion := ctx.GetContext(grayConfig.BackendGrayTag).(string)
|
||||||
// 设置前端的版本
|
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%s; Path=/;", grayConfig.BackendGrayTag, backendVersion, grayConfig.UserStickyMaxAge))
|
||||||
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s,%s; Max-Age=%s; Path=/;", config.XPreHigressTag, frontendVersion, xUniqueClient, grayConfig.UserStickyMaxAge))
|
|
||||||
// 设置后端的版本
|
|
||||||
if util.IsBackendGrayEnabled(grayConfig) {
|
|
||||||
backendVersion := ctx.GetContext(grayConfig.BackendGrayTag).(string)
|
|
||||||
proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%s; Path=/;", grayConfig.BackendGrayTag, backendVersion, grayConfig.UserStickyMaxAge))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return types.ActionContinue
|
return types.ActionContinue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user