-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
333 lines (284 loc) · 8.16 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package warc
import (
"bufio"
"bytes"
"crypto/sha1"
"crypto/sha256"
"encoding/base32"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
gzip "github.com/klauspost/compress/gzip"
"github.com/klauspost/pgzip"
"github.com/klauspost/compress/zstd"
)
func GetSHA1(r io.Reader) string {
sha := sha1.New()
block := make([]byte, 256)
for {
n, err := r.Read(block)
if n > 0 {
sha.Write(block[:n])
}
if err == io.EOF {
break
}
if err != nil {
return "ERROR"
}
}
return base32.StdEncoding.EncodeToString(sha.Sum(nil))
}
func GetSHA256(r io.Reader) string {
sha := sha256.New()
block := make([]byte, 256)
for {
n, err := r.Read(block)
if n > 0 {
sha.Write(block[:n])
}
if err == io.EOF {
break
}
if err != nil {
return "ERROR"
}
}
return base32.StdEncoding.EncodeToString(sha.Sum(nil))
}
func GetSHA256Base16(r io.Reader) string {
sha := sha256.New()
block := make([]byte, 256)
for {
n, err := r.Read(block)
if n > 0 {
sha.Write(block[:n])
}
if err == io.EOF {
break
}
if err != nil {
return "ERROR"
}
}
return hex.EncodeToString(sha.Sum(nil))
}
// splitKeyValue parses WARC record header fields.
func splitKeyValue(line string) (string, string) {
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return "", ""
}
return parts[0], strings.TrimSpace(parts[1])
}
func isLineStartingWithHTTPMethod(line string) bool {
if strings.HasPrefix(line, "GET ") ||
strings.HasPrefix(line, "HEAD ") ||
strings.HasPrefix(line, "POST ") ||
strings.HasPrefix(line, "PUT ") ||
strings.HasPrefix(line, "DELETE ") ||
strings.HasPrefix(line, "CONNECT ") ||
strings.HasPrefix(line, "OPTIONS ") ||
strings.HasPrefix(line, "TRACE ") ||
strings.HasPrefix(line, "PATCH ") {
return true
}
return false
}
// NewWriter creates a new WARC writer.
func NewWriter(writer io.Writer, fileName string, compression string, contentLengthHeader string) (*Writer, error) {
if compression != "" {
if compression == "GZIP" {
var gzipWriter *gzip.Writer
// If the record's Content-Length is bigger than a megabyte, we use the parallel gzip library
if contentLengthHeader != "" {
contentLength, err := strconv.Atoi(contentLengthHeader)
if err != nil {
return nil, err
}
if contentLength > 1000000 {
pgzipWriter := pgzip.NewWriter(writer)
return &Writer{
FileName: fileName,
Compression: compression,
PGZIPWriter: pgzipWriter,
FileWriter: bufio.NewWriter(pgzipWriter),
}, nil
}
}
gzipWriter = gzip.NewWriter(writer)
return &Writer{
FileName: fileName,
Compression: compression,
GZIPWriter: gzipWriter,
FileWriter: bufio.NewWriter(gzipWriter),
}, nil
} else if compression == "ZSTD" {
zstdWriter, err := zstd.NewWriter(writer)
if err != nil {
return nil, err
}
return &Writer{
FileName: fileName,
Compression: compression,
ZSTDWriter: zstdWriter,
FileWriter: bufio.NewWriter(zstdWriter),
}, nil
}
return nil, errors.New("Invalid compression algorithm: " + compression)
}
return &Writer{
FileName: fileName,
Compression: "",
FileWriter: bufio.NewWriter(writer),
}, nil
}
// NewRecord creates a new WARC record.
func NewRecord(tempDir string, fullOnDisk bool) *Record {
return &Record{
Header: NewHeader(),
Content: NewSpooledTempFile("warc", tempDir, fullOnDisk),
}
}
// NewRecordBatch creates a record batch,
// it also initialize the capture time
func NewRecordBatch() *RecordBatch {
return &RecordBatch{
CaptureTime: time.Now().UTC().Format(time.RFC3339Nano),
}
}
// NewRotatorSettings creates a RotatorSettings structure
// and initialize it with default values
func NewRotatorSettings() *RotatorSettings {
return &RotatorSettings{
WarcinfoContent: NewHeader(),
Prefix: "WARC",
WarcSize: 1000,
Compression: "GZIP",
OutputDirectory: "./",
}
}
// checkRotatorSettings validate RotatorSettings settings, and set
// default values if needed
func checkRotatorSettings(settings *RotatorSettings) (err error) {
// Get host name as reported by the kernel
hostName, err := os.Hostname()
if err != nil {
panic(err)
}
// Check if output directory is specified, if not, set it to the current directory
if settings.OutputDirectory == "" {
settings.OutputDirectory = "./"
} else {
// If it is specified, check if output directory exist
if _, err := os.Stat(settings.OutputDirectory); os.IsNotExist(err) {
// If it doesn't exist, create it
// MkdirAll will create all parent directories if needed
err = os.MkdirAll(settings.OutputDirectory, os.ModePerm)
if err != nil {
return err
}
}
}
if settings.WARCWriterPoolSize == 0 {
settings.WARCWriterPoolSize = 1
}
// Add a trailing slash to the output directory
if settings.OutputDirectory[len(settings.OutputDirectory)-1:] != "/" {
settings.OutputDirectory = settings.OutputDirectory + "/"
}
// If prefix isn't specified, set it to "WARC"
if settings.Prefix == "" {
settings.Prefix = "WARC"
}
// If WARC size isn't specified, set it to 1GB (10^9 bytes) by default
if settings.WarcSize == 0 {
settings.WarcSize = 1000
}
// Check if the specified compression algorithm is valid
if settings.Compression != "" && settings.Compression != "GZIP" && settings.Compression != "ZSTD" {
return errors.New("Invalid compression algorithm: " + settings.Compression)
}
// Add few headers to the warcinfo payload, to not have it empty
settings.WarcinfoContent.Set("hostname", hostName)
settings.WarcinfoContent.Set("format", "WARC file version 1.1")
settings.WarcinfoContent.Set("conformsTo", "http://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/")
return nil
}
// isFielSizeExceeded compare the size of a file (filePath) with
// a max size (maxSize), if the size of filePath exceed maxSize,
// it returns true, else, it returns false
func isFileSizeExceeded(filePath string, maxSize float64) bool {
// Open the file
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
// Get actual file size
stat, err := file.Stat()
if err != nil {
panic(err)
}
fileSize := (float64)((stat.Size() / 1024) / 1024)
// If fileSize exceed maxSize, return true
return fileSize >= maxSize
}
// formatSerial add the correct padding to the serial
// E.g. with serial = 23 and format = 5:
// formatSerial return 00023
func formatSerial(atomicSerial *int64, format string) string {
return fmt.Sprintf("%0"+format+"d", atomic.LoadInt64(atomicSerial))
}
// GenerateWarcFileName generate a WARC file name following recommendations
// of the specs:
// Prefix-Timestamp-Serial-Crawlhost.warc.gz
func GenerateWarcFileName(prefix string, compression string, atomicSerial *int64) (fileName string) {
// Get host name as reported by the kernel
hostName, err := os.Hostname()
if err != nil {
panic(err)
}
// Don't let atomicSerial overflow past 99999, the current maximum with 5 serial digits.
if atomic.LoadInt64(atomicSerial) >= 99999 {
atomic.StoreInt64(atomicSerial, 0)
}
// Atomically increase the global serial number
atomic.AddInt64(atomicSerial, 1)
formattedSerial := formatSerial(atomicSerial, "5")
now := time.Now().UTC()
date := now.Format("20060102150405") + strconv.Itoa(now.Nanosecond())[:3]
if compression != "" {
if compression == "GZIP" {
return prefix + "-" + date + "-" + formattedSerial + "-" + hostName + ".warc.gz.open"
}
if compression == "ZSTD" {
return prefix + "-" + date + "-" + formattedSerial + "-" + hostName + ".warc.zst.open"
}
}
return prefix + "-" + date + "-" + formattedSerial + "-" + hostName + ".warc.open"
}
func getContentLength(rwsc ReadWriteSeekCloser) int {
// If the FileName leads to no existing file, it means that the SpooledTempFile
// never had the chance to buffer to disk instead of memory, in which case we can
// just read the buffer (which should be <= 2MB) and return the length
if rwsc.FileName() == "" {
rwsc.Seek(0, 0)
buf := new(bytes.Buffer)
buf.ReadFrom(rwsc)
return buf.Len()
} else {
// Else, we return the size of the file on disk
fileInfo, err := os.Stat(rwsc.FileName())
if err != nil {
panic(err)
}
return int(fileInfo.Size())
}
}