-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoverequestheaders.go
More file actions
35 lines (31 loc) · 912 Bytes
/
removerequestheaders.go
File metadata and controls
35 lines (31 loc) · 912 Bytes
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
package httpproxy
import (
"net/http"
"strings"
)
func headerContains(header http.Header, name, value string) bool {
for _, vv := range header[name] {
for v := range strings.SplitSeq(vv, ",") {
for partv := range strings.SplitSeq(v, ";") {
if strings.EqualFold(value, strings.TrimSpace(partv)) {
return true
}
}
}
}
return false
}
func isWebSocketHandshake(header http.Header) bool {
return headerContains(header, "Upgrade", "websocket") && headerContains(header, "Connection", "Upgrade")
}
// RemoveRequestHeaders removes request headers which should not propagate to the next hop.
func RemoveRequestHeaders(r *http.Request) {
r.RequestURI = ""
delete(r.Header, "Accept-Encoding")
delete(r.Header, "Proxy-Connection")
delete(r.Header, "Proxy-Authenticate")
delete(r.Header, "Proxy-Authorization")
if !isWebSocketHandshake(r.Header) {
delete(r.Header, "Connection")
}
}