Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Sep 5, 2022
0 parents commit 642b9a7
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 darkweak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions README.md
@@ -0,0 +1,23 @@
go-esi
------

go-esi is the implementation of the non-standard ESI (Edge-Side-Include) specification from the w3. With that you'll be able to use the ESI tags and process them in your favorite golang servers.

## References
https://www.w3.org/TR/esi-lang/

## Install
```bash
go get -u github.com/darkweak/go-esi
```

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

import "errors"

var errNotFound = errors.New("not found")
60 changes: 60 additions & 0 deletions esi/esi.go
@@ -0,0 +1,60 @@
package esi

import (
"net/http"
)

func findTagName(b []byte) tag {
name := tagname.FindSubmatch(b)
if name == nil {
return nil
}

switch string(name[1]) {
case comment:
return &commentTag{
baseTag: newBaseTag(),
}
case choose:
case escape:
case include:
return &includeTag{
baseTag: newBaseTag(),
}
case remove:
return &removeTag{
baseTag: newBaseTag(),
}
case otherwise:
case try:
case vars:
case when:
default:
}

return nil
}

func Parse(b []byte, req *http.Request) []byte {
pointer := 0

for pointer < len(b) {
next := b[pointer:]
tagIdx := esi.FindIndex(next)

if tagIdx == nil {
break
}

esiPointer := tagIdx[1]
t := findTagName(next[esiPointer:])

res, p := t.process(next[esiPointer:], req)
esiPointer += p

b = append(b[:pointer], append(next[:tagIdx[0]], append(res, next[esiPointer:]...)...)...)
pointer += len(res)
}

return b
}
34 changes: 34 additions & 0 deletions esi/esi_test.go
@@ -0,0 +1,34 @@
package esi

import (
"fmt"
"os"
"testing"
)

func loadFromFixtures(name string) []byte {
b, e := os.ReadFile("../fixtures/" + name)
if e != nil {
panic("The file " + name + " doesn't exist.")
}

return b
}

var (
commentMock = loadFromFixtures("comment")
// chooseMock = loadFromFixtures("choose")
// escapeMock = loadFromFixtures("escape")
includeMock = loadFromFixtures("include")
removeMock = loadFromFixtures("remove")
// tryMock = loadFromFixtures("try")
// varsMock = loadFromFixtures("vars")
)

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

import "regexp"

const (
choose = "choose"
comment = "comment"
escape = "escape"
include = "include"
remove = "remove"
otherwise = "otherwise"
try = "try"
vars = "vars"
when = "when"
)

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="?(.+?)"?( |/>)`)
)
110 changes: 110 additions & 0 deletions esi/type.go
@@ -0,0 +1,110 @@
package esi

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

var clientPool = &sync.Pool{
New: func() any {
return &http.Client{}
},
}

type (
tag interface {
process([]byte, *http.Request) ([]byte, int)
}

baseTag struct {
length int
}
includeTag struct {
*baseTag
src string
alt string
}
removeTag struct {
*baseTag
}
commentTag struct {
*baseTag
}
)

func newBaseTag() *baseTag {
return &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
}
11 changes: 11 additions & 0 deletions fixtures/choose
@@ -0,0 +1,11 @@
<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>
2 changes: 2 additions & 0 deletions fixtures/comment
@@ -0,0 +1,2 @@
<esi:comment text="the following animation will have a 24 hr TTL." />
<esi:include src="http://domain.com/chained-esi-include-1" alt=http://domain.com/alt-esi-include/>
3 changes: 3 additions & 0 deletions fixtures/escape
@@ -0,0 +1,3 @@
<!--esi
<p><esi:vars>Hello, $(HTTP_COOKIE{name})!</esi:vars></p>
-->
2 changes: 2 additions & 0 deletions fixtures/include
@@ -0,0 +1,2 @@
<esi:include src="http://domain.com/chained-esi-include-1" alt=http://domain.com/alt-esi-include/>
<h2>blah</h2>
6 changes: 6 additions & 0 deletions fixtures/remove
@@ -0,0 +1,6 @@
<esi:include src="http://domain.com/chained-esi-include-1"/>
<esi:remove>
<a href="http://www.example.com">www.example.com</a>
</esi:remove>
<esi:include src="http://domain.com/chained-esi-include-1"/>
<esi:include src="http://domain.com/chained-esi-include-1"/>
10 changes: 10 additions & 0 deletions fixtures/try
@@ -0,0 +1,10 @@
<esi:try>
<esi:attempt>
<esi:comment text="Include an ad"/>
<esi:include src="http://www.example.com/ad1.html"/>
</esi:attempt>
<esi:except>
<esi:comment text="Just write some HTML instead"/>
<a href="www.akamai.com">www.example.com</a>
</esi:except>
</esi:try>
3 changes: 3 additions & 0 deletions fixtures/vars
@@ -0,0 +1,3 @@
<esi:vars>
<img src="http://www.example.com/$(HTTP_COOKIE{type})/hello.gif"/ >
</esi:vars>
3 changes: 3 additions & 0 deletions go.mod
@@ -0,0 +1,3 @@
module github.com/darkweak/go-esi

go 1.18
27 changes: 27 additions & 0 deletions mock/Caddyfile
@@ -0,0 +1,27 @@
{
debug
}

:80 {
respond 404

route /chained-esi-include-1 {
header Content-Type text/html
respond `<esi:include src="http://domain.com/chained-esi-include-2"/>`
}

route /chained-esi-include-2 {
header Content-Type text/html
respond "<h1>CHAINED 2</h1>"
}

route /esi-include {
header Content-Type text/html
respond "<h1>ESI INCLUDE</h1>"
}

route /alt-esi-include {
header Content-Type text/html
respond "<h1>ALTERNATE ESI INCLUDE</h1>"
}
}
Binary file added mock/caddy
Binary file not shown.

0 comments on commit 642b9a7

Please sign in to comment.