Skip to content

Commit c7e3409

Browse files
rectify control of write callback
1 parent 343ef0b commit c7e3409

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/Curl/Easy.jl

Lines changed: 10 additions & 8 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[],
@@ -356,12 +359,11 @@ function write_callback(
356359
count :: Csize_t,
357360
easy_p :: Ptr{Cvoid},
358361
)::Csize_t
362+
@assert size == sizeof(Cchar)
359363
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
364+
buf = unsafe_wrap(Vector{Cchar}, data, size*count)
365+
write(easy.output, buf)
366+
return size*count
365367
end
366368

367369
function progress_callback(

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)