aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGgicci <ggicci.t@gmail.com>2019-02-11 18:00:02 +0800
committerBrad Fitzpatrick <bradfitz@golang.org>2019-02-26 22:55:59 +0000
commitea65d015b838f2ca75debacacbbb039fe5e96b26 (patch)
tree3d1e2b732c76b659372502653e806fb94ced0697 /src
parent5a7e8f466e9becdb7277ac725b5540d6bd8e3727 (diff)
downloadgo-ea65d015b838f2ca75debacacbbb039fe5e96b26.tar.xz
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/161738 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/net/http/serve_test.go9
-rw-r--r--src/net/http/server.go2
2 files changed, 10 insertions, 1 deletions
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index 6eb0088a96..86cdb34ebb 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -2900,6 +2900,15 @@ func TestStripPrefix(t *testing.T) {
t.Errorf("test 2: got status %v, want %v", g, e)
}
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.
diff --git a/src/net/http/server.go b/src/net/http/server.go
index aa9c3f5d2e..e68ec2f01e 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -2030,7 +2030,7 @@ func StripPrefix(prefix string, h Handler) Handler {
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
- r2.URL.Path = p
+ r2.URL.Path = cleanPath(p)
h.ServeHTTP(w, r2)
} else {
NotFound(w, r)