Skip to content

Commit 5cadf45

Browse files
committed
Merge branch 'master' into panic_router_find_fails_on_params_with_no_root
2 parents 85e521f + 095af21 commit 5cadf45

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

context.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ func (c *context) RealIP() string {
276276
}
277277
// Fall back to legacy behavior
278278
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
279-
return strings.Split(ip, ", ")[0]
279+
i := strings.IndexAny(ip, ", ")
280+
if i > 0 {
281+
return ip[:i]
282+
}
283+
return ip
280284
}
281285
if ip := c.request.Header.Get(HeaderXRealIP); ip != "" {
282286
return ip

context_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ func BenchmarkAllocXML(b *testing.B) {
7272
}
7373
}
7474

75+
func BenchmarkRealIPForHeaderXForwardFor(b *testing.B) {
76+
c := context{request: &http.Request{
77+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}},
78+
}}
79+
for i := 0; i < b.N; i++ {
80+
c.RealIP()
81+
}
82+
}
83+
7584
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
7685
return t.templates.ExecuteTemplate(w, name, data)
7786
}
@@ -847,6 +856,14 @@ func TestContext_RealIP(t *testing.T) {
847856
},
848857
"127.0.0.1",
849858
},
859+
{
860+
&context{
861+
request: &http.Request{
862+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}},
863+
},
864+
},
865+
"127.0.0.1",
866+
},
850867
{
851868
&context{
852869
request: &http.Request{

0 commit comments

Comments
 (0)