mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
fix rule parser
This commit is contained in:
parent
28b5d44e11
commit
f2df756c17
2 changed files with 132 additions and 19 deletions
|
@ -1,7 +1,8 @@
|
||||||
package rules
|
package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"bytes"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
)
|
)
|
||||||
|
@ -22,15 +23,30 @@ var escapedChars = map[rune]rune{
|
||||||
// error 403 "Forbidden 'foo' 'bar'"
|
// error 403 "Forbidden 'foo' 'bar'"
|
||||||
// error 403 Forbidden\ \"foo\"\ \"bar\".
|
// error 403 Forbidden\ \"foo\"\ \"bar\".
|
||||||
func parse(v string) (subject string, args []string, err E.Error) {
|
func parse(v string) (subject string, args []string, err E.Error) {
|
||||||
v = strings.TrimSpace(v)
|
buf := bytes.NewBuffer(make([]byte, 0, len(v)))
|
||||||
var buf strings.Builder
|
|
||||||
escaped := false
|
escaped := false
|
||||||
quotes := make([]rune, 0, 4)
|
quote := rune(0)
|
||||||
flush := func() {
|
flush := func(quoted bool) {
|
||||||
if subject == "" {
|
part := buf.String()
|
||||||
subject = buf.String()
|
if !quoted {
|
||||||
|
beg := 0
|
||||||
|
for i, r := range part {
|
||||||
|
if unicode.IsSpace(r) {
|
||||||
|
beg = i + 1
|
||||||
} else {
|
} else {
|
||||||
args = append(args, buf.String())
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if beg == len(part) { // all spaces
|
||||||
|
return
|
||||||
|
}
|
||||||
|
part = part[beg:] // trim leading spaces
|
||||||
|
}
|
||||||
|
if subject == "" {
|
||||||
|
subject = part
|
||||||
|
} else {
|
||||||
|
args = append(args, part)
|
||||||
}
|
}
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
}
|
}
|
||||||
|
@ -51,29 +67,30 @@ func parse(v string) (subject string, args []string, err E.Error) {
|
||||||
continue
|
continue
|
||||||
case '"', '\'':
|
case '"', '\'':
|
||||||
switch {
|
switch {
|
||||||
case len(quotes) > 0 && quotes[len(quotes)-1] == r:
|
case quote == 0:
|
||||||
quotes = quotes[:len(quotes)-1]
|
quote = r
|
||||||
if len(quotes) == 0 {
|
flush(false)
|
||||||
flush()
|
case r == quote:
|
||||||
} else {
|
quote = 0
|
||||||
buf.WriteRune(r)
|
flush(true)
|
||||||
}
|
|
||||||
case len(quotes) == 0:
|
|
||||||
quotes = append(quotes, r)
|
|
||||||
default:
|
default:
|
||||||
buf.WriteRune(r)
|
buf.WriteRune(r)
|
||||||
}
|
}
|
||||||
case ' ':
|
case ' ':
|
||||||
flush()
|
if quote == 0 {
|
||||||
|
flush(false)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
default:
|
default:
|
||||||
buf.WriteRune(r)
|
buf.WriteRune(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(quotes) > 0 {
|
if quote != 0 {
|
||||||
err = ErrUnterminatedQuotes
|
err = ErrUnterminatedQuotes
|
||||||
} else {
|
} else {
|
||||||
flush()
|
flush(false)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
96
internal/route/rules/parser_test.go
Normal file
96
internal/route/rules/parser_test.go
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
subject string
|
||||||
|
args []string
|
||||||
|
wantErr E.Error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic",
|
||||||
|
input: "rewrite / /foo/bar",
|
||||||
|
subject: "rewrite",
|
||||||
|
args: []string{"/", "/foo/bar"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with quotes",
|
||||||
|
input: `error 403 "Forbidden 'foo' 'bar'."`,
|
||||||
|
subject: "error",
|
||||||
|
args: []string{"403", "Forbidden 'foo' 'bar'."},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with quotes 2",
|
||||||
|
input: `basic_auth "username" "password"`,
|
||||||
|
subject: "basic_auth",
|
||||||
|
args: []string{"username", "password"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with escaped",
|
||||||
|
input: `foo bar\ baz bar\r\n\tbaz bar\'\"baz`,
|
||||||
|
subject: "foo",
|
||||||
|
args: []string{"bar baz", "bar\r\n\tbaz", `bar'"baz`},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty string",
|
||||||
|
input: `foo '' ""`,
|
||||||
|
subject: "foo",
|
||||||
|
args: []string{"", ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid_escape",
|
||||||
|
input: `foo \bar`,
|
||||||
|
wantErr: ErrUnsupportedEscapeChar,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "chaos",
|
||||||
|
input: `error 403 "Forbidden "foo" "bar""`,
|
||||||
|
subject: "error",
|
||||||
|
args: []string{"403", "Forbidden ", "foo", " ", "bar", ""},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "chaos2",
|
||||||
|
input: `foo "'bar' 'baz'" abc\ 'foo "bar"'.`,
|
||||||
|
subject: "foo",
|
||||||
|
args: []string{"'bar' 'baz'", "abc ", `foo "bar"`, "."},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
subject, args, err := parse(tt.input)
|
||||||
|
if tt.wantErr != nil {
|
||||||
|
ExpectError(t, tt.wantErr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// t.Log(subject, args, err)
|
||||||
|
ExpectNoError(t, err)
|
||||||
|
ExpectEqual(t, subject, tt.subject)
|
||||||
|
ExpectEqual(t, len(args), len(tt.args))
|
||||||
|
for i, arg := range args {
|
||||||
|
ExpectEqual(t, arg, tt.args[i])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
t.Run("unterminated quotes", func(t *testing.T) {
|
||||||
|
tests := []string{
|
||||||
|
`error 403 "Forbidden 'foo' 'bar'`,
|
||||||
|
`error 403 "Forbidden 'foo 'bar'`,
|
||||||
|
`error 403 "Forbidden foo "bar'"`,
|
||||||
|
}
|
||||||
|
for i, test := range tests {
|
||||||
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||||
|
_, _, err := parse(test)
|
||||||
|
ExpectError(t, ErrUnterminatedQuotes, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue