-
Notifications
You must be signed in to change notification settings - Fork 34
/
utils.go
99 lines (82 loc) · 2.38 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
package hargo
import (
"bufio"
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"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, ignoreHarCookies bool) (*http.Request, error) {
body := ""
if len(entry.Request.PostData.Params) == 0 {
body = entry.Request.PostData.Text
} else {
form := url.Values{}
for _, p := range entry.Request.PostData.Params {
form.Add(p.Name, 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, h.Value)
}
}
if !ignoreHarCookies {
for _, c := range entry.Request.Cookies {
cookie := &http.Cookie{Name: c.Name, Value: 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
}