From 3f9167309b8e36ec02cd35dbdd59a141bf4eb398 Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Fri, 12 Jan 2024 17:23:57 -0300 Subject: [PATCH] fix: port parsing when using HTTP/HTTPS with omitted port (#3524) * 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 --- server/tracedb/connection/port_linting.go | 33 +++++++++++-------- .../tracedb/connection/port_linting_test.go | 24 ++++++++++++++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/server/tracedb/connection/port_linting.go b/server/tracedb/connection/port_linting.go index a7c2fc632b..d11e4616e2 100644 --- a/server/tracedb/connection/port_linting.go +++ b/server/tracedb/connection/port_linting.go @@ -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] } diff --git a/server/tracedb/connection/port_linting_test.go b/server/tracedb/connection/port_linting_test.go index f04eb0de92..439edcfe0d 100644 --- a/server/tracedb/connection/port_linting_test.go +++ b/server/tracedb/connection/port_linting_test.go @@ -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 {