Repository navigation
Expand file tree
/
Copy pathhttpcache.go
More file actions
420 lines (352 loc) · 9.52 KB
/
Copy pathhttpcache.go
File metadata and controls
420 lines (352 loc) · 9.52 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package httpcache
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/liuzl/store"
"github.com/projectdiscovery/useragent"
)
var (
cacheDir = flag.String("cache_dir", ".httpcache", "Directory for HTTP cache storage")
policiesFile = flag.String("policies_file", ".httpcache/policies.txt", "File containing cache policies, one per line in format: regex=duration")
)
type CacheEntry struct {
Data []byte `json:"data"`
URL string `json:"url"`
FinalURL string `json:"final_url"`
CrawledAt time.Time `json:"crawled_at"`
ExpiresAt time.Time `json:"expires_at"`
}
type CachePolicy struct {
Pattern *regexp.Regexp
TTL time.Duration
}
type Cache struct {
Store *store.LevelStore
Policies []CachePolicy
}
type HTTPClient struct {
cache *Cache
client *http.Client
}
var (
instance *HTTPClient
once sync.Once
)
func LoadPoliciesFromFile(filename string) ([]CachePolicy, error) {
defaultPolicy := CachePolicy{
Pattern: regexp.MustCompile(".*"),
TTL: 10 * time.Minute,
}
if filename == "" {
return []CachePolicy{defaultPolicy}, nil
}
file, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
return []CachePolicy{defaultPolicy}, nil
}
return nil, fmt.Errorf("failed to open policies file: %v", err)
}
defer file.Close()
policies := []CachePolicy{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comment-only lines
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Remove inline comments
if idx := strings.Index(line, "#"); idx != -1 {
line = strings.TrimSpace(line[:idx])
}
// Split on last = character
idx := strings.LastIndex(line, "=")
if idx == -1 {
return nil, fmt.Errorf("invalid policy format: %s", line)
}
pattern := strings.TrimSpace(line[:idx])
duration := strings.TrimSpace(line[idx+1:])
// Compile pattern
compiledPattern, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("invalid regex pattern: %s", err)
}
// Parse duration
parsedDuration, err := time.ParseDuration(duration)
if err != nil {
return nil, fmt.Errorf("invalid duration: %s", err)
}
policies = append(policies, CachePolicy{
Pattern: compiledPattern,
TTL: parsedDuration,
})
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading policies file: %v", err)
}
policies = append(policies, defaultPolicy)
return policies, nil
}
func GetClient() *HTTPClient {
once.Do(func() {
policies, err := LoadPoliciesFromFile(*policiesFile)
if err != nil {
log.Fatalf("Failed to load cache policies: %v", err)
}
store, err := store.NewLevelStore(*cacheDir + "/data")
if err != nil {
log.Fatalf("Failed to initialize cache: %v", err)
}
instance = &HTTPClient{
cache: &Cache{
Store: store,
Policies: policies,
},
client: &http.Client{},
}
})
if instance == nil {
log.Fatal("Failed to initialize HTTPClient")
}
return instance
}
func (c *Cache) GetTTL(url string) time.Duration {
for _, policy := range c.Policies {
if policy.Pattern.MatchString(url) {
return policy.TTL
}
}
return 0
}
func hashKey(url string) string {
hash := sha256.Sum256([]byte(url))
return hex.EncodeToString(hash[:])
}
type ContentValidator func([]byte) bool
// StatusError reports a non-2xx HTTP response. It carries the status code so
// callers can tell a permanent 404 from a transient 503 and decide whether to
// retry.
type StatusError struct {
Code int
URL string
}
func (e *StatusError) Error() string {
return fmt.Sprintf("unexpected HTTP status %d (%s) for %s", e.Code, http.StatusText(e.Code), e.URL)
}
// Temporary reports whether the status is one worth retrying (429, 5xx).
func (e *StatusError) Temporary() bool {
return e.Code == http.StatusTooManyRequests || e.Code >= 500
}
// checkStatus turns a non-2xx response into an error and drains the body, so an
// error page can never be mistaken for content.
//
// Without this, an error body is returned with err == nil and cached like any
// other page. Downstream parsers then extract zero items from it and report no
// error — the fetch "succeeds" with empty content. That is how a burst of HKJC
// 503s ("The service is unavailable.", 27 bytes) silently emptied whole pages
// for consumers of this library, and, in one case, drove a differential
// updater to delete the rows it could no longer see.
func checkStatus(resp *http.Response, url string) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
_, _ = io.Copy(io.Discard, resp.Body)
return &StatusError{Code: resp.StatusCode, URL: url}
}
func (hc *HTTPClient) GetWithValidator(url string, validator ContentValidator) ([]byte, string, error) {
key := hashKey(url)
ttl := hc.cache.GetTTL(url)
if ttl > 0 {
if value, finalURL, found := hc.cache.Get(key); found {
if validator == nil || validator(value) {
return value, finalURL, nil
}
// invalid cache, delete it
_ = hc.cache.Delete(key)
}
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, "", err
}
req.Header.Set("User-Agent", useragent.UserAgents[0].String())
resp, err := hc.client.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
finalURL := resp.Request.URL.String()
if err := checkStatus(resp, url); err != nil {
return nil, finalURL, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, finalURL, err
}
shouldCache := true
if validator != nil {
shouldCache = validator(body)
}
if shouldCache && ttl > 0 {
hc.cache.Set(key, body, url, finalURL, ttl)
}
return body, finalURL, nil
}
func (hc *HTTPClient) Get(url string) ([]byte, error) {
data, _, err := hc.GetWithValidator(url, nil)
return data, err
}
func (hc *HTTPClient) GetWithFinalURL(url string) ([]byte, string, error) {
return hc.GetWithValidator(url, nil)
}
func (c *Cache) Get(key string) ([]byte, string, bool) {
value, err := c.Store.Get(key)
if err != nil || value == nil {
return nil, "", false
}
var entry CacheEntry
if err := store.BytesToObject(value, &entry); err != nil {
return nil, "", false
}
// Check if entry has expired
now := time.Now()
isExpired := false
// If CrawledAt is set (not zero time), use it with the matching policy TTL
if !entry.CrawledAt.IsZero() {
ttl := c.GetTTL(entry.URL)
if now.Sub(entry.CrawledAt) > ttl {
isExpired = true
}
} else {
// Backward compatibility: use ExpiresAt for older entries
isExpired = now.After(entry.ExpiresAt)
}
if isExpired {
_ = c.Store.Delete(key)
return nil, "", false
}
return entry.Data, entry.FinalURL, true
}
func (c *Cache) Set(key string, data []byte, url string, finalURL string, ttl time.Duration) {
now := time.Now()
entry := CacheEntry{
Data: data,
URL: url,
FinalURL: finalURL,
CrawledAt: now,
ExpiresAt: now.Add(ttl),
}
encoded, err := store.ObjectToBytes(entry)
if err != nil {
log.Printf("Failed to encode cache entry: %v", err)
return
}
if err := c.Store.Put(key, encoded); err != nil {
log.Printf("Failed to store cache entry: %v", err)
}
}
func (hc *HTTPClient) Close() {
if err := hc.cache.Store.Close(); err != nil {
log.Printf("Failed to close cache: %v", err)
}
instance = nil
once = sync.Once{}
}
// NewClient creates a new HTTPClient instance with custom policies and cache directory
func NewClient(cacheDir string, policies []CachePolicy) (*HTTPClient, error) {
if cacheDir == "" {
return nil, fmt.Errorf("cache directory is required")
}
store, err := store.NewLevelStore(cacheDir + "/data")
if err != nil {
return nil, fmt.Errorf("failed to initialize cache: %+v", err)
}
return &HTTPClient{
cache: &Cache{
Store: store,
Policies: policies,
},
client: &http.Client{},
}, nil
}
func (hc *HTTPClient) Fetch(url string, validator ContentValidator) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", useragent.UserAgents[0].String())
resp, err := hc.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := checkStatus(resp, url); err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
ttl := hc.cache.GetTTL(url)
if ttl > 0 {
shouldCache := true
if validator != nil {
shouldCache = validator(body)
}
if shouldCache {
key := hashKey(url)
hc.cache.Set(key, body, url, "", ttl)
}
}
return body, nil
}
// Delete removes an entry from the cache
func (c *Cache) Delete(key string) error {
return c.Store.Delete(key)
}
// DeleteURL removes the cached entry for the given URL
func (hc *HTTPClient) DeleteURL(url string) error {
key := hashKey(url)
return hc.cache.Delete(key)
}
func (hc *HTTPClient) FetchWithFinalURL(url string) ([]byte, string, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, "", err
}
req.Header.Set("User-Agent", useragent.UserAgents[0].String())
resp, err := hc.client.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
finalURL := resp.Request.URL.String()
if err := checkStatus(resp, url); err != nil {
return nil, finalURL, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, finalURL, err
}
ttl := hc.cache.GetTTL(url)
if ttl > 0 {
key := hashKey(url)
hc.cache.Set(key, body, url, finalURL, ttl)
}
return body, finalURL, nil
}
func (hc *HTTPClient) GetStore() *store.LevelStore {
return hc.cache.Store
}