/
http_headers.go
125 lines (115 loc) · 3.28 KB
/
http_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
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
package http_utils
import (
"fmt"
"net/http"
"strings"
"github.com/pyneda/sukyan/db"
"github.com/rs/zerolog/log"
)
// type RequestHeaders map[string][]string
func SetRequestHeadersFromHistoryItem(request *http.Request, historyItem *db.History) error {
headers, err := historyItem.GetRequestHeadersAsMap()
if err != nil {
log.Error().Err(err).Msg("Error setting headers for a new request due to an error getting the original request headers")
return err
}
for key, values := range headers {
if strings.ToLower(key) == "content-length" {
continue
}
for _, value := range values {
log.Debug().Str("key", key).Str("value", value).Msg("Setting header")
request.Header.Set(key, value)
}
}
return nil
}
func HeadersToString(headersMap map[string][]string) string {
headers := make([]string, 0, len(headersMap))
for name, values := range headersMap {
for _, value := range values {
headers = append(headers, fmt.Sprintf("%s: %s", name, value))
}
}
return strings.Join(headers, "\n")
}
// ClassifyHTTPResponseHeader classifies a given HTTP response header key by its purpose.
func ClassifyHTTPResponseHeader(headerKey string) string {
headerCategories := map[string]map[string]bool{
"Caching": {
"Age": true,
"Cache-Control": true,
"Expires": true,
"Pragma": true,
"Vary": true,
"Warning": true,
},
"Security": {
"Access-Control-Allow-Origin": true,
"Access-Control-Allow-Methods": true,
"Access-Control-Allow-Headers": true,
"Access-Control-Allow-Credentials": true,
"Access-Control-Max-Age": true,
"Access-Control-Expose-Headers": true,
"Access-Control-Request-Method": true,
"Access-Control-Request-Headers": true,
"Strict-Transport-Security": true,
"Content-Security-Policy": true,
"X-Content-Type-Options": true,
"X-XSS-Protection": true,
"X-Frame-Options": true,
},
"Transport": {
"Transfer-Encoding": true,
"Trailer": true,
"Connection": true,
"Keep-Alive": true,
"Upgrade": true,
},
"Information": {
"Allow": true,
"Date": true,
"Location": true,
"Retry-After": true,
"Via": true,
},
"Content": {
"Accept-Ranges": true,
"Content-Encoding": true,
"Content-Language": true,
"Content-Length": true,
"Content-Location": true,
"Content-MD5": true,
"Content-Range": true,
"Content-Type": true,
"ETag": true,
"Last-Modified": true,
},
"Rate-Limiting": {
"RateLimit-Limit": true,
"RateLimit-Remaining": true,
"RateLimit-Reset": true,
},
"Authentication": {
"WWW-Authenticate": true,
"Set-Cookie": true,
},
"Fingerprint": {
"Server": true,
"X-Powered-By": true,
"X-AspNet-Version": true,
"X-Runtime": true,
"X-Version": true,
"X-Generator": true,
"X-Drupal-Cache": true,
},
}
// Normalize the header key to capitalize each word, similar to the canonical MIME header key format
canonicalHeaderKey := strings.Title(headerKey)
for category, headers := range headerCategories {
if headers[canonicalHeaderKey] {
return category
}
}
return "Uncommon"
}