diff --git a/admin/main.go b/admin/main.go index 91f15942..62924014 100644 --- a/admin/main.go +++ b/admin/main.go @@ -19,18 +19,40 @@ import ( var PKIDir string var StaticDir string +type ResponseWriterPrefix struct { + real http.ResponseWriter + prefix string +} + +func (r ResponseWriterPrefix) Header() http.Header { + return r.real.Header() +} + +func (r ResponseWriterPrefix) WriteHeader(s int) { + if v, exists := r.real.Header()["Location"]; exists { + r.real.Header().Set("Location", r.prefix + v[0]) + } + r.real.WriteHeader(s) +} + +func (r ResponseWriterPrefix) Write(z []byte) (int, error) { + return r.real.Write(z) +} + func StripPrefix(prefix string, h http.Handler) http.Handler { if prefix == "" { return h } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) { + if prefix != "/" && r.URL.Path == "/" { + http.Redirect(w, r, prefix + "/", http.StatusFound) + } else if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) { r2 := new(http.Request) *r2 = *r r2.URL = new(url.URL) *r2.URL = *r.URL r2.URL.Path = p - h.ServeHTTP(w, r2) + h.ServeHTTP(ResponseWriterPrefix{w, prefix}, r2) } else { h.ServeHTTP(w, r) }