@@ -66,7 +66,6 @@ import (
66
66
"fmt"
67
67
"mime"
68
68
"path"
69
- "reflect"
70
69
"unsafe"
71
70
)
72
71
@@ -105,28 +104,35 @@ type CURL struct {
105
104
progressFunction * func (float64 , float64 , float64 , float64 , interface {}) bool
106
105
fnmatchFunction * func (string , string , interface {}) int
107
106
// callback datas
108
- headerData , writeData , readData , progressData , fnmatchData * interface {}
107
+ headerData , writeData , readData , progressData , fnmatchData interface {}
109
108
// list of C allocs
110
109
mallocAllocs []* C.char
111
110
}
112
111
112
+ var context_map = make (map [uintptr ]* CURL )
113
+
113
114
// curl_easy_init - Start a libcurl easy session
114
115
func EasyInit () * CURL {
115
116
p := C .curl_easy_init ()
116
- return & CURL {handle : p , mallocAllocs : make ([]* C.char , 0 )} // other field defaults to nil
117
+ c := & CURL {handle : p , mallocAllocs : make ([]* C.char , 0 )} // other field defaults to nil
118
+ context_map [uintptr (p )] = c
119
+ return c
117
120
}
118
121
119
122
// curl_easy_duphandle - Clone a libcurl session handle
120
123
func (curl * CURL ) Duphandle () * CURL {
121
- p := curl .handle
122
- return & CURL {handle : C .curl_easy_duphandle (p )}
124
+ p := C .curl_easy_duphandle (curl .handle )
125
+ c := & CURL {handle : p }
126
+ context_map [uintptr (p )] = c
127
+ return c
123
128
}
124
129
125
130
// curl_easy_cleanup - End a libcurl easy session
126
131
func (curl * CURL ) Cleanup () {
127
132
p := curl .handle
128
133
C .curl_easy_cleanup (p )
129
134
curl .MallocFreeAfter (0 )
135
+ delete (context_map , uintptr (p ))
130
136
}
131
137
132
138
// curl_easy_setopt - set options for a curl easy handle
@@ -140,16 +146,16 @@ func (curl *CURL) Setopt(opt int, param interface{}) error {
140
146
switch {
141
147
// not really set
142
148
case opt == OPT_READDATA : // OPT_INFILE
143
- curl .readData = & param
149
+ curl .readData = param
144
150
return nil
145
151
case opt == OPT_PROGRESSDATA :
146
- curl .progressData = & param
152
+ curl .progressData = param
147
153
return nil
148
154
case opt == OPT_HEADERDATA : // also known as OPT_WRITEHEADER
149
- curl .headerData = & param
155
+ curl .headerData = param
150
156
return nil
151
157
case opt == OPT_WRITEDATA : // OPT_FILE
152
- curl .writeData = & param
158
+ curl .writeData = param
153
159
return nil
154
160
155
161
case opt == OPT_READFUNCTION :
@@ -158,8 +164,7 @@ func (curl *CURL) Setopt(opt int, param interface{}) error {
158
164
159
165
ptr := C .return_read_function ()
160
166
if err := newCurlError (C .curl_easy_setopt_pointer (p , C .CURLoption (opt ), ptr )); err == nil {
161
- return newCurlError (C .curl_easy_setopt_pointer (p , OPT_READDATA ,
162
- unsafe .Pointer (reflect .ValueOf (curl ).Pointer ())))
167
+ return newCurlError (C .curl_easy_setopt_pointer (p , OPT_READDATA , unsafe .Pointer (curl .handle )))
163
168
} else {
164
169
return err
165
170
}
@@ -170,8 +175,7 @@ func (curl *CURL) Setopt(opt int, param interface{}) error {
170
175
171
176
ptr := C .return_progress_function ()
172
177
if err := newCurlError (C .curl_easy_setopt_pointer (p , C .CURLoption (opt ), ptr )); err == nil {
173
- return newCurlError (C .curl_easy_setopt_pointer (p , OPT_PROGRESSDATA ,
174
- unsafe .Pointer (reflect .ValueOf (curl ).Pointer ())))
178
+ return newCurlError (C .curl_easy_setopt_pointer (p , OPT_PROGRESSDATA , unsafe .Pointer (curl .handle )))
175
179
} else {
176
180
return err
177
181
}
@@ -182,8 +186,7 @@ func (curl *CURL) Setopt(opt int, param interface{}) error {
182
186
183
187
ptr := C .return_header_function ()
184
188
if err := newCurlError (C .curl_easy_setopt_pointer (p , C .CURLoption (opt ), ptr )); err == nil {
185
- return newCurlError (C .curl_easy_setopt_pointer (p , OPT_HEADERDATA ,
186
- unsafe .Pointer (reflect .ValueOf (curl ).Pointer ())))
189
+ return newCurlError (C .curl_easy_setopt_pointer (p , OPT_HEADERDATA , unsafe .Pointer (curl .handle )))
187
190
} else {
188
191
return err
189
192
}
@@ -194,8 +197,7 @@ func (curl *CURL) Setopt(opt int, param interface{}) error {
194
197
195
198
ptr := C .return_write_function ()
196
199
if err := newCurlError (C .curl_easy_setopt_pointer (p , C .CURLoption (opt ), ptr )); err == nil {
197
- return newCurlError (C .curl_easy_setopt_pointer (p , OPT_WRITEDATA ,
198
- unsafe .Pointer (reflect .ValueOf (curl ).Pointer ())))
200
+ return newCurlError (C .curl_easy_setopt_pointer (p , OPT_WRITEDATA , unsafe .Pointer (curl .handle )))
199
201
} else {
200
202
return err
201
203
}
0 commit comments