Skip to content

Commit

Permalink
Support otherwise, vars tags
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Sep 8, 2022
1 parent c5ad658 commit 37af074
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 43 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 16 additions & 25 deletions esi/choose.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package esi

import (
"fmt"
"net/http"
"regexp"
)

const (
choose = "choose"
choose = "choose"
otherwise = "otherwise"
when = "when"
)

var (
closeChoose = regexp.MustCompile("</esi:choose>")
whenRg = regexp.MustCompile(`(?s)<esi:when test="(.+?)">(.+?)</esi:when>`)
otherwiseRg = regexp.MustCompile(`(?s)<esi:otherwise>(.+?)</esi:otherwise>`)
testAttribute = regexp.MustCompile(`test="(.+?)" ?>`)
)

type chooseTag struct {
*baseTag
}

func matchTestAttribute(b []byte) bool {
fmt.Println(string(b))

return false
}

// Input (e.g.
// <esi:choose>
// <esi:when test="$(HTTP_COOKIE{group})=='Advanced'">
Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions esi/esi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 26 additions & 5 deletions esi/esi_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package esi

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)
Expand All @@ -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))))
}
1 change: 0 additions & 1 deletion esi/otherwise.go

This file was deleted.

5 changes: 1 addition & 4 deletions esi/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package esi
import "regexp"

const (
otherwise = "otherwise"
try = "try"
vars = "vars"
when = "when"
try = "try"
)

var (
Expand Down
30 changes: 30 additions & 0 deletions esi/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("</esi:vars>")
)

func parseVariables(b []byte, req *http.Request) string {
Expand Down Expand Up @@ -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)
}
8 changes: 5 additions & 3 deletions fixtures/choose
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<esi:choose>
<esi:when test="$(HTTP_COOKIE{group})=='Advanced'">
<esi:include src="http://www.example.com/advanced.html"/>
<esi:include src="http://domain.com/chained-esi-include-1"/>
</esi:when>
<esi:when test="$(HTTP_COOKIE{group})=='Basic User'">
<esi:include src="http://www.example.com/basic.html"/>
<esi:include src="https://google.com"/>
</esi:when>
<esi:otherwise>
<esi:include src="http://www.example.com/new_user.html"/>
<div>
<esi:include src="http://domain.com/chained-esi-include-1"/>
</div>
</esi:otherwise>
</esi:choose>

0 comments on commit 37af074

Please sign in to comment.