-
Notifications
You must be signed in to change notification settings - Fork 130
/
callback.go
59 lines (52 loc) · 1.57 KB
/
callback.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
package curl
/*
#cgo freebsd CFLAGS: -I/usr/local/include
#cgo freebsd LDFLAGS: -L/usr/local/lib -lcurl
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
*/
import "C"
import (
"unsafe"
)
//export goCallHeaderFunction
func goCallHeaderFunction(ptr *C.char, size C.size_t, ctx unsafe.Pointer) uintptr {
curl := context_map.Get(uintptr(ctx))
buf := C.GoBytes(unsafe.Pointer(ptr), C.int(size))
if (*curl.headerFunction)(buf, curl.headerData) {
return uintptr(size)
}
return C.CURL_WRITEFUNC_PAUSE
}
//export goCallWriteFunction
func goCallWriteFunction(ptr *C.char, size C.size_t, ctx unsafe.Pointer) uintptr {
curl := context_map.Get(uintptr(ctx))
buf := C.GoBytes(unsafe.Pointer(ptr), C.int(size))
if (*curl.writeFunction)(buf, curl.writeData) {
return uintptr(size)
}
return C.CURL_WRITEFUNC_PAUSE
}
//export goCallProgressFunction
func goCallProgressFunction(dltotal, dlnow, ultotal, ulnow C.double, ctx unsafe.Pointer) int {
curl := context_map.Get(uintptr(ctx))
if (*curl.progressFunction)(float64(dltotal), float64(dlnow),
float64(ultotal), float64(ulnow),
curl.progressData) {
return 0
}
return 1
}
//export goCallReadFunction
func goCallReadFunction(ptr *C.char, size C.size_t, ctx unsafe.Pointer) uintptr {
curl := context_map.Get(uintptr(ctx))
buf := C.GoBytes(unsafe.Pointer(ptr), C.int(size))
ret := (*curl.readFunction)(buf, curl.readData)
str := C.CString(string(buf))
defer C.free(unsafe.Pointer(str))
if C.memcpy(unsafe.Pointer(ptr), unsafe.Pointer(str), C.size_t(ret)) == nil {
panic("read_callback memcpy error!")
}
return uintptr(ret)
}