cURL / Mailing Lists / curl-library / Single Mail

curl-library

Stream to stdout even using CURL_WRITEFUNCTION

From: Angelo Di Iorio <diioangelo_at_gmail.com>
Date: Sun, 7 Oct 2012 19:54:40 +0200

Hi all,

I have a strange problem. As far as I understand looking on Google,
once I set CURL_WRITEFUNCTION I shouldn't get any stream on the stdout
at all... but I still have it. Here's some code's snippets: the first
one is my function (actually, I just copied it from the curl's
examples, since it fits perfectly for my purposes). The second one is
how I set my curl handle (again, it's very similar to "getinmemory.c"
sample)

size_t writeMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp)
{
  size_t realsize = size * nmemb;
  MemoryStruct* mem = (MemoryStruct*) userp;

  mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  if (mem->memory == NULL)
  {
    /* out of memory! */
    printf("not enough memory (realloc returned NULL)\n");
    exit(EXIT_FAILURE);
  }

  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;

  return realsize;
}

int simpleHttpRequest(char* url)
{
    CURL* curl; //curl handler
    CURLcode res; //result of the request

    MemoryStruct chunk; //defined in HttpRequestSupport.h

    chunk.memory = malloc(1); // will be grown as needed by the
realloc istruction in writeMemoryCallback
    chunk.size = 0; // no data at this point

    int result = 1;

    curl = curl_easy_init();

    result = login(curl); //this function will return an error if the
login phase fails
    if(result != 0) return result; //an error occoured during the login phase

    if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, url);

        /* send all data to this function */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeMemoryCallback);

        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) &chunk);

        /* some servers don't like requests that are made without a
user-agent field, so we provide one */
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        /* Check for errors */
        result = check(res,chunk.memory);
        /* always cleanup */
        curl_easy_cleanup(curl);
        if(chunk.memory) free(chunk.memory);

        return result;
    }
    else return -9;
}

sorry for being such a newbie, I appreciate your help!

-- 
Angelo Di Iorio
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2012-10-07