Skip to content

Commit b0ca456

Browse files
committed
internal/http3: fix Write in Server Handler returning the wrong value
When changing the approach used in go.dev/cl/746760 (i.e. adjusting slices, rather than keeping track of active part of the slices in Write), the return value was unfortunately not updated properly. This CL fixes the problem and modifies the test to also verify that Write returns the correct value. For golang/go#70914 Change-Id: Ic7dda43d47a85c74c03cf02208a0cd461b58dcf2 Reviewed-on: https://go-review.googlesource.com/c/net/+/748680 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Nicholas Husin <husin@google.com>
1 parent 1558ba7 commit b0ca456

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

internal/http3/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,12 @@ func (rw *responseWriter) Write(b []byte) (int, error) {
414414
// As a special case, we always want to save b to the buffer even when b is
415415
// big if we had yet to write our header, so we can infer headers like
416416
// "Content-Type" with as much information as possible.
417+
initialBLen := len(b)
417418
initialBufLen := len(rw.bb)
418419
if !rw.wroteHeader || len(b) <= cap(rw.bb)-len(rw.bb) {
419420
b = rw.bb.write(b)
420421
if len(b) == 0 {
421-
return len(b), nil
422+
return initialBLen, nil
422423
}
423424
}
424425

@@ -430,13 +431,13 @@ func (rw *responseWriter) Write(b []byte) (int, error) {
430431
// 3. Reset the current body buffer so it can be used again.
431432
rw.writeHeaderLockedOnce()
432433
if rw.cannotHaveBody {
433-
return len(b), nil
434+
return initialBLen, nil
434435
}
435436
if n, err := rw.bw.write(rw.bb, b); err != nil {
436437
return max(0, n-initialBufLen), err
437438
}
438439
rw.bb.discard()
439-
return len(b), nil
440+
return initialBLen, nil
440441
}
441442

442443
func (rw *responseWriter) Flush() {

internal/http3/server_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,13 @@ func TestServerBuffersBodyWrite(t *testing.T) {
755755
ts := newTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
756756
for n := 0; n < tt.bodyLen; n += tt.writeSize {
757757
data := slices.Repeat([]byte("a"), min(tt.writeSize, tt.bodyLen-n))
758-
w.Write(data)
758+
n, err := w.Write(data)
759+
if err != nil {
760+
t.Fatal(err)
761+
}
762+
if n != len(data) {
763+
t.Errorf("got %v bytes when writing in server handler, want %v", n, len(data))
764+
}
759765
if tt.flushes {
760766
w.(http.Flusher).Flush()
761767
}

0 commit comments

Comments
 (0)