From cb733da42c582bfce9387a4811e8b757b058d1b6 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 7 Mar 2024 23:57:41 +0700 Subject: [PATCH] net/http: set Cache-Control header only if presents on error CL 544019 changes http.Error to remove misleading response headers. However, it also adds new "Cache-Control" header unconditionally, which may breaks existing clients out there, who do not expect to see the this header in the response like test in golang.org/x/net/http2. To keep thing backward compatible, http.Error should only add Cache-Control header if it has been presented. Updates #50905 Change-Id: I989e9f999a30ec170df4fb28905f50aed0267dad Reviewed-on: https://go-review.googlesource.com/c/go/+/569815 Reviewed-by: Damien Neil LUCI-TryBot-Result: Go LUCI Auto-Submit: Cuong Manh Le Reviewed-by: Bryan Mills --- src/net/http/server.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/net/http/server.go b/src/net/http/server.go index fa953d842e..d0e4dc69e5 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2181,8 +2181,10 @@ func Error(w ResponseWriter, error string, code int) { h.Del("Etag") h.Del("Last-Modified") // There might be cache control headers set for some other content, - // but we reset it to no-cache for the error content. - h.Set("Cache-Control", "no-cache") + // but we reset it to no-cache for the error content if presents. + if h.has("Cache-Control") { + h.Set("Cache-Control", "no-cache") + } // There might be content type already set, but we reset it to // text/plain for the error message. h.Set("Content-Type", "text/plain; charset=utf-8") -- 2.48.1