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
@@ -9,17 +9,17 @@ mutable struct Easy
99 res_hdrs :: Vector{String}
1010 code :: CURLcode
1111 errbuf :: Vector{UInt8}
12+ readbuf :: Vector{UInt8}
1213end
1314
14- const EMPTY_BYTE_VECTOR = UInt8[]
15-
1615function Easy (
17- output:: IO ,
18- progress:: Union{Function,Nothing} ,
16+ input :: IO ,
17+ output :: IO ,
18+ progress :: Union{Function,Nothing} ,
1919)
2020 easy = Easy (
2121 curl_easy_init (),
22- EMPTY_BYTE_VECTOR ,
22+ input ,
2323 Threads. Event (),
2424 nothing ,
2525 output,
@@ -28,6 +28,7 @@ function Easy(
2828 String[],
2929 typemax (CURLcode),
3030 zeros (UInt8, CURL_ERROR_SIZE),
31+ zeros (UInt8, 0 ),
3132 )
3233 finalizer (done!, easy)
3334 add_callbacks (easy)
@@ -293,44 +294,22 @@ function header_callback(
293294 easy_p :: Ptr{Cvoid} ,
294295):: Csize_t
295296 easy = unsafe_pointer_to_objref (easy_p):: Easy
296- n = size * count
297+ n = size* count
297298 hdr = unsafe_string (data, n)
298299 push! (easy. res_hdrs, hdr)
299300 return n
300301end
301302
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-
315303function read_callback (
316304 data :: Ptr{Cchar} ,
317305 size :: Csize_t ,
318306 count :: Csize_t ,
319307 easy_p :: Ptr{Cvoid} ,
320308):: Csize_t
321309 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)
310+ eof (easy. input) && return 0
311+ n = readbytes! (easy. input, easy. readbuf, size* count)
312+ ccall (:memcpy , Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), data, easy. readbuf, n)
334313 return n
335314end
336315
@@ -347,7 +326,7 @@ function seek_callback(
347326 easy. seeker === nothing && return CURL_SEEKFUNC_CANTSEEK
348327 try easy. seeker (offset)
349328 catch err
350- @async @error (" seek_callback: seeker failed" , err)
329+ @async @error (" seek_callback: seek failed" , err)
351330 return CURL_SEEKFUNC_FAIL
352331 end
353332 return CURL_SEEKFUNC_OK
0 commit comments