Skip to content

Commit

Permalink
Supports the when and choose tag
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Sep 6, 2022
1 parent 642b9a7 commit c5ad658
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 101 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -12,12 +12,12 @@ go get -u github.com/darkweak/go-esi
```

## Roadmap
- [ ] choose tag
- [x] choose tag
- [x] comment tag
- [ ] escape tag
- [x] include tag
- [x] remove tag
- [ ] otherwise tag
- [ ] try tag
- [ ] vars tag
- [ ] when tag
- [x] when tag
70 changes: 70 additions & 0 deletions esi/choose.go
@@ -0,0 +1,70 @@
package esi

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

const (
choose = "choose"
)

var (
closeChoose = regexp.MustCompile("</esi:choose>")
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'">
// <esi:include src="http://www.example.com/advanced.html"/>
// </esi:when>
// <esi:when test="$(HTTP_COOKIE{group})=='Basic User'">
// <esi:include src="http://www.example.com/basic.html"/>
// </esi:when>
// <esi:otherwise>
// <esi:include src="http://www.example.com/new_user.html"/>
// </esi:otherwise>
// </esi:choose>
//)
func (c *chooseTag) process(b []byte, req *http.Request) ([]byte, int) {
found := closeChoose.FindIndex(b)
if found == nil {
return nil, len(b)
}
c.length = found[1]

// first when esi tag
tagIdx := esi.FindIndex(b[:found[1]])

if tagIdx == nil {
return []byte{}, len(b)
}

name := tagname.FindSubmatch(b[tagIdx[1]:found[1]])
if name == nil || string(name[1]) != "when" {
return []byte{}, len(b)
}

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)
}
25 changes: 25 additions & 0 deletions esi/comment.go
@@ -0,0 +1,25 @@
package esi

import (
"net/http"
"regexp"
)

const comment = "comment"

var closeComment = regexp.MustCompile("/>")

type commentTag struct {
*baseTag
}

// Input (e.g. comment text="This is a comment." />)
func (c *commentTag) process(b []byte, req *http.Request) ([]byte, int) {
found := closeComment.FindIndex(b)
if found == nil {
return nil, len(b)
}
c.length = found[1]

return []byte{}, len(b)
}
7 changes: 7 additions & 0 deletions esi/escape.go
@@ -0,0 +1,7 @@
package esi

import "regexp"

const escape = "escape"

var closeEscape = regexp.MustCompile("-->")
4 changes: 4 additions & 0 deletions esi/esi.go
Expand Up @@ -16,6 +16,9 @@ func findTagName(b []byte) tag {
baseTag: newBaseTag(),
}
case choose:
return &chooseTag{
baseTag: newBaseTag(),
}
case escape:
case include:
return &includeTag{
Expand All @@ -30,6 +33,7 @@ func findTagName(b []byte) tag {
case vars:
case when:
default:
return nil
}

return nil
Expand Down
7 changes: 2 additions & 5 deletions esi/esi_test.go
@@ -1,7 +1,6 @@
package esi

import (
"fmt"
"os"
"testing"
)
Expand All @@ -17,7 +16,7 @@ func loadFromFixtures(name string) []byte {

var (
commentMock = loadFromFixtures("comment")
// chooseMock = loadFromFixtures("choose")
chooseMock = loadFromFixtures("choose")
// escapeMock = loadFromFixtures("escape")
includeMock = loadFromFixtures("include")
removeMock = loadFromFixtures("remove")
Expand All @@ -26,9 +25,7 @@ var (
)

func Test_Parse_includeMock(t *testing.T) {
fmt.Println(string(commentMock))
fmt.Println(string(includeMock))
fmt.Println(string(removeMock))
// fmt.Println(string(Parse(chooseMock, httptest.NewRequest(http.MethodGet, "/", nil))))
// x := Parse(removeMock, httptest.NewRequest(http.MethodGet, "/", nil))
// t.Error(string(x))
}
68 changes: 68 additions & 0 deletions esi/include.go
@@ -0,0 +1,68 @@
package esi

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

const include = "include"

var (
closeInclude = regexp.MustCompile("/>")
srcAttribute = regexp.MustCompile(`src="?(.+?)"?( |/>)`)
altAttribute = regexp.MustCompile(`alt="?(.+?)"?( |/>)`)
)

type includeTag struct {
*baseTag
src string
alt string
}

func (i *includeTag) loadAttributes(b []byte) error {
src := srcAttribute.FindSubmatch(b)
if src == nil {
return errNotFound
}
i.src = string(src[1])

alt := altAttribute.FindSubmatch(b)
if alt != nil {
i.alt = string(alt[1])
}

return nil
}

// Input (e.g. include src="https://domain.com/esi-include" alt="https://domain.com/alt-esi-include" />)
// With or without the alt
// With or without a space separator before the closing
// With or without the quotes around the src/alt value
func (i *includeTag) process(b []byte, req *http.Request) ([]byte, int) {
closeIdx := closeInclude.FindIndex(b)

if closeIdx == nil {
return nil, len(b)
}

i.length = closeIdx[1]
if e := i.loadAttributes(b[8:i.length]); e != nil {
return nil, len(b)
}

rq, _ := http.NewRequest(http.MethodGet, i.src, nil)
response, err := clientPool.Get().(*http.Client).Do(rq)
if err != nil || response.StatusCode >= 400 {
rq, _ = http.NewRequest(http.MethodGet, i.src, nil)
response, err = clientPool.Get().(*http.Client).Do(rq)
if err != nil || response.StatusCode >= 400 {
return nil, len(b)
}
}

x, _ := io.ReadAll(response.Body)
b = Parse(x, req)

return b, i.length
}
1 change: 1 addition & 0 deletions esi/otherwise.go
@@ -0,0 +1 @@
package esi
24 changes: 24 additions & 0 deletions esi/remove.go
@@ -0,0 +1,24 @@
package esi

import (
"net/http"
"regexp"
)

const remove = "remove"

var closeRemove = regexp.MustCompile("</esi:remove>")

type removeTag struct {
*baseTag
}

func (r *removeTag) process(b []byte, req *http.Request) ([]byte, int) {
closeIdx := closeRemove.FindIndex(b)
if closeIdx == nil {
return []byte{}, len(b)
}
r.length = closeIdx[1]

return []byte{}, r.length
}
12 changes: 0 additions & 12 deletions esi/tags.go
Expand Up @@ -3,11 +3,6 @@ package esi
import "regexp"

const (
choose = "choose"
comment = "comment"
escape = "escape"
include = "include"
remove = "remove"
otherwise = "otherwise"
try = "try"
vars = "vars"
Expand All @@ -18,15 +13,8 @@ var (
esi = regexp.MustCompile("<esi:")
tagname = regexp.MustCompile("^([a-z]+?)( |>)")

// closeChoose = regexp.MustCompile("</esi:choose>")
closeComment = regexp.MustCompile("/>")
closeInclude = regexp.MustCompile("/>")
closeRemove = regexp.MustCompile("</esi:remove>")
// closeOtherwise = regexp.MustCompile("</esi:otherwise>")
// closeTry = regexp.MustCompile("</esi:try>")
// closeVars = regexp.MustCompile("</esi:vars>")
// closeWhen = regexp.MustCompile("</esi:when>")

srcAttribute = regexp.MustCompile(`src="?(.+?)"?( |/>)`)
altAttribute = regexp.MustCompile(`alt="?(.+?)"?( |/>)`)
)
1 change: 1 addition & 0 deletions esi/try.go
@@ -0,0 +1 @@
package esi
80 changes: 0 additions & 80 deletions esi/type.go
@@ -1,7 +1,6 @@
package esi

import (
"io"
"net/http"
"sync"
)
Expand All @@ -20,17 +19,6 @@ type (
baseTag struct {
length int
}
includeTag struct {
*baseTag
src string
alt string
}
removeTag struct {
*baseTag
}
commentTag struct {
*baseTag
}
)

func newBaseTag() *baseTag {
Expand All @@ -40,71 +28,3 @@ func newBaseTag() *baseTag {
func (b *baseTag) process(content []byte, _ *http.Request) ([]byte, int) {
return []byte{}, len(content)
}

func (i *includeTag) loadAttributes(b []byte) error {
src := srcAttribute.FindSubmatch(b)
if src == nil {
return errNotFound
}
i.src = string(src[1])

alt := altAttribute.FindSubmatch(b)
if alt != nil {
i.alt = string(alt[1])
}

return nil
}

// Input (e.g. include src="https://domain.com/esi-include" alt="https://domain.com/alt-esi-include" />)
// With or without the alt
// With or without a space separator before the closing
// With or without the quotes around the src/alt value
func (i *includeTag) process(b []byte, req *http.Request) ([]byte, int) {
closeIdx := closeInclude.FindIndex(b)

if closeIdx == nil {
return nil, len(b)
}

i.length = closeIdx[1]
if e := i.loadAttributes(b[8:i.length]); e != nil {
return nil, len(b)
}

rq, _ := http.NewRequest(http.MethodGet, i.src, nil)
response, err := clientPool.Get().(*http.Client).Do(rq)
if err != nil || response.StatusCode >= 400 {
rq, _ = http.NewRequest(http.MethodGet, i.src, nil)
response, err = clientPool.Get().(*http.Client).Do(rq)
if err != nil || response.StatusCode >= 400 {
return nil, len(b)
}
}

x, _ := io.ReadAll(response.Body)
b = Parse(x, req)

return b, i.length
}

// Input (e.g. comment text="This is a comment." />)
func (c *commentTag) process(b []byte, req *http.Request) ([]byte, int) {
found := closeComment.FindIndex(b)
if found == nil {
return nil, len(b)
}
c.length = found[1]

return []byte{}, len(b)
}

func (r *removeTag) process(b []byte, req *http.Request) ([]byte, int) {
closeIdx := closeRemove.FindIndex(b)
if closeIdx == nil {
return []byte{}, len(b)
}
r.length = closeIdx[1]

return []byte{}, r.length
}

0 comments on commit c5ad658

Please sign in to comment.