Skip to content

Commit

Permalink
client: fix wrong usage of url.Parse (#7916)
Browse files Browse the repository at this point in the history
close #7900

Signed-off-by: Cabinfever_B <[email protected]>
  • Loading branch information
CabinfeverB authored Mar 14, 2024
1 parent 7d22c4f commit c2c5d84
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
23 changes: 12 additions & 11 deletions client/pd_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,20 +1131,21 @@ func addrsToURLs(addrs []string, tlsCfg *tls.Config) []string {
return urls
}

func modifyURLScheme(uStr string, tlsCfg *tls.Config) string {
u, err := url.Parse(uStr)
if err != nil {
if tlsCfg != nil {
return httpsSchemePrefix + uStr
func modifyURLScheme(url string, tlsCfg *tls.Config) string {
if tlsCfg == nil {
if strings.HasPrefix(url, httpsSchemePrefix) {
url = httpSchemePrefix + strings.TrimPrefix(url, httpsSchemePrefix)
} else if !strings.HasPrefix(url, httpSchemePrefix) {
url = httpSchemePrefix + url
}
return httpSchemePrefix + uStr
}
if tlsCfg != nil {
u.Scheme = httpsScheme
} else {
u.Scheme = httpScheme
if strings.HasPrefix(url, httpSchemePrefix) {
url = httpsSchemePrefix + strings.TrimPrefix(url, httpSchemePrefix)
} else if !strings.HasPrefix(url, httpsSchemePrefix) {
url = httpsSchemePrefix + url
}
}
return u.String()
return url
}

// pickMatchedURL picks the matched URL based on the TLS config.
Expand Down
32 changes: 31 additions & 1 deletion client/pd_service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,44 @@ func TestServiceClientScheme(t *testing.T) {
func TestSchemeFunction(t *testing.T) {
re := require.New(t)
tlsCfg := &tls.Config{}

endpoints1 := []string{
"http://tc-pd:2379",
"tc-pd:2379",
"https://tc-pd:2379",
}
endpoints2 := []string{
"127.0.0.1:2379",
"http://127.0.0.1:2379",
"https://127.0.0.1:2379",
}
urls := addrsToURLs(endpoints1, tlsCfg)
for _, u := range urls {
re.Equal("https://tc-pd:2379", u)
}
urls = addrsToURLs(endpoints2, tlsCfg)
for _, u := range urls {
re.Equal("https://127.0.0.1:2379", u)
}
urls = addrsToURLs(endpoints1, nil)
for _, u := range urls {
re.Equal("http://tc-pd:2379", u)
}
urls = addrsToURLs(endpoints2, nil)
for _, u := range urls {
re.Equal("http://127.0.0.1:2379", u)
}

re.Equal("https://127.0.0.1:2379", modifyURLScheme("https://127.0.0.1:2379", tlsCfg))
re.Equal("https://127.0.0.1:2379", modifyURLScheme("http://127.0.0.1:2379", tlsCfg))
re.Equal("https://127.0.0.1:2379", modifyURLScheme("127.0.0.1:2379", tlsCfg))
re.Equal("https://tc-pd:2379", modifyURLScheme("tc-pd:2379", tlsCfg))
re.Equal("http://127.0.0.1:2379", modifyURLScheme("https://127.0.0.1:2379", nil))
re.Equal("http://127.0.0.1:2379", modifyURLScheme("http://127.0.0.1:2379", nil))
re.Equal("http://127.0.0.1:2379", modifyURLScheme("127.0.0.1:2379", nil))
re.Equal("http://tc-pd:2379", modifyURLScheme("tc-pd:2379", nil))

urls := []string{
urls = []string{
"http://127.0.0.1:2379",
"https://127.0.0.1:2379",
}
Expand Down

0 comments on commit c2c5d84

Please sign in to comment.