fix: Golang filter supports skipping processing at the body stage. (#1989)

This commit is contained in:
Jingze
2025-04-01 15:27:38 +08:00
committed by GitHub
parent 7b6e4154f4
commit a602f7a725

View File

@@ -25,6 +25,7 @@ type filter struct {
serverName string serverName string
message bool message bool
proxyURL *url.URL proxyURL *url.URL
skip bool
} }
type RequestURL struct { type RequestURL struct {
@@ -55,6 +56,7 @@ func (f *filter) DecodeHeaders(header api.RequestHeaderMap, endStream bool) api.
// Check if request matches any rule in match_list // Check if request matches any rule in match_list
if !internal.IsMatch(f.config.matchList, url.host, f.path) { if !internal.IsMatch(f.config.matchList, url.host, f.path) {
f.skip = true
api.LogDebugf("Request does not match any rule in match_list: %s", url.parsedURL.String()) api.LogDebugf("Request does not match any rule in match_list: %s", url.parsedURL.String())
return api.Continue return api.Continue
} }
@@ -118,6 +120,9 @@ func (f *filter) DecodeHeaders(header api.RequestHeaderMap, endStream bool) api.
// DecodeData might be called multiple times during handling the request body. // DecodeData might be called multiple times during handling the request body.
// The endStream is true when handling the last piece of the body. // The endStream is true when handling the last piece of the body.
func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.StatusType { func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.StatusType {
if f.skip {
return api.Continue
}
if f.message { if f.message {
if endStream { if endStream {
for _, server := range f.config.servers { for _, server := range f.config.servers {
@@ -140,6 +145,9 @@ func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.Statu
// Callbacks which are called in response path // Callbacks which are called in response path
// The endStream is true if the response doesn't have body // The endStream is true if the response doesn't have body
func (f *filter) EncodeHeaders(header api.ResponseHeaderMap, endStream bool) api.StatusType { func (f *filter) EncodeHeaders(header api.ResponseHeaderMap, endStream bool) api.StatusType {
if f.skip {
return api.Continue
}
if f.serverName != "" { if f.serverName != "" {
header.Set("Content-Type", "text/event-stream") header.Set("Content-Type", "text/event-stream")
header.Set("Cache-Control", "no-cache") header.Set("Cache-Control", "no-cache")
@@ -154,6 +162,9 @@ func (f *filter) EncodeHeaders(header api.ResponseHeaderMap, endStream bool) api
// EncodeData might be called multiple times during handling the response body. // EncodeData might be called multiple times during handling the response body.
// The endStream is true when handling the last piece of the body. // The endStream is true when handling the last piece of the body.
func (f *filter) EncodeData(buffer api.BufferInstance, endStream bool) api.StatusType { func (f *filter) EncodeData(buffer api.BufferInstance, endStream bool) api.StatusType {
if f.skip {
return api.Continue
}
if !endStream { if !endStream {
return api.StopAndBuffer return api.StopAndBuffer
} }