aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas S. Husin <nsh@golang.org>2026-04-01 17:15:26 -0400
committerNicholas Husin <nsh@golang.org>2026-04-08 14:09:51 -0700
commite67d773034fde21e6f726c4add5eeba5882198ae (patch)
tree5f897f98bfb679c56749a73d24dff759bf821724
parentc0598ed41b610ebb2ba171c3b594ecf72bcb1a00 (diff)
downloadgo-e67d773034fde21e6f726c4add5eeba5882198ae.tar.xz
net/http/internal/http2: prevent alloc when writing status code for responses
Previously, writing responses with non-200 and non-404 status code requires an allocation. Now that CL 762040 prevents header names and values from escaping, we can modify writeFrame to not allocate status codes on the heap. Change-Id: I230bed1b83627c1fb389c0507106d8e16a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/762140 Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
-rw-r--r--src/net/http/internal/http2/http2.go11
-rw-r--r--src/net/http/internal/http2/write.go7
2 files changed, 5 insertions, 13 deletions
diff --git a/src/net/http/internal/http2/http2.go b/src/net/http/internal/http2/http2.go
index 86c28e888b..9e4ef7c8e7 100644
--- a/src/net/http/internal/http2/http2.go
+++ b/src/net/http/internal/http2/http2.go
@@ -21,7 +21,6 @@ import (
"net"
"os"
"slices"
- "strconv"
"strings"
"sync"
"time"
@@ -215,16 +214,6 @@ func validWireHeaderFieldName(v string) bool {
return true
}
-func httpCodeString(code int) string {
- switch code {
- case 200:
- return "200"
- case 404:
- return "404"
- }
- return strconv.Itoa(code)
-}
-
// A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
type closeWaiter chan struct{}
diff --git a/src/net/http/internal/http2/write.go b/src/net/http/internal/http2/write.go
index 59c4e625a5..5e6f810c41 100644
--- a/src/net/http/internal/http2/write.go
+++ b/src/net/http/internal/http2/write.go
@@ -10,6 +10,8 @@ import (
"log"
"net/http/internal/httpcommon"
"net/url"
+ "strconv"
+ "strings"
"golang.org/x/net/http/httpguts"
"golang.org/x/net/http2/hpack"
@@ -200,7 +202,7 @@ type writeResHeaders struct {
func encKV(enc *hpack.Encoder, k, v string) {
if VerboseLogs {
- log.Printf("http2: server encoding header %q = %q", k, v)
+ log.Printf("http2: server encoding header %q = %q", strings.Clone(k), strings.Clone(v))
}
enc.WriteField(hpack.HeaderField{Name: k, Value: v})
}
@@ -221,7 +223,8 @@ func (w *writeResHeaders) writeFrame(ctx writeContext) error {
buf.Reset()
if w.httpResCode != 0 {
- encKV(enc, ":status", httpCodeString(w.httpResCode))
+ codeBuf := strconv.AppendInt(make([]byte, 0, 3), int64(w.httpResCode), 10)
+ encKV(enc, ":status", string(codeBuf))
}
encodeHeaders(enc, w.h, w.trailers)