-
Notifications
You must be signed in to change notification settings - Fork 9
/
har.go
299 lines (260 loc) · 7.15 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package goharproxy
import (
"time"
"net/http"
"net/url"
"strings"
"log"
"io/ioutil"
)
var startingEntrySize int = 1000
type Har struct {
HarLog HarLog `json:"harLog"`
}
type HarLog struct {
Version string `json:"version"`
Creator string `json:"creator"`
Browser string `json:"browser"`
Pages []HarPage `json:"pages"`
Entries []HarEntry `json:"entries"`
}
func newHarLog() *HarLog {
harLog := HarLog {
Version : "1.2",
Creator : "GoHarProxy 0.1",
Browser : "",
Pages : make([]HarPage, 0, 10),
Entries : makeNewEntries(),
}
return &harLog
}
func (harLog *HarLog) addEntry(entry ...HarEntry) {
entries := harLog.Entries
m := len(entries)
n := m + len(entry)
if n > cap(entries) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newEntries := make([]HarEntry, (n+1)*2)
copy(newEntries, entries)
entries = newEntries
}
entries = entries[0:n]
copy(entries[m:n], entry)
harLog.Entries = entries
log.Println("Added entry ", entry[0].Request.Url)
}
func makeNewEntries() []HarEntry {
return make([]HarEntry, 0, startingEntrySize)
}
type HarPage struct {
Id string `json:"id"`
StartedDateTime time.Time `json:"startedDateTime"`
Title string `json:"title"`
PageTimings HarPageTimings `json:"pageTimings"`
}
type HarEntry struct {
PageRef string `json:"pageRef"`
StartedDateTime time.Time `json:"startedDateTime"`
Time int64 `json:"time"`
Request *HarRequest `json:"request"`
Response *HarResponse `json:"response"`
Timings HarTimings `json:"timings"`
ServerIpAddress string `json:"serverIpAddress"`
Connection string `json:"connection"`
}
type HarRequest struct {
Method string `json:"method"`
Url string `json:"url"`
HttpVersion string `json:"httpVersion"`
Cookies []HarCookie `json:"cookies"`
Headers []HarNameValuePair `json:"headers"`
QueryString []HarNameValuePair `json:"queryString"`
PostData *HarPostData `json:"postData"`
BodySize int64 `json:"bodySize"`
HeadersSize int64 `json:"headersSize"`
}
var captureContent bool = false
func parseRequest(req *http.Request) *HarRequest {
if req == nil {
return nil
}
harRequest := HarRequest {
Method : req.Method,
Url : req.URL.String(),
HttpVersion : req.Proto,
Cookies : parseCookies(req.Cookies()),
Headers : parseStringArrMap(req.Header),
QueryString : parseStringArrMap((req.URL.Query())),
BodySize : req.ContentLength,
HeadersSize : calcHeaderSize(req.Header),
}
if captureContent && (req.Method == "POST" || req.Method == "PUT") {
harRequest.PostData = parsePostData(req)
}
return &harRequest
}
func calcHeaderSize(header http.Header) int64 {
headerSize := 0
for headerName, headerValues := range header {
headerSize += len(headerName) + 2
for _, v := range headerValues {
headerSize += len(v)
}
}
return int64(headerSize)
}
func parsePostData(req *http.Request) *HarPostData {
defer func() {
if e := recover(); e != nil {
log.Printf("Error parsing request to %v: %v\n", req.URL, e)
}
}()
harPostData := new(HarPostData)
contentType := req.Header["Content-Type"]
if contentType == nil {
panic("Missing content type in request")
}
harPostData.MimeType = contentType[0]
if len(req.PostForm) > 0 {
index := 0
params := make([]HarPostDataParam, len(req.PostForm))
for k, v := range req.PostForm {
param := HarPostDataParam {
Name : k,
Value : strings.Join(v, ","),
}
params[index] = param
index++
}
harPostData.Params = params
} else {
str, _ := ioutil.ReadAll(req.Body)
harPostData.Text = string(str)
}
return harPostData
}
func parseStringArrMap(stringArrMap map[string][]string) []HarNameValuePair {
index := 0
harQueryString := make([]HarNameValuePair, len(stringArrMap))
for k, v := range stringArrMap {
escapedKey, _ := url.QueryUnescape(k)
escapedValues, _ := url.QueryUnescape(strings.Join(v, ","))
harNameValuePair := HarNameValuePair {
Name : escapedKey,
Value : escapedValues,
}
harQueryString[index] = harNameValuePair
index++
}
return harQueryString
}
func parseCookies(cookies []*http.Cookie) []HarCookie {
harCookies := make([]HarCookie, len(cookies))
for i, cookie := range cookies {
harCookie := HarCookie {
Name : cookie.Name,
Domain : cookie.Domain,
Expires : cookie.Expires,
HttpOnly : cookie.HttpOnly,
Path : cookie.Path,
Secure : cookie.Secure,
Value : cookie.Value,
}
harCookies[i] = harCookie
}
return harCookies
}
type HarResponse struct {
Status int `json:"status"`
StatusText string `json:"statusText"`
HttpVersion string `json:"httpVersion`
Cookies []HarCookie `json:"cookies"`
Headers []HarNameValuePair `json:"headers"`
Content *HarContent `json:"content"`
RedirectUrl string `json:"redirectUrl"`
BodySize int64 `json:"bodySize"`
HeadersSize int64 `json:"headersSize"`
}
func parseResponse(resp *http.Response) *HarResponse {
if resp == nil {
return nil
}
harResponse := HarResponse {
Status : resp.StatusCode,
StatusText : resp.Status,
HttpVersion : resp.Proto,
Cookies : parseCookies(resp.Cookies()),
Headers : parseStringArrMap(resp.Header),
RedirectUrl : "",
BodySize : resp.ContentLength,
HeadersSize : calcHeaderSize(resp.Header),
}
if captureContent {
harResponse.Content = parseContent(resp)
}
return &harResponse
}
func parseContent(resp *http.Response) *HarContent{
defer func() {
if e := recover(); e != nil {
log.Printf("Error parsing response to %v: %v\n", resp.Request.URL, e)
}
}()
harContent := new(HarContent)
contentType := resp.Header["Content-Type"]
if contentType == nil {
panic("Missing content type in response")
}
harContent.MimeType = contentType[0]
if (resp.ContentLength <= 0) {
log.Println("Empty content")
return nil
}
body, _ := ioutil.ReadAll(resp.Body)
harContent.Text = string(body)
return harContent
}
type HarCookie struct {
Name string `json:"name"`
Value string `json:"value"`
Path string `json:"path"`
Domain string `json:"domain"`
Expires time.Time `json:"expires"`
HttpOnly bool `json:"httpOnly"`
Secure bool `json:"secure"`
}
type HarNameValuePair struct {
Name string `json:"name"`
Value string `json:"value"`
}
type HarPostData struct {
MimeType string `json:"mimeType"`
Params []HarPostDataParam `json:"params"`
Text string `json:"text"`
}
type HarPostDataParam struct {
Name string `json:"name"`
Value string `json:"value"`
FileName string `json:"fileName"`
ContentType string `json:"contentType`
}
type HarContent struct {
Size int64 `json:"size"`
Compression int64 `json:"compression"`
MimeType string `json:"mimeType"`
Text string `json:"text"`
Encoding string `json:"encoding"`
}
type HarPageTimings struct {
OnContentLoad int64 `json:"onContentLoad"`
OnLoad int64 `json:"onLoad"`
}
type HarTimings struct {
Blocked int64
Dns int64
Connect int64
Send int64
Wait int64
Receive int64
Ssl int64
}