@@ -99,9 +99,8 @@ local function make_iterator(self, perform)
9999
100100 if n <= remain then
101101 while true do
102- local h , ok , err = assert (self :info_read ())
103- if h == 0 then break end
104- local e = assert (self ._easy [h ])
102+ local e , ok , err = assert (self :info_read ())
103+ if e == 0 then break end
105104 if ok then
106105 ok = e :getinfo_response_code () or ok
107106 buffers :append (e , " done" , ok )
@@ -423,6 +422,22 @@ function Multi:remove_handle(e)
423422 return remove_handle (self , h )
424423end
425424
425+ function Multi :info_read (...)
426+ while true do
427+ local h , ok , err = self :handle ():info_read (... )
428+ if not h then return nil , ok end
429+ if h == 0 then return h end
430+
431+ local e = self ._easy [h ]
432+ if e then
433+ if ... then
434+ self ._easy [h ], self ._easy .n = nil , self ._easy .n - 1
435+ end
436+ return e , ok , err
437+ end
438+ end
439+ end
440+
426441end
427442---- ---------------------------------------
428443
@@ -556,14 +571,74 @@ function Multi:remove_handle(e)
556571end
557572
558573function Multi :info_read (...)
559- local h , ok , err = self :handle ():info_read (... )
560- if not h then return nil , ok end
561- if h == 0 then return h end
574+ while true do
575+ local h , ok , err = self :handle ():info_read (... )
576+ if not h then return nil , ok end
577+ if h == 0 then return h end
578+
579+ local e = self ._easy [h ]
580+ if e then
581+ if ... then
582+ self ._easy [h ], self ._easy .n = nil , self ._easy .n - 1
583+ end
584+ return e , ok , err
585+ end
586+ end
587+ end
562588
563- if ... and self ._easy [h ] then
564- self ._easy [h ], self ._easy .n = nil , self ._easy .n - 1
589+ function wrap_callback (...)
590+ local n = select (" #" , ... )
591+ local fn , ctx , has_ctx
592+ if n >= 2 then
593+ has_ctx , fn , ctx = true , assert (... )
594+ else
595+ fn = assert (... )
596+ if type (fn ) ~= " function" then
597+ has_ctx , fn , ctx = true , assert (fn .socket ), fn
598+ end
599+ end
600+ if has_ctx then
601+ return function (...) return fn (ctx , ... ) end
565602 end
566- return h , ok , err
603+ return function (...) return fn (... ) end
604+ end
605+
606+ function wrap_socketfunction (self , cb )
607+ return function (h , ...)
608+ local e = self ._easy [h ]
609+ if e then return cb (e , ... ) end
610+ return 0
611+ end
612+ end
613+
614+ local setopt_socketfunction = wrap_function (" setopt_socketfunction" )
615+ function Multi :setopt_socketfunction (...)
616+ local cb = wrap_callback (... )
617+
618+ return setopt_socketfunction (wrap_socketfunction (self , cb ))
619+ end
620+
621+ local setopt = wrap_function (" setopt" )
622+ function Multi :setopt (k , v )
623+ if type (k ) == ' table' then
624+ local t = k
625+
626+ local socketfunction = t .socketfunction or t [curl .OPT_SOCKETFUNCTION ]
627+ if socketfunction then
628+ t = clone (t )
629+ local fn = wrap_socketfunction (self , socketfunction )
630+ if t .socketfunction then t .socketfunction = fn end
631+ if t [curl .OPT_SOCKETFUNCTION ] then t [curl .OPT_SOCKETFUNCTION ] = fn end
632+ end
633+
634+ return setopt (self , t )
635+ end
636+
637+ if k == curl .OPT_SOCKETFUNCTION then
638+ return self :setopt_httppost (wrap_socketfunction (v ))
639+ end
640+
641+ return setopt (self , k , v )
567642end
568643
569644end
0 commit comments