mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 10:26:29 +02:00
internal/httputil: add strip port function (#106)
This commit is contained in:
parent
d235f8ebf2
commit
286aad3b92
3 changed files with 45 additions and 1 deletions
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/pomerium/pomerium/internal/https"
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/pomerium/pomerium/internal/middleware"
|
||||
"github.com/pomerium/pomerium/internal/urlutil"
|
||||
"github.com/pomerium/pomerium/internal/version"
|
||||
pbAuthenticate "github.com/pomerium/pomerium/proto/authenticate"
|
||||
pbAuthorize "github.com/pomerium/pomerium/proto/authorize"
|
||||
|
@ -58,7 +59,7 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("cmd/pomerium: new authenticate")
|
||||
}
|
||||
authHost = opts.AuthenticateURL.Host
|
||||
authHost = urlutil.StripPort(opts.AuthenticateURL.Host)
|
||||
pbAuthenticate.RegisterAuthenticatorServer(grpcServer, authenticateService)
|
||||
}
|
||||
|
||||
|
|
14
internal/urlutil/url.go
Normal file
14
internal/urlutil/url.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package urlutil // import "github.com/pomerium/pomerium/internal/urlutil"
|
||||
|
||||
import "strings"
|
||||
|
||||
func StripPort(hostport string) string {
|
||||
colon := strings.IndexByte(hostport, ':')
|
||||
if colon == -1 {
|
||||
return hostport
|
||||
}
|
||||
if i := strings.IndexByte(hostport, ']'); i != -1 {
|
||||
return strings.TrimPrefix(hostport[:i], "[")
|
||||
}
|
||||
return hostport[:colon]
|
||||
}
|
29
internal/urlutil/url_test.go
Normal file
29
internal/urlutil/url_test.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package urlutil // import "github.com/pomerium/pomerium/internal/urlutil"
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_StripPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
hostport string
|
||||
want string
|
||||
}{
|
||||
{"localhost", "localhost", "localhost"},
|
||||
{"localhost with port", "localhost:443", "localhost"},
|
||||
{"IPv6 localhost", "[::1]:80", "::1"},
|
||||
{"IPv6 localhost without port", "[::1]", "::1"},
|
||||
{"domain with port", "example.org:8080", "example.org"},
|
||||
{"domain without port", "example.org", "example.org"},
|
||||
{"long domain with port", "some.super.long.domain.example.org:8080", "some.super.long.domain.example.org"},
|
||||
{"IPv6 with port", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
|
||||
{"IPv6 without port", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := StripPort(tt.hostport); got != tt.want {
|
||||
t.Errorf("StripPort() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue