fix CurlTest error code 23 on Windows

This commit is contained in:
andyque 2015-01-05 15:55:40 +08:00
parent 7407105f24
commit 2a266efc1a
1 changed files with 31 additions and 1 deletions

View File

@ -21,6 +21,31 @@ CurlTest::CurlTest()
_label->retain();
}
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
// the test code is
// http://curl.haxx.se/mail/lib-2009-12/0071.html
@ -30,10 +55,15 @@ void CurlTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
CURLcode res;
char buffer[10];
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://webtest.cocos2d-x.org/curltest");
curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com");
//code from http://curl.haxx.se/libcurl/c/getinmemory.html
//If we don't provide a write function for curl, it will recieve error code 23 on windows.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);