From: Joshua Harshman Date: Wed, 27 May 2020 15:54:41 +0000 (-0600) Subject: net/http: add to deadlines only when positive X-Git-Tag: go1.17beta1~616 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f08c552dabf4a75739e0f198804ae14368d31105;p=gostls13.git 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 Reviewed-by: Damien Neil Run-TryBot: Emmanuel Odeke TryBot-Result: Go Bot Trust: Damien Neil --- diff --git a/src/net/http/server.go b/src/net/http/server.go index f095b7edd2..d90418b56d 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -964,14 +964,14 @@ func (c *conn) readRequest(ctx context.Context) (w *response, err error) { hdrDeadline time.Time // or zero if none ) t0 := time.Now() - if d := c.server.readHeaderTimeout(); d != 0 { + if d := c.server.readHeaderTimeout(); d > 0 { hdrDeadline = t0.Add(d) } - if d := c.server.ReadTimeout; d != 0 { + if d := c.server.ReadTimeout; d > 0 { wholeReqDeadline = t0.Add(d) } c.rwc.SetReadDeadline(hdrDeadline) - if d := c.server.WriteTimeout; d != 0 { + if d := c.server.WriteTimeout; d > 0 { defer func() { 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 d := c.server.ReadTimeout; d != 0 { + if d := c.server.ReadTimeout; d > 0 { 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)) } if err := tlsConn.HandshakeContext(ctx); err != nil { @@ -2567,7 +2567,8 @@ type Server struct { TLSConfig *tls.Config // 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 // 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 // request's header is read. Like ReadTimeout, it does not // let Handlers make decisions on a per-request basis. + // A zero or negative value means there will be no timeout. WriteTimeout time.Duration // IdleTimeout is the maximum amount of time to wait for the