http3: automatically add content-length for small responses (#3989)

* response writer: add content-length automatically when response is small enough and doesn't call Flush

* fix comment

* add integration test

* Update http3/response_writer.go

* Update integrationtests/self/http_test.go

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
This commit is contained in:
WeidiDeng
2023-08-21 11:31:22 +08:00
committed by GitHub
parent ced65c0ddc
commit 824fd8a2f2
4 changed files with 126 additions and 32 deletions

View File

@@ -9,6 +9,7 @@ import (
"net"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
@@ -627,7 +628,12 @@ func (s *Server) handleRequest(conn quic.Connection, str quic.Stream, decoder *q
// only write response when there is no panic
if !panicked {
r.WriteHeader(http.StatusOK)
// response not written to the client yet, set Content-Length
if !r.written {
if _, haveCL := r.header["Content-Length"]; !haveCL {
r.header.Set("Content-Length", strconv.FormatInt(r.numWritten, 10))
}
}
r.Flush()
}
// If the EOF was read by the handler, CancelRead() is a no-op.