
#include "hbclass.ch"
#include "hbcurl.ch"


CLASS TcURLEasy

   METHOD      New( cUrl )
   DESTRUCTOR  Free()

   METHOD      Perform()
   METHOD      Reset()

   // callbacks
   METHOD      Write( cStr, nSize, nMemb )                     VIRTUAL
   METHOD      Read( cStr, nSize, nMemb )                      VIRTUAL
   METHOD      Progress( nDLTotal, nDLNow, nULTotal, nULNow )  VIRTUAL


   ASSIGN      URL( cUrl )                   INLINE   ::SetUrl( cUrl )
   ASSIGN      FOLLOWLOCATION( lValue )      INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_FOLLOWLOCATION, iif( lValue, 1, 0 ) )
   ASSIGN      UPLOAD( lValue )              INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_UPLOAD, iif( lValue, 1, 0 ) )
   ASSIGN      INFILESIZE_LARGE( nValue )    INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_INFILESIZE_LARGE, nValue )
   ASSIGN      FTP_USE_EPSV( lValue )        INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_FTP_USE_EPSV, iif( lValue, 1, 0 ) )
   ASSIGN      TCP_NODELAY( lValue )         INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_TCP_NODELAY, iif( lValue, 1, 0 ) )
   ASSIGN      BUFFERSIZE( nValue )          INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_BUFFERSIZE, nValue )
   ASSIGN      NOPROGRESS( lValue )          INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_NOPROGRESS, iif( lValue, 1, 0 ) )

   // When passing an array you _have to_ pass Self as fourth parameter
   ASSIGN      POSTQUOTE( aValue )           INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_POSTQUOTE, aValue, Self )
   ASSIGN      PREQUOTE( aValue )            INLINE   curl_easy_setopt( ::pcUrl, CURLOPT_PREQUOTE, aValue, Self )



   // Should be protected, at least, but I don't know how to call such a method from C code :/
   METHOD      AddSList( pSList )

   PROTECTED:

   METHOD      SetUrl( cUrl )

   VAR   pcURL    INIT  NIL      // Handle of cURL object

   HIDDEN:

   VAR   cUrl     INIT  ""
   VAR   aSList   INIT  {}

ENDCLASS



METHOD New( cUrl ) CLASS TcURLEasy

   ::pcUrl := curl_easy_init()

   if ! Empty( ::pcUrl )

      if __ClsMsgAssigned( Self, "WRITE" )
         // I use this option to setup the write redirection to this object ::Write() method
         // that means NEVER call curl_easy_setopt() with CURLOPT_WRITEFUNCTION !
         curl_easy_setopt( ::pcURL, CURLOPT_WRITEDATA, Self )
      endif

      if __ClsMsgAssigned( Self, "READ" )
         // I use this option to setup the read redirection to this object ::Read() method
         // that means NEVER call curl_easy_setopt() with CURLOPT_READFUNCTION !
         curl_easy_setopt( ::pcURL, CURLOPT_READDATA, Self )
      endif

      if __ClsMsgAssigned( Self, "PROGRESS" )
         // I use this option to setup the read redirection to this object ::Read() method
         // that means NEVER call curl_easy_setopt() with CURLOPT_PROGRESSFUNCTION !
         curl_easy_setopt( ::pcURL, CURLOPT_PROGRESSDATA, Self )
      endif

      if ! Empty( cUrl )
         ::SetUrl( cUrl )
      endif
   endif

RETURN Self



PROCEDURE Free() CLASS TcURLEasy

   local pSList

   if ::pcURL <> NIL
      curl_easy_cleanup( ::pcURL )
      ::pcURL := NIL
   endif

   if ! Empty( ::aSList )
      for each pSList in ::aSList
         curl_slist_free_all( pSList )
      next
      ::aSList := {}
   endif

RETURN



METHOD SetUrl( cUrl ) CLASS TcURLEasy

   curl_easy_setopt( ::pcURL, CURLOPT_URL, cUrl )
   ::cUrl := cUrl

RETURN Self



METHOD Perform() CLASS TcURLEasy

   local nErr

   nErr := curl_easy_perform( ::pcUrl )

RETURN nErr



METHOD Reset() CLASS TcURLEasy

   curl_easy_reset( ::pcUrl )

RETURN Self



METHOD AddSList( pSList ) CLASS TcURLEasy

   AAdd( ::aSList, pSList )

   ? "AddSList", pSList

RETURN Self



/* This method does what the standard curl_writefunc does, that is, it writes to stdout what
   it receives. Since it is defined as virtual, I leave this here just for documentation
METHOD Write( cStr, nSize, nMemb ) CLASS TcURLEasy

   ? cStr

RETURN nSize * nMemb
*/



