aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2023-07-24 16:19:49 +0000
committerDavid Chase <drchase@google.com>2023-08-04 16:24:02 +0000
commite973d242614d30b7a3aaf8f65d279b062976fa9f (patch)
tree46d7d63719163de16c3021c2ea9b4b9de72aba93
parent2e6276df34f0d59924113548fe00fd33d5286bf4 (diff)
downloadgo-e973d242614d30b7a3aaf8f65d279b062976fa9f.tar.gz
go-e973d242614d30b7a3aaf8f65d279b062976fa9f.zip
[release-branch.go1.21] Revert "net/http: use Copy in ServeContent if CopyN not needed"
This reverts CL 446276. Reason for revert: Causing surprising performance regression. Fixes #61530 Change-Id: Ic970f2e05d875b606ce274ea621f7e4c8c337481 Reviewed-on: https://go-review.googlesource.com/c/go/+/512615 Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit df0a1297899aff1c46b66523e75aa12b0ff5049f) Reviewed-on: https://go-review.googlesource.com/c/go/+/515795 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Damien Neil <dneil@google.com>
-rw-r--r--src/net/http/fs.go9
-rw-r--r--src/net/http/fs_test.go43
2 files changed, 4 insertions, 48 deletions
diff --git a/src/net/http/fs.go b/src/net/http/fs.go
index 55094400ac..41e0b43ac8 100644
--- a/src/net/http/fs.go
+++ b/src/net/http/fs.go
@@ -349,13 +349,8 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
w.WriteHeader(code)
- if r.Method != MethodHead {
- if sendSize == size {
- // use Copy in the non-range case to make use of WriterTo if available
- io.Copy(w, sendContent)
- } else {
- io.CopyN(w, sendContent, sendSize)
- }
+ if r.Method != "HEAD" {
+ io.CopyN(w, sendContent, sendSize)
}
}
diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go
index e37e0f04c9..3fb9e01235 100644
--- a/src/net/http/fs_test.go
+++ b/src/net/http/fs_test.go
@@ -924,7 +924,6 @@ func testServeContent(t *testing.T, mode testMode) {
wantContentType string
wantContentRange string
wantStatus int
- wantContent []byte
}
htmlModTime := mustStat(t, "testdata/index.html").ModTime()
tests := map[string]testCase{
@@ -1140,24 +1139,6 @@ func testServeContent(t *testing.T, mode testMode) {
wantStatus: 412,
wantLastMod: htmlModTime.UTC().Format(TimeFormat),
},
- "uses_writeTo_if_available_and_non-range": {
- content: &panicOnNonWriterTo{seekWriterTo: strings.NewReader("foobar")},
- serveContentType: "text/plain; charset=utf-8",
- wantContentType: "text/plain; charset=utf-8",
- wantStatus: StatusOK,
- wantContent: []byte("foobar"),
- },
- "do_not_use_writeTo_for_range_requests": {
- content: &panicOnWriterTo{ReadSeeker: strings.NewReader("foobar")},
- serveContentType: "text/plain; charset=utf-8",
- reqHeader: map[string]string{
- "Range": "bytes=0-4",
- },
- wantContentType: "text/plain; charset=utf-8",
- wantContentRange: "bytes 0-4/6",
- wantStatus: StatusPartialContent,
- wantContent: []byte("fooba"),
- },
}
for testName, tt := range tests {
var content io.ReadSeeker
@@ -1171,8 +1152,7 @@ func testServeContent(t *testing.T, mode testMode) {
} else {
content = tt.content
}
- contentOut := &strings.Builder{}
- for _, method := range []string{MethodGet, MethodHead} {
+ for _, method := range []string{"GET", "HEAD"} {
//restore content in case it is consumed by previous method
if content, ok := content.(*strings.Reader); ok {
content.Seek(0, io.SeekStart)
@@ -1198,8 +1178,7 @@ func testServeContent(t *testing.T, mode testMode) {
if err != nil {
t.Fatal(err)
}
- contentOut.Reset()
- io.Copy(contentOut, res.Body)
+ io.Copy(io.Discard, res.Body)
res.Body.Close()
if res.StatusCode != tt.wantStatus {
t.Errorf("test %q using %q: got status = %d; want %d", testName, method, res.StatusCode, tt.wantStatus)
@@ -1213,28 +1192,10 @@ func testServeContent(t *testing.T, mode testMode) {
if g, e := res.Header.Get("Last-Modified"), tt.wantLastMod; g != e {
t.Errorf("test %q using %q: got last-modified = %q, want %q", testName, method, g, e)
}
- if g, e := contentOut.String(), tt.wantContent; e != nil && method == MethodGet && g != string(e) {
- t.Errorf("test %q using %q: got unexpected content %q, want %q", testName, method, g, e)
- }
}
}
}
-type seekWriterTo interface {
- io.Seeker
- io.WriterTo
-}
-
-type panicOnNonWriterTo struct {
- io.Reader
- seekWriterTo
-}
-
-type panicOnWriterTo struct {
- io.ReadSeeker
- io.WriterTo
-}
-
// Issue 12991
func TestServerFileStatError(t *testing.T) {
rec := httptest.NewRecorder()