cURL / Mailing Lists / curl-library / Single Mail

curl-library

How to use libcurl to do HTTP GET request and get response in Apache module...

From: <ohaya_at_cox.net>
Date: Sat, 23 Jun 2012 16:33:09 -0400

Hi,

I'm trying to use libcurl in an Apache module that I'm working on, to make an HTTP GET request, and to get and process the response.

I've been following examples like this one:

http://www.dimuthu.org/blog/2009/01/28/making-web-requests-using-curl-from-c-and-php/

and trying to modify it for my module and the Apache module environment, but I keep getting a segfault.

I have a function, callCurl():

void callCurl(request_rec *r) {
  CURL *curl;
  CURLcode res;

  const char * response;

  printf("\n\n\nIn callCurl()...\n");

  printf("In callCurl(): About to allocate memory from pool for response...\n");
  // allocate 1000 bytes/chars for response
  response = apr_pcalloc(r->pool, 1000);
  printf("In callCurl(): Returned from allocating memory from pool for response...\n");

  printf("In callCurl(): About to call curl_easy_init()...\n");
  curl = curl_easy_init();
  printf("In callCurl(): Returned from calling curl_easy_init()...\n");

  if(curl) {
    printf("In callCurl(): About to call curl_easy_setopt()...\n");
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/caller/index.jsp");

    /* setting a callback function to return the data */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_func);

    /* passing the pointer to the response as the callback parameter */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    printf("In callCurl(): Returned from calling curl_easy_setopt()...\n");

    printf("In callCurl(): About to call curl_easy_perform()...\n");
    res = curl_easy_perform(curl);
    printf("In callCurl(): Returned from calling curl_easy_perform()...\n");

    printf("\n\n\nres=[%s]\n", res);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return;

} // end callCurl()

And, here's the callback:

/* the function to invoke as the data recieved */
size_t static write_callback_func(void *buffer,
                        size_t size,
                        size_t nmemb,
                        void *userp)
{
    printf("\n\nIn write_callback_func()...\n");

    int bufferlen = strlen(buffer);
    printf("In write_callback_func(): strlen(buffer) = [%d]\n", bufferlen);
    printf("In write_callback_func(): buffer = [%s]\n", buffer);

    char **response_ptr = (char**)userp;

    printf("In write_callback_func(): About to call strndup()...\n");
    /* assuming the response is a string */
    //*response_ptr = strndup(buffer, (size_t)(size *nmemb));
    apr_cpystrn((const char *)userp, buffer, bufferlen);
    printf("In write_callback_func(): Finished setting *response_ptr, returning...\n");

} // end write_callback_func()

The printf in write_callback_func() that prints 'buffer' works ok, i.e., I see the response, and Apache apparently through both callCurl() and write_callback_func(), but then I get the segfault.

Has anyone tried this successfully (using libcurl inside an Apache module, using C)? And, if so, what might be wrong with what I'm doing?

Thanks,
Jim

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-06-23