summaryrefslogtreecommitdiff
path: root/internal/http/headers.go
blob: d06d5845effe9a2e85c57498e0681ca3b766c606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package http

import (
	"net"
	"net/http"
)

// NormalizeHost strips the port from a Host header value when it is the
// default for the scheme (443 for HTTPS, 80 for HTTP), so that we never
// send "Host: example.com:443" to an upstream server.
func NormalizeHost(host string, tls bool) string {
	h, port, err := net.SplitHostPort(host)
	if err != nil {
		return host
	}
	if (tls && port == "443") || (!tls && port == "80") {
		return h
	}
	return host
}

// StripRequestCacheHeaders removes conditional-request headers so the
// upstream always returns a fresh response.
func StripRequestCacheHeaders(h http.Header) {
	h.Del("If-None-Match")
	h.Del("If-Modified-Since")
	h.Del("If-Range")
	h.Del("If-Match")
	h.Del("If-Unmodified-Since")
}

// StripResponseCacheHeaders removes caching directives from a response and
// sets no-store so the browser never serves a stale cached copy.
func StripResponseCacheHeaders(h http.Header) {
	h.Del("Cache-Control")
	h.Del("Expires")
	h.Del("ETag")
	h.Del("Last-Modified")
	h.Del("Pragma")
	h.Set("Cache-Control", "no-store, no-cache, must-revalidate")
}

// HopByHop is the set of headers that must not be forwarded to the upstream.
var HopByHop = map[string]bool{
	"Connection":          true,
	"Proxy-Connection":    true,
	"Keep-Alive":          true,
	"Proxy-Authenticate":  true,
	"Proxy-Authorization": true,
	"Te":                  true,
	"Trailers":            true,
	"Transfer-Encoding":   true,
	"Upgrade":             true,
}