-
Notifications
You must be signed in to change notification settings - Fork 2
/
arc.go
303 lines (280 loc) · 7.5 KB
/
arc.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
// Copyright 2015 Richard Lehane. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webarchive
import (
"bytes"
"io"
"strconv"
"time"
)
// ARCTime is a time format string for the ARC time format
const ARCTime = "20060102150405"
// ARCRecord represents the common fields shared by ARC version 1
// and ARC version 2 URL record blocks.
// ARC version 2 URL record blocks have additional fields not exposed
// here. These fields are available in the Fields() map.
// To access the IP() method of an ARCRecord, do an interface
// assertion on a Record.
//
// Example:
//
// record, _ := reader.Next()
// arcrecord, ok := record.(ARCRecord)
// if ok {fmt.Println(arcrecord.IP())}
type ARCRecord interface {
IP() string
Record
}
// ARC structs represent the Version blocks at the start of ARC files.
// Provides information about the ARC file as a whole such as version,
// file path of the archive file, and date creation of the archive file.
type ARC struct {
FileDesc string // Original pathname of the archive file
Address string // IP address of machine that created the archive file
FileDate time.Time // Date the archive file was created
Version int // ARC version (1 or 2) - this will affect the fields available in the Fields() map
OriginCode string // Name of gathering organization
}
// ARCReader is the ARC implementation of a webarchive Reader
type ARCReader struct {
*ARC
*reader
arcHeader
}
type arcHeader interface {
IP() string
Header
size() int64
setfields([]byte)
}
// Version 1 URL record
type url1 struct {
url string
ip string // dotted-quad (eg 192.216.46.98 or 0.0.0.0)
date time.Time // YYYYMMDDhhmmss (Greenwich Mean Time)
mime string // "no-type"|MIME type of data (e.g., "text/html")
sz int64
fields []byte
}
func (u *url1) URL() string { return u.url }
func (u *url1) Date() time.Time { return u.date }
func (u *url1) Fields() map[string][]string {
var fields map[string][]string
if len(u.fields) > 0 {
fields = getAllValues(u.fields)
} else {
fields = make(map[string][]string)
}
fields["URL"] = []string{u.url}
fields["IP"] = []string{u.ip}
fields["Date"] = []string{u.date.Format(ARCTime)}
fields["MIME"] = []string{u.mime}
fields["Size"] = []string{strconv.FormatInt(u.sz, 10)}
return fields
}
func (u *url1) IP() string { return u.ip }
func (u *url1) MIME() string { return u.mime }
func (u *url1) transferEncodings() []string {
if len(u.fields) == 0 {
return nil
}
vals := getSelectValues(u.fields, "Transfer-Encoding")
if vals[0] == "" {
return nil
}
return splitAndReverse(vals[0])
}
func (u *url1) encodings() []string {
if len(u.fields) == 0 {
return nil
}
vals := getSelectValues(u.fields, "Transfer-Encoding", "Content-Encoding")
if vals[0] == "" {
if vals[1] == "" {
return nil
}
return splitAndReverse(vals[1])
}
if vals[1] == "" {
return splitAndReverse(vals[0])
}
return append(splitAndReverse(vals[0]), splitAndReverse(vals[1])...)
}
func (u *url1) size() int64 { return u.sz }
func (u *url1) setfields(f []byte) { u.fields = f }
// Version 2 URL record
type url2 struct {
*url1
statusCode int
checksum string
location string
offset int64
filename string
}
func (u *url2) Fields() map[string][]string {
fields := u.url1.Fields()
fields["StatusCode"] = []string{strconv.Itoa(u.statusCode)}
fields["Checksum"] = []string{u.checksum}
fields["Location"] = []string{u.location}
fields["Offset"] = []string{strconv.FormatInt(u.offset, 10)}
fields["Filename"] = []string{u.location}
return fields
}
// NewARCReader creates a new ARC reader from the supplied io.Reader.
// Use instead of NewReader if you are only working with ARC files.
func NewARCReader(r io.Reader) (*ARCReader, error) {
rdr, err := newReader(r)
if err != nil {
return nil, err
}
return newARCReader(rdr)
}
func newARCReader(r *reader) (*ARCReader, error) {
arc := &ARCReader{reader: r}
var err error
arc.ARC, err = arc.readVersionBlock()
return arc, err
}
// Reset allows re-use of an ARC reader
func (a *ARCReader) Reset(r io.Reader) error {
a.reader.reset(r)
return a.reset()
}
func (a *ARCReader) reset() error {
var err error
a.ARC, err = a.readVersionBlock()
return err
}
// Next iterates to the next Record. Returns io.EOF at the end of file.
func (a *ARCReader) Next() (Record, error) {
buf, err := a.next()
if err != nil {
return nil, err
}
parts := bytes.Split(bytes.TrimSpace(buf), []byte(" "))
if a.Version == 1 {
a.arcHeader, err = makeUrl1(parts)
} else {
a.arcHeader, err = makeUrl2(parts)
}
if err != nil {
return nil, err
}
a.thisIdx, a.sz = 0, a.size()
return a, err
}
// NextPayload iterates to the next payload record.
// As ARC files do not differentiate between different types of records,
// the effect of NextPayload for an ARC reader is just to strip HTTP
// headers. These stripped headers are then made available in the Fields() map.
func (a *ARCReader) NextPayload() (Record, error) {
r, err := a.Next()
if err != nil {
return r, err
}
if v, err := a.peek(5); err == nil && string(v) == "HTTP/" {
f, err := a.storeLines(0, true)
if err != nil {
return r, err
}
a.setfields(f)
}
return r, err
}
func (r *ARCReader) readVersionBlock() (*ARC, error) {
buf, _ := r.readLine()
if len(buf) == 0 {
return nil, ErrVersionBlock
}
line1 := bytes.Split(buf, []byte(" "))
if len(line1) < 3 {
return nil, ErrVersionBlock
}
t, err := time.Parse(ARCTime, string(line1[2]))
if err != nil {
return nil, ErrVersionBlock
}
buf, _ = r.readLine()
line2 := bytes.Split(buf, []byte(" "))
if len(line2) < 3 {
return nil, ErrVersionBlock
}
version, err := strconv.Atoi(string(line2[0]))
if err != nil {
return nil, ErrVersionBlock
}
l, err := strconv.Atoi(string(bytes.TrimSpace(line1[len(line1)-1])))
if err != nil {
return nil, ErrVersionBlock
}
// now scan ahead to first doc
l -= len(buf)
if r.slicer {
r.idx += int64(l)
} else {
r.buf.Discard(l)
}
return &ARC{
FileDesc: string(line1[0]),
Address: string(line1[1]),
FileDate: t,
Version: version,
OriginCode: string(bytes.TrimSpace(line2[len(line2)-1])),
}, nil
}
func makeUrl1(p [][]byte) (*url1, error) {
if len(p) < 5 {
return nil, ErrARCHeader
}
date, err := time.Parse(ARCTime, string(p[2]))
if err != nil {
return nil, ErrARCHeader
}
l, err := strconv.ParseInt(string(p[len(p)-1]), 10, 64)
if err != nil {
return nil, ErrARCHeader
}
return &url1{
url: string(p[0]),
ip: string(p[1]),
date: date,
mime: string(p[3]),
sz: l,
}, nil
}
func makeUrl2(p [][]byte) (*url2, error) {
if len(p) != 10 {
return nil, ErrARCHeader
}
u1, err := makeUrl1(p)
if err != nil {
return nil, ErrARCHeader
}
status, err := strconv.Atoi(string(p[4]))
if err != nil {
return nil, ErrARCHeader
}
offset, err := strconv.ParseInt(string(p[7]), 10, 64)
if err != nil {
return nil, ErrARCHeader
}
return &url2{
url1: u1,
statusCode: status,
checksum: string(p[5]),
location: string(p[6]),
offset: offset,
filename: string(p[8]),
}, nil
}