Skip to content

Commit c69a172

Browse files
authored
refactor(operator): use url.Parse instead of regex for sending rpc information to telemetry service (#1327)
2 parents 8c41436 + 207a83d commit c69a172

File tree

2 files changed

+13
-30
lines changed

2 files changed

+13
-30
lines changed

operator/pkg/utils.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package operator
22

33
import (
4-
"fmt"
54
"github.com/yetanotherco/aligned_layer/common"
65
"math/big"
7-
"regexp"
6+
"net/url"
87
)
98

109
func IsVerifierDisabled(disabledVerifiersBitmap *big.Int, verifierId common.ProvingSystemId) bool {
@@ -17,18 +16,11 @@ func IsVerifierDisabled(disabledVerifiersBitmap *big.Int, verifierId common.Prov
1716
}
1817

1918
func BaseUrlOnly(input string) (string, error) {
20-
// Define a regex pattern to match the URL format
21-
// The pattern captures the scheme, host, and path
22-
pattern := `^(?P<scheme>[^:]+)://(?P<host>[^/]+)(?P<path>/.*)?$`
23-
re := regexp.MustCompile(pattern)
24-
25-
matches := re.FindStringSubmatch(input)
26-
27-
if matches == nil {
28-
return "", fmt.Errorf("invalid URL: %s", input)
19+
// https://gobyexample.com/url-parsing
20+
u, err := url.Parse(input)
21+
if err != nil {
22+
return "", err
2923
}
3024

31-
host := matches[2]
32-
33-
return host, nil
25+
return u.Host, nil
3426
}

operator/pkg/utils_test.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ func TestBaseUrlOnlyHappyPath(t *testing.T) {
6464
{"https://a.b.c.d/1234", "a.b.c.d"},
6565
{"https://a.b.c.d/1234/5678", "a.b.c.d"},
6666
{"https://a.b.c.d.e/1234/", "a.b.c.d.e"},
67+
{"https://a.b.c.d?e=1234", "a.b.c.d"},
68+
{"https://a.b.c.d/rpc?e=1234", "a.b.c.d"},
69+
{"wss://a.b.c.d/1234", "a.b.c.d"},
70+
{"wss://a.b.c.d/1234/5678", "a.b.c.d"},
71+
{"wss://a.b.c.d.e/1234/", "a.b.c.d.e"},
72+
{"wss://a.b.c.d?e=1234", "a.b.c.d"},
73+
{"wss://a.b.c.d/rpc?e=1234", "a.b.c.d"},
6774
}
6875

6976
for _, pair := range urls {
@@ -81,19 +88,3 @@ func TestBaseUrlOnlyHappyPath(t *testing.T) {
8188
}
8289
}
8390
}
84-
85-
func TestBaseUrlOnlyFailureCases(t *testing.T) {
86-
87-
urls := [...]string{
88-
"localhost:8545/asdfoij2a7831has89%342jddav98j2748",
89-
"this-is-all-wrong",
90-
}
91-
92-
for _, url := range urls {
93-
baseUrl, err := BaseUrlOnly(url)
94-
95-
if err == nil {
96-
t.Errorf("An error was expected, but received %s", baseUrl)
97-
}
98-
}
99-
}

0 commit comments

Comments
 (0)