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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ func esiMiddleware(next http.Handler) http.Handler {
}
```

### Resolve relative includes against another host
By default, a relative include (e.g. `<esi:include src="/fragment"/>`) is fetched from the host of the incoming request.
`writer.WithBaseURL` resolves them against another origin instead, e.g. an internal service. The path of the incoming
request is kept, and the `Cookie` and `Authorization` headers are forwarded to that origin as it becomes the same-origin
target. Absolute includes are not affected.
```go
// The scheme is optional and defaults to http.
base, err := writer.ParseBaseURL("static-webserver.my-app.svc.cluster.local")
if err != nil {
return err
}

cw := writer.NewWriter(rw, r, writer.WithBaseURL(base))
```
Every middleware exposes it as a `default host` setting, see below.

## Available as middleware
- [x] Caddy
- [x] Træfik
Expand All @@ -86,6 +102,16 @@ xcaddy build --with github.com/darkweak/go-esi/middleware/caddy
```
Refer to the [sample Caddyfile](https://github.com/darkweak/go-esi/blob/master/middleware/caddy/Caddyfile) to know how to use that.

To resolve the relative includes against another host:
```caddyfile
route /* {
esi {
default_host backend:8080
}
reverse_proxy backend:8080
}
```

### Roadrunner middleware
To use the `go-esi` processor as Roadrunner middleware, you just have to follow the steps below.
You have to build your `rr` binary with the `go-esi` dependency.
Expand Down Expand Up @@ -118,6 +144,9 @@ After that, you'll be able to set enable and add the esi processor to the middle
http:
# Other http sub keys
esi: {}
# Or, to resolve the relative includes against another host:
# esi:
# default_host: backend:8080
middleware:
- headers
- gzip
Expand Down Expand Up @@ -147,6 +176,18 @@ http:
plugin:
esi: {}
```
To resolve the relative includes against another host (e.g. a Kubernetes service), set `defaultHost`:
```yaml
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: esi
namespace: my-app
spec:
plugin:
esi:
defaultHost: static-webserver.my-app.svc.cluster.local # or http(s)://host:port
```
Refer to the [sample traefik file](https://github.com/darkweak/go-esi/blob/master/middleware/traefik/esi-configuration.yml) to know how to use that.

## TODO
Expand Down
58 changes: 54 additions & 4 deletions middleware/caddy/esi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,54 @@ func init() {
return &ESI{}, nil
})
httpcaddyfile.RegisterHandlerDirective("esi", func(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
return &ESI{}, nil
e := &ESI{}
err := e.UnmarshalCaddyfile(h.Dispenser)

return e, err
})
}

// ESI to handle, process and serve ESI tags.
type ESI struct{}
type ESI struct {
// DefaultHost is the origin the relative include URLs are resolved
// against instead of the incoming request host, e.g. "backend:8080" or
// "https://static.example.com".
DefaultHost string `json:"default_host,omitempty"`

opts []writer.Option
}

// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
//
// esi {
// default_host <origin>
// }
func (e *ESI) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if d.NextArg() {
return d.ArgErr()
}

for d.NextBlock(0) {
switch d.Val() {
case "default_host":
if !d.NextArg() {
return d.ArgErr()
}

e.DefaultHost = d.Val()

if d.NextArg() {
return d.ArgErr()
}
default:
return d.Errf("unrecognized esi subdirective %q", d.Val())
}
}
}

return nil
}

// CaddyModule returns the Caddy module information.
func (ESI) CaddyModule() caddy.ModuleInfo {
Expand All @@ -34,14 +76,21 @@ func (ESI) CaddyModule() caddy.ModuleInfo {

// ServeHTTP implements caddyhttp.MiddlewareHandler
func (e *ESI) ServeHTTP(rw http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
cw := writer.NewWriter(rw, r)
cw := writer.NewWriter(rw, r, e.opts...)
err := next.ServeHTTP(cw, r)

return errors.Join(err, cw.Close())
}

// Provision implements caddy.Provisioner
func (*ESI) Provision(caddy.Context) error {
func (e *ESI) Provision(caddy.Context) error {
base, err := writer.ParseBaseURL(e.DefaultHost)
if err != nil {
return err
}

e.opts = []writer.Option{writer.WithBaseURL(base)}

return nil
}

Expand All @@ -55,4 +104,5 @@ var (
_ caddy.Module = (*ESI)(nil)
_ caddy.Provisioner = (*ESI)(nil)
_ caddy.App = (*ESI)(nil)
_ caddyfile.Unmarshaler = (*ESI)(nil)
)
28 changes: 28 additions & 0 deletions middleware/caddy/esi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,31 @@ func TestUnitary(t *testing.T) {
_, _ = tester.AssertGetResponse(`http://domain.com:9080/include`, http.StatusOK, "<h1>CHAINED 2</h1>")
_, _ = tester.AssertGetResponse(`http://domain.com:9080/alt`, http.StatusOK, "<h1>ALTERNATE ESI INCLUDE</h1>")
}

