diff options
author | Damien Neil <dneil@google.com> | 2023-04-05 10:19:44 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-04-25 15:50:56 +0000 |
commit | 25b4f4062589a349117aaf52edf8db8ffa68773b (patch) | |
tree | c9d98dee2535554bc2dd2545f9baf5284e0b1232 | |
parent | 484535e67b03ff8c757b66806032aee54a2a3bde (diff) | |
download | go-git-25b4f4062589a349117aaf52edf8db8ffa68773b.tar.gz |
[release-branch.go1.20] Revert "net/http: FileServer method check + minimal OPTIONS implementation"
This reverts https://go.dev/cl/413554
Reason for revert: Backwards-incompatible change in behavior.
For #53501
For #59375
Fixes #59469
Change-Id: Ic3f63b378f9c819599b32e5e6e410f6163849317
Reviewed-on: https://go-review.googlesource.com/c/go/+/482635
Reviewed-by: Tatiana Bradley <tatianabradley@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
(cherry picked from commit c02fa75086dbc6db0d90f477e7b4c839140fdeb2)
Reviewed-on: https://go-review.googlesource.com/c/go/+/488635
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
-rw-r--r-- | src/net/http/fs.go | 20 | ||||
-rw-r--r-- | src/net/http/fs_test.go | 41 |
2 files changed, 5 insertions, 56 deletions
diff --git a/src/net/http/fs.go b/src/net/http/fs.go index 83459046bf..41e0b43ac8 100644 --- a/src/net/http/fs.go +++ b/src/net/http/fs.go @@ -858,22 +858,12 @@ func FileServer(root FileSystem) Handler { } func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) { - const options = MethodOptions + ", " + MethodGet + ", " + MethodHead - - switch r.Method { - case MethodGet, MethodHead: - if !strings.HasPrefix(r.URL.Path, "/") { - r.URL.Path = "/" + r.URL.Path - } - serveFile(w, r, f.root, path.Clean(r.URL.Path), true) - - case MethodOptions: - w.Header().Set("Allow", options) - - default: - w.Header().Set("Allow", options) - Error(w, "read-only", StatusMethodNotAllowed) + upath := r.URL.Path + if !strings.HasPrefix(upath, "/") { + upath = "/" + upath + r.URL.Path = upath } + serveFile(w, r, f.root, path.Clean(upath), true) } // httpRange specifies the byte range to be sent to the client. diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go index 74f7a80e27..e25a580b1f 100644 --- a/src/net/http/fs_test.go +++ b/src/net/http/fs_test.go @@ -24,7 +24,6 @@ import ( "reflect" "regexp" "runtime" - "sort" "strings" "testing" "time" @@ -420,46 +419,6 @@ func testFileServerImplicitLeadingSlash(t *testing.T, mode testMode) { } } -func TestFileServerMethodOptions(t *testing.T) { run(t, testFileServerMethodOptions) } -func testFileServerMethodOptions(t *testing.T, mode testMode) { - const want = "GET, HEAD, OPTIONS" - ts := newClientServerTest(t, mode, FileServer(Dir("."))).ts - - tests := []struct { - method string - wantStatus int - }{ - {MethodOptions, StatusOK}, - - {MethodDelete, StatusMethodNotAllowed}, - {MethodPut, StatusMethodNotAllowed}, - {MethodPost, StatusMethodNotAllowed}, - } - - for _, test := range tests { - req, err := NewRequest(test.method, ts.URL, nil) - if err != nil { - t.Fatal(err) - } - res, err := ts.Client().Do(req) - if err != nil { - t.Fatal(err) - } - defer res.Body.Close() - - if res.StatusCode != test.wantStatus { - t.Errorf("%s got status %q, want code %d", test.method, res.Status, test.wantStatus) - } - - a := strings.Split(res.Header.Get("Allow"), ", ") - sort.Strings(a) - got := strings.Join(a, ", ") - if got != want { - t.Errorf("%s got Allow header %q, want %q", test.method, got, want) - } - } -} - func TestDirJoin(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("skipping test on windows") |