Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
0d004ccb
Commit
0d004ccb
authored
Oct 23, 2015
by
makpoc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Attempt to fix windows command parsing + add more tests
parent
fec491fb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
9 deletions
+96
-9
middleware/commands.go
middleware/commands.go
+59
-1
middleware/commands_test.go
middleware/commands_test.go
+37
-8
No files found.
middleware/commands.go
View file @
0d004ccb
...
@@ -2,6 +2,7 @@ package middleware
...
@@ -2,6 +2,7 @@ package middleware
import
(
import
(
"errors"
"errors"
"fmt"
"runtime"
"runtime"
"strings"
"strings"
"unicode"
"unicode"
...
@@ -46,7 +47,7 @@ func SplitCommandAndArgs(command string) (cmd string, args []string, err error)
...
@@ -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
// Loosely based off the rules here: http://stackoverflow.com/a/4094897/1048862
// True parsing is much, much trickier.
// True parsing is much, much trickier.
func
parseWindowsCommand
(
cmd
string
)
[]
string
{
func
parseWindowsCommand
2
(
cmd
string
)
[]
string
{
var
parts
[]
string
var
parts
[]
string
var
part
string
var
part
string
var
quoted
bool
var
quoted
bool
...
@@ -97,3 +98,60 @@ func parseWindowsCommand(cmd string) []string {
...
@@ -97,3 +98,60 @@ func parseWindowsCommand(cmd string) []string {
return
parts
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
}
middleware/commands_test.go
View file @
0d004ccb
...
@@ -7,7 +7,7 @@ import (
...
@@ -7,7 +7,7 @@ import (
)
)
func
TestParseWindowsCommand
(
t
*
testing
.
T
)
{
func
TestParseWindowsCommand
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
[]
struct
{
tests
:=
[]
struct
{
input
string
input
string
expected
[]
string
expected
[]
string
}{
}{
...
@@ -51,25 +51,54 @@ func TestParseWindowsCommand(t *testing.T) {
...
@@ -51,25 +51,54 @@ func TestParseWindowsCommand(t *testing.T) {
input
:
`mkdir "C:\ space"`
,
input
:
`mkdir "C:\ space"`
,
expected
:
[]
string
{
`mkdir`
,
`C:\ space`
},
expected
:
[]
string
{
`mkdir`
,
`C:\ space`
},
},
},
{
// 10
// 10
input
:
`\\"`
,
{
expected
:
[]
string
{
`\`
},
input
:
`mkdir \\?\C:\Users`
,
expected
:
[]
string
{
`mkdir`
,
`\\?\C:\Users`
},
},
},
{
// 11
// 11
input
:
`"\\\""`
,
{
expected
:
[]
string
{
`\"`
},
input
:
`mkdir "\\?\C:\Program Files"`
,
expected
:
[]
string
{
`mkdir`
,
`\\?\C:\Program Files`
},
},
},
}
{
}
var
nTests
int
for
i
,
test
:=
range
tests
{
fmt
.
Printf
(
"====== Test %d ======
\n
"
,
i
)
actual
:=
parseWindowsCommand
(
test
.
input
)
actual
:=
parseWindowsCommand
(
test
.
input
)
if
len
(
actual
)
!=
len
(
test
.
expected
)
{
if
len
(
actual
)
!=
len
(
test
.
expected
)
{
fmt
.
Printf
(
"Test %d: Expected %d parts, got %d: %#v"
,
i
,
len
(
test
.
expected
),
len
(
actual
),
actual
)
fmt
.
Println
()
t
.
Errorf
(
"Test %d: Expected %d parts, got %d: %#v"
,
i
,
len
(
test
.
expected
),
len
(
actual
),
actual
)
t
.
Errorf
(
"Test %d: Expected %d parts, got %d: %#v"
,
i
,
len
(
test
.
expected
),
len
(
actual
),
actual
)
continue
continue
}
}
for
j
:=
0
;
j
<
len
(
actual
);
j
++
{
for
j
:=
0
;
j
<
len
(
actual
);
j
++
{
if
expectedPart
,
actualPart
:=
test
.
expected
[
j
],
actual
[
j
];
expectedPart
!=
actualPart
{
if
expectedPart
,
actualPart
:=
test
.
expected
[
j
],
actual
[
j
];
expectedPart
!=
actualPart
{
fmt
.
Printf
(
"Test %d: Expected: %v Actual: %v (index %d)"
,
i
,
expectedPart
,
actualPart
,
j
)
fmt
.
Println
()
t
.
Errorf
(
"Test %d: Expected: %v Actual: %v (index %d)"
,
i
,
expectedPart
,
actualPart
,
j
)
t
.
Errorf
(
"Test %d: Expected: %v Actual: %v (index %d)"
,
i
,
expectedPart
,
actualPart
,
j
)
}
}
}
}
nTests
+=
1
}
for
_
,
test
:=
range
tests
{
fmt
.
Printf
(
"====== Test %d ======
\n
"
,
nTests
)
actual
:=
parseWindowsCommand2
(
test
.
input
)
if
len
(
actual
)
!=
len
(
test
.
expected
)
{
fmt
.
Printf
(
"Test %d: Expected %d parts, got %d: %#v"
,
nTests
,
len
(
test
.
expected
),
len
(
actual
),
actual
)
fmt
.
Println
()
t
.
Errorf
(
"Test %d: Expected %d parts, got %d: %#v"
,
nTests
,
len
(
test
.
expected
),
len
(
actual
),
actual
)
continue
}
for
j
:=
0
;
j
<
len
(
actual
);
j
++
{
if
expectedPart
,
actualPart
:=
test
.
expected
[
j
],
actual
[
j
];
expectedPart
!=
actualPart
{
fmt
.
Printf
(
"Test %d: Expected: %v Actual: %v (index %d)"
,
nTests
,
expectedPart
,
actualPart
,
j
)
fmt
.
Println
()
t
.
Errorf
(
"Test %d: Expected: %v Actual: %v (index %d)"
,
nTests
,
expectedPart
,
actualPart
,
j
)
}
}
nTests
+=
1
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment