11mutable struct Easy
22 handle :: Ptr{Cvoid}
3- input :: Union{Vector{UInt8},Nothing}
3+ input :: IO
44 ready :: Threads.Event
55 seeker :: Union{Function,Nothing}
66 output :: IO
@@ -11,15 +11,14 @@ mutable struct Easy
1111 errbuf :: Vector{UInt8}
1212end
1313
14- const EMPTY_BYTE_VECTOR = UInt8[]
15-
1614function Easy (
17- output:: IO ,
18- progress:: Union{Function,Nothing} ,
15+ input :: IO ,
16+ output :: IO ,
17+ progress :: Union{Function,Nothing} ,
1918)
2019 easy = Easy (
2120 curl_easy_init (),
22- EMPTY_BYTE_VECTOR ,
21+ input ,
2322 Threads. Event (),
2423 nothing ,
2524 output,
@@ -287,50 +286,28 @@ end
287286# callbacks
288287
289288function header_callback (
290- data :: Ptr{Cchar } ,
289+ data :: Ptr{UInt8 } ,
291290 size :: Csize_t ,
292291 count :: Csize_t ,
293292 easy_p :: Ptr{Cvoid} ,
294293):: Csize_t
295294 easy = unsafe_pointer_to_objref (easy_p):: Easy
296- n = size * count
295+ n = size* count
297296 hdr = unsafe_string (data, n)
298297 push! (easy. res_hdrs, hdr)
299298 return n
300299end
301300
302- # feed data to read_callback
303- function upload_data (easy:: Easy , input:: IO )
304- while true
305- data = eof (input) ? nothing : readavailable (input)
306- easy. input === nothing && break
307- easy. input = data
308- curl_easy_pause (easy. handle, Curl. CURLPAUSE_CONT)
309- wait (easy. ready)
310- easy. input === nothing && break
311- easy. ready = Threads. Event ()
312- end
313- end
314-
315301function read_callback (
316- data :: Ptr{Cchar } ,
302+ data :: Ptr{UInt8 } ,
317303 size :: Csize_t ,
318304 count :: Csize_t ,
319305 easy_p :: Ptr{Cvoid} ,
320306):: Csize_t
321307 easy = unsafe_pointer_to_objref (easy_p):: Easy
322- buf = easy. input
323- if buf === nothing
324- notify (easy. ready)
325- return 0 # done uploading
326- end
327- if isempty (buf)
328- notify (easy. ready)
329- return CURL_READFUNC_PAUSE # wait for more data
330- end
331- n = min (size * count, length (buf))
332- ccall (:memcpy , Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), data, buf, n)
333- deleteat! (buf, 1 : n)
308+ eof (easy. input) && return 0
309+ buf = unsafe_wrap (Vector{UInt8}, data, size* count)
310+ n = readbytes! (easy. input, buf, size* count)
334311 return n
335312end
336313
@@ -347,7 +324,7 @@ function seek_callback(
347324 easy. seeker === nothing && return CURL_SEEKFUNC_CANTSEEK
348325 try easy. seeker (offset)
349326 catch err
350- @async @error (" seek_callback: seeker failed" , err)
327+ @async @error (" seek_callback: seek failed" , err)
351328 return CURL_SEEKFUNC_FAIL
352329 end
353330 return CURL_SEEKFUNC_OK
@@ -387,7 +364,7 @@ function add_callbacks(easy::Easy)
387364
388365 # set header callback
389366 header_cb = @cfunction (header_callback,
390- Csize_t, (Ptr{Cchar }, Csize_t, Csize_t, Ptr{Cvoid}))
367+ Csize_t, (Ptr{UInt8 }, Csize_t, Csize_t, Ptr{Cvoid}))
391368 setopt (easy, CURLOPT_HEADERFUNCTION, header_cb)
392369 setopt (easy, CURLOPT_HEADERDATA, easy_p)
393370
@@ -410,7 +387,7 @@ function add_upload_callbacks(easy::Easy)
410387
411388 # set read callback
412389 read_cb = @cfunction (read_callback,
413- Csize_t, (Ptr{Cchar }, Csize_t, Csize_t, Ptr{Cvoid}))
390+ Csize_t, (Ptr{UInt8 }, Csize_t, Csize_t, Ptr{Cvoid}))
414391 setopt (easy, CURLOPT_READFUNCTION, read_cb)
415392 setopt (easy, CURLOPT_READDATA, easy_p)
416393end
0 commit comments