-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
44 lines (40 loc) · 1.03 KB
/
util.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
package request
import (
"net/http"
"strings"
)
// This constraint is used to retrieve the request host.
type RequestConstraint interface {
*Request | *http.Request
}
func GetHost[T RequestConstraint](r T) string {
var host string
switch r := any(r).(type) {
case *Request:
host = r.Request.Host
case *http.Request:
host = r.Host
}
if strings.Contains(host, ":") {
host = strings.Split(host, ":")[0]
}
return host
}
// Add a header onto the request.
// This will append the value to the current value.
// If the value already exists, it will not be added.
func AddHeader(w http.ResponseWriter, name, value string) {
// Get the current value.
current := w.Header().Get(name)
// If the current value is empty, set the value.
if current == "" {
w.Header().Set(name, value)
return
}
// If the value already exists, do nothing.
if strings.Contains(current, value) {
return
}
// If the current value is not empty, append the value.
w.Header().Set(name, current+"; "+value)
}