admin: Fix redirections when using baseurl
This commit is contained in:
parent
d03350f6b3
commit
82ecd0d6dd
1 changed files with 24 additions and 2 deletions
|
@ -19,18 +19,40 @@ import (
|
||||||
var PKIDir string
|
var PKIDir string
|
||||||
var StaticDir 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 {
|
func StripPrefix(prefix string, h http.Handler) http.Handler {
|
||||||
if prefix == "" {
|
if prefix == "" {
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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 := new(http.Request)
|
||||||
*r2 = *r
|
*r2 = *r
|
||||||
r2.URL = new(url.URL)
|
r2.URL = new(url.URL)
|
||||||
*r2.URL = *r.URL
|
*r2.URL = *r.URL
|
||||||
r2.URL.Path = p
|
r2.URL.Path = p
|
||||||
h.ServeHTTP(w, r2)
|
h.ServeHTTP(ResponseWriterPrefix{w, prefix}, r2)
|
||||||
} else {
|
} else {
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue