Commit ea65d015 authored by Ggicci's avatar Ggicci Committed by Brad Fitzpatrick

net/http: clean the path of the stripped URL by StripPrefix

The path of the new stripped URL should also be cleaned. Since an empty path
may cause unexpected errors in some HTTP handlers, e.g. http.ServeFile.

Fixes #30165

Change-Id: Ib44fdce6388b5d62ffbcab5266925ef8f13f26e2
Reviewed-on: https://go-review.googlesource.com/c/161738Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 5a7e8f46
...@@ -2900,6 +2900,15 @@ func TestStripPrefix(t *testing.T) { ...@@ -2900,6 +2900,15 @@ func TestStripPrefix(t *testing.T) {
t.Errorf("test 2: got status %v, want %v", g, e) t.Errorf("test 2: got status %v, want %v", g, e)
} }
res.Body.Close() res.Body.Close()
res, err = c.Get(ts.URL + "/foo")
if err != nil {
t.Fatal(err)
}
if g, e := res.Header.Get("X-Path"), "/"; g != e {
t.Errorf("test 3: got %s, want %s", g, e)
}
res.Body.Close()
} }
// https://golang.org/issue/18952. // https://golang.org/issue/18952.
......
...@@ -2030,7 +2030,7 @@ func StripPrefix(prefix string, h Handler) Handler { ...@@ -2030,7 +2030,7 @@ func StripPrefix(prefix string, h Handler) Handler {
*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 = cleanPath(p)
h.ServeHTTP(w, r2) h.ServeHTTP(w, r2)
} else { } else {
NotFound(w, r) NotFound(w, r)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment