[go] net/http: add MaxBytesError

127 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Nov 4, 2021, 2:54:39 PM11/4/21
to goph...@pubsubhelper.golang.org, Carl Johnson, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

net/http: add MaxBytesError

Fixes #30715

Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
GitHub-Last-Rev: fea999aefc925a71446214907bcf54641889f3c0
GitHub-Pull-Request: golang/go#49359
---
M src/net/http/request.go
M src/net/http/serve_test.go
2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/src/net/http/request.go b/src/net/http/request.go
index 76c2317..9ef3275 100644
--- a/src/net/http/request.go
+++ b/src/net/http/request.go
@@ -1119,7 +1119,7 @@
// MaxBytesReader is similar to io.LimitReader but is intended for
// limiting the size of incoming request bodies. In contrast to
// io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a
-// non-EOF error for a Read beyond the limit, and closes the
+// MaxBytesError for a Read beyond the limit, and closes the
// underlying reader when its Close method is called.
//
// MaxBytesReader prevents clients from accidentally or maliciously
@@ -1128,12 +1128,23 @@
if n < 0 { // Treat negative limits as equivalent to 0.
n = 0
}
- return &maxBytesReader{w: w, r: r, n: n}
+ return &maxBytesReader{w: w, r: r, i: n, n: n}
+}
+
+// MaxBytesError is returned by MaxBytesReader when its read limit is exceeded.
+type MaxBytesError struct {
+ Limit int64
+}
+
+func (e MaxBytesError) Error() string {
+ // Due to Hyrum's law, this text cannot be changed.
+ return "http: request body too large"
}

type maxBytesReader struct {
w ResponseWriter
r io.ReadCloser // underlying reader
+ i int64 // max bytes initially, for MaxBytesError
n int64 // max bytes remaining
err error // sticky error
}
@@ -1175,7 +1186,7 @@
if res, ok := l.w.(requestTooLarger); ok {
res.requestTooLarge()
}
- l.err = errors.New("http: request body too large")
+ l.err = MaxBytesError{l.i}
return n, l.err
}

diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index a98d6c3..3dab1b2 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -2978,6 +2978,13 @@
if n != limit {
t.Errorf("io.Copy = %d, want %d", n, limit)
}
+ mb, ok := err.(MaxBytesError)
+ if !ok {
+ t.Errorf("expected MaxBytesError, got %T", err)
+ }
+ if mb.Limit != limit {
+ t.Errorf("MaxBytesError.Limit = %d, want %d", mb.Limit, limit)
+ }
}))
defer cst.close()


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

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
Gerrit-Change-Number: 361397
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-MessageType: newchange

Carl Johnson (Gerrit)

unread,
Nov 4, 2021, 3:52:43 PM11/4/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Damien Neil, Russ Cox, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil.

View Change

1 comment:

  • File src/net/http/request.go:

    • Patch Set #1, Line 1139: func (e MaxBytesError) Error() string {

      Minor implementation question: Should this be a pointer?

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

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
Gerrit-Change-Number: 361397
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Comment-Date: Thu, 04 Nov 2021 19:52:38 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Damien Neil (Gerrit)

unread,
Nov 4, 2021, 5:01:31 PM11/4/21
to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Russ Cox, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Carl Johnson.

View Change

1 comment:

  • File src/net/http/request.go:

    • Patch Set #1, Line 1139: func (e MaxBytesError) Error() string {

      Minor implementation question: Should this be a pointer?

    • Yes, for general consistency.

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

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
Gerrit-Change-Number: 361397
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
Gerrit-Comment-Date: Thu, 04 Nov 2021 21:01:27 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Carl Johnson <m...@carlmjohnson.net>
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Nov 4, 2021, 5:44:44 PM11/4/21
to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Carl Johnson.

Gerrit Bot uploaded patch set #2 to this change.

View Change

net/http: add MaxBytesError

Fixes #30715

Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
GitHub-Last-Rev: 218d9ca608f44fa745a6f4209aa24fe7437b6437

GitHub-Pull-Request: golang/go#49359
---
M src/net/http/request.go
M src/net/http/serve_test.go
2 files changed, 34 insertions(+), 3 deletions(-)

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

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
Gerrit-Change-Number: 361397
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Carl Johnson <m...@carlmjohnson.net>
Gerrit-MessageType: newpatchset

