curl-library
Re: Issues on callback write_data
Date: Sat, 24 Mar 2007 19:30:13 -0300
Hi Fellows: Let's find out that's going on..
> On Fri, 23 Mar 2007, Gustavo Elias Siqueira wrote:
> I'm a beginner curl user, asking for a hand to solve a problem.
> When I call curl to perform, the libcurl at the
> first callback write_data, returns a buffer with 1100 and show a parcial
> data.For the second time, the buffer returns me 1768, but theres no data
> avaiable.
> On Fri, 23 Mar 2007 22:20:02 +0100 (CET), Daniel Stenberg wrote:
> When you copy the getinmemory.c example, make sure you also copy the
> CURLOPT_WRITEDATA-using line!
Ok...Did it!
> On Fri, 23 Mar 2007 14:44:46 -0700, Dan Fandrich wrote:
> You're using a pointer (data) that has never been set in your program.
> It's just going to contain garbage, if anything. You need to allocate
> space for your MemoryStruct structure somewhere and pass in the pointer
> using the CURLOPT_WRITEDATA option. Then, after curl_easy_perform
> returns, you use the same pointer to access the downloaded data.
Ok...Did it!
But the issue still remains...
The second callback have a bigger size, but no data avaiable. After
curl_easy_perform, that's all I got in the first one:
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html
lang="en"><head><title>CNN.com - Breaking News, U.S., World, Weather,
Entertainment & Video News</title> <meta http-equiv="content-type"
content="text/html; charset=iso-8859-1">"
--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Crawler.cpp : main project file.
#include <stdafx.h>
#include <iostream>
#include <curl/curl.h>
#include <Threads.h>
using namespace System;
struct MemoryStruct {
char *memory;
size_t size;
}*mem;
size_t write_data(void *ptr, size_t size, size_t nmemb, void *data) {
register int realsize = size * nmemb;
mem = (struct MemoryStruct *)data;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
int main(array<System::String ^> ^args)
{
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.cnn.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if(chunk.memory)
free(chunk.memory);
return 1;
}
Thanks again!
Gustavo
Received on 2007-03-24