Repository navigation
feat(writer): configurable default host for relative includes - #17
Merged
Merged
Conversation
Relative include URLs were always resolved against the incoming request
host, so a single proxy routing several apps (e.g. Traefik in Kubernetes)
had to hardcode the fragment host in the HTML and rebuild per environment.
- writer: add ParseBaseURL and the WithBaseURL option (NewWriter is now
variadic, so existing callers keep compiling). The include request gets
the base scheme/host while keeping the incoming path; the incoming
request is not modified.
- traefik: `defaultHost` plugin setting.
- caddy: `default_host` subdirective (`esi { default_host ... }`).
- roadrunner: `http.esi.default_host` setting.
Closes #14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #14
Problem
A relative include (
<esi:include src="/my-esi-content/whatever"/>) is always resolved against the host of the incoming request. When one proxy (e.g. Traefik in Kubernetes) routes several apps, the fragments live on an internal service, so the host had to be hardcoded in the HTML and the app rebuilt per environment.Change
ParseBaseURL(string)andWithBaseURL(*url.URL)option.NewWriter(rw, rq, opts ...Option)is variadic, so existing callers are unaffected.host[:port](scheme defaults tohttp) orhttp(s)://host[:port]. Rejects paths, queries, userinfo and other schemes.src="fragment"still resolves relative to the page. The incoming request is not modified, and$(HTTP_HOST)still reports the original host.defaultHostplugin setting:esi { default_host backend:8080 }http.esi.default_hostNotes
Cookie/Authorizationare forwarded to the default host, since it becomes the same-origin target for relative includes (the same as before, when they were forwarded to the request host). Absolute includes pointing at the public host no longer count as same-origin when a default host is set.service+namespaceform isn't included: a plugin can't resolve Kubernetes references, andservice.namespace.svc.cluster.local[:port]does the same thing.Testing
ParseBaseURLandWithBaseURL(include routing, relative path, cookie forwarding, original request unchanged), plus one integration test each for the Traefik, Caddy (caddytest) and RoadRunner middlewares.go test -race ./...passes in the root module and all three middleware modules, andgolangci-lintreports no new issues.traefik:latest(Yaegi) withdefaultHost: whoami: the relative include went to thewhoamiservice withHost: whoami.🤖 Generated with Claude Code