-
Notifications
You must be signed in to change notification settings - Fork 225
/
hop_by_hop.go
54 lines (48 loc) · 1.32 KB
/
hop_by_hop.go
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 handlers
import (
"net/http"
"strings"
"code.cloudfoundry.org/gorouter/config"
"code.cloudfoundry.org/gorouter/logger"
)
type HopByHop struct {
cfg *config.Config
logger logger.Logger
}
// NewHopByHop creates a new handler that sanitizes hop-by-hop headers based on the HopByHopHeadersToFilter config
func NewHopByHop(cfg *config.Config, logger logger.Logger) *HopByHop {
return &HopByHop{
logger: logger,
cfg: cfg,
}
}
func (h *HopByHop) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
h.SanitizeRequestConnection(r)
next(rw, r)
}
func (h *HopByHop) SanitizeRequestConnection(r *http.Request) {
if len(h.cfg.HopByHopHeadersToFilter) == 0 {
return
}
connections := r.Header.Values("Connection")
for index, connection := range connections {
if connection != "" {
values := strings.Split(connection, ",")
connectionHeader := []string{}
for i := range values {
trimmedValue := strings.TrimSpace(values[i])
found := false
for _, item := range h.cfg.HopByHopHeadersToFilter {
if strings.EqualFold(item, trimmedValue) {
found = true
break
}
}
if !found {
connectionHeader = append(connectionHeader, trimmedValue)
}
}
r.Header[http.CanonicalHeaderKey("Connection")][index] = strings.Join(connectionHeader, ", ")
}
}
}