@@ -354,11 +354,16 @@ function header_callback(
354354 count :: Csize_t ,
355355 easy_p :: Ptr{Cvoid} ,
356356):: Csize_t
357- easy = unsafe_pointer_to_objref (easy_p):: Easy
358- n = size * count
359- hdr = unsafe_string (data, n)
360- push! (easy. res_hdrs, hdr)
361- return n
357+ try
358+ easy = unsafe_pointer_to_objref (easy_p):: Easy
359+ n = size * count
360+ hdr = unsafe_string (data, n)
361+ push! (easy. res_hdrs, hdr)
362+ return n
363+ catch err
364+ @async @error (" header_callback: unexpected error" , err= err, maxlog= 1_000 )
365+ return typemax (Csize_t)
366+ end
362367end
363368
364369# feed data to read_callback
@@ -380,39 +385,49 @@ function read_callback(
380385 count :: Csize_t ,
381386 easy_p :: Ptr{Cvoid} ,
382387):: Csize_t
383- easy = unsafe_pointer_to_objref (easy_p):: Easy
384- buf = easy. input
385- if buf === nothing
386- notify (easy. ready)
387- return 0 # done uploading
388- end
389- if isempty (buf)
390- notify (easy. ready)
391- return CURL_READFUNC_PAUSE # wait for more data
388+ try
389+ easy = unsafe_pointer_to_objref (easy_p):: Easy
390+ buf = easy. input
391+ if buf === nothing
392+ notify (easy. ready)
393+ return 0 # done uploading
394+ end
395+ if isempty (buf)
396+ notify (easy. ready)
397+ return CURL_READFUNC_PAUSE # wait for more data
398+ end
399+ n = min (size * count, length (buf))
400+ ccall (:memcpy , Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), data, buf, n)
401+ deleteat! (buf, 1 : n)
402+ return n
403+ catch err
404+ @async @error (" read_callback: unexpected error" , err= err, maxlog= 1_000 )
405+ return CURL_READFUNC_ABORT
392406 end
393- n = min (size * count, length (buf))
394- ccall (:memcpy , Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), data, buf, n)
395- deleteat! (buf, 1 : n)
396- return n
397407end
398408
399409function seek_callback (
400410 easy_p :: Ptr{Cvoid} ,
401411 offset :: curl_off_t ,
402412 origin :: Cint ,
403413):: Cint
404- if origin != 0
405- @async @error (" seek_callback: unsupported seek origin" , origin, maxlog= 1_000 )
406- return CURL_SEEKFUNC_CANTSEEK
407- end
408- easy = unsafe_pointer_to_objref (easy_p):: Easy
409- easy. seeker === nothing && return CURL_SEEKFUNC_CANTSEEK
410- try easy. seeker (offset)
414+ try
415+ if origin != 0
416+ @async @error (" seek_callback: unsupported seek origin" , origin, maxlog= 1_000 )
417+ return CURL_SEEKFUNC_CANTSEEK
418+ end
419+ easy = unsafe_pointer_to_objref (easy_p):: Easy
420+ easy. seeker === nothing && return CURL_SEEKFUNC_CANTSEEK
421+ try easy. seeker (offset)
422+ catch err
423+ @async @error (" seek_callback: seeker failed" , err, maxlog= 1_000 )
424+ return CURL_SEEKFUNC_FAIL
425+ end
426+ return CURL_SEEKFUNC_OK
411427 catch err
412- @async @error (" seek_callback: seeker failed " , err, maxlog= 1_000 )
428+ @async @error (" seek_callback: unexpected error " , err = err, maxlog= 1_000 )
413429 return CURL_SEEKFUNC_FAIL
414430 end
415- return CURL_SEEKFUNC_OK
416431end
417432
418433function write_callback (
@@ -421,12 +436,17 @@ function write_callback(
421436 count :: Csize_t ,
422437 easy_p :: Ptr{Cvoid} ,
423438):: Csize_t
424- easy = unsafe_pointer_to_objref (easy_p):: Easy
425- n = size * count
426- buf = Array {UInt8} (undef, n)
427- ccall (:memcpy , Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), buf, data, n)
428- put! (easy. output, buf)
429- return n
439+ try
440+ easy = unsafe_pointer_to_objref (easy_p):: Easy
441+ n = size * count
442+ buf = Array {UInt8} (undef, n)
443+ ccall (:memcpy , Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), buf, data, n)
444+ put! (easy. output, buf)
445+ return n
446+ catch err
447+ @async @error (" write_callback: unexpected error" , err= err, maxlog= 1_000 )
448+ return typemax (Csize_t)
449+ end
430450end
431451
432452function progress_callback (
@@ -436,9 +456,14 @@ function progress_callback(
436456 ul_total :: curl_off_t ,
437457 ul_now :: curl_off_t ,
438458):: Cint
439- easy = unsafe_pointer_to_objref (easy_p):: Easy
440- put! (easy. progress, (dl_total, dl_now, ul_total, ul_now))
441- return 0
459+ try
460+ easy = unsafe_pointer_to_objref (easy_p):: Easy
461+ put! (easy. progress, (dl_total, dl_now, ul_total, ul_now))
462+ return 0
463+ catch err
464+ @async @error (" progress_callback: unexpected error" , err= err, maxlog= 1_000 )
465+ return - 1
466+ end
442467end
443468
444469function debug_callback (
@@ -448,10 +473,15 @@ function debug_callback(
448473 size :: Csize_t ,
449474 easy_p :: Ptr{Cvoid} ,
450475):: Cint
451- easy = unsafe_pointer_to_objref (easy_p):: Easy
452- @assert easy. handle == handle
453- easy. debug (info_type (type), unsafe_string (data, size))
454- return 0
476+ try
477+ easy = unsafe_pointer_to_objref (easy_p):: Easy
478+ @assert easy. handle == handle
479+ easy. debug (info_type (type), unsafe_string (data, size))
480+ return 0
481+ catch err
482+ @async @error (" debug_callback: unexpected error" , err= err, maxlog= 1_000 )
483+ return - 1
484+ end
455485end
456486
457487function add_callbacks (easy:: Easy )
0 commit comments