Skip to content

Commit e973d24

Browse files
neilddr2chase
authored andcommitted
[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 <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> (cherry picked from commit df0a129) Reviewed-on: https://go-review.googlesource.com/c/go/+/515795 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 2e6276d commit e973d24

File tree

2 files changed

+4
-48
lines changed

2 files changed

+4
-48
lines changed

src/net/http/fs.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,8 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
349349

350350
w.WriteHeader(code)
351351

352-
if r.Method != MethodHead {
353-
if sendSize == size {
354-
// use Copy in the non-range case to make use of WriterTo if available
355-
io.Copy(w, sendContent)
356-
} else {
357-
io.CopyN(w, sendContent, sendSize)
358-
}
352+
if r.Method != "HEAD" {
353+
io.CopyN(w, sendContent, sendSize)
359354
}
360355
}
361356

src/net/http/fs_test.go

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,6 @@ func testServeContent(t *testing.T, mode testMode) {
924924
wantContentType string
925925
wantContentRange string
926926
wantStatus int
927-
wantContent []byte
928927
}
929928
htmlModTime := mustStat(t, "testdata/index.html").ModTime()
930929
tests := map[string]testCase{
@@ -1140,24 +1139,6 @@ func testServeContent(t *testing.T, mode testMode) {
11401139
wantStatus: 412,
11411140
wantLastMod: htmlModTime.UTC().Format(TimeFormat),
11421141
},
1143-
"uses_writeTo_if_available_and_non-range": {
1144-
content: &panicOnNonWriterTo{seekWriterTo: strings.NewReader("foobar")},
1145-
serveContentType: "text/plain; charset=utf-8",
1146-
wantContentType: "text/plain; charset=utf-8",
1147-
wantStatus: StatusOK,
1148-
wantContent: []byte("foobar"),
1149-
},
1150-
"do_not_use_writeTo_for_range_requests": {
1151-
content: &panicOnWriterTo{ReadSeeker: strings.NewReader("foobar")},
1152-
serveContentType: "text/plain; charset=utf-8",
1153-
reqHeader: map[string]string{
1154-
"Range": "bytes=0-4",
1155-
},
1156-
wantContentType: "text/plain; charset=utf-8",
1157-
wantContentRange: "bytes 0-4/6",
1158-
wantStatus: StatusPartialContent,
1159-
wantContent: []byte("fooba"),
1160-
},
11611142
}
11621143
for testName, tt := range tests {
11631144
var content io.ReadSeeker
@@ -1171,8 +1152,7 @@ func testServeContent(t *testing.T, mode testMode) {
11711152
} else {
11721153
content = tt.content
11731154
}
1174-
contentOut := &strings.Builder{}
1175-
for _, method := range []string{MethodGet, MethodHead} {
1155+
for _, method := range []string{"GET", "HEAD"} {
11761156
//restore content in case it is consumed by previous method
11771157
if content, ok := content.(*strings.Reader); ok {
11781158
content.Seek(0, io.SeekStart)
@@ -1198,8 +1178,7 @@ func testServeContent(t *testing.T, mode testMode) {
11981178
if err != nil {
11991179
t.Fatal(err)
12001180
}
1201-
contentOut.Reset()
1202-
io.Copy(contentOut, res.Body)
1181+
io.Copy(io.Discard, res.Body)
12031182
res.Body.Close()
12041183
if res.StatusCode != tt.wantStatus {
12051184
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) {
12131192
if g, e := res.Header.Get("Last-Modified"), tt.wantLastMod; g != e {
12141193
t.Errorf("test %q using %q: got last-modified = %q, want %q", testName, method, g, e)
12151194
}
1216-
if g, e := contentOut.String(), tt.wantContent; e != nil && method == MethodGet && g != string(e) {
1217-
t.Errorf("test %q using %q: got unexpected content %q, want %q", testName, method, g, e)
1218-
}
12191195
}
12201196
}
12211197
}
12221198

1223-
type seekWriterTo interface {
1224-
io.Seeker
1225-
io.WriterTo
1226-
}
1227-
1228-
type panicOnNonWriterTo struct {
1229-
io.Reader
1230-
seekWriterTo
1231-
}
1232-
1233-
type panicOnWriterTo struct {
1234-
io.ReadSeeker
1235-
io.WriterTo
1236-
}
1237-
12381199
// Issue 12991
12391200
func TestServerFileStatError(t *testing.T) {
12401201
rec := httptest.NewRecorder()

0 commit comments

Comments
 (0)