2021-07-30 10:20:07 +00:00
|
|
|
package connectionheader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/textproto"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/http/httpguts"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
connectionHeader = "Connection"
|
|
|
|
upgradeHeader = "Upgrade"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Remover removes hop-by-hop headers listed in the "Connection" header.
|
|
|
|
// See RFC 7230, section 6.1.
|
|
|
|
func Remover(next http.Handler) http.HandlerFunc {
|
|
|
|
return func(rw http.ResponseWriter, req *http.Request) {
|
2024-01-08 08:10:06 +00:00
|
|
|
next.ServeHTTP(rw, Remove(req))
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 10:20:07 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
// Remove removes hop-by-hop header on the request.
|
|
|
|
func Remove(req *http.Request) *http.Request {
|
|
|
|
var reqUpType string
|
|
|
|
if httpguts.HeaderValuesContainsToken(req.Header[connectionHeader], upgradeHeader) {
|
|
|
|
reqUpType = req.Header.Get(upgradeHeader)
|
|
|
|
}
|
2021-07-30 10:20:07 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
removeConnectionHeaders(req.Header)
|
2021-07-30 10:20:07 +00:00
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
if reqUpType != "" {
|
|
|
|
req.Header.Set(connectionHeader, upgradeHeader)
|
|
|
|
req.Header.Set(upgradeHeader, reqUpType)
|
|
|
|
} else {
|
|
|
|
req.Header.Del(connectionHeader)
|
2021-07-30 10:20:07 +00:00
|
|
|
}
|
2024-01-08 08:10:06 +00:00
|
|
|
|
|
|
|
return req
|
2021-07-30 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeConnectionHeaders(h http.Header) {
|
|
|
|
for _, f := range h[connectionHeader] {
|
|
|
|
for _, sf := range strings.Split(f, ",") {
|
|
|
|
if sf = textproto.TrimString(sf); sf != "" {
|
|
|
|
h.Del(sf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|