mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-24 19:49:56 +08:00
Attempt to fix windows command parsing + add more tests
This commit is contained in:
@ -2,6 +2,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"unicode"
|
||||
@ -46,7 +47,7 @@ func SplitCommandAndArgs(command string) (cmd string, args []string, err error)
|
||||
//
|
||||
// Loosely based off the rules here: http://stackoverflow.com/a/4094897/1048862
|
||||
// True parsing is much, much trickier.
|
||||
func parseWindowsCommand(cmd string) []string {
|
||||
func parseWindowsCommand2(cmd string) []string {
|
||||
var parts []string
|
||||
var part string
|
||||
var quoted bool
|
||||
@ -97,3 +98,60 @@ func parseWindowsCommand(cmd string) []string {
|
||||
|
||||
return parts
|
||||
}
|
||||
|
||||
func parseWindowsCommand(cmd string) []string {
|
||||
var parts []string
|
||||
var part string
|
||||
var inQuotes bool
|
||||
var wasBackslash bool
|
||||
|
||||
prefix := "DEBUG:"
|
||||
|
||||
fmt.Println(prefix, "Parsing cmd:", cmd)
|
||||
|
||||
for i, ch := range cmd {
|
||||
fmt.Println(" ", prefix, "Looking at char:", string(ch), "at index", string(i))
|
||||
|
||||
if ch == '\\' {
|
||||
wasBackslash = true
|
||||
// put it in the part - for now we don't know if it's escaping char or path separator
|
||||
part += string(ch)
|
||||
continue
|
||||
}
|
||||
|
||||
if ch == '"' {
|
||||
if wasBackslash {
|
||||
// remove the backslash from the part and add the escaped quote instead
|
||||
part = part[:len(part)-1]
|
||||
part += string(ch)
|
||||
wasBackslash = false
|
||||
continue
|
||||
} else {
|
||||
// normal escaping quotes
|
||||
fmt.Println(" ", prefix, "and it's a quote")
|
||||
inQuotes = !inQuotes
|
||||
continue
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if unicode.IsSpace(ch) && !inQuotes && len(part) > 0 {
|
||||
fmt.Println(" ", prefix, "and it's a space outside quotes")
|
||||
parts = append(parts, part)
|
||||
part = ""
|
||||
wasBackslash = false
|
||||
continue
|
||||
}
|
||||
|
||||
wasBackslash = false
|
||||
part += string(ch)
|
||||
}
|
||||
|
||||
if len(part) > 0 {
|
||||
parts = append(parts, part)
|
||||
part = ""
|
||||
}
|
||||
|
||||
fmt.Println(prefix, strings.Join(parts, ","))
|
||||
return parts
|
||||
}
|
||||
|
Reference in New Issue
Block a user