AutoHotkey Community

It is currently May 26th, 2012, 9:03 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 51 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: libcurl Wrapper (WIP)
PostPosted: May 20th, 2008, 9:25 pm 
Offline

Joined: March 25th, 2007, 3:47 am
Posts: 43
Location: Earth-Moon L2
A while back I started working on a wrapper for the libcurl easy interface. (libcurl is the DLL version of cURL.) I got it pretty much done, except there is an error while doing a form POST that I couldn't nail down. I'm posting it here because it still has a lot of uses even without the ability to POST, and because someone taking a fresh look at it might be able to spot my mistake.
I based it upon olfen's libcurl example.

Here is the libcurl documentation: http://curl.haxx.se/libcurl/c/

Downloads:

libcurl.ahk - The wrapper include file.
libcurl.dll - Win32 libcurl 7.18.1 without SSL. (latest version on 5/20/2008)
libcurl.dll - Win32 libcurl 7.18.1 with SSL. Requires SSL libraries.
libcurl_test.ahk - olfen's test script running through the wrapper.
libcurl_imageshacktest.ahk - A modified test script that posts to imageshack. (erroneous)

The test scripts require FileHelper.ahk.

Functions:
The functions use the global variables CurlDllName and hCurlModule. Be sure not to overwrite them.

CurlGlobalInit( Location = "", flags = 3 )
You must call this first. It loads the DLL and returns curl_global_init( flags ) or -1 if the DLL failed to load. (0 = Success) Location is the path to libcurl and the file name minus ".dll". If Location is omitted, it defaults to "libcurl". It stores the name in a global variable so the other functions will know it.
Code:
;Flags:
;CURL_GLOBAL_SSL = 1
;CURL_GLOBAL_WIN32 = 2
;CURL_GLOBAL_ALL = 3
;CURL_GLOBAL_NOTHING = 0
;CURL_GLOBAL_DEFAULT = 3
CurlGlobalInit( "Common\libcurl" )
CurlGlobalInit( "", 2 ) ; CURL_GLOBAL_WIN32
CurlGlobalInit( "libcurl-4", 0 )

CurlFreeLibrary()
This frees the libcurl DLL. All handles and pointers created by libcurl will be invalidated and libcurl will become unusable until CurlGlobalInit() is called again.

CurlEasyInit()
Returns curl_easy_init().

CurlEasyReset( EasyHandle )
Calls curl_easy_reset( EasyHandle ).

CurlEasyCleanup( EasyHandle )
Calls curl_easy_cleanup( EasyHandle ).

CurlShowErrors( Yes = true )
If set to true, causes some of the wrapper functions to display libcurl string errors in message boxes.

CurlEasySetOption( EasyHandle, Option, Parameter )
If Parameter is an integer it returns curl_easy_setopt( EasyHandle, Option, Parameter ). Otherwise, it returns curl_easy_setopt( EasyHandle, Option, &Parameter ).
You can use CurlEasyDefineOptions() to define the CURLOPT options as variables for easier use of this function.

CurlSlistAppend( ByRef pSlist, String )
Calls curl_slist_append( pSlist, String ) and sets pSlist to the returned value.

CurlSlistFreeAll( ByRef pSlist )
Calls curl_slist_free_all( pSlist ) and sets pSlist to 0.

CurlFormAdd( ByRef pFirstItem, ByRef pLastItem, Option1, Value1, Option2 = 0, Value2 = 0, ........, Option8 = 0, Value8 = 0 )
Returns curl_formadd( &pFirstItem, &pLastItem, Option1, Value1, Option2, Value2, ........, Option8, Value8 ). Non-numeric strings are automatically handled correctly. Numeric strings or user-inputted strings should be passed as an address (& operator). If this is the first call and is supposed to initialize the form, you should set pFirstItem and pLastItem to 0 prior to calling this function.
You can use CurlEasyDefineOptions() to define the CURLFORM options as variables for easier use of this function.
Note: curl_formadd() is not limited to 8 options. Writing more options into this function and increasing the string conversion loop will allow more.

CurlFormFree( pFirstItem )
Calls curl_formfree( pFirstItem ).

CurlEasyPerform( EasyHandle )
Returns curl_easy_perform( EasyHandle ).

CurlEasyGetinfo( EasyHandle, Information )
Returns the output of curl_easy_getinfo( EasyHandle, Information, Ouput ). The function automatically manages the interpretation of the output.

