libcurl 을 이용하여 웹페이지 긁어오기

MingW용 libcurl을 빌드후에.. http 페이지 긁어오는 걸 테스트해 봄..
static 빌드시에.. curl.h 파일 include 전에 CURL_STATICLIB define 필요.

#include <stdlib.h>
#include <stdio.h>
#define CURL_STATICLIB
#include <curl/curl.h>

struct url_data {
    size_t size;
    char* data;
};

size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) {
    size_t index = data->size;
    size_t n = (size * nmemb);
    char* tmp;

    data->size += (size * nmemb);

#ifdef DEBUG
    fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb);
#endif
    tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */

    if(tmp) {
        data->data = tmp;
    } else {
        if(data->data) {
            free(data->data);
        }
        fprintf(stderr, "Failed to allocate memory.\n");
        return 0;
    }

    memcpy((data->data + index), ptr, n);
    data->data[data->size] = '\0';

    return size * nmemb;
}

char *handle_url(char* url) {
    CURL *curl;

    struct url_data data;
    data.size = 0;
    data.data = malloc(4096); /* reasonable size initial buffer */
    if(NULL == data.data) {
        fprintf(stderr, "Failed to allocate memory.\n");
        return NULL;
    }

    data.data[0] = '\0';

    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
                fprintf(stderr, "curl_easy_perform() failed: %s\n",  
                        curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);

    }
    return data.data;
}

int main(int argc, char* argv[]) {
    char* data;
    data = handle_url("http://www.findip.kr");

    if(data) {
        printf("%s\n", data);
        free(data);
    }

    return 0;
}
$ gcc http.c -o http.exe -I/workspace/ETC/tclpackages/3rdparty/lib/curl-7.42.1/release/win32/include -L/workspace/ETC/tclpackages/3rdparty/lib/libssh2-1.4.4-20140926/release/win32/lib -L/workspace/ETC/tclpackages/3rdparty/lib/curl-7.42.1/release/win32/lib -L/workspace/ETC/tclpackages/3rdparty/lib/openssl-1.0.2a/release/win32/lib -lcurl -lws2_32 -lcrypt32 -lssh2 -lssl -lcrypto -lws2_32 -lgdi32	-lcrypt32 -lwldap32 /workspace/ETC/tclpackages/3rdparty/lib/zlib-1.2.8/release/win32/lib/libz.a

LibSoup

홈페이지: http://live.gnome.org/LibSoup

gnome 환경을 위한 http 클라이언트/서버 라이브러리.

libsoup is an HTTP client/server library for GNOME. It uses GObjects and the glib main loop, to integrate well with GNOME applications, and also has a synchronous API, for use in threaded applications.

다운로드: 2.28.1
참고: http://crew.emstone.com/~keedi/timetolog/