From 7303932ab2283f14b093dea467141cdbf772323f Mon Sep 17 00:00:00 2001 From: 1911860538 Date: Tue, 3 Jun 2025 20:16:27 +0800 Subject: [PATCH] perf: use textproto.TrimString for HTTP header parsing to improve performance --- gin.go | 3 ++- utils.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/gin.go b/gin.go index 1965a429..5202e324 100644 --- a/gin.go +++ b/gin.go @@ -9,6 +9,7 @@ import ( "html/template" "net" "net/http" + "net/textproto" "os" "path" "regexp" @@ -475,7 +476,7 @@ func (engine *Engine) validateHeader(header string) (clientIP string, valid bool } items := strings.Split(header, ",") for i := len(items) - 1; i >= 0; i-- { - ipStr := strings.TrimSpace(items[i]) + ipStr := textproto.TrimString(items[i]) ip := net.ParseIP(ipStr) if ip == nil { break diff --git a/utils.go b/utils.go index 47106a7a..1c96e0d9 100644 --- a/utils.go +++ b/utils.go @@ -7,6 +7,7 @@ package gin import ( "encoding/xml" "net/http" + "net/textproto" "os" "path" "reflect" @@ -107,7 +108,7 @@ func parseAccept(acceptHeader string) []string { if i := strings.IndexByte(part, ';'); i > 0 { part = part[:i] } - if part = strings.TrimSpace(part); part != "" { + if part = textproto.TrimString(part); part != "" { out = append(out, part) } }