Skip to content

Commit 921b418

Browse files
1oca1h0stMecozea
authored andcommitted
refactor: simplify URL parameter normalization in commandLine
优化了 normalizeCommandLine 函数,通过使用 Encode() 方法重建查询参数,自动处理参数排序和编码,简化了代码逻辑。此更改确保了在比较 URL 参数时的准确性和一致性,进一步提升了隧道编辑功能的稳定性。
1 parent e6f5364 commit 921b418

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

internal/api/tunnel.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,17 @@ func normalizeCommandLine(commandLine string) (string, error) {
5959
return "", fmt.Errorf("解析查询参数失败: %w", err)
6060
}
6161

62-
// 获取所有的参数键并排序
63-
keys := make([]string, 0, len(values))
64-
for k := range values {
65-
keys = append(keys, k)
62+
// 使用 Encode() 方法重建查询参数
63+
// Encode() 会自动:
64+
// 1. 按字母顺序排序键
65+
// 2. 正确处理 URL 编码
66+
// 3. 正确处理裸键和多值参数
67+
encoded := values.Encode()
68+
if encoded == "" {
69+
return base, nil
6670
}
67-
sort.Strings(keys)
6871

69-
// 按排序后的顺序重建查询参数
70-
params := make([]string, 0, len(keys))
71-
for _, k := range keys {
72-
for _, v := range values[k] {
73-
params = append(params, fmt.Sprintf("%s=%s", k, v))
74-
}
75-
}
76-
77-
// 重建完整的 commandLine
78-
if len(params) > 0 {
79-
return base + "?" + strings.Join(params, "&"), nil
80-
}
81-
return base, nil
72+
return base + "?" + encoded, nil
8273
}
8374

8475
// setupTunnelRoutes 设置隧道相关路由

0 commit comments

Comments
 (0)