forked from psanford/sqlite3vfshttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3vfshttp.go
179 lines (139 loc) · 3.36 KB
/
sqlite3vfshttp.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
package sqlite3vfshttp
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/psanford/sqlite3vfs"
)
type HttpVFS struct {
URL string
CacheHandler CacheHandler
RoundTripper http.RoundTripper
}
type CacheHandler interface {
Get(p []byte, off int64) bool
Put(p []byte, off int64)
}
func (vfs *HttpVFS) Open(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File, sqlite3vfs.OpenFlag, error) {
tf := &httpFile{
url: vfs.URL,
cacheHandler: vfs.CacheHandler,
roundTripper: vfs.RoundTripper,
}
return tf, flags, nil
}
func (vfs *HttpVFS) Delete(name string, dirSync bool) error {
return sqlite3vfs.ReadOnlyError
}
func (vfs *HttpVFS) Access(name string, flag sqlite3vfs.AccessFlag) (bool, error) {
if strings.HasSuffix(name, "-wal") || strings.HasSuffix(name, "-journal") {
return false, nil
}
return true, nil
}
func (vfs *HttpVFS) FullPathname(name string) string {
return name
}
type httpFile struct {
url string
cacheHandler CacheHandler
roundTripper http.RoundTripper
}
func (tf *httpFile) Close() error {
return nil
}
func (tf *httpFile) client() *http.Client {
if tf.roundTripper == nil {
return http.DefaultClient
}
return &http.Client{
Transport: tf.roundTripper,
}
}
func (tf *httpFile) ReadAt(p []byte, off int64) (int, error) {
if tf.cacheHandler != nil {
if ok := tf.cacheHandler.Get(p, off); ok {
return len(p), nil
}
}
req, err := http.NewRequest("GET", tf.url, nil)
if err != nil {
return 0, err
}
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", off, off+int64(len(p)-1)))
resp, err := tf.client().Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
n, err := io.ReadFull(resp.Body, p)
if err != nil {
return 0, err
}
if tf.cacheHandler != nil {
tf.cacheHandler.Put(p, off)
}
return n, nil
}
func (tf *httpFile) WriteAt(b []byte, off int64) (n int, err error) {
return 0, sqlite3vfs.ReadOnlyError
}
func (tf *httpFile) Truncate(size int64) error {
return sqlite3vfs.ReadOnlyError
}
func (tf *httpFile) Sync(flag sqlite3vfs.SyncType) error {
return nil
}
var invalidContentRangeErr = errors.New("invalid Content-Range response")
func (tf *httpFile) FileSize() (int64, error) {
req, err := http.NewRequest("GET", tf.url, nil)
if err != nil {
return 0, err
}
req.Header.Set("Range", "bytes=0-0")
resp, err := tf.client().Do(req)
if err != nil {
return 0, err
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
rangeHeader := resp.Header.Get("Content-Range")
rangeFields := strings.Fields(rangeHeader)
if len(rangeFields) != 2 {
return 0, invalidContentRangeErr
}
if strings.ToLower(rangeFields[0]) != "bytes" {
return 0, invalidContentRangeErr
}
amts := strings.Split(rangeFields[1], "/")
if len(amts) != 2 {
return 0, invalidContentRangeErr
}
if amts[1] == "*" {
return 0, invalidContentRangeErr
}
n, err := strconv.Atoi(amts[1])
if err != nil {
return 0, invalidContentRangeErr
}
return int64(n), nil
}
func (tf *httpFile) Lock(elock sqlite3vfs.LockType) error {
return nil
}
func (tf *httpFile) Unlock(elock sqlite3vfs.LockType) error {
return nil
}
func (tf *httpFile) CheckReservedLock() (bool, error) {
return false, nil
}
func (tf *httpFile) SectorSize() int64 {
return 0
}
func (tf *httpFile) DeviceCharacteristics() sqlite3vfs.DeviceCharacteristic {
return sqlite3vfs.IocapImmutable
}