forked from mrichman/hargo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
149 lines (129 loc) · 3.61 KB
/
utils.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
package hargo
import (
"bufio"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"sort"
"strings"
"golang.org/x/net/http/httpguts"
log "github.com/sirupsen/logrus"
)
// Decode reads from a reader and returns Har object
func Decode(r *bufio.Reader) (Har, error) {
dec := json.NewDecoder(r)
var har Har
err := dec.Decode(&har)
if err != nil {
log.Error(err)
}
// Delete ws:// entries as they block execution
for i, entry := range har.Log.Entries {
if strings.HasPrefix(entry.Request.URL, "ws://") {
har.Log.Entries[i] = har.Log.Entries[len(har.Log.Entries)-1]
har.Log.Entries = har.Log.Entries[:len(har.Log.Entries)-1]
}
}
// Sort the entries by StartedDateTime to ensure they will be processed
// in the same order as they happened
sort.Slice(har.Log.Entries, func(i, j int) bool {
return har.Log.Entries[i].StartedDateTime < har.Log.Entries[j].StartedDateTime
})
return har, err
}
// EntryToRequest converts a HAR entry type to an http.Request
func EntryToRequest(entry *Entry, hc HarConfig, ignoreHarCookies bool) (*http.Request, error) {
body := ""
if len(entry.Request.PostData.Params) == 0 {
body = hc.ReplaceVariables(entry.Request.PostData.Text)
} else {
form := url.Values{}
for _, p := range entry.Request.PostData.Params {
form.Add(p.Name, hc.ReplaceVariables(p.Value))
}
body = form.Encode()
}
req, _ := http.NewRequest(entry.Request.Method, entry.Request.URL, bytes.NewBuffer([]byte(body)))
for _, h := range entry.Request.Headers {
if httpguts.ValidHeaderFieldName(h.Name) && httpguts.ValidHeaderFieldValue(h.Value) && h.Name != "Cookie" {
req.Header.Add(h.Name, hc.ReplaceVariables(h.Value))
}
}
if !ignoreHarCookies {
for _, c := range entry.Request.Cookies {
cookie := &http.Cookie{Name: c.Name, Value: hc.ReplaceVariables(c.Value), HttpOnly: false, Domain: c.Domain}
req.AddCookie(cookie)
}
}
return req, nil
}
func check(err error) {
if err != nil {
log.Error(err)
}
}
// NewReader returns a bufio.Reader that will skip over initial UTF-8 byte order marks.
// https://tools.ietf.org/html/rfc7159#section-8.1
func NewReader(r io.Reader) *bufio.Reader {
buf := bufio.NewReader(r)
b, err := buf.Peek(3)
if err != nil {
// not enough bytes
return buf
}
if b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf {
log.Warn("BOM detected. Skipping first 3 bytes of file. Consider removing the BOM from this file. " +
"See https://tools.ietf.org/html/rfc7159#section-8.1 for details.")
buf.Discard(3)
}
return buf
}
//GetHarConfig gets the har config
func GetHarConfig(harFile string) HarConfig {
hc := HarConfig{}
harConfigFile := strings.TrimRight(harFile, ".har") + ".json"
if _, err := os.Stat(harConfigFile); err == nil {
log.Info("with har config file: ", harConfigFile)
bytes, _ := ioutil.ReadFile(harConfigFile)
json.Unmarshal(bytes, &hc)
}
return hc
}
// ReplaceVariables is to replace config variables
func (h *HarConfig) ReplaceVariables(s string) string {
if len(h.Variables) <= 0 {
re := regexp.MustCompile(`\{(\w+)\}`)
if re.MatchString(s) {
for _, g := range re.FindAllStringSubmatch(s, -1) {
if v, ok := h.Variables[g[1]]; ok {
s = strings.Replace(s, g[0], v, -1)
}
}
}
}
return s
}
// ReplaceAlias is to replace config alias
func (h *HarConfig) ReplaceAlias(s string) string {
if len(h.Alias) > 0 {
if val, ok := h.Alias[s]; ok {
return val
}
for k, v := range h.Alias {
//this is a regexp
if k[0] == '(' {
if re, err := regexp.Compile(k); err == nil && re.MatchString(s) {
s2 := re.ReplaceAllString(s, v)
h.Alias[s] = s2
return s2
}
}
}
}
return s
}