-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsqlitehttpcli.go
112 lines (91 loc) · 2.14 KB
/
sqlitehttpcli.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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/psanford/sqlite3vfs"
"github.com/psanford/sqlite3vfshttp"
)
var url = flag.String("url", "", "URL of sqlite db")
var query = flag.String("query", "", "Query to run")
var referer = flag.String("referer", "", "HTTP Referer")
var userAgent = flag.String("user-agent", "", "HTTP User agent")
func main() {
flag.Parse()
if *url == "" || *query == "" {
log.Printf("-url and -query are required")
flag.Usage()
os.Exit(1)
}
vfs := sqlite3vfshttp.HttpVFS{
URL: *url,
RoundTripper: &roundTripper{
referer: *referer,
userAgent: *userAgent,
},
}
err := sqlite3vfs.RegisterVFS("httpvfs", &vfs)
if err != nil {
log.Fatalf("register vfs err: %s", err)
}
db, err := sql.Open("sqlite3", "not_a_real_name.db?vfs=httpvfs&mode=ro")
if err != nil {
log.Fatalf("open db err: %s", err)
}
rows, err := db.Query(*query)
if err != nil {
log.Fatalf("query err: %s", err)
}
cols, _ := rows.Columns()
for rows.Next() {
rows.Columns()
columns := make([]*string, len(cols))
columnPointers := make([]interface{}, len(cols))
for i := range columns {
columnPointers[i] = &columns[i]
}
err = rows.Scan(columnPointers...)
if err != nil {
log.Fatalf("query rows err: %s", err)
}
names := make([]string, 0, len(columns))
for _, col := range columns {
if col == nil {
names = append(names, "NULL")
} else {
names = append(names, *col)
}
}
fmt.Printf("row: %+v\n", names)
}
err = rows.Close()
if err != nil {
log.Fatalf("query rows err: %s", err)
}
}
type roundTripper struct {
referer string
userAgent string
}
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if rt.referer != "" {
req.Header.Set("Referer", rt.referer)
}
if rt.userAgent != "" {
req.Header.Set("User-Agent", rt.userAgent)
}
tr := http.DefaultTransport
if req.URL.Scheme == "file" {
path := req.URL.Path
root := filepath.Dir(path)
base := filepath.Base(path)
tr = http.NewFileTransport(http.Dir(root))
req.URL.Path = base
}
return tr.RoundTrip(req)
}