mirror of
https://github.com/alibaba/higress.git
synced 2026-06-26 02:35:02 +08:00
fix: avoid waiting for missing request bodies in transformer (#4024)
Signed-off-by: johnlanni <zty98751@alibaba-inc.com>
This commit is contained in:
@@ -39,6 +39,7 @@ func init() {
|
||||
wrapper.ProcessRequestBodyBy(onHttpRequestBody),
|
||||
wrapper.ProcessResponseHeadersBy(onHttpResponseHeaders),
|
||||
wrapper.ProcessResponseBodyBy(onHttpResponseBody),
|
||||
wrapper.WithRebuildMaxMemBytes[TransformerConfig](200*1024*1024),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -343,11 +344,12 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config TransformerConfig, log
|
||||
isValidRequestContent := isValidRequestContentType(contentType)
|
||||
isBodyChange := config.reqTrans.IsBodyChange()
|
||||
needBodyMapSource := config.reqTrans.NeedBodyMapSource()
|
||||
hasRequestBody := ctx.HasRequestBody()
|
||||
|
||||
log.Debugf("contentType:%s, isValidRequestContent:%v, isBodyChange:%v, needBodyMapSource:%v",
|
||||
contentType, isValidRequestContent, isBodyChange, needBodyMapSource)
|
||||
log.Debugf("contentType:%s, isValidRequestContent:%v, isBodyChange:%v, needBodyMapSource:%v, hasRequestBody:%v",
|
||||
contentType, isValidRequestContent, isBodyChange, needBodyMapSource, hasRequestBody)
|
||||
|
||||
if isBodyChange && isValidRequestContent {
|
||||
if isBodyChange && isValidRequestContent && hasRequestBody {
|
||||
delete(hs, "content-length")
|
||||
}
|
||||
|
||||
@@ -361,7 +363,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config TransformerConfig, log
|
||||
ctx.SetContext("headers", hs)
|
||||
ctx.SetContext("querys", qs)
|
||||
|
||||
if !isValidRequestContent || (!isBodyChange && !needBodyMapSource) {
|
||||
if !hasRequestBody || !isValidRequestContent || (!isBodyChange && !needBodyMapSource) {
|
||||
ctx.DontReadRequestBody()
|
||||
} else if needBodyMapSource {
|
||||
// we need do transform during body phase
|
||||
|
||||
@@ -424,6 +424,43 @@ func TestRequest_MapFromBody_DelaysHeaderTransform(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRequest_MapFromBody_NoRequestBodyDoesNotPause(t *testing.T) {
|
||||
test.RunTest(t, func(t *testing.T) {
|
||||
host, status := test.NewTestHost(configJSON(map[string]any{
|
||||
"reqRules": []map[string]any{
|
||||
{
|
||||
"operate": "map",
|
||||
"mapSource": "body",
|
||||
"headers": []map[string]any{
|
||||
{"fromKey": "user.id", "toKey": "X-User-Id"},
|
||||
},
|
||||
},
|
||||
{
|
||||
"operate": "add",
|
||||
"headers": []map[string]any{
|
||||
{"key": "X-Static", "value": "ok"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
action := host.CallOnHttpRequestHeaders([][2]string{
|
||||
{":authority", "test.com"},
|
||||
{":path", "/p"},
|
||||
{":method", "POST"},
|
||||
{"content-type", "application/json"},
|
||||
}, test.WithEndOfStream(true))
|
||||
require.Equal(t, types.ActionContinue, action, "request without body must not wait for body callback")
|
||||
|
||||
got := headersToMap(host.GetRequestHeaders())
|
||||
require.Equal(t, []string{"ok"}, got["x-static"])
|
||||
require.NotContains(t, got, "x-user-id")
|
||||
host.CompleteHttp()
|
||||
})
|
||||
}
|
||||
|
||||
// --- regex template ---
|
||||
|
||||
func TestRequest_Headers_AddWithHostPattern(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user