Skip to content

Commit 92d1f78

Browse files
rectify control of write callback
1 parent 0855f96 commit 92d1f78

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

src/Curl/Easy.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mutable struct Easy
33
input :: Union{Vector{UInt8},Nothing}
44
ready :: Threads.Event
55
seeker :: Union{Function,Nothing}
6-
output :: Channel{Vector{UInt8}}
6+
output :: IO
77
progress :: Function
88
req_hdrs :: Ptr{curl_slist_t}
99
res_hdrs :: Vector{String}
@@ -13,13 +13,16 @@ end
1313

1414
const EMPTY_BYTE_VECTOR = UInt8[]
1515

16-
function Easy(progress::Union{Function,Nothing})
16+
function Easy(
17+
output::IO,
18+
progress::Union{Function,Nothing},
19+
)
1720
easy = Easy(
1821
curl_easy_init(),
1922
EMPTY_BYTE_VECTOR,
2023
Threads.Event(),
2124
nothing,
22-
Channel{Vector{UInt8}}(Inf),
25+
output,
2326
something(progress, (_, _, _, _) -> nothing),
2427
C_NULL,
2528
String[],
@@ -351,17 +354,14 @@ function seek_callback(
351354
end
352355

353356
function write_callback(
354-
data :: Ptr{Cchar},
357+
data :: Ptr{UInt8},
355358
size :: Csize_t,
356359
count :: Csize_t,
357360
easy_p :: Ptr{Cvoid},
358361
)::Csize_t
359362
easy = unsafe_pointer_to_objref(easy_p)::Easy
360-
n = size * count
361-
buf = Array{UInt8}(undef, n)
362-
ccall(:memcpy, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), buf, data, n)
363-
put!(easy.output, buf)
364-
return n
363+
unsafe_write(easy.output, data, size*count)
364+
return size*count
365365
end
366366

367367
function progress_callback(
@@ -393,7 +393,7 @@ function add_callbacks(easy::Easy)
393393

394394
# set write callback
395395
write_cb = @cfunction(write_callback,
396-
Csize_t, (Ptr{Cchar}, Csize_t, Csize_t, Ptr{Cvoid}))
396+
Csize_t, (Ptr{UInt8}, Csize_t, Csize_t, Ptr{Cvoid}))
397397
setopt(easy, CURLOPT_WRITEFUNCTION, write_cb)
398398
setopt(easy, CURLOPT_WRITEDATA, easy_p)
399399

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-
close(easy.output)
8988
easy.input = nothing
9089
notify(easy.ready)
9190
else

src/Downloads.jl

Lines changed: 2 additions & 4 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(progress)) do easy
311+
with_handle(Easy(output, progress)) do easy
312312
# setup the request
313313
set_url(easy, url)
314314
set_timeout(easy, timeout)
@@ -345,12 +345,10 @@ function request(
345345
add_handle(downloader.multi, easy)
346346
try # ensure handle is removed
347347
@sync begin
348-
@async for buf in easy.output
349-
write(output, buf)
350-
end
351348
if have_input
352349
@async upload_data(easy, input)
353350
end
351+
@async wait(easy.ready)
354352
end
355353
finally
356354
remove_handle(downloader.multi, easy)

0 commit comments

Comments
 (0)