[go] net/http: rewind request body unconditionally

212 views
Skip to first unread message

Gobot Gobot (Gerrit)

unread,
Aug 27, 2018, 8:30:35 PM8/27/18
to Aleksandr Razumov, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
Within the next week or so, a maintainer will review your change and provide
feedback. See https://golang.org/doc/contribute.html#review for more info and
tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11, it means that this CL will be reviewed as part of the next development
cycle. See https://golang.org/s/release for more details.

View Change

    To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
    Gerrit-Change-Number: 131755
    Gerrit-PatchSet: 1
    Gerrit-Owner: Aleksandr Razumov <a...@cydev.ru>
    Gerrit-CC: Gobot Gobot <go...@golang.org>
    Gerrit-Comment-Date: Tue, 28 Aug 2018 00:30:32 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Aleksandr Razumov (Gerrit)

    unread,
    Aug 27, 2018, 9:47:43 PM8/27/18
    to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Aleksandr Razumov has uploaded this change for review.

    View Change

    net/http: rewind request body unconditionally

    When http2 fails with ErrNoCachedConn the request is retried with body
    that has already been read.

    Fixes #25009

    Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
    ---
    M src/net/http/transport.go
    M src/net/http/transport_internal_test.go
    2 files changed, 85 insertions(+), 3 deletions(-)

    diff --git a/src/net/http/transport.go b/src/net/http/transport.go
    index ffe4cdc..c534477 100644
    --- a/src/net/http/transport.go
    +++ b/src/net/http/transport.go
    @@ -477,9 +477,8 @@
    }
    testHookRoundTripRetried()

    - // Rewind the body if we're able to. (HTTP/2 does this itself so we only
    - // need to do it for HTTP/1.1 connections.)
    - if req.GetBody != nil && pconn.alt == nil {
    + // Rewind the body if we're able to.
    + if req.GetBody != nil {
    newReq := *req
    var err error
    newReq.Body, err = req.GetBody()
    diff --git a/src/net/http/transport_internal_test.go b/src/net/http/transport_internal_test.go
    index a5f29c9..92729e6 100644
    --- a/src/net/http/transport_internal_test.go
    +++ b/src/net/http/transport_internal_test.go
    @@ -7,8 +7,13 @@
    package http

    import (
    + "bytes"
    + "crypto/tls"
    "errors"
    + "io"
    + "io/ioutil"
    "net"
    + "net/http/internal"
    "strings"
    "testing"
    )
    @@ -178,3 +183,81 @@
    }
    }
    }
    +
    +type roundTripFunc func(r *Request) (*Response, error)
    +
    +func (f roundTripFunc) RoundTrip(r *Request) (*Response, error) {
    + return f(r)
    +}
    +
    +// Issue 25009
    +func TestTransportBodyAltRewind(t *testing.T) {
    + cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
    + if err != nil {
    + t.Fatal(err)
    + }
    + ln := newLocalListener(t)
    + defer ln.Close()
    +
    + go func() {
    + tln := tls.NewListener(ln, &tls.Config{
    + NextProtos: []string{"foo"},
    + Certificates: []tls.Certificate{cert},
    + })
    + for i := 0; i < 2; i++ {
    + sc, err := tln.Accept()
    + if err != nil {
    + t.Error(err)
    + return
    + }
    + if err := sc.(*tls.Conn).Handshake(); err != nil {
    + t.Error(err)
    + return
    + }
    + sc.Close()
    + }
    + }()
    +
    + addr := ln.Addr().String()
    + req, _ := NewRequest("POST", "https://example.org/", bytes.NewBufferString("request"))
    + roundTripped := false
    + tr := &Transport{
    + DisableKeepAlives: true,
    + TLSNextProto: map[string]func(string, *tls.Conn) RoundTripper{
    + "foo": func(authority string, c *tls.Conn) RoundTripper {
    + return roundTripFunc(func(r *Request) (*Response, error) {
    + n, _ := io.Copy(ioutil.Discard, r.Body)
    + if n == 0 {
    + t.Error("body length is zero")
    + }
    + if roundTripped {
    + return &Response{
    + Body: NoBody,
    + StatusCode: 200,
    + }, nil
    + }
    + roundTripped = true
    + return nil, http2noCachedConnError{}
    + })
    + },
    + },
    + DialTLS: func(_, _ string) (net.Conn, error) {
    + tc, err := tls.Dial("tcp", addr, &tls.Config{
    + InsecureSkipVerify: true,
    + NextProtos: []string{"foo"},
    + })
    + if err != nil {
    + return nil, err
    + }
    + if err := tc.Handshake(); err != nil {
    + return nil, err
    + }
    + return tc, nil
    + },
    + }
    + c := &Client{Transport: tr}
    + _, err = c.Do(req)
    + if err != nil {
    + t.Error(err)
    + }
    +}

    To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
    Gerrit-Change-Number: 131755
    Gerrit-PatchSet: 1
    Gerrit-Owner: Aleksandr Razumov <a...@cydev.ru>
    Gerrit-MessageType: newchange

    Iskander Sharipov (Gerrit)

    unread,
    Sep 6, 2018, 5:28:50 AM9/6/18
    to Aleksandr Razumov, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Ian Lance Taylor, Gobot Gobot, golang-co...@googlegroups.com

    Patch set 1:Run-TryBot +1

    View Change

      To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
      Gerrit-Change-Number: 131755
      Gerrit-PatchSet: 1
      Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
      Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-Comment-Date: Thu, 06 Sep 2018 09:28:46 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      Gobot Gobot (Gerrit)

      unread,
      Sep 6, 2018, 5:29:01 AM9/6/18
      to Aleksandr Razumov, goph...@pubsubhelper.golang.org, Iskander Sharipov, Brad Fitzpatrick, Ian Lance Taylor, golang-co...@googlegroups.com

      TryBots beginning. Status page: https://farmer.golang.org/try?commit=380055ba

      View Change

        To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
        Gerrit-Change-Number: 131755
        Gerrit-PatchSet: 1
        Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
        Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-Comment-Date: Thu, 06 Sep 2018 09:28:59 +0000

        Gobot Gobot (Gerrit)

        unread,
        Sep 6, 2018, 5:37:24 AM9/6/18
        to Aleksandr Razumov, goph...@pubsubhelper.golang.org, Iskander Sharipov, Brad Fitzpatrick, Ian Lance Taylor, golang-co...@googlegroups.com

        TryBots are happy.

        Patch set 1:TryBot-Result +1

        View Change

          To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
          Gerrit-Change-Number: 131755
          Gerrit-PatchSet: 1
          Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
          Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
          Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
          Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
          Gerrit-Comment-Date: Thu, 06 Sep 2018 09:37:21 +0000

          Aleksandr Razumov (Gerrit)

          unread,
          Sep 20, 2018, 5:29:57 PM9/20/18
          to goph...@pubsubhelper.golang.org, Gobot Gobot, Iskander Sharipov, Brad Fitzpatrick, Ian Lance Taylor, golang-co...@googlegroups.com

          ping

          View Change

            To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
            Gerrit-Change-Number: 131755
            Gerrit-PatchSet: 1
            Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
            Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
            Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
            Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
            Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
            Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
            Gerrit-Comment-Date: Thu, 20 Sep 2018 21:29:54 +0000

            Brad Fitzpatrick (Gerrit)

            unread,
            Sep 21, 2018, 4:31:23 AM9/21/18
            to Aleksandr Razumov, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Gobot Gobot, Iskander Sharipov, Ian Lance Taylor, golang-co...@googlegroups.com

            Sorry, you mailed this CL at the beginning of my month-long vacation. I'm back next week and will look at it then.

            View Change

              To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
              Gerrit-Change-Number: 131755
              Gerrit-PatchSet: 1
              Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
              Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
              Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
              Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
              Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
              Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
              Gerrit-Comment-Date: Fri, 21 Sep 2018 08:31:18 +0000

              Aleksandr Razumov (Gerrit)

              unread,
              Oct 1, 2018, 4:57:20 AM10/1/18
              to goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Gobot Gobot, Iskander Sharipov, Ian Lance Taylor, golang-co...@googlegroups.com

              gentle ping (Sorry for doing it again, I just really want that issue to be fixed ^^)

              View Change

                To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

                Gerrit-Project: go
                Gerrit-Branch: master
                Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
                Gerrit-Change-Number: 131755
                Gerrit-PatchSet: 1
                Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
                Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
                Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
                Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
                Gerrit-Comment-Date: Mon, 01 Oct 2018 08:57:17 +0000

                Brad Fitzpatrick (Gerrit)

                unread,
                Oct 1, 2018, 6:50:40 PM10/1/18
                to Aleksandr Razumov, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Gobot Gobot, Iskander Sharipov, Ian Lance Taylor, golang-co...@googlegroups.com

                This is still on my radar. I was out for a month and I'm still catching up. I'm sure this change fixes the bug; I just want to understand the bug first.

                View Change

                  To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

                  Gerrit-Project: go
                  Gerrit-Branch: master
                  Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
                  Gerrit-Change-Number: 131755
                  Gerrit-PatchSet: 1
                  Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
                  Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
                  Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
                  Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                  Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                  Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
                  Gerrit-Comment-Date: Mon, 01 Oct 2018 22:50:38 +0000

                  Aleksandr Razumov (Gerrit)

                  unread,
                  Oct 1, 2018, 11:14:19 PM10/1/18
                  to goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Gobot Gobot, Iskander Sharipov, Ian Lance Taylor, golang-co...@googlegroups.com

                  Patch Set 1:

                  This is still on my radar. I was out for a month and I'm still catching up. I'm sure this change fixes the bug; I just want to understand the bug first.

                  Please also see my comments on github issue: https://github.com/golang/go/issues/25009#issuecomment-415939211

                  I've tried to describe the bug there in more details. Sorry, forgot to reference it.
                  Feel free to ask anything regarding that bug here or on github issue, I'm still in context.

                  Also I'm sure that we can handle that case in more "intelligent" way (e.g. checking for "ErrNoCachedConn" explicitly instead of removing the "pconn.alt == nil" condition entirely), but from my point of view it is more error-prone.

                  View Change

                    To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

                    Gerrit-Project: go
                    Gerrit-Branch: master
                    Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
                    Gerrit-Change-Number: 131755
                    Gerrit-PatchSet: 1
                    Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
                    Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
                    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
                    Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                    Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
                    Gerrit-Comment-Date: Tue, 02 Oct 2018 03:14:16 +0000

                    Brad Fitzpatrick (Gerrit)

                    unread,
                    Oct 2, 2018, 5:11:21 PM10/2/18
                    to Aleksandr Razumov, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Gobot Gobot, Iskander Sharipov, Ian Lance Taylor, golang-co...@googlegroups.com

                    Patch set 1:Code-Review +2

                    View Change

                      To view, visit change 131755. To unsubscribe, or for help writing mail filters, visit settings.

                      Gerrit-Project: go
                      Gerrit-Branch: master
                      Gerrit-Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
                      Gerrit-Change-Number: 131755
                      Gerrit-PatchSet: 1
                      Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
                      Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
                      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
                      Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                      Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
                      Gerrit-Comment-Date: Tue, 02 Oct 2018 21:11:20 +0000

                      Brad Fitzpatrick (Gerrit)

                      unread,
                      Oct 2, 2018, 5:11:25 PM10/2/18
                      to Brad Fitzpatrick, Aleksandr Razumov, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gobot Gobot, Iskander Sharipov, Ian Lance Taylor, golang-co...@googlegroups.com

                      Brad Fitzpatrick merged this change.

                      View Change

                      Approvals: Brad Fitzpatrick: Looks good to me, approved Iskander Sharipov: Run TryBots Gobot Gobot: TryBots succeeded
                      net/http: rewind request body unconditionally

                      When http2 fails with ErrNoCachedConn the request is retried with body
                      that has already been read.

                      Fixes #25009

                      Change-Id: I51ed5c8cf469dd8b17c73fff6140ab80162bf267
                      Reviewed-on: https://go-review.googlesource.com/c/131755
                      Run-TryBot: Iskander Sharipov <iskander...@intel.com>
                      TryBot-Result: Gobot Gobot <go...@golang.org>
                      Reviewed-by: Brad Fitzpatrick <brad...@golang.org>

                      ---
                      M src/net/http/transport.go
                      M src/net/http/transport_internal_test.go
                      2 files changed, 85 insertions(+), 3 deletions(-)

                      diff --git a/src/net/http/transport.go b/src/net/http/transport.go
                      index 7f8fd50..e649303 100644
                      --- a/src/net/http/transport.go
                      +++ b/src/net/http/transport.go
                      @@ -478,9 +478,8 @@
                      Gerrit-PatchSet: 2
                      Gerrit-Owner: Aleksandr Razumov <a...@gortc.io>
                      Gerrit-Reviewer: Aleksandr Razumov <a...@gortc.io>
                      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
                      Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                      Gerrit-Reviewer: Iskander Sharipov <iskander...@intel.com>
                      Gerrit-MessageType: merged
                      Reply all
                      Reply to author
                      Forward
                      0 new messages