diff --git a/README.md b/README.md index 0f9300e..181d8ed 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,18 @@ https://www.w3.org/TR/esi-lang/ go get -u github.com/darkweak/go-esi ``` -## Roadmap +## Available as middleware +- [ ] Caddy +- [ ] Træfik +- [ ] Roadrunner + +## TODO - [x] choose tag - [x] comment tag - [ ] escape tag - [x] include tag - [x] remove tag -- [ ] otherwise tag +- [x] otherwise tag - [ ] try tag -- [ ] vars tag +- [x] vars tag - [x] when tag \ No newline at end of file diff --git a/esi/choose.go b/esi/choose.go index 038a166..be72091 100644 --- a/esi/choose.go +++ b/esi/choose.go @@ -1,17 +1,20 @@ package esi import ( - "fmt" "net/http" "regexp" ) const ( - choose = "choose" + choose = "choose" + otherwise = "otherwise" + when = "when" ) var ( closeChoose = regexp.MustCompile("") + whenRg = regexp.MustCompile(`(?s)(.+?)`) + otherwiseRg = regexp.MustCompile(`(?s)(.+?)`) testAttribute = regexp.MustCompile(`test="(.+?)" ?>`) ) @@ -19,12 +22,6 @@ type chooseTag struct { *baseTag } -func matchTestAttribute(b []byte) bool { - fmt.Println(string(b)) - - return false -} - // Input (e.g. // // @@ -46,25 +43,19 @@ func (c *chooseTag) process(b []byte, req *http.Request) ([]byte, int) { c.length = found[1] // first when esi tag - tagIdx := esi.FindIndex(b[:found[1]]) - - if tagIdx == nil { - return []byte{}, len(b) + tagIdxs := whenRg.FindAllSubmatch(b, -1) + var res []byte + for _, v := range tagIdxs { + if validateTest(v[1], req) { + res = Parse(v[2], req) + break + } } - name := tagname.FindSubmatch(b[tagIdx[1]:found[1]]) - if name == nil || string(name[1]) != "when" { - return []byte{}, len(b) + tagIdx := otherwiseRg.FindSubmatch(b) + if tagIdx != nil { + res = Parse(tagIdx[1], req) } - testAttr := testAttribute.FindSubmatch(b[tagIdx[1]:found[1]]) - if testAttr == nil { - return nil, len(b) - } - - matchTestAttribute(testAttr[1]) - - // fmt.Println(string(name[1]), string(b[tagIdx[1]:found[1]])) - - return []byte{}, len(b) + return res, len(b) } diff --git a/esi/esi.go b/esi/esi.go index 4cb2157..98663f1 100644 --- a/esi/esi.go +++ b/esi/esi.go @@ -28,10 +28,11 @@ func findTagName(b []byte) tag { return &removeTag{ baseTag: newBaseTag(), } - case otherwise: case try: case vars: - case when: + return &varsTag{ + baseTag: newBaseTag(), + } default: return nil } diff --git a/esi/esi_test.go b/esi/esi_test.go index 6150318..0d57963 100644 --- a/esi/esi_test.go +++ b/esi/esi_test.go @@ -1,6 +1,9 @@ package esi import ( + "fmt" + "net/http" + "net/http/httptest" "os" "testing" ) @@ -17,15 +20,33 @@ func loadFromFixtures(name string) []byte { var ( commentMock = loadFromFixtures("comment") chooseMock = loadFromFixtures("choose") - // escapeMock = loadFromFixtures("escape") + escapeMock = loadFromFixtures("escape") includeMock = loadFromFixtures("include") removeMock = loadFromFixtures("remove") // tryMock = loadFromFixtures("try") - // varsMock = loadFromFixtures("vars") + varsMock = loadFromFixtures("vars") ) func Test_Parse_includeMock(t *testing.T) { - // fmt.Println(string(Parse(chooseMock, httptest.NewRequest(http.MethodGet, "/", nil)))) - // x := Parse(removeMock, httptest.NewRequest(http.MethodGet, "/", nil)) - // t.Error(string(x)) + fmt.Println(string(Parse(includeMock, httptest.NewRequest(http.MethodGet, "/", nil)))) +} + +func Test_Parse_commentMock(t *testing.T) { + fmt.Println(string(Parse(commentMock, httptest.NewRequest(http.MethodGet, "/", nil)))) +} + +func Test_Parse_chooseMock(t *testing.T) { + fmt.Println(string(Parse(chooseMock, httptest.NewRequest(http.MethodGet, "/", nil)))) +} + +func Test_Parse_escapeMock(t *testing.T) { + fmt.Println(string(Parse(escapeMock, httptest.NewRequest(http.MethodGet, "/", nil)))) +} + +func Test_Parse_removeMock(t *testing.T) { + fmt.Println(string(Parse(removeMock, httptest.NewRequest(http.MethodGet, "/", nil)))) +} + +func Test_Parse_varsMock(t *testing.T) { + fmt.Println(string(Parse(varsMock, httptest.NewRequest(http.MethodGet, "/", nil)))) } diff --git a/esi/otherwise.go b/esi/otherwise.go deleted file mode 100644 index 6a8d63c..0000000 --- a/esi/otherwise.go +++ /dev/null @@ -1 +0,0 @@ -package esi diff --git a/esi/tags.go b/esi/tags.go index 9f7c8b9..41b99eb 100644 --- a/esi/tags.go +++ b/esi/tags.go @@ -3,10 +3,7 @@ package esi import "regexp" const ( - otherwise = "otherwise" - try = "try" - vars = "vars" - when = "when" + try = "try" ) var ( diff --git a/esi/vars.go b/esi/vars.go index c5c4fda..f9e501b 100644 --- a/esi/vars.go +++ b/esi/vars.go @@ -13,11 +13,16 @@ const ( httpReferrer = "HTTP_REFERER" httpUserAgent = "HTTP_USER_AGENT" httpQueryString = "QUERY_STRING" + + vars = "vars" ) var ( interpretedVar = regexp.MustCompile(`\$\((.+?)(\{(.+)\}(.+)?)?\)`) defaultExtractor = regexp.MustCompile(`\|('|")(.+?)('|")`) + stringExtractor = regexp.MustCompile(`('|")(.+)('|")`) + + closeVars = regexp.MustCompile("") ) func parseVariables(b []byte, req *http.Request) string { @@ -50,7 +55,32 @@ func parseVariables(b []byte, req *http.Request) string { if len(defaultValues) > 2 { return string(defaultValues[2]) } + + return "" + } + } else { + strs := stringExtractor.FindSubmatch(b) + + if len(strs) > 2 { + return string(strs[2]) } } return string(b) } + +type varsTag struct { + *baseTag +} + +// Input (e.g. comment text="This is a comment." />) +func (c *varsTag) process(b []byte, req *http.Request) ([]byte, int) { + found := closeVars.FindIndex(b) + if found == nil { + return nil, len(b) + } + c.length = found[1] + + return interpretedVar.ReplaceAllFunc(b[5:found[0]], func(b []byte) []byte { + return []byte(parseVariables(b, req)) + }), len(b) +} diff --git a/fixtures/choose b/fixtures/choose index 9e410a3..bc0fc2b 100644 --- a/fixtures/choose +++ b/fixtures/choose @@ -1,11 +1,13 @@ - + - + - +
+ +
\ No newline at end of file