Skip to content

Commit 07104bd

Browse files
neildbradfitz
authored andcommitted
[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 golang#53501 For golang#59375 Fixes golang#59469 Change-Id: Ic3f63b378f9c819599b32e5e6e410f6163849317 Reviewed-on: https://go-review.googlesource.com/c/go/+/482635 Reviewed-by: Tatiana Bradley <[email protected]> Run-TryBot: Damien Neil <[email protected]> TryBot-Result: Gopher Robot <[email protected]> (cherry picked from commit c02fa75) Reviewed-on: https://go-review.googlesource.com/c/go/+/488635 Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 6718616 commit 07104bd

File tree

2 files changed

+5
-56
lines changed

2 files changed

+5
-56
lines changed

src/net/http/fs.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -858,22 +858,12 @@ func FileServer(root FileSystem) Handler {
858858
}
859859

860860
func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
861-
const options = MethodOptions + ", " + MethodGet + ", " + MethodHead
862-
863-
switch r.Method {
864-
case MethodGet, MethodHead:
865-
if !strings.HasPrefix(r.URL.Path, "/") {
866-
r.URL.Path = "/" + r.URL.Path
867-
}
868-
serveFile(w, r, f.root, path.Clean(r.URL.Path), true)
869-
870-
case MethodOptions:
871-
w.Header().Set("Allow", options)
872-
873-
default:
874-
w.Header().Set("Allow", options)
875-
Error(w, "read-only", StatusMethodNotAllowed)
861+
upath := r.URL.Path
862+
if !strings.HasPrefix(upath, "/") {
863+
upath = "/" + upath
864+
r.URL.Path = upath
876865
}
866+
serveFile(w, r, f.root, path.Clean(upath), true)
877867
}
878868

879869
// httpRange specifies the byte range to be sent to the client.

src/net/http/fs_test.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"reflect"
2525
"regexp"
2626
"runtime"
27-
"sort"
2827
"strings"
2928
"testing"
3029
"time"
@@ -420,46 +419,6 @@ func testFileServerImplicitLeadingSlash(t *testing.T, mode testMode) {
420419
}
421420
}
422421

423-
func TestFileServerMethodOptions(t *testing.T) { run(t, testFileServerMethodOptions) }
424-
func testFileServerMethodOptions(t *testing.T, mode testMode) {
425-
const want = "GET, HEAD, OPTIONS"
426-
ts := newClientServerTest(t, mode, FileServer(Dir("."))).ts
427-
428-
tests := []struct {
429-
method string
430-
wantStatus int
431-
}{
432-
{MethodOptions, StatusOK},
433-
434-
{MethodDelete, StatusMethodNotAllowed},
435-
{MethodPut, StatusMethodNotAllowed},
436-
{MethodPost, StatusMethodNotAllowed},
437-
}
438-
439-
for _, test := range tests {
440-
req, err := NewRequest(test.method, ts.URL, nil)
441-
if err != nil {
442-
t.Fatal(err)
443-
}
444-
res, err := ts.Client().Do(req)
445-
if err != nil {
446-
t.Fatal(err)
447-
}
448-
defer res.Body.Close()
449-
450-
if res.StatusCode != test.wantStatus {
451-
t.Errorf("%s got status %q, want code %d", test.method, res.Status, test.wantStatus)
452-
}
453-
454-
a := strings.Split(res.Header.Get("Allow"), ", ")
455-
sort.Strings(a)
456-
got := strings.Join(a, ", ")
457-
if got != want {
458-
t.Errorf("%s got Allow header %q, want %q", test.method, got, want)
459-
}
460-
}
461-
}
462-
463422
func TestDirJoin(t *testing.T) {
464423
if runtime.GOOS == "windows" {
465424
t.Skip("skipping test on windows")

0 commit comments

Comments
 (0)