diff options
| author | Mitar <mitar.git@tnode.com> | 2024-03-05 23:45:51 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-03-06 19:15:50 +0000 |
| commit | d463de61b38cc037808970a8aeed8255082a1df5 (patch) | |
| tree | cdb19e1861072e98074a96530b4ac5c67f3495df /src/net/http/server.go | |
| parent | 25af691c43efdc4ff15e3b5355d1f2638bd67e0a (diff) | |
| download | go-d463de61b38cc037808970a8aeed8255082a1df5.tar.xz | |
net/http: remove misleading response headers on error
ServeContent API is to set some headers you want to see in the response
before calling ServeContent. But if there is an error, those headers
should be removed otherwise they might confused the client.
Removing those headers is useful in general in the case of an error,
so we remove them in http.Error.
Fixes #50905.
Change-Id: If8d2786c1512906ac93e6b388df6ab1c5ecd1ea9
GitHub-Last-Rev: 32b6f045a791cf7bc391f018452a05cc872041ba
GitHub-Pull-Request: golang/go#64312
Reviewed-on: https://go-review.googlesource.com/c/go/+/544019
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/net/http/server.go')
| -rw-r--r-- | src/net/http/server.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/net/http/server.go b/src/net/http/server.go index b0a2a1d888..fa953d842e 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -2173,9 +2173,20 @@ func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { // writes are done to w. // The error message should be plain text. func Error(w ResponseWriter, error string, code int) { - w.Header().Del("Content-Length") - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - w.Header().Set("X-Content-Type-Options", "nosniff") + h := w.Header() + // We delete headers which might be valid for some other content, + // but not anymore for the error content. + h.Del("Content-Length") + h.Del("Content-Encoding") + 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") + // 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") + h.Set("X-Content-Type-Options", "nosniff") w.WriteHeader(code) fmt.Fprintln(w, error) } |
