Skip to content

Commit b166376

Browse files
committed
Merge branch 'master' into fix_router_find_after_invalid_set_param_values
2 parents 23c2187 + 095af21 commit b166376

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
}
@@ -881,6 +890,14 @@ func TestContext_RealIP(t *testing.T) {
881890
},
882891
"127.0.0.1",
883892
},
893+
{
894+
&context{
895+
request: &http.Request{
896+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}},
897+
},
898+
},
899+
"127.0.0.1",
900+
},
884901
{
885902
&context{
886903
request: &http.Request{

0 commit comments

Comments
 (0)