-
Notifications
You must be signed in to change notification settings - Fork 11
/
do.go
831 lines (693 loc) · 22.3 KB
/
do.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
"net/url"
"strings"
"time"
"github.com/augmentable-dev/vtab"
"go.riyazali.net/sqlite"
)
// timestamp layout to match SQLite's 'datetime()' format, ISO8601 subset
const sqliteDatetimeFormat = "2006-01-02 15:04:05.999"
// Format ghe given time as a SQLite date timestamp
func formatSqliteDatetime(t *time.Time) *string {
s := t.UTC().Format(sqliteDatetimeFormat)
return &s
}
// Give the result of the given HTTP request as a SQLite response, the body
func resultResponseBody(client *http.Client, request *http.Request, ctx *sqlite.Context) {
response, err := client.Do(request)
if err != nil {
ctx.ResultError(err)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
ctx.ResultError(err)
return
}
ctx.ResultBlob(body)
}
// Give the result of the given HTTP request as a SQLite response, the headers
func resultResponseHeaders(client *http.Client, request *http.Request, ctx *sqlite.Context) {
response, err := client.Do(request)
if err != nil {
ctx.ResultError(err)
return
}
buf := new(bytes.Buffer)
response.Header.Write(buf)
ctx.ResultText(buf.String())
}
/* http_do_body(method, url, headers, body, cookies)
* Perform a HTTP request with the given method, URL, headers,
* body, and cookies. Returns the HTTP body as a BLOB, errors if fails.
*/
type HttpDoBodyFunc struct{}
func (*HttpDoBodyFunc) Deterministic() bool { return true }
func (*HttpDoBodyFunc) Args() int { return -1 }
func (*HttpDoBodyFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values) < 2 || len(values) > 5 {
c.ResultError(errors.New("usage: http_do_body(method, url, headers, body, cookies)"))
return
}
method := values[0].Text()
url := values[1].Text()
var headers string
var cookies string
var body []byte
if len(values) >= 3 {
headers = values[2].Text()
}
if len(values) >= 4 {
body = values[3].Blob()
}
if len(values) >= 5 {
cookies = values[4].Text()
}
client, request, err := prepareRequest(&PrepareRequestParams{method: method, url: url, headers: headers, body: body, cookies: cookies})
if err != nil {
c.ResultError(err)
return
}
resultResponseBody(client, request, c)
}
/* http_post_body(url, headers, body, cookies)
* Perform a POST request with the given URL, headers,
* body, and cookies. Returns the HTTP body as a BLOB, errors if fails.
*/
type HttpPostBodyFunc struct{}
func (*HttpPostBodyFunc) Deterministic() bool { return true }
func (*HttpPostBodyFunc) Args() int { return -1 }
func (*HttpPostBodyFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values) < 1 || len(values) > 4 {
c.ResultError(errors.New("usage: http_post_body(url, headers, body, cookies)"))
return
}
url := values[0].Text()
var headers string
var cookies string
var body []byte
if len(values) >= 2 {
headers = values[1].Text()
}
if len(values) >= 3 {
body = values[2].Blob()
}
if len(values) >= 4 {
cookies = values[3].Text()
}
client, request, err := prepareRequest(&PrepareRequestParams{method: "POST", url: url, headers: headers, body: body, cookies: cookies})
if err != nil {
c.ResultError(err)
return
}
resultResponseBody(client, request, c)
}
/* http_get_body(url, headers, cookies)
* Perform a HTTP request with the given URL, headers, and cookies.
* Returns the HTTP body as a BLOB, errors if fails.
*/
type HttpGetBodyFunc struct{}
func (*HttpGetBodyFunc) Deterministic() bool { return true }
func (*HttpGetBodyFunc) Args() int { return -1 }
func (*HttpGetBodyFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values) < 1 || len(values) > 3 {
c.ResultError(errors.New("usage: http_get_body(url, headers, cookies)"))
return
}
url := values[0].Text()
var headers string
var cookies string
if len(values) >= 2 {
headers = values[1].Text()
}
if len(values) >= 3 {
cookies = values[2].Text()
}
client, request, err := prepareRequest(&PrepareRequestParams{method: "GET", url: url, headers: headers, body: nil, cookies: cookies})
if err != nil {
c.ResultError(err)
return
}
resultResponseBody(client, request, c)
}
/* http_get_headers(url, headers, body, cookies)
* Perform a GET request on the given URL, headers, body, and cookies.
* Returns the HTTP response headers in wire format, errors if fails.
*/
type HttpGetHeadersFunc struct{}
func (*HttpGetHeadersFunc) Deterministic() bool { return true }
func (*HttpGetHeadersFunc) Args() int { return -1 }
func (*HttpGetHeadersFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values) < 1 || len(values) > 3 {
c.ResultError(errors.New("usage: http_get_headers(url, headers, cookies)"))
return
}
url := values[0].Text()
var headers string
var cookies string
if len(values) >= 2 {
headers = values[1].Text()
}
if len(values) >= 3 {
cookies = values[2].Text()
}
client, request, err := prepareRequest(&PrepareRequestParams{method: "GET", url: url, headers: headers, body: nil, cookies: cookies})
if err != nil {
c.ResultError(err)
return
}
resultResponseHeaders(client, request, c)
}
// http_post_headers(url, headers, body, cookies)
type HttpPostHeadersFunc struct{}
func (*HttpPostHeadersFunc) Deterministic() bool { return true }
func (*HttpPostHeadersFunc) Args() int { return -1 }
func (*HttpPostHeadersFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values) < 1 || len(values) > 4 {
c.ResultError(errors.New("usage: http_post_headers(url, headers, body, cookies)"))
return
}
url := values[0].Text()
var headers string
var cookies string
var body []byte
if len(values) >= 2 {
headers = values[1].Text()
}
if len(values) >= 3 {
body = values[2].Blob()
}
if len(values) >= 3 {
cookies = values[3].Text()
}
client, request, err := prepareRequest(&PrepareRequestParams{method: "POST", url: url, headers: headers, body: body, cookies: cookies})
if err != nil {
c.ResultError(err)
}
resultResponseHeaders(client, request, c)
}
// http_do_headers(method, url, headers, body, cookies)
type HttpDoHeadersFunc struct{}
func (*HttpDoHeadersFunc) Deterministic() bool { return true }
func (*HttpDoHeadersFunc) Args() int { return -1 }
func (*HttpDoHeadersFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values) < 2 || len(values) > 5 {
c.ResultError(errors.New("usage: http_do_headers(method, url, headers, body, cookies)"))
return
}
method := values[0].Text()
url := values[1].Text()
var headers string
var cookies string
var body []byte
if len(values) >= 3 {
headers = values[2].Text()
}
if len(values) >= 4 {
body = values[3].Blob()
}
if len(values) >= 5 {
cookies = values[4].Text()
}
client, request, err := prepareRequest(&PrepareRequestParams{method: method, url: url, headers: headers, body: body, cookies: cookies})
if err != nil {
c.ResultError(err)
}
resultResponseHeaders(client, request, c)
}
var SharedDoTableColumns = []vtab.Column{
{Name: "request_url", Type: sqlite.SQLITE_TEXT.String()},
{Name: "request_method", Type: sqlite.SQLITE_TEXT.String()},
{Name: "request_headers", Type: sqlite.SQLITE_TEXT.String()},
{Name: "request_cookies", Type: sqlite.SQLITE_TEXT.String()},
{Name: "request_body", Type: sqlite.SQLITE_TEXT.String()},
{Name: "response_status", Type: sqlite.SQLITE_TEXT.String()},
{Name: "response_status_code", Type: sqlite.SQLITE_INTEGER.String()},
{Name: "response_headers", Type: sqlite.SQLITE_TEXT.String()},
{Name: "response_cookies", Type: sqlite.SQLITE_TEXT.String()},
{Name: "response_body", Type: sqlite.SQLITE_BLOB.String()},
{Name: "remote_address", Type: sqlite.SQLITE_TEXT.String()},
{Name: "timings", Type: sqlite.SQLITE_TEXT.String()},
{Name: "meta", Type: sqlite.SQLITE_TEXT.String()},
}
var GetTableColumns = append([]vtab.Column{
{Name: "url", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "headers", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "cookies", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
}, SharedDoTableColumns...)
var PostTableColumns = append([]vtab.Column{
{Name: "url", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "headers", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "body", Type: sqlite.SQLITE_BLOB.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "cookies", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
}, SharedDoTableColumns...)
var DoTableColumns = append([]vtab.Column{
{Name: "method", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "url", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "headers", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "body", Type: sqlite.SQLITE_BLOB.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
{Name: "cookies", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, OmitCheck: true}}},
}, SharedDoTableColumns...)
type Timings struct {
Started *time.Time
FirstResponseByte *time.Time
DNSStart *time.Time
DNSDone *time.Time
GotConn *time.Time
ConnectStart *time.Time
ConnectDone *time.Time
TLSHandshakeStart *time.Time
TLSHandshakeDone *time.Time
WroteHeaders *time.Time
BodyStart *time.Time
BodyEnd *time.Time
}
type TimingJSON struct {
Started *string `json:"start"`
FirstResponseByte *string `json:"first_byte"`
GotConn *string `json:"connection"`
WroteHeaders *string `json:"wrote_headers"`
DNSStart *string `json:"dns_start"`
DNSDone *string `json:"dns_end"`
ConnectStart *string `json:"connect_start"`
ConnectDone *string `json:"connect_end"`
TLSHandshakeStart *string `json:"tls_handshake_start"`
TLSHandshakeDone *string `json:"tls_handshake_end"`
BodyStart *string `json:"body_start"`
BodyEnd *string `json:"body_end"`
}
func (t Timings) MarshalJSON() ([]byte, error) {
tj := TimingJSON{}
if t.Started != nil {
tj.Started = formatSqliteDatetime(t.Started)
}
if t.FirstResponseByte != nil {
tj.FirstResponseByte = formatSqliteDatetime(t.FirstResponseByte)
}
if t.DNSStart != nil {
tj.DNSStart = formatSqliteDatetime(t.DNSStart)
}
if t.DNSDone != nil {
tj.DNSDone = formatSqliteDatetime(t.DNSDone)
}
if t.GotConn != nil {
tj.GotConn = formatSqliteDatetime(t.GotConn)
}
if t.ConnectStart != nil {
tj.ConnectStart = formatSqliteDatetime(t.ConnectStart)
}
if t.ConnectDone != nil {
tj.ConnectDone = formatSqliteDatetime(t.ConnectDone)
}
if t.TLSHandshakeStart != nil {
tj.TLSHandshakeStart = formatSqliteDatetime(t.TLSHandshakeStart)
}
if t.TLSHandshakeDone != nil {
tj.TLSHandshakeDone = formatSqliteDatetime(t.TLSHandshakeDone)
}
if t.WroteHeaders != nil {
tj.WroteHeaders = formatSqliteDatetime(t.WroteHeaders)
}
if t.BodyStart != nil {
tj.BodyStart = formatSqliteDatetime(t.BodyStart)
}
if t.BodyEnd != nil {
tj.BodyEnd = formatSqliteDatetime(t.BodyEnd)
}
return json.Marshal(tj)
}
type PrepareRequestParams struct {
method string
url string
headers string
body []byte
cookies string
}
// helper functions around http.NewRequest, takes headers/body in sqlite-http formats
func prepareRequest(params *PrepareRequestParams) (*http.Client, *http.Request, error) {
bodyReader := bytes.NewReader(params.body)
request, err := http.NewRequest(params.method, params.url, bodyReader)
if err != nil {
return nil, nil, err
}
if params.headers != "" {
h := readHeader(params.headers)
// step 1: clear default headers
for key := range request.Header {
request.Header.Del(key)
}
for key, values := range h {
for i := range values {
request.Header.Add(key, values[i])
}
}
}
if params.cookies != "" {
var parsed map[string]string
err := json.Unmarshal([]byte(params.cookies), &parsed)
if err != nil {
return nil, nil, errors.New("invalid cookes")
}
for name, value := range parsed {
request.AddCookie(&http.Cookie{
Name: name,
Value: value,
})
}
}
client := &http.Client{
Timeout: DoTimeout,
}
// block to rate limit properly
<-DoTicker.C
return client, request, nil
}
type HttpDoCursor struct {
current int
// Original HTTP request made
request *http.Request
// HTTP response to the original request
response *http.Response
// Timestamps of various lower-level events for the timings column
timing Timings
// Remote network address, filled in when HTTP connection is made, IP address
RemoteAddr string
response_body []byte
columns []vtab.Column
}
func (cur *HttpDoCursor) Column(ctx vtab.Context, c int) error {
col := cur.columns[c]
// TODO this should be a compile-time (?) option. response is nil bc commented out error handling in client.Do in GET
if strings.HasPrefix(col.Name, "response_") && cur.response == nil {
ctx.ResultNull()
return nil
}
switch col.Name {
case "url":
ctx.ResultText("")
case "headers":
ctx.ResultText("")
case "cookies":
ctx.ResultText("")
case "request_url":
ctx.ResultText(cur.request.URL.String())
case "request_method":
ctx.ResultText(cur.request.Method)
case "request_headers":
buf := new(bytes.Buffer)
cur.request.Header.Write(buf)
ctx.ResultText(buf.String())
case "request_cookies":
cookies := make([]string, len(cur.request.Cookies()))
for _, c := range cur.request.Cookies() {
cookies = append(cookies, c.Raw)
}
buf, err := json.Marshal(cookies)
if err != nil {
ctx.ResultError(err)
} else {
ctx.ResultText(string(buf))
}
case "request_body":
body, err := ioutil.ReadAll(cur.request.Body)
if err != nil {
ctx.ResultError(err)
}
ctx.ResultBlob(body)
case "response_status":
ctx.ResultText(cur.response.Status)
case "response_status_code":
ctx.ResultInt(cur.response.StatusCode)
case "response_headers":
buf := new(bytes.Buffer)
cur.response.Header.Write(buf)
ctx.ResultText(buf.String())
case "response_cookies":
cookies := make([]string, len(cur.response.Cookies()))
for _, c := range cur.response.Cookies() {
cookies = append(cookies, c.Raw)
}
buf, err := json.Marshal(cookies)
if err != nil {
ctx.ResultError(err)
} else {
ctx.ResultText(string(buf))
}
case "response_body":
if cur.response_body == nil {
start := time.Now()
cur.timing.BodyStart = &start
body, err := ioutil.ReadAll(cur.response.Body)
end := time.Now()
cur.timing.BodyEnd = &end
if err != nil {
ctx.ResultError(err)
} else {
ctx.ResultBlob(body)
cur.response_body = body
}
} else {
ctx.ResultBlob(cur.response_body)
}
case "remote_address":
ctx.ResultText(cur.RemoteAddr)
case "timings":
buf, err := json.Marshal(cur.timing)
if err != nil {
ctx.ResultError(err)
return nil
}
ctx.ResultText(string(buf))
case "meta":
ctx.ResultNull()
}
return nil
}
// one row for now
func (cur *HttpDoCursor) Next() (vtab.Row, error) {
cur.current += 1
if cur.current >= 1 {
return nil, io.EOF
}
return cur, nil
}
func GetTableIterator(constraints []*vtab.Constraint, order []*sqlite.OrderBy) (vtab.Iterator, error) {
var headers string
var cookies string
url := ""
for _, constraint := range constraints {
if constraint.Op == sqlite.INDEX_CONSTRAINT_EQ {
column := GetTableColumns[constraint.ColIndex]
switch column.Name {
case "url":
url = constraint.Value.Text()
case "headers":
headers = constraint.Value.Text()
case "cookies":
cookies = constraint.Value.Text()
}
}
}
cursor := HttpDoCursor{
columns: GetTableColumns,
}
client, request, err := prepareRequest(&PrepareRequestParams{method: "GET", url: url, headers: headers, body: nil, cookies: cookies})
if err != nil {
return nil, sqlite.SQLITE_ERROR
}
request = traceAndInclude(request, &cursor)
started := time.Now()
cursor.timing.Started = &started
response, err := client.Do(request)
if err != nil {
return nil, sqlite.SQLITE_ERROR
}
cursor.current = -1
cursor.request = request
cursor.response = response
return &cursor, nil
}
func PostTableIterator(constraints []*vtab.Constraint, order []*sqlite.OrderBy) (vtab.Iterator, error) {
var headers string
var cookies string
var body []byte
url := ""
for _, constraint := range constraints {
if constraint.Op == sqlite.INDEX_CONSTRAINT_EQ {
column := PostTableColumns[constraint.ColIndex]
switch column.Name {
case "url":
url = constraint.Value.Text()
case "headers":
headers = constraint.Value.Text()
case "body":
body = constraint.Value.Blob()
case "cookies":
cookies = constraint.Value.Text()
}
}
}
cursor := HttpDoCursor{
columns: PostTableColumns,
}
client, request, err := prepareRequest(&PrepareRequestParams{method: "POST", url: url, headers: headers, body: body, cookies: cookies})
if err != nil {
return nil, fmt.Errorf("error preparing request: %s", err)
}
request = traceAndInclude(request, &cursor)
started := time.Now()
cursor.timing.Started = &started
response, err := client.Do(request)
// TODO make this configurable. I don't want it to always error
// if there's some connection error, but maybe other want that
if err != nil {
return nil, fmt.Errorf("error on client.Do: %s", err)
}
cursor.current = -1
cursor.request = request
cursor.response = response
return &cursor, nil
}
func DoTableIterator(constraints []*vtab.Constraint, order []*sqlite.OrderBy) (vtab.Iterator, error) {
var method string
var headers string
var cookies string
var body []byte
url := ""
for _, constraint := range constraints {
if constraint.Op == sqlite.INDEX_CONSTRAINT_EQ {
column := DoTableColumns[constraint.ColIndex]
switch column.Name {
case "method":
method = constraint.Value.Text()
case "url":
url = constraint.Value.Text()
case "headers":
headers = constraint.Value.Text()
case "body":
body = constraint.Value.Blob()
case "cookies":
cookies = constraint.Value.Text()
}
}
}
cursor := HttpDoCursor{
columns: DoTableColumns,
}
client, request, err := prepareRequest(&PrepareRequestParams{method: method, url: url, headers: headers, body: body, cookies: cookies})
if err != nil {
return nil, fmt.Errorf("error preparing request: %s", err)
}
request = traceAndInclude(request, &cursor)
t := time.Now()
cursor.timing.Started = &t
response, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("error on client.Do: %s", err)
}
cursor.current = -1
cursor.request = request
cursor.response = response
return &cursor, nil
}
// For the given HTTP request, write all timing info to the given
// cursor's "timing" object, so we can surface as a column later
func traceAndInclude(request *http.Request, cursor *HttpDoCursor) *http.Request {
trace := &httptrace.ClientTrace{
GotFirstResponseByte: func() {
t := time.Now()
cursor.timing.FirstResponseByte = &t
},
DNSStart: func(i httptrace.DNSStartInfo) {
t := time.Now()
cursor.timing.DNSStart = &t
},
DNSDone: func(i httptrace.DNSDoneInfo) {
t := time.Now()
cursor.timing.DNSDone = &t
},
ConnectStart: func(network string, addr string) {
t := time.Now()
cursor.timing.ConnectStart = &t
},
ConnectDone: func(network, addr string, err error) {
t := time.Now()
cursor.timing.ConnectDone = &t
},
GotConn: func(g httptrace.GotConnInfo) {
t := time.Now()
cursor.timing.GotConn = &t
cursor.RemoteAddr = g.Conn.RemoteAddr().String()
},
TLSHandshakeStart: func() {
t := time.Now()
cursor.timing.TLSHandshakeStart = &t
},
TLSHandshakeDone: func(c tls.ConnectionState, e error) {
t := time.Now()
cursor.timing.TLSHandshakeDone = &t
},
WroteHeaders: func() {
t := time.Now()
cursor.timing.WroteHeaders = &t
},
}
return request.WithContext(httptrace.WithClientTrace(request.Context(), trace))
}
// http_post_form_url_encoded(name1, value1, ...)
type HttpPostFormUrlEncoded struct{}
func (*HttpPostFormUrlEncoded) Deterministic() bool { return true }
func (*HttpPostFormUrlEncoded) Args() int { return -1 }
func (*HttpPostFormUrlEncoded) Apply(c *sqlite.Context, values ...sqlite.Value) {
if len(values)%2 != 0 {
c.ResultError(errors.New("http_post_form_url_encoded must have even-numbered arguments"))
return
}
data := url.Values{}
for i := 0; i < len(values); i = i + 2 {
key := values[i].Text()
value := values[i+1].Text()
data.Set(key, value)
}
c.ResultText(data.Encode())
}
// TODO HttpPostMultipartForm
var DoModules = map[string]sqlite.Module{
"http_get": vtab.NewTableFunc("http_get", GetTableColumns, GetTableIterator),
"http_post": vtab.NewTableFunc("http_post", PostTableColumns, PostTableIterator),
"http_do": vtab.NewTableFunc("http_do", DoTableColumns, DoTableIterator),
}
var DoFunctions = map[string]sqlite.Function{
"http_get_body": &HttpGetBodyFunc{},
"http_post_body": &HttpPostBodyFunc{},
"http_do_body": &HttpDoBodyFunc{},
"http_get_headers": &HttpGetHeadersFunc{},
"http_post_headers": &HttpPostHeadersFunc{},
"http_do_headers": &HttpDoHeadersFunc{},
"http_post_form_urlencoded": &HttpPostFormUrlEncoded{},
"http_rate_limit": &HttpRateLimit{},
"http_timeout_set": &HttpTimeoutSet{},
}
func RegisterDo(api *sqlite.ExtensionApi) error {
for name, module := range DoModules {
if err := api.CreateModule(name, module); err != nil {
return err
}
}
for name, function := range DoFunctions {
if err := api.CreateFunction(name, function); err != nil {
return err
}
}
return nil
}