fix ai proxy check request body logic (#1252)

This commit is contained in:
澄潭
2024-08-26 13:33:44 +08:00
committed by GitHub
parent a64cb172bf
commit 496346fe95
2 changed files with 11 additions and 6 deletions

View File

@@ -74,16 +74,16 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf
if handler, ok := activeProvider.(provider.RequestHeadersHandler); ok { if handler, ok := activeProvider.(provider.RequestHeadersHandler); ok {
// Disable the route re-calculation since the plugin may modify some headers related to the chosen route. // Disable the route re-calculation since the plugin may modify some headers related to the chosen route.
ctx.DisableReroute() ctx.DisableReroute()
hasRequestBody := wrapper.HasRequestBody()
_, err := handler.OnRequestHeaders(ctx, apiName, log) action, err := handler.OnRequestHeaders(ctx, apiName, log)
if err == nil { if err == nil {
if wrapper.HasRequestBody() { if hasRequestBody {
ctx.SetRequestBodyBufferLimit(defaultMaxBodyBytes) ctx.SetRequestBodyBufferLimit(defaultMaxBodyBytes)
// Always return types.HeaderStopIteration to support fallback routing, // Always return types.HeaderStopIteration to support fallback routing,
// as long as onHttpRequestBody can be called. // as long as onHttpRequestBody can be called.
return types.HeaderStopIteration return types.HeaderStopIteration
} }
return types.ActionContinue return action
} }
_ = util.SendResponse(500, "ai-proxy.proc_req_headers_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process request headers: %v", err)) _ = util.SendResponse(500, "ai-proxy.proc_req_headers_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process request headers: %v", err))
return types.ActionContinue return types.ActionContinue

View File

@@ -84,14 +84,19 @@ func IsBinaryResponseBody() bool {
} }
func HasRequestBody() bool { func HasRequestBody() bool {
contentTypeStr, _ := proxywasm.GetHttpRequestHeader("content-type")
contentLengthStr, _ := proxywasm.GetHttpRequestHeader("content-length") contentLengthStr, _ := proxywasm.GetHttpRequestHeader("content-length")
transferEncodingStr, _ := proxywasm.GetHttpRequestHeader("transfer-encoding")
proxywasm.LogDebugf("check has request body: contentType:%s, contentLengthStr:%s, transferEncodingStr:%s",
contentTypeStr, contentLengthStr, transferEncodingStr)
if contentTypeStr != "" {
return true
}
if contentLengthStr != "" { if contentLengthStr != "" {
contentLength, err := strconv.Atoi(contentLengthStr) contentLength, err := strconv.Atoi(contentLengthStr)
if err == nil && contentLength > 0 { if err == nil && contentLength > 0 {
return true return true
} }
} }
transferEncodingStr, _ := proxywasm.GetHttpRequestHeader("transfer-encoding")
return strings.Contains(transferEncodingStr, "chunked") return strings.Contains(transferEncodingStr, "chunked")
} }