Skip to content

Commit 2958934

Browse files
rectify control of read callback
1 parent 92d1f78 commit 2958934

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

src/Curl/Easy.jl

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mutable 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}
1212
end
1313

14-
const EMPTY_BYTE_VECTOR = UInt8[]
15-
1614
function 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

289288
function 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
300299
end
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-
315301
function 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
335312
end
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)
416393
end

src/Curl/Multi.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ function check_multi_info(multi::Multi)
8585
easy = unsafe_pointer_to_objref(easy_p_ref[])::Easy
8686
@assert easy_handle == easy.handle
8787
easy.code = message.code
88-
easy.input = nothing
8988
notify(easy.ready)
9089
else
9190
@async @error("curl_multi_info_read: unknown message", message)

src/Downloads.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function request(
308308
progress = p_func(progress, input, output)
309309
arg_read(input) do input
310310
arg_write(output) do output
311-
with_handle(Easy(output, progress)) do easy
311+
with_handle(Easy(input, output, progress)) do easy
312312
# setup the request
313313
set_url(easy, url)
314314
set_timeout(easy, timeout)
@@ -343,13 +343,7 @@ function request(
343343

344344
# do the request
345345
add_handle(downloader.multi, easy)
346-
try # ensure handle is removed
347-
@sync begin
348-
if have_input
349-
@async upload_data(easy, input)
350-
end
351-
@async wait(easy.ready)
352-
end
346+
try wait(easy.ready) # can this throw?
353347
finally
354348
remove_handle(downloader.multi, easy)
355349
end

0 commit comments

Comments
 (0)