forked from andelf/go-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
62 lines (52 loc) · 1.46 KB
/
share.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
package curl
/*
#include <curl/curl.h>
static CURLSHcode curl_share_setopt_long(CURLSH *handle, CURLSHoption option, long parameter) {
return curl_share_setopt(handle, option, parameter);
}
static CURLSHcode curl_share_setopt_pointer(CURLSH *handle, CURLSHoption option, void *parameter) {
return curl_share_setopt(handle, option, parameter);
}
*/
import "C"
import "unsafe"
// implement os.Error interface
type CurlShareError C.CURLMcode
func (e CurlShareError) Error() string {
// ret is const char*, no need to free
ret := C.curl_share_strerror(C.CURLSHcode(e))
return C.GoString(ret)
}
func newCurlShareError(errno C.CURLSHcode) error {
if errno == 0 { // if nothing wrong
return nil
}
return CurlShareError(errno)
}
type CURLSH struct {
handle unsafe.Pointer
}
func ShareInit() *CURLSH {
p := C.curl_share_init()
return &CURLSH{p}
}
func (shcurl *CURLSH) Cleanup() error {
p := shcurl.handle
return newCurlShareError(C.curl_share_cleanup(p))
}
func (shcurl *CURLSH) Setopt(opt int, param interface{}) error {
p := shcurl.handle
if param == nil {
return newCurlShareError(C.curl_share_setopt_pointer(p, C.CURLSHoption(opt), nil))
}
switch opt {
// case SHOPT_LOCKFUNC, SHOPT_UNLOCKFUNC, SHOPT_USERDATA:
// panic("not supported")
case SHOPT_SHARE, SHOPT_UNSHARE:
if val, ok := param.(int); ok {
return newCurlShareError(C.curl_share_setopt_long(p, C.CURLSHoption(opt), C.long(val)))
}
}
panic("not supported CURLSH.Setopt opt or param")
return nil
}