mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-10 07:27:22 +01:00
Simplify
This commit is contained in:
parent
569c2f3879
commit
a9c0587f87
@ -49,12 +49,11 @@ func New(db geo.Reader) *Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ipFromForwardedForHeader(v string) string {
|
func ipFromForwardedForHeader(v string) string {
|
||||||
// Handle both comma and comma+space separator
|
sep := strings.Index(v, ",")
|
||||||
ips := strings.Fields(strings.Replace(v, ",", " ", -1))
|
if sep == -1 {
|
||||||
if len(ips) == 0 {
|
return v
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
return ips[0]
|
return v[:sep]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipFromRequest(headers []string, r *http.Request) (net.IP, error) {
|
func ipFromRequest(headers []string, r *http.Request) (net.IP, error) {
|
||||||
|
@ -163,6 +163,7 @@ func TestIPFromRequest(t *testing.T) {
|
|||||||
{"127.0.0.1:9999", "X-Forwarded-For", "1.3.3.7", []string{"X-Real-IP", "X-Forwarded-For"}, "1.3.3.7"}, // Second trusted header matches
|
{"127.0.0.1:9999", "X-Forwarded-For", "1.3.3.7", []string{"X-Real-IP", "X-Forwarded-For"}, "1.3.3.7"}, // Second trusted header matches
|
||||||
{"127.0.0.1:9999", "X-Forwarded-For", "1.3.3.7,4.2.4.2", []string{"X-Forwarded-For"}, "1.3.3.7"}, // X-Forwarded-For with multiple entries (commas separator)
|
{"127.0.0.1:9999", "X-Forwarded-For", "1.3.3.7,4.2.4.2", []string{"X-Forwarded-For"}, "1.3.3.7"}, // X-Forwarded-For with multiple entries (commas separator)
|
||||||
{"127.0.0.1:9999", "X-Forwarded-For", "1.3.3.7, 4.2.4.2", []string{"X-Forwarded-For"}, "1.3.3.7"}, // X-Forwarded-For with multiple entries (space+comma separator)
|
{"127.0.0.1:9999", "X-Forwarded-For", "1.3.3.7, 4.2.4.2", []string{"X-Forwarded-For"}, "1.3.3.7"}, // X-Forwarded-For with multiple entries (space+comma separator)
|
||||||
|
{"127.0.0.1:9999", "X-Forwarded-For", "", []string{"X-Forwarded-For"}, "127.0.0.1"}, // Empty header
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
r := &http.Request{
|
r := &http.Request{
|
||||||
|
Loading…
Reference in New Issue
Block a user