Skip to content

Commit

Permalink
update host normalization to be a bit smarter about evaluating the in…
Browse files Browse the repository at this point in the history
…coming uri, update unit tests
  • Loading branch information
austin-denoble committed Aug 1, 2024
1 parent 6437ad4 commit 3c29ae5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions pinecone/index_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"log"
"net/url"
"strings"

"github.com/pinecone-io/go-pinecone/internal/gen/data"
Expand Down Expand Up @@ -35,16 +36,20 @@ type newIndexParameters struct {
}

func newIndexConnection(in newIndexParameters, dialOpts ...grpc.DialOption) (*IndexConnection, error) {
config := &tls.Config{}
target := normalizeHost(in.host)

// configure default gRPC DialOptions
grpcOptions := []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(config)),
grpc.WithAuthority(target),
grpc.WithUserAgent(useragent.BuildUserAgentGRPC(in.sourceTag)),
}

// if the target includes an http:// address, don't include TLS
if strings.HasPrefix(target, "http://") {
config := &tls.Config{}
grpcOptions = append(grpcOptions, grpc.WithTransportCredentials(credentials.NewTLS(config)))
}

// if we have user-provided dialOpts, append them to the defaults here
dialOpts = append(grpcOptions, dialOpts...)

Expand Down Expand Up @@ -1094,18 +1099,21 @@ func sparseValToGrpc(sv *SparseValues) *data.SparseValues {
}

func normalizeHost(host string) string {
hasPort := strings.Contains(host, ":")

// remove https:// from the host
host = strings.TrimPrefix(host, "https://")
parsedHost, err := url.Parse(host)
if err != nil {
log.Default().Printf("Failed to parse host %s: %v", host, err)
return host
}

// if plaintext without a port, strip http:// as well
if !hasPort {
// if https:// or http:// without a port, strip the scheme
if parsedHost.Scheme == "https" {
host = strings.TrimPrefix(host, "https://")
} else if parsedHost.Scheme == "http" && parsedHost.Port() == "" {
host = strings.TrimPrefix(host, "http://")
}

// if a port was provided leave it, otherwise we append :443
if !hasPort {
if parsedHost.Port() == "" {
host = host + ":443"
}

Expand Down
2 changes: 1 addition & 1 deletion pinecone/index_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ func TestToUsageUnit(t *testing.T) {
}
}

func TestNormalizeHost(t *testing.T) {
func TestNormalizeHostUnit(t *testing.T) {
tests := []struct {
name string
host string
Expand Down

0 comments on commit 3c29ae5

Please sign in to comment.