diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index f8687416fe9..a9714682c7e 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -6507,3 +6507,20 @@ func TestDisableKeepAliveUpgrade(t *testing.T) { t.Fatalf("unexpected value read from body:\ngot: %q\nwant: %q", b, "hello") } } + +func TestMuxRedirectRelative(t *testing.T) { + setParallel(t) + req, err := ReadRequest(bufio.NewReader(strings.NewReader("GET http://example.com HTTP/1.1\r\nHost: test\r\n\r\n"))) + if err != nil { + t.Errorf("%s", err) + } + mux := NewServeMux() + resp := httptest.NewRecorder() + mux.ServeHTTP(resp, req) + if got, want := resp.Header().Get("Location"), "/"; got != want { + t.Errorf("Location header expected %q; got %q", want, got) + } + if got, want := resp.Code, StatusMovedPermanently; got != want { + t.Errorf("Expected response code %d; got %d", want, got) + } +} diff --git a/src/net/http/server.go b/src/net/http/server.go index e52a78e652c..4e73508973a 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2404,9 +2404,8 @@ func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) { if path != r.URL.Path { _, pattern = mux.handler(host, path) - url := *r.URL - url.Path = path - return RedirectHandler(url.String(), StatusMovedPermanently), pattern + u := &url.URL{Path: path, RawQuery: r.URL.RawQuery} + return RedirectHandler(u.String(), StatusMovedPermanently), pattern } return mux.handler(host, r.URL.Path)