-
Notifications
You must be signed in to change notification settings - Fork 3
/
header.go
executable file
·158 lines (138 loc) · 3.86 KB
/
header.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Package httpKit
/*
请求头 || 响应头
key: 不区分大小写
*/
package httpKit
import (
"github.com/richelieu-yang/chimera/v2/src/core/mapKit"
"github.com/richelieu-yang/chimera/v2/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"net/http"
)
// HeaderToMap http.Header(即 map[string][]string) => map[string]interface{}
func HeaderToMap(header http.Header) map[string]interface{} {
return mapKit.MapValues(header, func(value []string, key string) interface{} {
return value
})
}
// SetHeader
/*
e.g.
header := make(map[string][]string)
httpKit.AddHeader(header, "k", "0")
fmt.Println(header) // map[K:[0]]
httpKit.AddHeader(header, "k", "1")
fmt.Println(header) // map[K:[0 1]]
httpKit.SetHeader(header, "k", "2")
fmt.Println(header) // map[K:[2]]
httpKit.SetHeader(header, "k", "")
fmt.Println(header) // map[]
*/
func SetHeader(header http.Header, key, value string) {
if value == "" {
DelHeader(header, key)
return
}
header.Set(key, value)
}
// SetHeaderIfMissing
/*
PS: 区分大小写.
*/
func SetHeaderIfMissing(header http.Header, key, value string) {
if !HeaderContainsValue(header, key, value) {
SetHeader(header, key, value)
}
}
// SetHeaderIfMissingIgnoreCase
/*
PS: 不区分大小写.
*/
func SetHeaderIfMissingIgnoreCase(header http.Header, key, value string) {
if !HeaderContainsValueIgnoreCase(header, key, value) {
SetHeader(header, key, value)
}
}
// GetHeader
/*
PS: 存在多个值的话([]string),返回第一个值.
@param key 不存在对应值的话,将返回 ""
*/
func GetHeader(header http.Header, key string) string {
return header.Get(key)
}
// GetHeaderValues
/*
@param key 不存在对应值的话,将返回 nil
*/
func GetHeaderValues(header http.Header, key string) []string {
return header.Values(key)
}
func DelHeader(header http.Header, key string) {
header.Del(key)
}
// GetUserAgent 获取http请求头中"User Agent"的值.
/*
参考: https://www.sunzhongwei.com/golang-gin-for-user-agent-in-http-request-header-value
e.g.
Chrome浏览器: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Safari浏览器: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
*/
func GetUserAgent(header http.Header) string {
return GetHeader(header, "User-Agent")
}
// GetOrigin 获取请求的Origin
func GetOrigin(header http.Header) string {
return GetHeader(header, "Origin")
}
// AddHeader
/*
e.g.
header := make(map[string][]string)
AddHeader(header, "k", "0")
fmt.Println(header) // map[K:[0]]
AddHeader(header, "k", "1")
fmt.Println(header) // map[K:[0 1]]
AddHeader(header, "k", "1")
fmt.Println(header) // map[K:[0 1 1]]
*/
func AddHeader(header http.Header, key, value string) {
header.Add(key, value)
}
// AddHeaderIfMissing
/*
PS: 区分大小写.
*/
func AddHeaderIfMissing(header http.Header, key, value string) {
if !HeaderContainsValue(header, key, value) {
AddHeader(header, key, value)
}
}
// AddHeaderIfMissingIgnoreCase
/*
PS: 不区分大小写.
*/
func AddHeaderIfMissingIgnoreCase(header http.Header, key, value string) {
if !HeaderContainsValueIgnoreCase(header, key, value) {
AddHeader(header, key, value)
}
}
// HeaderContainsValue Header中,指定key对应的value切片是否包含指定value?
/*
PS: 区分大小写.
*/
func HeaderContainsValue(header http.Header, key, value string) bool {
values := header.Values(key)
return sliceKit.Contains(values, value)
}
// HeaderContainsValueIgnoreCase Header中,指定key对应的value切片是否包含指定value?\
/*
PS: 不区分大小写.
*/
func HeaderContainsValueIgnoreCase(header http.Header, key, value string) bool {
values := header.Values(key)
return sliceKit.ContainsBy(values, func(item string) bool {
return strKit.EqualsIgnoreCase(item, value)
})
}