Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/middlewares.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ jobs:
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
-
name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v7
with:
go-version: 1.19
go-version: "1.27"
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v7
-
name: Install xcaddy
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build go-esi as caddy module
run: cd middleware/caddy && xcaddy build --with github.com/darkweak/go-esi/middleware/caddy=./ --with github.com/darkweak/go-esi@latest=../..
run: cd middleware/caddy && xcaddy build --with github.com/darkweak/go-esi=../.. --with github.com/darkweak/go-esi/middleware/caddy=./
-
name: Run Caddy tests
run: cd middleware/caddy && go test -v ./...
Expand All @@ -39,12 +39,12 @@ jobs:
steps:
-
name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v7
with:
go-version: 1.19
go-version: "1.27"
-
name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v7
-
name: Run Roadrunner tests
run: cd middleware/roadrunner && go test -v ./...
10 changes: 6 additions & 4 deletions .github/workflows/non-regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ jobs:
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
-
name: checkout code
uses: actions/checkout@v3
uses: actions/checkout@v7
-
name: install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v7
with:
go-version: 1.19
go-version: "1.27"
-
name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v9
with:
version: v2.15
-
name: install xcaddy
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ jobs:
steps:
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v7
with:
go-version: 1.19
go-version: "1.27"
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v7
with:
fetch-depth: 0
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
uses: goreleaser/goreleaser-action@v7
with:
version: latest
args: release --rm-dist
version: "~> v2"
args: release --clean
workdir: ./middleware/server
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48 changes: 38 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
version: "2"
run:
timeout: 30s
issues-exit-code: 1

linters:
enable-all: true
default: all
disable:
- bodyclose
- cyclop
- exhaustivestruct
- depguard
- embeddedstructfieldcheck
- exhaustruct
- exhaustruct_v5
- forbidigo
- funcorder
- gochecknoglobals
- gci
- golint
- gomnd
- ifshort
- inamedparam
- ireturn
- mnd
- nestif
- noinlineerr
- nonamedreturns
- nosnakecase
- revive
- testpackage
- stylecheck
- varnamelen
- wrapcheck
- wsl
exclusions:
generated: lax
rules:
- path: _test\.go
linters:
- gosec
- noctx
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
- gofumpt
- goimports
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,55 @@ go get -u github.com/darkweak/go-esi
```

## Usage
### Parse a whole document
```go
import (
// ...
github.com/darkweak/go-esi/esi
"github.com/darkweak/go-esi/esi"
)

//...

func functionToParseESITags(b []byte, r *http.Request) []byte {
// Returns the parsed response.
// Returns the parsed response. The include tags are fetched in parallel.
res := esi.Parse(b, r)

//...
return res
}
```

### Stream a document
`esi.Stream` processes the ESI tags while the document is written to it. The static content is forwarded as soon as it is
read, the include tags are fetched concurrently, and their content is written in the document order. The destination is
flushed (if it implements `http.Flusher`) whenever the output has to wait for an include or for more input.
```go
func streamESI(dst io.Writer, src io.Reader, r *http.Request) error {
s := esi.NewStream(dst, r)
if _, err := io.Copy(s, src); err != nil {
_ = s.Close()

return err
}

// Close processes the remaining content and waits for the pending includes.
return s.Close()
}
```

### Wrap an `http.ResponseWriter`
```go
import "github.com/darkweak/go-esi/writer"

func esiMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
cw := writer.NewWriter(rw, r)
next.ServeHTTP(cw, r)
_ = cw.Close()
})
}
```

## Available as middleware
- [x] Caddy
- [x] Træfik
Expand Down Expand Up @@ -99,7 +131,7 @@ experimental:
plugins:
souin:
moduleName: github.com/darkweak/go-esi
version: v0.0.6
version: v0.0.7
```
```yaml
# anywhere/dynamic-configuration
Expand Down
17 changes: 7 additions & 10 deletions esi/choose.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package esi

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

const choose = "choose"

var (
closeChoose = regexp.MustCompile("</esi:choose>")
closeChoose = []byte("</esi:choose>")
whenRg = regexp.MustCompile(`(?s)<esi:when test="(.+?)">(.+?)</esi:when>`)
otherwiseRg = regexp.MustCompile(`(?s)<esi:otherwise>(.+?)</esi:otherwise>`)
)
Expand All @@ -33,12 +34,12 @@ type chooseTag struct {
// </esi:choose>
// ).
func (c *chooseTag) Process(b []byte, req *http.Request) ([]byte, int) {
found := closeChoose.FindIndex(b)
if found == nil {
end := closeEnd(b, closeChoose)
if end == 0 {
return nil, len(b)
}

c.length = found[1]
c.length = end
tagIdxs := whenRg.FindAllSubmatch(b, -1)

var res []byte
Expand All @@ -60,13 +61,9 @@ func (c *chooseTag) Process(b []byte, req *http.Request) ([]byte, int) {
}

func (*chooseTag) HasClose(b []byte) bool {
return closeChoose.FindIndex(b) != nil
return bytes.Contains(b, closeChoose)
}

func (*chooseTag) GetClosePosition(b []byte) int {
if idx := closeChoose.FindIndex(b); idx != nil {
return idx[1]
}

return 0
return closeEnd(b, closeChoose)
}
35 changes: 23 additions & 12 deletions esi/comment.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
package esi

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

const comment = "comment"

var closeComment = regexp.MustCompile("/>((\n| +)+)?")
var closeComment = []byte("/>")

type commentTag struct {
*baseTag
}

// commentEnd returns the end of the closing "/>" including the spaces and
// newlines following it, or 0.
func commentEnd(b []byte) int {
end := closeEnd(b, closeComment)
if end == 0 {
return 0
}

for end < len(b) && isSpace(b[end]) {
end++
}

return end
}

// 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 {
func (c *commentTag) Process(b []byte, _ *http.Request) ([]byte, int) {
end := commentEnd(b)
if end == 0 {
return nil, len(b)
}

return []byte{}, found[1]
return []byte{}, end
}

func (*commentTag) HasClose(b []byte) bool {
return closeComment.FindIndex(b) != nil
return bytes.Contains(b, closeComment)
}

func (*commentTag) GetClosePosition(b []byte) int {
if idx := closeComment.FindIndex(b); idx != nil {
return idx[1]
}

return 0
return commentEnd(b)
}
30 changes: 10 additions & 20 deletions esi/escape.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
package esi

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

const escape = "<!--esi"

var (
escapeRg = regexp.MustCompile("<!--esi")
closeEscape = regexp.MustCompile("((\n| +)+)?-->")
startEscape = regexp.MustCompile("((\n| +)+)?")
)
var closeEscape = []byte("-->")

type escapeTag struct {
*baseTag
}

func (e *escapeTag) Process(b []byte, req *http.Request) ([]byte, int) {
closeIdx := closeEscape.FindIndex(b)

if closeIdx == nil {
closeStart, end := findClose(b, closeEscape)
if closeStart == -1 {
return nil, len(b)
}

startPosition := 0
if startIdx := startEscape.FindIndex(b); startIdx != nil {
startPosition = startIdx[1]
for startPosition < closeStart && isSpace(b[startPosition]) {
startPosition++
}

e.length = closeIdx[1]
b = b[startPosition:closeIdx[0]]
e.length = end

return b, e.length
return b[startPosition:closeStart], e.length
}

func (*escapeTag) HasClose(b []byte) bool {
return closeEscape.FindIndex(b) != nil
return bytes.Contains(b, closeEscape)
}

func (*escapeTag) GetClosePosition(b []byte) int {
if idx := closeEscape.FindIndex(b); idx != nil {
return idx[1]
}

return 0
return closeEnd(b, closeEscape)
}
Loading
Loading