Skip to content

Commit d191d98

Browse files
committed
allow multiple response_body references, closes #27
1 parent e6cc6a7 commit d191d98

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

do.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ type HttpDoCursor struct {
466466
// Remote network address, filled in when HTTP connection is made, IP address
467467
RemoteAddr string
468468

469+
response_body []byte
470+
469471
columns []vtab.Column
470472
}
471473

@@ -531,18 +533,24 @@ func (cur *HttpDoCursor) Column(ctx vtab.Context, c int) error {
531533
ctx.ResultText(string(buf))
532534
}
533535
case "response_body":
534-
start := time.Now()
535-
cur.timing.BodyStart = &start
536-
537-
body, err := ioutil.ReadAll(cur.response.Body)
538-
end := time.Now()
539-
cur.timing.BodyEnd = &end
540-
541-
if err != nil {
542-
ctx.ResultError(err)
536+
if cur.response_body == nil {
537+
start := time.Now()
538+
cur.timing.BodyStart = &start
539+
540+
body, err := ioutil.ReadAll(cur.response.Body)
541+
end := time.Now()
542+
cur.timing.BodyEnd = &end
543+
544+
if err != nil {
545+
ctx.ResultError(err)
546+
} else {
547+
ctx.ResultBlob(body)
548+
cur.response_body = body
549+
}
543550
} else {
544-
ctx.ResultBlob(body)
551+
ctx.ResultBlob(cur.response_body)
545552
}
553+
546554
case "remote_address":
547555
ctx.ResultText(cur.RemoteAddr)
548556
case "timings":

tests/test-loadable.py

+10
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ def test_http_get(self):
173173
self.assertTrue(d["remote_address"] in ("127.0.0.1:8080", "[::1]:8080"))
174174
self.assertEqual(d["meta"], None)
175175

176+
@skip_do
177+
def test_http_get_multiple_response_body(self):
178+
d = db.execute("select response_body as r1, response_body as r2 from http_get('http://localhost:8080/base64/YWxleA==')").fetchone()
179+
self.assertEqual(d["r1"], b"alex")
180+
self.assertEqual(d["r2"], b"alex")
181+
182+
d = db.execute("select response_body from json_each('[\"YWxleA==\", \"YW5nZWw=\"]') join http_get('http://localhost:8080/base64/' || value)").fetchall()
183+
self.assertEqual(d[0]["response_body"], b"alex")
184+
self.assertEqual(d[1]["response_body"], b"angel")
185+
176186
@skip_do
177187
def test_http_get_body(self):
178188
d, = db.execute("""

0 commit comments

Comments
 (0)