-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
72 lines (64 loc) · 1.39 KB
/
response.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
package mmdbserver
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"hash"
"io"
"io/fs"
"net/http"
"time"
)
type Response struct {
gzipped bytes.Buffer
md5Hash hash.Hash
ModTime time.Time
}
func NewResponse() (response *Response) {
return &Response{
md5Hash: md5.New(),
ModTime: time.Now(),
}
}
func (r *Response) ReadFile(file fs.File) (n int64, err error) {
if file == nil {
err = ErrDatabaseNotFound
return
}
var stat fs.FileInfo
if stat, err = file.Stat(); err != nil || stat.IsDir() {
err = ErrDatabaseNotFound
return
}
r.ModTime = stat.ModTime()
return r.ReadFrom(file)
}
func (r *Response) ReadFrom(file io.Reader) (n int64, err error) {
if file == nil {
err = ErrDatabaseNotFound
return
}
r.gzipped.Reset()
r.md5Hash.Reset()
return io.Copy(
io.MultiWriter(gzip.NewWriter(&r.gzipped), r.md5Hash),
file,
)
}
func (r *Response) WriteTo(w io.Writer) (n int64, err error) {
if rw, ok := w.(http.ResponseWriter); ok {
header := rw.Header()
header.Set("Content-Type", "application/x-gzip")
header.Set("Content-Encoding", "gzip")
header.Set("Last-Modified", r.ModTime.UTC().Format(http.TimeFormat))
header.Set("X-Database-Md5", hex.EncodeToString(r.MD5Hash()))
}
return r.gzipped.WriteTo(w)
}
func (r *Response) MD5Hash() []byte {
return r.md5Hash.Sum(nil)
}
func (r *Response) Valid() bool {
return r.gzipped.Len() > 0 && !r.ModTime.IsZero()
}