Carl Johnson (Gerrit)

unread,
Nov 4, 2021, 5:45:16 PM11/4/21
to Gerrit Bot, goph...@pubsubhelper.golang.org, Damien Neil, Russ Cox, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Damien Neil.

View Change

1 comment:

  • File src/net/http/request.go:

    • Yes, for general consistency.

      Done

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

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
Gerrit-Change-Number: 361397
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Comment-Date: Thu, 04 Nov 2021 21:45:11 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Damien Neil <dn...@google.com>

Damien Neil (Gerrit)

unread,
Nov 5, 2021, 4:14:15 PM11/5/21
to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Russ Cox, Go Bot, golang-co...@googlegroups.com

Patch set 2:Run-TryBot +1Trust +1

View Change

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
    Gerrit-Change-Number: 361397
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
    Gerrit-CC: Go Bot <go...@golang.org>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Comment-Date: Fri, 05 Nov 2021 20:14:12 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Damien Neil (Gerrit)

    unread,
    Nov 5, 2021, 4:22:35 PM11/5/21
    to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Russ Cox, Go Bot, golang-co...@googlegroups.com

    View Change

    1 comment:

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
    Gerrit-Change-Number: 361397
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
    Gerrit-CC: Go Bot <go...@golang.org>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Comment-Date: Fri, 05 Nov 2021 20:22:32 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Gerrit Bot (Gerrit)

    unread,
    Nov 5, 2021, 8:00:45 PM11/5/21
    to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #3 to this change.

    View Change

    net/http: add MaxBytesError

    Fixes #30715

    Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
    GitHub-Last-Rev: fa2db73445e941bcf89e6f75bc0508d82d036738

    GitHub-Pull-Request: golang/go#49359
    ---
    M src/net/http/request.go
    M src/net/http/serve_test.go
    2 files changed, 34 insertions(+), 3 deletions(-)

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
    Gerrit-Change-Number: 361397
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-MessageType: newpatchset

    Carl Johnson (Gerrit)

    unread,
    Nov 5, 2021, 8:01:17 PM11/5/21
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Go Bot, Damien Neil, Russ Cox, golang-co...@googlegroups.com

    Attention is currently required from: Damien Neil.

    View Change

    1 comment:

    • File src/net/http/serve_test.go:

      • Done

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

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
    Gerrit-Change-Number: 361397
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Comment-Date: Sat, 06 Nov 2021 00:01:11 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Damien Neil <dn...@google.com>
    Gerrit-MessageType: comment

    Damien Neil (Gerrit)

    unread,
    Nov 9, 2021, 1:21:59 PM11/9/21
    to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Go Bot, Russ Cox, golang-co...@googlegroups.com

    Patch set 3:Run-TryBot +1Trust +1

    View Change

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

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
      Gerrit-Change-Number: 361397
      Gerrit-PatchSet: 3
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
      Gerrit-CC: Russ Cox <r...@golang.org>
      Gerrit-Comment-Date: Tue, 09 Nov 2021 18:21:54 +0000

      Damien Neil (Gerrit)

      unread,
      Nov 9, 2021, 2:07:22 PM11/9/21
      to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Go Bot, Russ Cox, golang-co...@googlegroups.com

      Patch set 3:Code-Review +2

      View Change

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

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
        Gerrit-Change-Number: 361397
        Gerrit-PatchSet: 3
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Go Bot <go...@golang.org>
        Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
        Gerrit-CC: Russ Cox <r...@golang.org>
        Gerrit-Comment-Date: Tue, 09 Nov 2021 19:07:17 +0000

        Ian Lance Taylor (Gerrit)

        unread,
        Nov 9, 2021, 2:13:52 PM11/9/21
        to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Damien Neil, Go Bot, Russ Cox, golang-co...@googlegroups.com

        Patch set 3:Trust +1

        View Change

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

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
          Gerrit-Change-Number: 361397
          Gerrit-PatchSet: 3
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Go Bot <go...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
          Gerrit-CC: Russ Cox <r...@golang.org>
          Gerrit-Comment-Date: Tue, 09 Nov 2021 19:13:48 +0000

          Gerrit Bot (Gerrit)

          unread,
          Mar 28, 2022, 12:54:55 PM3/28/22
          to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

          Gerrit Bot uploaded patch set #4 to this change.

          View Change

          net/http: add MaxBytesError

          Fixes #30715

          Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
          GitHub-Last-Rev: cd6ff5d35ac2f42ca24a1de01d7f303532b05287
          GitHub-Pull-Request: golang/go#49359
          ---
          A api/next/30715.txt
          M src/net/http/request.go
          M src/net/http/serve_test.go
          3 files changed, 37 insertions(+), 3 deletions(-)

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

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
          Gerrit-Change-Number: 361397
          Gerrit-PatchSet: 4
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
          Gerrit-CC: Russ Cox <r...@golang.org>
          Gerrit-MessageType: newpatchset

          Gerrit Bot (Gerrit)

          unread,
          Mar 28, 2022, 1:02:52 PM3/28/22
          to Carl Johnson, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

          Gerrit Bot uploaded patch set #5 to this change.

          View Change

          net/http: add MaxBytesError

          Fixes #30715

          Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
          GitHub-Last-Rev: 6ae68402a5a7c57f7f18e945d48c69ba2b134078

          GitHub-Pull-Request: golang/go#49359
          ---
          A api/next/30715.txt
          M src/net/http/request.go
          M src/net/http/serve_test.go
          3 files changed, 40 insertions(+), 4 deletions(-)

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

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
          Gerrit-Change-Number: 361397
          Gerrit-PatchSet: 5

          Carl Johnson (Gerrit)

          unread,
          Apr 25, 2022, 12:15:55 PM4/25/22
          to Gerrit Bot, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Damien Neil, Gopher Robot, Russ Cox, golang-co...@googlegroups.com

          View Change

          1 comment:

          • Patchset:

            • Patch Set #5:

              TryBots are happy. Can this get a review and merged before the quiet week and code freeze?

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

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
          Gerrit-Change-Number: 361397
          Gerrit-PatchSet: 5
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
          Gerrit-CC: Russ Cox <r...@golang.org>
          Gerrit-Comment-Date: Mon, 25 Apr 2022 16:15:50 +0000
          Gerrit-HasComments: Yes
          Gerrit-Has-Labels: No
          Gerrit-MessageType: comment

          Ian Lance Taylor (Gerrit)

          unread,
          Apr 25, 2022, 7:23:14 PM4/25/22
          to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Damien Neil, Gopher Robot, Russ Cox, golang-co...@googlegroups.com

          Patch set 5:Run-TryBot +1Auto-Submit +1Code-Review +1

          View Change

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

            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
            Gerrit-Change-Number: 361397
            Gerrit-PatchSet: 5
            Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
            Gerrit-Reviewer: Damien Neil <dn...@google.com>
            Gerrit-Reviewer: Gopher Robot <go...@golang.org>
            Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
            Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
            Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
            Gerrit-CC: Russ Cox <r...@golang.org>
            Gerrit-Comment-Date: Mon, 25 Apr 2022 23:23:10 +0000

            Damien Neil (Gerrit)

            unread,
            Apr 25, 2022, 7:27:49 PM4/25/22
            to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, Russ Cox, golang-co...@googlegroups.com

            Patch set 5:Code-Review +2

            View Change

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

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
              Gerrit-Change-Number: 361397
              Gerrit-PatchSet: 5
              Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
              Gerrit-Reviewer: Damien Neil <dn...@google.com>
              Gerrit-Reviewer: Gopher Robot <go...@golang.org>
              Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
              Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
              Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
              Gerrit-CC: Russ Cox <r...@golang.org>
              Gerrit-Comment-Date: Mon, 25 Apr 2022 23:27:45 +0000

              Gopher Robot (Gerrit)

              unread,
              Apr 25, 2022, 7:36:55 PM4/25/22
              to Gerrit Bot, Carl Johnson, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Ian Lance Taylor, Damien Neil, Russ Cox, golang-co...@googlegroups.com

              Gopher Robot submitted this change.

              View Change


              Approvals: Damien Neil: Looks good to me, approved Ian Lance Taylor: Looks good to me, but someone else must approve; Run TryBots; Automatically submit change Gopher Robot: TryBots succeeded
              net/http: add MaxBytesError

              Fixes #30715

              Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
              GitHub-Last-Rev: 6ae68402a5a7c57f7f18e945d48c69ba2b134078
              GitHub-Pull-Request: golang/go#49359
              Reviewed-on: https://go-review.googlesource.com/c/go/+/361397
              Reviewed-by: Ian Lance Taylor <ia...@google.com>
              Run-TryBot: Ian Lance Taylor <ia...@google.com>
              Auto-Submit: Ian Lance Taylor <ia...@google.com>
              Reviewed-by: Damien Neil <dn...@google.com>
              TryBot-Result: Gopher Robot <go...@golang.org>

              ---
              A api/next/30715.txt
              M src/net/http/request.go
              M src/net/http/serve_test.go
              3 files changed, 46 insertions(+), 4 deletions(-)

              diff --git a/api/next/30715.txt b/api/next/30715.txt
              new file mode 100644
              index 0000000..077a8d1
              --- /dev/null
              +++ b/api/next/30715.txt
              @@ -0,0 +1,3 @@
              +pkg net/http, type MaxBytesError struct #30715
              +pkg net/http, type MaxBytesError struct, Limit int64 #30715
              +pkg net/http, method (*MaxBytesError) Error() string #30715
              diff --git a/src/net/http/request.go b/src/net/http/request.go
              index 3122119..d091f3c 100644
              --- a/src/net/http/request.go
              +++ b/src/net/http/request.go
              @@ -1126,21 +1126,34 @@

              // MaxBytesReader is similar to io.LimitReader but is intended for
              // limiting the size of incoming request bodies. In contrast to
              // io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a
              -// non-EOF error for a Read beyond the limit, and closes the
              +// MaxBytesError for a Read beyond the limit, and closes the
              // underlying reader when its Close method is called.
              //
              // MaxBytesReader prevents clients from accidentally or maliciously
              -// sending a large request and wasting server resources.
              +// sending a large request and wasting server resources. If possible,
              +// it tells the ResponseWriter to close the connection after the limit
              +// has been reached.
              func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser {

              if n < 0 { // Treat negative limits as equivalent to 0.
              n = 0
              }
              - return &maxBytesReader{w: w, r: r, n: n}
              + return &maxBytesReader{w: w, r: r, i: n, n: n}
              +}
              +
              +// MaxBytesError is returned by MaxBytesReader when its read limit is exceeded.
              +type MaxBytesError struct {
              + Limit int64
              +}
              +
              +func (e *MaxBytesError) Error() string {

              + // Due to Hyrum's law, this text cannot be changed.
              + return "http: request body too large"
              }

              type maxBytesReader struct {
              w ResponseWriter
              r io.ReadCloser // underlying reader
              + i int64 // max bytes initially, for MaxBytesError
              n int64 // max bytes remaining
              err error // sticky error
              }
              @@ -1182,7 +1195,7 @@

              if res, ok := l.w.(requestTooLarger); ok {
              res.requestTooLarge()
              }
              - l.err = errors.New("http: request body too large")
              +	l.err = &MaxBytesError{l.i}

              return n, l.err
              }

              diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
              index 1c85a66..404cca0 100644
              --- a/src/net/http/serve_test.go
              +++ b/src/net/http/serve_test.go
              @@ -3035,6 +3035,13 @@

              if n != limit {
              t.Errorf("io.Copy = %d, want %d", n, limit)
              }
              +		mbErr, ok := err.(*MaxBytesError)

              + if !ok {
              + t.Errorf("expected MaxBytesError, got %T", err)
              + }
              +		if mbErr.Limit != limit {
              + t.Errorf("MaxBytesError.Limit = %d, want %d", mbErr.Limit, limit)
              + }
              }))
              defer cst.close()


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

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-Change-Id: Ia3712d248b6dc86abef71ccea6e705a571933d53
              Gerrit-Change-Number: 361397
              Gerrit-PatchSet: 6
              Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
              Gerrit-Reviewer: Damien Neil <dn...@google.com>
              Gerrit-Reviewer: Gopher Robot <go...@golang.org>
              Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
              Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
              Gerrit-CC: Carl Johnson <m...@carlmjohnson.net>
              Gerrit-CC: Russ Cox <r...@golang.org>
              Gerrit-MessageType: merged
              Reply all
              Reply to author
              Forward
              0 new messages