cURL setHeaderCallback() ?

BlitzMax Forums/Brucey's Modules/cURL setHeaderCallback() ?

Htbaa(Posted 2009) [#1]
How can I use TCurlEasy.setHeaderCallback()?
It's signature is
setHeaderCallback(headerFunction:Int(buffer:Byte Ptr, size:Int, data:Object), data:Object = Null)


I understand that the first parameter of the callback function is a buffer with a header line. Can this buffer be converted to a string with String.FromCArray()? Size is the length of the buffer in bytes and I understand that the 3rd parameter is a custom object I can pass to it.

But what about the second parameter of setHeaderCallback()? Is that also a custom object I can pass?

Can't really make it out of the documentation :-). Thanks.


Htbaa(Posted 2009) [#2]
And just a few minutes later I figured it out :P.

	Function HeaderCallback:Int(buffer:Byte Ptr, size:Int, data:Object)
		Local str:String = String.FromCString(buffer)
		Print str
		Return size
	End Function


But still not sure about the data:Object parameters. These are optional/custom data?

Edit: Figured it out. The 2nd parameter of HeaderCallback() is an optional object you can pass which in turn gets passed as the 3rd parameter of the actual callback function.


Brucey(Posted 2009) [#3]
Is that also a custom object I can pass?

Yes. It allows you to know what related "object" the call to the callback is for, and which you could cast back to the original passed-in object to do things with it.

Here's a not very useful, pseudo example :
Type BaseClass

    Function handleCallback:int(buffer:Byte Ptr, size:Int, data:Object)
        BaseClass(data).DoSomething(String.FromBytes(buffer, size))
        Return size
    End Function

    Method DoSomething(text:String)
        Print "The Header : " + text
    End Method

End Type

Type Subclass Extends BaseClass

    Method DoSomething(text:String)
        Print "Doing something else with the text... " + text
    End Method

End Type


' somewhere
Local base:BaseClass = new BaseClass
curl.setHeaderCallback(basclass.handleCallback, base)

' somewhere else..
Local sub:Subclass = new Subclass
curl.setHeaderCallback(basclass.handleCallback, sub)


I suppose you could say it makes the callback context-sensitive. And of course, the data parameter is optional.
But you'll find, in many libraries, this "user data" parameter as part of a callback definition.

With C++, you can actually specify an instance method to call, which is better in that it is not global. But when wrapping those things in BlitzMax, you need to jump through some hoops to make it transparent to the user. Fun fun ;-)