-
Notifications
You must be signed in to change notification settings - Fork 3
/
x-powered-by.go
46 lines (37 loc) · 1.01 KB
/
x-powered-by.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
package helmet
import "net/http"
// HeaderXPoweredBy is the X-Powered-By HTTP security header.
const HeaderXPoweredBy = "X-Powered-By"
// XPoweredBy represents the X-Powered-By HTTP security header.
type XPoweredBy struct {
Hide bool
Replacement string
}
// NewXPoweredBy creates a new XPoweredBy.
func NewXPoweredBy(hide bool, replacement string) *XPoweredBy {
return &XPoweredBy{
Hide: hide,
Replacement: replacement,
}
}
// EmptyXPoweredBy creates a blank slate XPoweredBy.
func EmptyXPoweredBy() *XPoweredBy {
return NewXPoweredBy(false, "")
}
// Empty returns whether the X-Powered-By is empty.
func (xpb XPoweredBy) Empty() bool {
return !xpb.Hide && xpb.Replacement == ""
}
// Header adds the X-Powered-By HTTP security header to the given http.ResponseWriter.
func (xpb XPoweredBy) Header(w http.ResponseWriter) {
if xpb.Empty() {
return
}
if xpb.Hide {
w.Header().Del(HeaderXPoweredBy)
return
}
if xpb.Replacement != "" {
w.Header().Set(HeaderXPoweredBy, xpb.Replacement)
}
}