Commit af59742d authored by Dmitri Shuralyov's avatar Dmitri Shuralyov Committed by Brad Fitzpatrick

net/http: don't modify Request in StripPrefix

As of https://golang.org/cl/21530, rules are updated to state
that Handlers shouldn't modify the provided Request. This change
updates StripPrefix to follow that rule.

Resolves #18952.

Change-Id: I29bbb580722e871131fa75a97e6e038ec64fdfcd
Reviewed-on: https://go-review.googlesource.com/36483Reviewed-by: default avatarMatt Layher <mdlayher@gmail.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a146dd3a
......@@ -2433,6 +2433,16 @@ func TestStripPrefix(t *testing.T) {
res.Body.Close()
}
// https://golang.org/issue/18952.
func TestStripPrefix_notModifyRequest(t *testing.T) {
h := StripPrefix("/foo", NotFoundHandler())
req := httptest.NewRequest("GET", "/foo/bar", nil)
h.ServeHTTP(httptest.NewRecorder(), req)
if req.URL.Path != "/foo/bar" {
t.Errorf("StripPrefix should not modify the provided Request, but it did")
}
}
func TestRequestLimit_h1(t *testing.T) { testRequestLimit(t, h1Mode) }
func TestRequestLimit_h2(t *testing.T) { testRequestLimit(t, h2Mode) }
func testRequestLimit(t *testing.T, h2 bool) {
......
......@@ -1973,8 +1973,12 @@ func StripPrefix(prefix string, h Handler) Handler {
}
return HandlerFunc(func(w ResponseWriter, r *Request) {
if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
r.URL.Path = p
h.ServeHTTP(w, r)
r2 := new(Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
h.ServeHTTP(w, r2)
} else {
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