mirror of
https://github.com/labstack/echo.git
synced 2025-07-07 01:06:40 +02:00
Merge pull request #1640 from imxyb/feature/opt-split
Use IndexAny instead of Split to reduce memory allocation
This commit is contained in:
@ -276,7 +276,11 @@ func (c *context) RealIP() string {
|
|||||||
}
|
}
|
||||||
// Fall back to legacy behavior
|
// Fall back to legacy behavior
|
||||||
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
|
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
|
||||||
return strings.Split(ip, ", ")[0]
|
i := strings.IndexAny(ip, ", ")
|
||||||
|
if i > 0 {
|
||||||
|
return ip[:i]
|
||||||
|
}
|
||||||
|
return ip
|
||||||
}
|
}
|
||||||
if ip := c.request.Header.Get(HeaderXRealIP); ip != "" {
|
if ip := c.request.Header.Get(HeaderXRealIP); ip != "" {
|
||||||
return ip
|
return ip
|
||||||
|
@ -72,6 +72,15 @@ func BenchmarkAllocXML(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkRealIPForHeaderXForwardFor(b *testing.B) {
|
||||||
|
c := context{request: &http.Request{
|
||||||
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}},
|
||||||
|
}}
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
c.RealIP()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
|
||||||
return t.templates.ExecuteTemplate(w, name, data)
|
return t.templates.ExecuteTemplate(w, name, data)
|
||||||
}
|
}
|
||||||
@ -847,6 +856,14 @@ func TestContext_RealIP(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
&context{
|
||||||
|
request: &http.Request{
|
||||||
|
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"127.0.0.1",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
&context{
|
&context{
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
|
Reference in New Issue
Block a user