-
Notifications
You must be signed in to change notification settings - Fork 34
/
read.go
66 lines (58 loc) · 1.16 KB
/
read.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
package hargo
import (
"encoding/json"
"io"
"os"
log "github.com/sirupsen/logrus"
)
// ReadStream reads the har file as a stream and puts the entries
// on a chan for consumption. When the end of a file is reached it
// will start over until the stop signal is given.
// https://golang.org/pkg/encoding/json/#example_Decoder_Decode_stream
func ReadStream(file *os.File, entries chan Entry, stop chan bool) {
for {
r := NewReader(file)
log.Infoln("reading HAR file")
decoder := json.NewDecoder(r)
// navigate to entries
loop:
for {
t, _ := decoder.Token()
if t == nil {
break
}
switch token := t.(type) {
case json.Token:
if token == "entries" {
break loop
}
}
}
// skip open bracket
_, err := decoder.Token()
if err != nil {
log.Fatal(err)
}
// read entries
for decoder.More() {
var e Entry
err := decoder.Decode(&e)
if err != nil {
log.Fatal(err)
}
if len(e.Request.URL) > 0 {
entries <- e
}
select {
default:
continue
case <-stop:
log.Infoln("stop reading HAR file")
close(entries)
return
}
}
log.Infoln("read HAR file")
file.Seek(0, io.SeekStart)
}
}