/
headers.go
60 lines (45 loc) · 1.12 KB
/
headers.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
package core
import (
"regexp"
"strings"
"sync"
)
// This will match <first-group>:<second-group> of a header
// Test it here https://regex101.com/r/cn0QvY/1
const (
PATTERN = "([a-zA-Z0-9-]+)[^*](.*)"
)
// Pre define the compiler
var pattern *regexp.Regexp = regexp.MustCompile(PATTERN)
type headers struct {
entries map[string]string
}
// Holder for our singelton instance
var headerInstance *headers
func ParseHeaders(args Arguments) *headers {
var once sync.Once
// This will run only once
once.Do(func() {
headerInstance = &headers{
entries: make(map[string]string),
}
for _, line := range args.Headers() {
header := pattern.FindStringSubmatch(line)
headerInstance.Add(header[1], strings.Trim(header[2], " "))
}
if args.IsJSONPayload() {
headerInstance.Add("Content-Type", "application/json; charset=UTF-8")
}
headerInstance.Add("User-Agent", "Rest In Peace")
})
return headerInstance
}
func HeaderInstance() *headers {
return headerInstance
}
func (h *headers) Add(key string, value string) {
h.entries[key] = value
}
func (h *headers) Headers() map[string]string {
return h.entries
}