Skip to content

Commit bf23401

Browse files
dmitshurgopherbot
authored andcommitted
cmd/coordinator: rewrite scheme-less URLs and redirects in dev mode
This was created in preparation of removal of dashboard v2, which was going to use a redirect. However dashboard v2 turned out to be useful enough to warrant keeping and maintaining it. Apply this enhancement to the local development serving path since it is generally useful and already prepared. For golang/go#65913. Change-Id: I6006bfa02d512675b63773c22c9ed21d8d5b4ab4 Reviewed-on: https://go-review.googlesource.com/c/build/+/567497 Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 9d46ba7 commit bf23401

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cmd/coordinator/coordinator.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,30 @@ func hostPathHandler(h http.Handler) http.Handler {
174174

175175
// A linkRewriter is a ResponseWriter that rewrites links in HTML output.
176176
// It rewrites relative links /foo to be /host/foo, and it rewrites any link
177-
// https://h/foo, where h is in validHosts, to be /h/foo. This corrects the
178-
// links to have the right form for the test server.
177+
// https://h/foo or //h/foo, where h is in validHosts, to be /h/foo.
178+
// This corrects the links to have the right form for the local development mode.
179179
type linkRewriter struct {
180180
http.ResponseWriter
181181
host string
182182
buf []byte
183183
ct string // content-type
184184
}
185185

186+
func (r *linkRewriter) WriteHeader(code int) {
187+
if l := r.Header().Get("Location"); l != "" {
188+
if u, err := url.Parse(l); err == nil {
189+
if u.Host == "" {
190+
u.Path = "/" + r.host + u.Path
191+
} else if validHosts[u.Host] {
192+
u.Path = "/" + u.Host + u.Path
193+
u.Scheme, u.Host = "", ""
194+
}
195+
r.Header().Set("Location", u.String())
196+
}
197+
}
198+
r.ResponseWriter.WriteHeader(code)
199+
}
200+
186201
func (r *linkRewriter) Write(data []byte) (int, error) {
187202
if r.ct == "" {
188203
ct := r.Header().Get("Content-Type")
@@ -200,12 +215,12 @@ func (r *linkRewriter) Write(data []byte) (int, error) {
200215
}
201216

202217
func (r *linkRewriter) Flush() {
203-
repl := []string{
204-
`href="/`, `href="/` + r.host + `/`,
205-
}
218+
var repl []string
206219
for host := range validHosts {
207220
repl = append(repl, `href="https://`+host, `href="/`+host)
221+
repl = append(repl, `href="//`+host, `href="/`+host) // Handle scheme-less URLs.
208222
}
223+
repl = append(repl, `href="/`, `href="/`+r.host+`/`)
209224
strings.NewReplacer(repl...).WriteString(r.ResponseWriter, string(r.buf))
210225
r.buf = nil
211226
}

0 commit comments

Comments
 (0)