net/http: add to deadlines only when positive

The existing implementation allows read / write deadlines to exist
in the past. This updates conditionals to only add to the deadline
when the value is positive.

Fixes: #39177
Change-Id: I841c30ba2849a337e7bc98c8aa136c4527c314ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/235437
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Damien Neil <dneil@google.com>
This commit is contained in:
Joshua Harshman 2020-05-27 09:54:41 -06:00 committed by Emmanuel Odeke
parent bdddfd10ec
commit f08c552dab

View file

@ -964,14 +964,14 @@ func (c *conn) readRequest(ctx context.Context) (w *response, err error) {
hdrDeadline time.Time // or zero if none hdrDeadline time.Time // or zero if none
) )
t0 := time.Now() t0 := time.Now()
if d := c.server.readHeaderTimeout(); d != 0 { if d := c.server.readHeaderTimeout(); d > 0 {
hdrDeadline = t0.Add(d) hdrDeadline = t0.Add(d)
} }
if d := c.server.ReadTimeout; d != 0 { if d := c.server.ReadTimeout; d > 0 {
wholeReqDeadline = t0.Add(d) wholeReqDeadline = t0.Add(d)
} }
c.rwc.SetReadDeadline(hdrDeadline) c.rwc.SetReadDeadline(hdrDeadline)
if d := c.server.WriteTimeout; d != 0 { if d := c.server.WriteTimeout; d > 0 {
defer func() { defer func() {
c.rwc.SetWriteDeadline(time.Now().Add(d)) c.rwc.SetWriteDeadline(time.Now().Add(d))
}() }()
@ -1831,10 +1831,10 @@ func (c *conn) serve(ctx context.Context) {
}() }()
if tlsConn, ok := c.rwc.(*tls.Conn); ok { if tlsConn, ok := c.rwc.(*tls.Conn); ok {
if d := c.server.ReadTimeout; d != 0 { if d := c.server.ReadTimeout; d > 0 {
c.rwc.SetReadDeadline(time.Now().Add(d)) c.rwc.SetReadDeadline(time.Now().Add(d))
} }
if d := c.server.WriteTimeout; d != 0 { if d := c.server.WriteTimeout; d > 0 {
c.rwc.SetWriteDeadline(time.Now().Add(d)) c.rwc.SetWriteDeadline(time.Now().Add(d))
} }
if err := tlsConn.HandshakeContext(ctx); err != nil { if err := tlsConn.HandshakeContext(ctx); err != nil {
@ -2567,7 +2567,8 @@ type Server struct {
TLSConfig *tls.Config TLSConfig *tls.Config
// ReadTimeout is the maximum duration for reading the entire // ReadTimeout is the maximum duration for reading the entire
// request, including the body. // request, including the body. A zero or negative value means
// there will be no timeout.
// //
// Because ReadTimeout does not let Handlers make per-request // Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or // decisions on each request body's acceptable deadline or
@ -2587,6 +2588,7 @@ type Server struct {
// writes of the response. It is reset whenever a new // writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not // request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis. // let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout time.Duration WriteTimeout time.Duration
// IdleTimeout is the maximum amount of time to wait for the // IdleTimeout is the maximum amount of time to wait for the