-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.go
74 lines (67 loc) · 2.04 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"net/url"
"strings"
)
// EncodeArgs encodes the arguments in the command using URL encoding
func EncodeArgs(command string) string {
parts := strings.SplitN(command, " ", 2)
if len(parts) < 2 {
return command
}
cmd := parts[0]
args := parts[1]
encodedArgs, _ := url.QueryUnescape(args)
return cmd + " " + encodedArgs
}
func DecodeArgs(command string) []string {
parts := strings.Split(command, " ")
var decodedParts []string
inQuote := false
var quotedArg strings.Builder
quoteChar := ""
for _, part := range parts {
if !inQuote && (strings.HasPrefix(part, "\"") || strings.HasPrefix(part, "'")) {
// Detect starting quote.
inQuote = true
quoteChar = string(part[0])
quotedPart := strings.TrimPrefix(part, quoteChar)
if strings.HasSuffix(quotedPart, quoteChar) {
// Single-word quoted argument.
inQuote = false
decodedPart, _ := url.QueryUnescape(strings.TrimSuffix(quotedPart, quoteChar))
decodedParts = append(decodedParts, decodedPart)
quoteChar = ""
} else {
quotedArg.WriteString(quotedPart + " ")
}
} else if inQuote && strings.HasSuffix(part, quoteChar) {
// Detect closing quote.
inQuote = false
quotedPart := strings.TrimSuffix(part, quoteChar)
quotedArg.WriteString(quotedPart)
decodedArg, _ := url.QueryUnescape(quotedArg.String())
decodedParts = append(decodedParts, decodedArg)
quotedArg.Reset()
quoteChar = ""
} else if inQuote {
// Inside a quoted argument.
quotedArg.WriteString(part + " ")
} else {
// Unquoted argument.
decodedArg, _ := url.QueryUnescape(part)
decodedParts = append(decodedParts, decodedArg)
}
}
// Handle case where the last part is still in quotes
if inQuote {
// Remove the trailing space added in the loop.
quotedArgStr := quotedArg.String()
if len(quotedArgStr) > 0 && quotedArgStr[len(quotedArgStr)-1] == ' ' {
quotedArgStr = quotedArgStr[:len(quotedArgStr)-1]
}
decodedArg, _ := url.QueryUnescape(quotedArgStr)
decodedParts = append(decodedParts, decodedArg)
}
return decodedParts
}