Libcurl获取缓冲区中的标题内容

时间:2013-10-14 05:24:31

标签: c libcurl

我正在尝试使用libcurl获取网页的标题内容并尝试将其存储在charecter缓冲区中。该程序会出现分段错误。我是libcurl的初学者,我不确定问题出在哪里。这是下面的代码。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

#include <curl/curl.h>



struct buf {
    char *buffer;
    size_t bufferlen;
    size_t writepos;
} buffer = {0};

struct buf HeaderBuffer;

static size_t write_data(void *ptr, size_t size, size_t nmemb, char *string)
{
  size_t nbytes = size*nmemb;
    if (!HeaderBuffer.buffer) {
        HeaderBuffer.buffer = malloc(1024);
        HeaderBuffer.bufferlen = 1024;
        HeaderBuffer.writepos = 0;
    }
    if (HeaderBuffer.writepos + nbytes < HeaderBuffer.bufferlen) {
        HeaderBuffer.bufferlen = 2 * HeaderBuffer.bufferlen;
        HeaderBuffer.buffer = (char *)realloc(HeaderBuffer.buffer, buffer.bufferlen);
    }
    assert(HeaderBuffer.buffer != NULL);
    memcpy(HeaderBuffer.buffer+HeaderBuffer.writepos,ptr,nbytes);
    return nbytes;

}

int main(void)
{
  CURL *curl_handle;

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */ 
  curl_handle = curl_easy_init();


  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");


  /* send all data to this function  */ 
  curl_easy_setopt(curl_handle,CURLOPT_HEADERFUNCTION, write_data); 

  /* get it! */ 
  curl_easy_perform(curl_handle);



printf("%s",HeaderBuffer.buffer);



  /* cleanup curl stuff */ 
  curl_easy_cleanup(curl_handle);

  return 0;
}

编辑:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct string {
  char *ptr;
  size_t len;
};

void init_string(struct string *s) {
  s->len = 0;
  s->ptr = (char *)malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
    exit(1);
  }
  s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
  size_t new_len = s->len + size*nmemb;
  s->ptr =(char *) realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
    exit(EXIT_FAILURE);
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);

  //sprintf (s->ptr+s->len,"New header:\n%s\n",(char *)ptr);


  s->ptr[new_len] = '\0';
  s->len = new_len;

  return size*nmemb;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  char url[50];



  char *ptr=NULL;
  int i=0;

  memset(Buffer,0,255);

  strcpy(url,"http://");
  strncat(url,"74.125.236.176",strlen("74.125.236.176"));

  curl = curl_easy_init();
  if(curl) {
    struct string s;
    init_string(&s);
    curl_easy_setopt(curl, CURLOPT_URL,url);


    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, writefunc);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &s);
    res = curl_easy_perform(curl);



     printf("\n \n %s",Buffer);

    printf("%s\n", s.ptr);




    free(s.ptr);

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

1 个答案:

答案 0 :(得分:0)

我觉得你应该这样做,

HeaderBuffer.bufferlen = nbytes * HeaderBuffer.bufferlen;

因为你不知道nbytes,它可能大于2*HeaderBuffer.bufferlen