libraries for fetching stuff off the net, WinInet, libcurl

I used to use WinInet until now, had it wrapped around with some code that evolved over the years, t’was ok.

Then this other I suddenly had to send a packet out of a specific connection and guess what… WinInet doesn’t support that. Neither does WinHTTP (WinInet’s predecessor).

So, looking around I bumped into libcurl, which I’ve tried to ignore all these years. Why ignore ? well because it looks big, and when a piece of code looks big it’s hard for it to attract coders. The bigger the code is, the higher the changes of an unknown convention of programming.

Guess what, this is what it takes to do download something with libcurl:

CString urlopen(const TCHAR* url)
{
USES_CONVERSION;

CURL* c = curl_easy_init();
ByteBucket bb;

curl_easy_setopt(c, CURLOPT_URL, T2A(url));
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(c, CURLOPT_WRITEDATA, &bb);
CURLcode success = curl_easy_perform(c);

bb.AddByte(0); // null-termination
return CString( A2T((char*)bb.GetData()) );
}

static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
ByteBucket* bb = (ByteBucket*)userp;
bb->AddBuffer((BYTE*)buffer, size*nmemb);
return size*nmemb;
}

Fantastic ha ? ByteBucket is a trivial class I wrote that helps with these sort of things.

So isn’t it funny ? fearing from having to deal with lots of curl code I ended up with lots of WinInet code, while eventually the libcurl code was much simpler to work with.

Conclusion ? Fear is a programmer’s worst enemy… I think.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>