diff options
Diffstat (limited to 'internal/http/headers.go')
| -rw-r--r-- | internal/http/headers.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/http/headers.go b/internal/http/headers.go new file mode 100644 index 0000000..d06d584 --- /dev/null +++ b/internal/http/headers.go @@ -0,0 +1,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, +} |
