Skip to content

Commit

Permalink
Merge pull request #7 from darkweak/feat/chore/support-relative-paths
Browse files Browse the repository at this point in the history
feat(chore): Support relative paths
  • Loading branch information
darkweak authored Nov 12, 2022
2 parents 1ea1571 + ca40ee6 commit 8f1e394
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 14 deletions.
10 changes: 8 additions & 2 deletions esi/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package esi
import (
"io"
"net/http"
"net/url"
"regexp"
)

Expand Down Expand Up @@ -36,6 +37,11 @@ func (i *includeTag) loadAttributes(b []byte) error {
return nil
}

func sanitizeURL(u string, reqUrl *url.URL) string {
parsed, _ := url.Parse(u)
return reqUrl.ResolveReference(parsed).String()
}

// 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
Expand All @@ -52,12 +58,12 @@ func (i *includeTag) Process(b []byte, req *http.Request) ([]byte, int) {
return nil, len(b)
}

rq, _ := http.NewRequest(http.MethodGet, i.src, nil)
rq, _ := http.NewRequest(http.MethodGet, sanitizeURL(i.src, req.URL), nil)
client := &http.Client{}
response, err := client.Do(rq)

if err != nil || response.StatusCode >= 400 {
rq, _ = http.NewRequest(http.MethodGet, i.src, nil)
rq, _ = http.NewRequest(http.MethodGet, sanitizeURL(i.alt, req.URL), nil)
response, err = client.Do(rq)

if err != nil || response.StatusCode >= 400 {
Expand Down
1 change: 1 addition & 0 deletions fixtures/alt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<esi:include src="http://invalid.do.main" alt=http://domain.com:9080/alt-esi-include/>
6 changes: 3 additions & 3 deletions fixtures/full.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<head>
<title><esi:vars>Hello from $(HTTP_HOST)</esi:vars></title>
<esi:remove>
<esi:include src="http://domain.com/chained-esi-include-1" />
<esi:include src="http://domain.com:9080/chained-esi-include-1" />
</esi:remove>
</head>
<body>
<!--esi
<esi:include src="domain.com/not-interpreted"/>
-->
<esi:include src="http://domain.com/chained-esi-include-1" />
<esi:include src="http://domain.com/chained-esi-include-1" />
<esi:include src="/chained-esi-include-1" />
<esi:include src="http://inexistent.abc/something" alt="//domain.com:9080/alt-esi-include" />
</body>
</html>
2 changes: 1 addition & 1 deletion fixtures/include
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<esi:include src="http://domain.com/chained-esi-include-1" alt=http://domain.com/alt-esi-include/>
<esi:include src="http://domain.com:9080/chained-esi-include-1" alt=http://domain.com:9080/alt-esi-include/>
12 changes: 5 additions & 7 deletions middleware/caddy/Caddyfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
order esi before basicauth
admin off
debug
http_port 9080
esi
}

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

route /chained-esi-include-2 {
Expand All @@ -28,8 +27,7 @@ localhost:443 {

route /* {
esi
reverse_proxy 127.0.0.1:81
# root * ../../fixtures
# file_server
root * ../../fixtures
file_server
}
}
59 changes: 59 additions & 0 deletions middleware/caddy/esi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package caddy_esi

import (
"net/http"
"os"
"testing"

"github.com/caddyserver/caddy/v2/caddytest"
)

const expectedOutput = `<html>
<head>
<title>Hello from domain.com:9080</title>
</head>
<body>
<esi:include src="domain.com/not-interpreted"/>
<h1>CHAINED 2</h1>
<h1>ALTERNATE ESI INCLUDE</h1>
</body>
</html>
`

func loadCaddyfile() string {
b, _ := os.ReadFile("./Caddyfile")
return string(b)
}

func TestFullHTML(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(loadCaddyfile(), "caddyfile")

_, _ = tester.AssertGetResponse(`http://domain.com:9080/full.html`, http.StatusOK, expectedOutput)
}

func TestInclude(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(loadCaddyfile(), "caddyfile")

_, _ = tester.AssertGetResponse(`http://domain.com:9080/include`, http.StatusOK, "<h1>CHAINED 2</h1>")
}

func TestIncludeAlt(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(loadCaddyfile(), "caddyfile")

_, _ = tester.AssertGetResponse(`http://domain.com:9080/alt`, http.StatusOK, "<h1>ALTERNATE ESI INCLUDE</h1>")
}

func TestEscape(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(loadCaddyfile(), "caddyfile")

_, _ = tester.AssertGetResponse(`http://domain.com:9080/escape`, http.StatusOK, `
<p><esi:vars>Hello, $(HTTP_COOKIE{name})!</esi:vars></p>
`)
}
23 changes: 23 additions & 0 deletions middleware/caddy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ require (
require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed // indirect
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/caddyserver/certmagic v0.16.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
Expand All @@ -26,16 +29,22 @@ require (
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.0.4-0.20200906165740-41ebdbffecfd // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/go-logfmt/logfmt v0.5.0 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/cel-go v0.11.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand Down Expand Up @@ -87,8 +96,20 @@ require (
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/tailscale/tscert v0.0.0-20220316030059-54bbcb9f74e2 // indirect
github.com/urfave/cli v1.22.5 // indirect
github.com/yuin/goldmark v1.4.12 // indirect
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.29.0 // indirect
go.opentelemetry.io/otel v1.4.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.4.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.4.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.4.0 // indirect
go.opentelemetry.io/otel/internal/metric v0.27.0 // indirect
go.opentelemetry.io/otel/metric v0.27.0 // indirect
go.opentelemetry.io/otel/sdk v1.4.0 // indirect
go.opentelemetry.io/otel/trace v1.4.0 // indirect
go.opentelemetry.io/proto/otlp v0.12.0 // indirect
go.step.sm/cli-utils v0.7.0 // indirect
go.step.sm/crypto v0.16.1 // indirect
go.step.sm/linkedca v0.15.0 // indirect
Expand All @@ -106,8 +127,10 @@ require (
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
google.golang.org/grpc v1.46.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
)

Expand Down
Loading

0 comments on commit 8f1e394

Please sign in to comment.