ApplyAppearance: keep a path StripPrefix already made relative
Behind http.StripPrefix the request path is relative to the base path, so for base path /feeds the page /feeds was cut to / again. A path that no longer matches RequestURI's is now taken as already stripped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0f9b40df9f
commit
28db4f707f
3 files changed
+51
-6
No files matched your search
+21
-4
@@ -69,11 +69,9 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) {
|
||||
if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) {
|
||||
h.Palette = c.Value
|
||||
}
|
||||
// Only a whole leading BasePath segment is cut: behind
|
||||
// http.StripPrefix the path is already relative, and "/r" must not
|
||||
// eat the start of "/release-notes".
|
||||
path := r.URL.Path
|
||||
if h.BasePath != "" && (path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) {
|
||||
if h.BasePath != "" && !strippedPath(r) &&
|
||||
(path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) {
|
||||
path = strings.TrimPrefix(path, h.BasePath)
|
||||
}
|
||||
if path == "" {
|
||||
@@ -83,6 +81,25 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) {
|
||||
h.AppearanceBack = WithoutFlash(back.String())
|
||||
}
|
||||
|
||||
// strippedPath reports whether r.URL.Path is already relative to the
|
||||
// UI's base path, i.e. a handler such as http.StripPrefix cut it down
|
||||
// from what the client asked for. Those keep RequestURI as sent, so a
|
||||
// path that no longer matches RequestURI's was stripped -- and must not
|
||||
// be stripped again: for base path "/feeds" the relative page "/feeds"
|
||||
// is a page of its own, not the start page. Without a RequestURI (a
|
||||
// request built in code) only a whole leading BasePath segment is cut,
|
||||
// so "/r" never eats the start of "/release-notes".
|
||||
func strippedPath(r *http.Request) bool {
|
||||
if r.RequestURI == "" {
|
||||
return false
|
||||
}
|
||||
u, err := url.ParseRequestURI(r.RequestURI)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return u.Path != r.URL.Path
|
||||
}
|
||||
|
||||
// AppearanceHandler stores a theme and/or palette choice from a POST
|
||||
// form (fields "theme", "palette", "back") in cookies and redirects to
|
||||
// back, a BasePath-relative path inside this UI (anything else goes to
|
||||
|
||||
Reference in new issue
Block a user