func TestDefaultHost(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
order esi before basicauth
esi
http_port 9080
}
localhost:9080 {
respond /my-esi-content/whatever "<h1>FROM DEFAULT HOST</h1>"
}
domain.com:9080 {
route /page {
header Content-Type text/html
esi {
default_host localhost:9080
}
respond `+"`<esi:include src=\"/my-esi-content/whatever\"/>`"+`
}
}`, "caddyfile")

rq, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1:9080/page", nil)
rq.Host = "domain.com"

_, _ = tester.AssertResponse(rq, http.StatusOK, "<h1>FROM DEFAULT HOST</h1>")
}
20 changes: 17 additions & 3 deletions middleware/roadrunner/esi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"go.uber.org/zap"
)

const configurationKey = "http.esi"
const (
configurationKey = "http.esi"
defaultHostKey = configurationKey + ".default_host"
)

type (
// Configurer interface used to parse yaml configuration.
Expand All @@ -20,7 +23,9 @@ type (
Has(name string) bool
}

Plugin struct{}
Plugin struct {
opts []writer.Option
}
)

// Name is the plugin name
Expand All @@ -35,14 +40,23 @@ func (p *Plugin) Init(cfg Configurer, log *zap.Logger) error {
return errors.E(op, errors.Disabled)
}

defaultHost, _ := cfg.Get(defaultHostKey).(string)

base, err := writer.ParseBaseURL(defaultHost)
if err != nil {
return errors.E(op, err)
}

p.opts = []writer.Option{writer.WithBaseURL(base)}

return nil
}

// Middleware is the request entrypoint to catch the response and
// process the esi tags if present.
func (p *Plugin) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
cw := writer.NewWriter(rw, r)
cw := writer.NewWriter(rw, r, p.opts...)
next.ServeHTTP(cw, r)
_ = cw.Close()
})
Expand Down
33 changes: 29 additions & 4 deletions middleware/roadrunner/esi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
)

type (
configWrapper struct{}
next struct{}
configWrapper struct {
values map[string]any
}
next struct{}
)

var nextFilter = &next{}
Expand All @@ -29,8 +31,8 @@ func (n *next) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
_, _ = rw.Write([]byte(`Hello Roadrunner! <esi:include src="/include" />`))
}

func (*configWrapper) Get(name string) any {
return nil
func (c *configWrapper) Get(name string) any {
return c.values[name]
}
func (*configWrapper) Has(name string) bool {
return true
Expand Down Expand Up @@ -78,3 +80,26 @@ func Test_Plugin_Middleware(t *testing.T) {
t.Error(`The returned response must be equal to "Hello Roadrunner! " because of non running service.`)
}
}

func Test_Plugin_DefaultHost(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("from default host " + r.URL.Path))
}))
defer upstream.Close()

p := &Plugin{}
if err := p.Init(&configWrapper{values: map[string]any{defaultHostKey: upstream.URL}}, nil); err != nil {
t.Fatal(err)
}

req, res := prepare("http://localhost/handled")
p.Middleware(nextFilter).ServeHTTP(res, req)

if got, want := res.Body.String(), "Hello Roadrunner! from default host /include"; got != want {
t.Errorf("want %q, got %q", want, got)
}

if (&Plugin{}).Init(&configWrapper{values: map[string]any{defaultHostKey: "ftp://svc"}}, nil) == nil {
t.Error("The Init method must fail with an invalid default_host.")
}
}
18 changes: 15 additions & 3 deletions middleware/traefik/esi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import (
)

// Config the ESI plugin configuration.
type Config struct{}
type Config struct {
// DefaultHost is the origin the relative include URLs are resolved
// against instead of the incoming request host, e.g.
// "static-webserver.my-app.svc.cluster.local" or "https://static.example.com".
DefaultHost string `json:"defaultHost,omitempty" toml:"defaultHost,omitempty" yaml:"defaultHost,omitempty"`
}

// CreateConfig creates the ESI plugin configuration.
func CreateConfig() *Config {
Expand All @@ -19,18 +24,25 @@ func CreateConfig() *Config {
type ESI struct {
next http.Handler
name string
opts []writer.Option
}

// New created a new ESI plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
base, err := writer.ParseBaseURL(config.DefaultHost)
if err != nil {
return nil, err
}

return &ESI{
next: next,
name: name,
opts: []writer.Option{writer.WithBaseURL(base)},
}, nil
}

func (e *ESI) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
cw := writer.NewWriter(rw, req)
cw := writer.NewWriter(rw, req, e.opts...)
e.next.ServeHTTP(cw, req)
_ = cw.Close()
}
49 changes: 49 additions & 0 deletions middleware/traefik/esi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package traefik_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/darkweak/go-esi/middleware/traefik"
)

func TestDefaultHost(t *testing.T) {
t.Parallel()

upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("fragment " + r.URL.Path))
}))
defer upstream.Close()

cfg := traefik.CreateConfig()
cfg.DefaultHost = upstream.Listener.Addr().String()

next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`<esi:include src="/my-esi-content/whatever"/>`))
})

h, err := traefik.New(context.Background(), next, cfg, "esi")
if err != nil {
t.Fatal(err)
}

rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "http://domain.com/", nil))

if got, want := rec.Body.String(), "fragment /my-esi-content/whatever"; got != want {
t.Errorf("want %q, got %q", want, got)
}
}

func TestInvalidDefaultHost(t *testing.T) {
t.Parallel()

cfg := traefik.CreateConfig()
cfg.DefaultHost = "ftp://svc"

if _, err := traefik.New(context.Background(), http.NotFoundHandler(), cfg, "esi"); err == nil {
t.Error("want an error for an invalid defaultHost")
}
}
Loading
Loading