generated from dnnrly/goclitem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
har.go
171 lines (152 loc) · 4.87 KB
/
har.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package hoofli
import (
"encoding/json"
"io"
"regexp"
"time"
)
// Property is a key value pair
type Property struct {
// Name of the value represented
Name string `json:"name"`
// Value being represented
Value string `json:"value"`
}
// Properties is a collection of Property
type Properties []Property
// Content is the payload of a request or response
type Content struct {
Mimetype string `json:"mimeType"`
Size int `json:"size"`
Text string `json:"text"`
}
// Page is the data that relates to a single browser page rather the requests and responses
type Page struct {
Starteddatetime time.Time `json:"startedDateTime"`
ID string `json:"id"`
Title string `json:"title"`
Pagetimings struct {
Oncontentload float32 `json:"onContentLoad"`
Onload float32 `json:"onLoad"`
} `json:"pageTimings"`
}
// Pages is a collection of Page
type Pages []Page
// Response represents the data about the response to a request
type Response struct {
Status int `json:"status"`
Statustext string `json:"statusText"`
Httpversion string `json:"httpVersion"`
Headers Properties `json:"headers"`
Cookies Properties `json:"cookies"`
Content Content `json:"content"`
Redirecturl string `json:"redirectURL"`
Headerssize int `json:"headersSize"`
Bodysize int `json:"bodySize"`
}
// Timing contains the timing of various different parts of the request and response for an entry
type Timing struct {
Blocked float32 `json:"blocked"`
DNS float32 `json:"dns"`
Connect float32 `json:"connect"`
Ssl float32 `json:"ssl"`
Send float32 `json:"send"`
Wait float32 `json:"wait"`
Receive float32 `json:"receive"`
}
// Cache represents data cached from a request's response
type Cache struct {
Afterrequest struct {
Expires string `json:"expires"`
Lastfetched string `json:"lastFetched"`
Etag string `json:"eTag"`
Fetchcount string `json:"fetchCount"`
Datasize string `json:"_dataSize"`
Lastmodified string `json:"_lastModified"`
Device string `json:"_device"`
} `json:"afterRequest"`
}
// Request represents information about the request portion of an Entry
type Request struct {
Bodysize int `json:"bodySize"`
Method string `json:"method"`
URL string `json:"url"`
Httpversion string `json:"httpVersion"`
Headers Properties `json:"headers"`
Cookies Properties `json:"cookies"`
Querystring Properties `json:"queryString"`
Headerssize int `json:"headersSize"`
Postdata Content `json:"postData"`
}
// Initiator represents information about the initiator of the request which
// caused the Entry
type Initiator struct {
Type string `json:"type"`
}
// Entry represents a single entry in the network log
type Entry struct {
Pageref string `json:"pageref"`
Starteddatetime time.Time `json:"startedDateTime"`
Response Response `json:"response,omitempty"`
Timings Timing `json:"timings"`
Time float32 `json:"time"`
Securitystate string `json:"_securityState"`
Serveripaddress string `json:"serverIPAddress,omitempty"`
Connection string `json:"connection,omitempty"`
Cache Cache `json:"cache,omitempty"`
Request Request `json:"request,omitempty"`
Initiator Initiator `json:"_initiator,omitempty"`
}
// Entries is a group of entries
type Entries []Entry
// ExcludeByURL filters out entries whose request URL matches the supplied regex
func (e Entries) ExcludeByURL(pattern string) Entries {
result := Entries{}
for i := range e {
p := regexp.MustCompile(pattern)
if !p.MatchString(e[i].Request.URL) {
result = append(result, e[i])
}
}
return result
}
// ExcludeByResponseHeader filters out entries by named headers whose value matches the supplied regex
func (e Entries) ExcludeByResponseHeader(header, value string) Entries {
result := Entries{}
p := regexp.MustCompile(value)
for i := range e {
matched := false
for j := range e[i].Response.Headers {
h := e[i].Response.Headers[j]
if h.Name == header && p.MatchString(h.Value) {
matched = true
}
}
if !matched {
result = append(result, e[i])
}
}
return result
}
// Log represents the contents of a Har log
type Log struct {
Version string `json:"version"`
Creator Property `json:"creator"`
Browser Property `json:"browser"`
Pages Pages `json:"pages"`
Entries Entries `json:"entries"`
}
// Har represents an entire HTTP Archive
// See the following for more details:
// - https://en.wikipedia.org/wiki/HAR_(file_format)
// - http://www.softwareishard.com/blog/har-12-spec/
// - https://w3c.github.io/web-performance/specs/HAR/Overview.html
type Har struct {
Log Log `json:"log"`
}
// NewHar creates a new Har from a reader
func NewHar(r io.Reader) (*Har, error) {
var har Har
err := json.NewDecoder(r).Decode(&har)
return &har, err
}