-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentry.go
304 lines (252 loc) · 6.81 KB
/
entry.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
package simplecache
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)
// Entry represents a HTTP response as stored in the cache.
// Each entry is stored in a file named "path/hash(url)_0".
type Entry struct {
URL string
hash uint64
path string
fileSize int64
keyLen int64
offset1 int64
dataSize1 int64
offset0 int64
dataSize0 int64
}
// Get returns the Entry for the specified URL.
// An error is returned if the format of the entry does not match the one expected.
func Get(url, path string) (*Entry, error) {
sum := sha1.Sum([]byte(url))
hash := binary.LittleEndian.Uint64(sum[:8])
name := filepath.Join(path, fmt.Sprintf("%016x_0", hash))
file, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("getting %s: %v", url, err)
}
defer close(file)
stat, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("getting %s: %v", url, err)
}
entry := Entry{
hash: hash,
path: path,
fileSize: stat.Size(),
}
if err = entry.readHeader(file); err != nil {
return nil, fmt.Errorf("getting %s: %v", url, err)
}
if err = entry.readStream0(file); err != nil {
return nil, fmt.Errorf("getting %s: %v", url, err)
}
if err = entry.readStream1(file); err != nil {
return nil, fmt.Errorf("getting %s: %v", url, err)
}
return &entry, nil
}
func readURL(hash uint64, path string) (string, error) {
name := filepath.Join(path, fmt.Sprintf("%016x_0", hash))
file, err := os.Open(name)
if err != nil {
return "", fmt.Errorf("readurl %016x_0: %v", hash, err)
}
defer close(file)
var entry Entry
err = entry.readHeader(file)
if err != nil {
return "", fmt.Errorf("readurl %016x_0: %v", hash, err)
}
return entry.URL, nil
}
func (e *Entry) readHeader(file io.Reader) error {
var header entryHeader
err := binary.Read(file, binary.LittleEndian, &header)
if err != nil {
return fmt.Errorf("read header: %v", err)
}
if header.Magic != initialMagicNumber {
return fmt.Errorf("header magic: %x, want: %x",
header.Magic, initialMagicNumber)
}
if header.Version != entryVersion {
return fmt.Errorf("header version: %d, want: %d",
header.Version, entryVersion)
}
// keyLen
e.keyLen = int64(header.KeyLen)
key := make([]byte, header.KeyLen)
err = binary.Read(file, binary.LittleEndian, &key)
if err != nil {
return fmt.Errorf("read header key: %v", err)
}
sfh := superFastHash(key)
if header.KeyHash != sfh {
return fmt.Errorf("header key hash: %x, want: %x",
header.KeyHash, sfh)
}
// URL
e.URL = string(key)
return nil
}
func (e *Entry) readStream0(file *os.File) error {
var stream0EOF entryEOF
_, err := file.Seek(-1*entryEOFSize, io.SeekEnd)
if err != nil {
return fmt.Errorf("seek stream0: %v", err)
}
err = binary.Read(file, binary.LittleEndian, &stream0EOF)
if err != nil {
return fmt.Errorf("read stream0 entryEOF: %v", err)
}
if stream0EOF.Magic != finalMagicNumber {
return fmt.Errorf("stream0 magic: %x, want: %x",
stream0EOF.Magic, finalMagicNumber)
}
// dataSize0
e.dataSize0 = int64(stream0EOF.StreamSize)
// offset0
e.offset0 = e.fileSize - entryEOFSize - e.dataSize0
if stream0EOF.HasSHA256() {
e.offset0 -= int64(sha256.Size)
}
// verifyStream0
if stream0EOF.HasCRC32() {
stream0 := make([]byte, e.dataSize0)
_, err = file.ReadAt(stream0, e.offset0)
if err != nil {
return fmt.Errorf("read stream0: %v", err)
}
actualCRC := crc32.ChecksumIEEE(stream0)
if stream0EOF.CRC != actualCRC {
return fmt.Errorf("stream0 CRC: %x, want: %x",
stream0EOF.CRC, actualCRC)
}
}
if stream0EOF.HasSHA256() {
var expectedSum256 [sha256.Size]byte
offset256 := e.offset0 + e.dataSize0
_, err = file.ReadAt(expectedSum256[:], offset256)
if err != nil {
return fmt.Errorf("read stream0 sha256: %v", err)
}
actualSum256 := sha256.Sum256([]byte(e.URL))
if expectedSum256 != actualSum256 {
return fmt.Errorf("stream0 sha256: %x, want: %x",
expectedSum256, actualSum256)
}
}
return nil
}
func (e *Entry) readStream1(file *os.File) error {
var stream1EOF entryEOF
_, err := file.Seek(e.offset0-entryEOFSize, io.SeekStart)
if err != nil {
return fmt.Errorf("seek stream1: %v", err)
}
err = binary.Read(file, binary.LittleEndian, &stream1EOF)
if err != nil {
return fmt.Errorf("read stream1 entryEOF: %v", err)
}
if stream1EOF.Magic != finalMagicNumber {
return fmt.Errorf("stream1 magic: %x, want: %x",
stream1EOF.Magic, finalMagicNumber)
}
// dataSize1
e.dataSize1 = int64(stream1EOF.StreamSize)
// offset1
e.offset1 = entryHeaderSize + e.keyLen
// verifyStream1
if e.dataSize1 > 0 && stream1EOF.HasCRC32() {
stream1 := make([]byte, e.dataSize1)
_, err := file.ReadAt(stream1, e.offset1)
if err != nil {
return fmt.Errorf("read stream1: %v", err)
}
actualCRC := crc32.ChecksumIEEE(stream1)
if stream1EOF.CRC != actualCRC {
return fmt.Errorf("stream1 CRC: %x, want: %x",
stream1EOF.CRC, actualCRC)
}
}
return nil
}
// Header returns the HTTP header.
func (e *Entry) Header() (http.Header, error) {
name := filepath.Join(e.path, fmt.Sprintf("%016x_0", e.hash))
file, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("open header: %v", err)
}
defer close(file)
stream0 := make([]byte, e.dataSize0)
_, err = file.ReadAt(stream0, e.offset0)
if err != nil {
return nil, fmt.Errorf("read header: %v", err)
}
var meta struct {
InfoSize int32
Flag int32
RequestTime int64
ResponseTime int64
HeaderSize int32
}
reader := bytes.NewReader(stream0)
err = binary.Read(reader, binary.LittleEndian, &meta)
if err != nil {
return nil, fmt.Errorf("read header metadata: %v", err)
}
buf := make([]byte, meta.HeaderSize)
err = binary.Read(reader, binary.LittleEndian, buf)
if err != nil {
return nil, fmt.Errorf("read header size: %v", err)
}
header := make(http.Header)
lines := bytes.Split(buf, []byte{0})
for _, line := range lines {
kv := bytes.SplitN(line, []byte{':'}, 2)
if len(kv) == 2 {
header.Add(
string(bytes.TrimSpace(kv[0])),
string(bytes.TrimSpace(kv[1])))
}
}
return header, nil
}
// Body returns the HTTP body.
// Body may read a file named "path/hash(url)_s".
func (e *Entry) Body() (io.ReadCloser, error) {
if e.dataSize1 == 0 {
return newSparseReader(e.hash, e.path)
}
name := filepath.Join(e.path, fmt.Sprintf("%016x_0", e.hash))
file, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("open body: %v", err)
}
defer close(file)
stream1 := make([]byte, e.dataSize1)
_, err = file.ReadAt(stream1, e.offset1)
if err != nil {
return nil, fmt.Errorf("read body: %v", err)
}
reader := bytes.NewReader(stream1)
return ioutil.NopCloser(reader), nil
}
func close(f *os.File) {
if err := f.Close(); err != nil {
log.Printf("Error closing file %s: %v\n", f.Name(), err)
}
}