-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathheaders.go
More file actions
69 lines (60 loc) · 2.18 KB
/
Copy pathheaders.go
File metadata and controls
69 lines (60 loc) · 2.18 KB
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
package surf
import (
"net/textproto"
"regexp"
"github.com/enetx/g"
"github.com/enetx/http"
)
// Headers represents a collection of HTTP Headers.
type Headers http.Header
// Contains checks if the header contains any of the specified patterns.
// It accepts a header name and a pattern (or list of patterns) and returns a boolean value
// indicating whether any of the patterns are found in the header values.
// The patterns can be a string, a slice of strings, or a slice of *regexp.Regexp.
func (h Headers) Contains(header g.String, patterns any) bool {
if h.Values(header) != nil {
for _, value := range h.Values(header) {
v := value.Lower()
switch ps := patterns.(type) {
case string:
if v.Contains(g.String(ps).Lower()) {
return true
}
case g.String:
if v.Contains(ps.Lower()) {
return true
}
case []string:
if v.ContainsAny(g.SliceOf(ps...).Iter().Map(g.NewString).Map(g.String.Lower).Collect()...) {
return true
}
case g.Slice[string]:
if v.ContainsAny(ps.Iter().Map(g.NewString).Map(g.String.Lower).Collect()...) {
return true
}
case g.Slice[g.String]:
if v.ContainsAny(ps.Iter().Map(g.String.Lower).Collect()...) {
return true
}
case []*regexp.Regexp:
if v.Regexp().MatchAny(ps...) {
return true
}
}
}
}
return false
}
// Values returns the values associated with a specified header key.
// It wraps the Values method from the textproto.MIMEHeader type.
func (h Headers) Values(key g.String) g.Slice[g.String] {
return g.SliceOf(textproto.MIMEHeader(h).Values(key.Std())...).Iter().Map(g.NewString).Collect()
}
// Get returns the first value associated with a specified header key.
// It wraps the Get method from the textproto.MIMEHeader type.
func (h Headers) Get(key g.String) g.String { return g.String(textproto.MIMEHeader(h).Get(key.Std())) }
// Del deletes the values associated with a specified header key.
// It wraps the Del method from the textproto.MIMEHeader type.
func (h Headers) Del(key g.String) { textproto.MIMEHeader(h).Del(key.Std()) }
// Clone returns a copy of Headers or nil if Headers is nil.
func (h Headers) Clone() Headers { return Headers(http.Header(h).Clone()) }