Skip to content

Commit bba3e1e

Browse files
committed
fix(analyze): Clean consecutive slashes
1 parent c1a21af commit bba3e1e

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

pkg/analyze/util.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,19 @@ func AdjacentPrefix(p netip.Prefix) netip.Prefix {
9494

9595
func TruncateURLPath(input string) string {
9696
parts := strings.SplitN(input, "?", 2)
97-
path := parts[0]
97+
path := filepath.Clean(parts[0])
98+
if strings.HasSuffix(parts[0], "/") {
99+
// filepath.Clean removes trailing slash
100+
// Add it back to preserve directory notation
101+
path += "/"
102+
}
98103
args := ""
99104
if len(parts) == 2 {
100105
args = "?..."
101106
}
102107
count := strings.Count(path, "/")
103108
if count <= 2 {
104-
return input + args
109+
return path + args
105110
}
106111
parts = strings.Split(path, "/")
107112
if parts[len(parts)-1] == "" {

pkg/analyze/util_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestAdjacentPrefix(t *testing.T) {
2323
func TestTruncateURLPath(t *testing.T) {
2424
testCases := [][2]string{
2525
{"/example/a/b/c/d/e/file.ext", "/example/.../file.ext"},
26+
{"///example//merge/slashes///file.ext", "/example/.../file.ext"},
2627
{"/example/a/b/c/d/e/dir/", "/example/.../dir/"},
2728
{"/short/", "/short/"},
2829
{"/short/file.ext", "/short/file.ext"},
@@ -43,12 +44,14 @@ func TestTruncateURLPathLen(t *testing.T) {
4344
testCases := []testCase{
4445
{"/example/a/b/c/d/e/file.ext", 30, "/example/.../file.ext"},
4546
{"/example/a/b/c/d/e/dir/", 30, "/example/.../dir/"},
47+
{"///example//merge/slashes///dir/", 30, "/example/.../dir/"},
4648
{"/with/args/?a=1&b=2", 30, "/with/args/?..."},
4749
{"/with/args/?a=1&b=2", 13, "/with/args/?"},
4850
{"/with/args/?a=1&b=2", 12, "/with/args/?"},
4951
{"/with/args/?a=1&b=2", 11, "/with/args/"},
5052
{"/with/args/?a=1&b=2", 8, "/with/*/"},
5153
{"/with/args/?a=1&b=2", 4, "/wit"},
54+
{"///with//args/?a=1&b=2", 4, "/wit"},
5255

5356
{"/example/a/b/c/d/e/file.with.long.name.ext", 30, "/example/.../file.with.l...ext"},
5457
{"/example/file.with.very.long.name.ext", 30, "/example/file.with.very....ext"},

0 commit comments

Comments
 (0)