CurlEasyStrError( ErrorCode )
Returns curl_easy_strerror( ErrorCode ).

CurlEasyEscape( EasyHandle, URL )
Returns curl_easy_escape( EasyHandle, URL ) through CurlFreeGet().

CurlEasyUnescape( EasyHandle, URL )
Returns curl_easy_unescape( EasyHandle, URL ) through CurlFreeGet().

CurlVersion()
Returns curl_version().

CurlFreeGet( pString )
Some libcurl functions that return a string say that you should use curl_free() to delete it when you're done with it. This function copies the string at the address specified in pString, calls curl_free( pString ), and returns the copy. It is used internally by other wrapper functions.

MergeDouble( l, h )
Merges the low and high parts of a "Double" together and returns it. Useful for CURLOPT_PROGRESSFUNCTION.

CurlGetInfoType( Information )
Returns the data type of a CURLINFO option. It is used internally by other wrapper functions.

CurlGetInfoDefine( All = true )
Defines variables to match the CURLINFO C constants. If All is false, it will only define the data types.

CurlEasyGetOptionType( Option )
Returns the CURLOPTTYPE of a CURLOPT option.

CurlEasyDefineOptions( All = true )
Defines variables to match the CURLOPTTYPE C constants and CURL_ERROR_SIZE. If All is true, it will also define the CURLFORM and CURLOPT constants.

Error Details:

libcurl gives error #27 "Out of memory" with the message "failed creating formpost data." With some debugging, I was able to trace the failure point in the libcurl code.

In libcurl_imageshacktest.ahk libcurl fails in the internal function Curl_getFormData when it hits this:
Code:
    result = AddFormDataf(&form, &size, "\"");
    if (result)
      break;
(Near line 1150 in formdata.c)
It seems to be passing in a huge value for size, which causes the memory allocation error inside of it. I also recall seeing a string filled with binary.
This apparently indicates that there's a bad pointer somewhere, but I wasn't able to find anything in my code.

Converting from cURL to libcurl

cURL provides the option "--libcurl" which will write out the equivalent libcurl C code.
Quote:
--libcurl <file>
Append this option to any ordinary curl command line, and you will get a libcurl-using source code written to the file that does the equivalent operation of what your command line operation does!

NOTE: this does not properly support -F and the sending of multipart formposts, so in those cases the output program will be missing necessary calls to curl_formadd(3), and possibly more.

If this option is used several times, the last given file name will be used. (Added in 7.16.1)

You can either use the resulting code as a reference for using the AHK wrapper or convert the code and use the program as-is.


Last edited by DeathByNukes on May 21st, 2008, 6:11 am, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2008, 10:17 pm 
Impressive! :)

Quote:
; YEA, WUT NAO?


:lol:

Your error sounds like you forgot to zero terminate something.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2008, 10:20 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Thx for this !

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2008, 10:32 pm 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
huzzah! thanks for this, im going to test it asap ;)

oh yea, can you provide your libeay32.dll? i want to use with the libcurl.dll provided.


Last edited by k3ph on May 21st, 2008, 6:20 pm, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2008, 5:29 am 
THANK YOU SO MUCH THIS WILL BE POPULAR !


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2008, 5:55 am 
Offline

Joined: March 25th, 2007, 3:47 am
Posts: 43
Location: Earth-Moon L2
k3ph wrote:
oh yea, can you provide your libeay32.dll? i want to use with the libcurl.dll provided.

question: how can I follow a post data using -Ld parameters?
Code:
curl -Ld "BUTTON_INPUT=Restart%20Cable%20Modem" http://192.168.100.1/configdata.html

reference:
Quote:
; -L/--location Follow Location: hints (H)
; -d/--data <data> HTTP POST data (H)


how can I make it work?

Code:
URL = http://192.168.100.1/configdata.html
POST = BUTTON_INPUT=Restart%20Cable%20Modem

CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL )
CurlEasySetOption( hCurlEasy, CURLOPT_FOLLOWLOCATION, 0 )
CurlEasySetOption( hCurlEasy, CURLOPT_HTTPPOST, &POST )

CurlEasyCleanup( hCurlEasy )
CurlFreeLibrary()
return


Ah, sorry about that. You can either download SSL here or get the version that doesn't require it. I've edited this into the top post as well.

As for that script, shouldn't CURLOPT_FOLLOWLOCATION be set to 1 to enable it? Also, you need to call CurlEasyPerform( hCurlEasy ) to actually begin the transfer.
I'm not sure if it'll work though because of the current bug in doing a POST.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2008, 9:59 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Cool, it is nicer than running cURL on the command line for each operation...
I will keep this library in mind next time I need to do some high level Internet access.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 21st, 2008, 6:23 pm 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
OOOOOOOO thank you so much, with more 3 tries I could run what i've wanted!

CURLOPT_POSTFIELDSIZE_LARGE is mandatory or it will keep waiting for it


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 25th, 2008, 5:07 am 
Offline

Joined: March 11th, 2008, 11:36 pm
Posts: 291
thanks for sharing this.
i was having inconvenience on handling curl at all
this will be my another favorite one!
*scraps

_________________
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 5:21 am 
Offline

Joined: July 15th, 2006, 6:07 pm
Posts: 254
Any progress on this? I need https connectivity and POST


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 24th, 2009, 7:03 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
DeathByNukes: Thank you very much for this wrapper, it makes things a lot easier. I'm trying to use your wrapper to to upload files via ftp and display the progress, but I can't seem to get it right. Can anyone lend a hand with this thread?

http://www.autohotkey.com/forum/viewtopic.php?t=45372


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 29th, 2009, 3:06 pm 
Offline

Joined: May 2nd, 2006, 11:16 pm
Posts: 800
Location: Greeley, CO
Murp|e wrote:
DeathByNukes: Thank you very much for this wrapper, it makes things a lot easier. I'm trying to use your wrapper to to upload files via ftp and display the progress, but I can't seem to get it right. Can anyone lend a hand with this thread?

http://www.autohotkey.com/forum/viewtopic.php?t=45372

I had decided to quit bumping until I found that Murp|e was having the same problem I was;
We have made advancements with our libcurl problems, but neither of us have managed to get a file to actually upload;
Still looking for an assist.

_________________
Image
SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 1st, 2009, 2:05 am 
Offline

Joined: May 12th, 2009, 3:36 pm
Posts: 27
Hello,
I only have to simple questions -- however I can't answer them myself...

With the scripts in this forum I am able to upload one oder more files to a specific directory on a server via ftp and this wrapper.

But how can I delete files?
And how can I read the files in a directory (get a directory listing)?

Simple, or? ;)

Thanks!
Carlos


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2010, 12:54 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
tov: To download a file and display the progress, use the libcurl wrapper, more specifically:

DeathByNukes wrote:
libcurl_test.ahk - olfen's test script running through the wrapper.


mtemp/tov: I tried to delete a file like this:
Code:
#SingleInstance, force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#include FileHelper.ahk ; http://www.autohotkey.com/forum/topic19608.html
#include libcurl.ahk
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

 if ( CurlGlobalInit( "libcurl" ) != 0 )
  ExitApp
 hCurlEasy := CurlEasyInit()
 CurlEasyDefineOptions()
 CurlGetInfoDefine()
 CurlShowErrors()

 Url = ftp://USERNAME:PASSWORD@ftp.HOST.com
 CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL )
 ;CurlEasySetOption( hCurlEasy, CURLOPT_VERBOSE, 1)
 ;CurlEasySetOption( hCurlEasy, CURLOPT_FTPLISTONLY, 1)
 CurlEasySetOption( hCurlEasy, CURLOPT_NOPROGRESS, 1)
 CurlEasySetOption( hCurlEasy, CURLOPT_QUOTE, "-DELE myfile.txt")
If ( CurlEasyPerform( hCurlEasy ) )
  MsgBox % CURL_ERROR ;%


 CurlEasyCleanup( hCurlEasy )
 CurlFreeLibrary()
return


I couldn't get it to work, hopefully someone else following this thread can give some advice???

To delete a file via FTP is rather easy using the command line curl.exe though:
curl.exe --quote "-dele myfile.txt" ftp://USER:PASS@HOST.com

mtemp: If you uncomment lines CURLOPT_VERBOSE and CURLOPT_FTPLISTONLY and comment line CURLOPT_QUOTE, the file list will be returned to the stdout (The bottom window in SciTE4AutoHotkey). Not sure if that's any help.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 18th, 2010, 1:46 pm 
@Murp|e Thanks for taking the time to reply.

I'll try libcurl_test.ahk and see how I get on, if the deletion can be made to work, that would be great :)


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 51 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: iBob35555VR and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group