Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Aszalos <gabriel.aszalos@gmail.com>
  • Loading branch information
Hellzy and gbbr committed Jun 9, 2022
1 parent 7c475c0 commit adc7b28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 2 additions & 6 deletions contrib/internal/httptrace/httptrace.go
Expand Up @@ -95,21 +95,17 @@ func getClientIP(remoteAddr string, headers http.Header, clientIPHeader string)
ipHeaders = []string{clientIPHeader}
}
check := func(value string) netaddr.IP {
for _, ip := range strings.Split(value, ",") {
ipStr := strings.Trim(ip, " ")
ip := parseIP(ipStr)

for _, ipstr := range strings.Split(value, ",") {
ip := parseIP(strings.TrimSpace(ipstr))
if !ip.IsValid() {
continue
}

if isGlobal(ip) {
return ip
}
}
return netaddr.IP{}
}

for _, hdr := range ipHeaders {
if value := headers.Get(hdr); value != "" {
if ip := check(value); ip.IsValid() {
Expand Down
14 changes: 10 additions & 4 deletions contrib/internal/httptrace/options_test.go
Expand Up @@ -13,13 +13,18 @@ import (
)

func TestConfig(t *testing.T) {
t.Run("config", func(t *testing.T) {
t.Run("client-ip-header-unset", func(t *testing.T) {
t.Run("client-ip", func(t *testing.T) {
t.Run("unset", func(t *testing.T) {
restore := cleanEnv()
err := os.Unsetenv(clientIPHeaderEnvVar)
require.NoError(t, err)
cfg := newConfig()
require.Empty(t, cfg.ipHeader)
defer restore()

})
t.Run("client-ip-header-empty", func(t *testing.T) {

t.Run("empty", func(t *testing.T) {
restore := cleanEnv()
err := os.Setenv(clientIPHeaderEnvVar, "")
require.NoError(t, err)
Expand All @@ -28,7 +33,8 @@ func TestConfig(t *testing.T) {
defer restore()

})
t.Run("client-ip-header-set", func(t *testing.T) {

t.Run("set", func(t *testing.T) {
restore := cleanEnv()
err := os.Setenv(clientIPHeaderEnvVar, "custom-header")
require.NoError(t, err)
Expand Down

0 comments on commit adc7b28

Please sign in to comment.