-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl_example.go
54 lines (45 loc) · 1.58 KB
/
curl_example.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
package main
import "C"
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// curl is example of stateful UDF function, just for fun.
// It takes 1 string param with url, and returns str contents of resource
// You can load it into the daemon with
// CREATE FUNCTION curl RETURNS STRING SONAME 'udfexample.so';
// SELECT curl('https://yandex.ru/robots.txt');
// in this function we check only num of args (1) and the type (must be string)
// Also we set zero to init value
//export curl_init
func curl_init(init *SPH_UDF_INIT, args *SPH_UDF_ARGS, errmsg *ERR_MSG) int32 {
sphWarning("invoked curl_init")
// check argument count
if args.arg_count != 1 || args.arg_type(0) != SPH_UDF_TYPE_STRING {
return errmsg.say("WEB() requires 1 string argument")
}
return 0
}
// here we execute provided action: extract arguments, make necessary calculations and return result back
//export curl
func curl(init *SPH_UDF_INIT, args *SPH_UDF_ARGS, errf *ERR_FLAG) uintptr {
url := args.stringval(0)
// Get the data
resp, _ := http.Get(url)
defer func() { _ = resp.Body.Close() }()
// Check server response
if resp.StatusCode != http.StatusOK {
return args.return_string(fmt.Sprintf("Bad status: %s", resp.Status))
}
// let's check content-type and avoid anything, but text
contentType := resp.Header.Get("Content-Type")
parts := strings.Split(contentType, "/")
if len(parts) >= 1 && parts[0] == "text" {
// retrieve whole body
text, _ := ioutil.ReadAll(resp.Body)
return args.return_string(string(text))
}
return args.return_string("Content type: " + contentType + ", will NOT download")
}