Skip to content

Commit

Permalink
fix: port parsing when using HTTP/HTTPS with omitted port (#3524)
Browse files Browse the repository at this point in the history
* fix: port parsing when using HTTP/HTTPS with omitted port

* clean up

* use real urls in test

* add examples of overriding the default ports for http and https
  • Loading branch information
mathnogueira committed Jan 12, 2024
1 parent 8db0ab7 commit 3f91673
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
33 changes: 20 additions & 13 deletions server/tracedb/connection/port_linting.go
Expand Up @@ -74,29 +74,36 @@ func formatAvailablePortsMessage(ports []string) string {
return fmt.Sprintf("%s, or %s", portsSeparatedByComma, lastPort)
}

var extractPortRegex = regexp.MustCompile("([0-9]+).*")
var extractPortRegex = regexp.MustCompile(":([0-9]+).*")

func parsePort(url string) string {
port := extractPort(url)
if port != "" {
return port
}

if strings.HasPrefix(url, "https://") {
return "443"
}

if strings.HasPrefix(url, "http://") {
return "80"
}

return ""
}

func extractPort(url string) string {
index := strings.LastIndex(url, ":")
if index < 0 {
return ""
}

substring := url[index+1:]
substring := url[index:]
regexGroups := extractPortRegex.FindStringSubmatch(substring)
if len(regexGroups) < 2 {
return ""
}

port := regexGroups[1]

if port == "1" {
if strings.Contains(url, "http") {
return "80"
} else {
return "443"
}
}

return port
return regexGroups[1]
}
24 changes: 24 additions & 0 deletions server/tracedb/connection/port_linting_test.go
Expand Up @@ -46,6 +46,30 @@ func TestPortLinter(t *testing.T) {
ExpectedPorts: []string{"9200", "9250", "9300"},
ExpectedStatus: model.StatusWarning,
},
{
Name: "shouldGetPort80InHTTP",
Endpoints: []string{"http://tempo-us-central1.grafana.net"},
ExpectedPorts: []string{"80"},
ExpectedStatus: model.StatusPassed,
},
{
Name: "shouldGetPort443InHTTPS",
Endpoints: []string{"https://tempo-us-central1.grafana.net"},
ExpectedPorts: []string{"443"},
ExpectedStatus: model.StatusPassed,
},
{
Name: "shouldGetSpecifiedInHTTP",
Endpoints: []string{"http://tempo-us-central1.grafana.net:8081"},
ExpectedPorts: []string{"8081"},
ExpectedStatus: model.StatusPassed,
},
{
Name: "shouldGetPort443InHTTPS",
Endpoints: []string{"https://tempo-us-central1.grafana.net:8082"},
ExpectedPorts: []string{"8082"},
ExpectedStatus: model.StatusPassed,
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit 3f91673

Please sign in to comment.