Skip to content

Commit b95c454

Browse files
committed
chore: review comments
1 parent d160a18 commit b95c454

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

httputil/client.go

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package httputil
33
import (
44
"io"
55
"net/http"
6+
"strings"
7+
)
8+
9+
const (
10+
headerXForwardedFor = "X-Forwarded-For"
611
)
712

813
// CloseResponse closes the response's body. But reads at least some of the body so if it's
@@ -15,3 +20,13 @@ func CloseResponse(resp *http.Response) {
1520
resp.Body.Close()
1621
}
1722
}
23+
24+
func GetRequestIP(req *http.Request) string {
25+
addresses := strings.Split(req.Header.Get(headerXForwardedFor), ",")
26+
if addresses[0] == "" {
27+
splits := strings.Split(req.RemoteAddr, ":")
28+
return strings.Join(splits[:len(splits)-1], ":") // When there is no load-balancer
29+
}
30+
31+
return strings.ReplaceAll(addresses[0], " ", "")
32+
}

ip/ip_test.go renamed to httputil/client_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package ip_test
1+
package httputil_test
22

33
import (
44
"net/http"
55
"testing"
66

77
"github.com/stretchr/testify/require"
88

9-
kitip "github.com/rudderlabs/rudder-go-kit/ip"
9+
"github.com/rudderlabs/rudder-go-kit/httputil"
1010
)
1111

12-
func TestIPFromReq(t *testing.T) {
12+
func TestGetRequestIP(t *testing.T) {
1313
testCases := []struct {
1414
name string
1515
headerValue string
@@ -42,7 +42,7 @@ func TestIPFromReq(t *testing.T) {
4242
Header: http.Header{"X-Forwarded-For": {testCase.headerValue}},
4343
RemoteAddr: testCase.remoteAddr,
4444
}
45-
require.Equal(t, testCase.expectedResult, kitip.FromReq(req))
45+
require.Equal(t, testCase.expectedResult, httputil.GetRequestIP(req))
4646
})
4747
}
4848
}

ip/ip.go

-20
This file was deleted.

0 commit comments

Comments
 (0)