source: gaycko/src/io/http.c @ 4716

Last change on this file since 4716 was 4716, checked in by Jean-Yves Lamoureux, 9 years ago
  • Initial commit -This line, and those below, will be ignored--

A gaycko/tests
A gaycko/tests/lnx.html
A gaycko/tests/html.html
A gaycko/tests/simple.html
A gaycko/configure.ac
A gaycko/src
A gaycko/src/dom
A gaycko/src/dom/dom.c
A gaycko/src/dom/dom.h
A gaycko/src/helpers
A gaycko/src/helpers/str.c
A gaycko/src/helpers/str.h
A gaycko/src/gaycko.h
A gaycko/src/parsing
A gaycko/src/parsing/parse.h
A gaycko/src/parsing/parse.c
A gaycko/src/io
A gaycko/src/io/file.c
A gaycko/src/io/http.c
A gaycko/src/io/file.h
A gaycko/src/io/http.h
A gaycko/src/io/io.c
A gaycko/src/io/io.h
A gaycko/src/Makefile.am
A gaycko/src/gaycko.c
A gaycko/Makefile.am
AM gaycko/bootstrap

File size: 1.4 KB
Line 
1#include "http.h"
2#include <stdlib.h>
3#include <string.h>
4
5        static size_t
6http_callback(void *ptr, size_t size, size_t nmemb, void *user)
7{
8        size_t realsize = size * nmemb;
9        gHttp *mem = (gHttp *)user;
10
11        mem->data = realloc(mem->data, mem->size + realsize + 1);
12
13        if (mem->data == NULL) {
14                /* out of memory! */
15                printf("not enough memory (realloc returned NULL)\n");
16                exit(EXIT_FAILURE);
17        }
18
19        memcpy(&(mem->data[mem->size]), ptr, realsize);
20        mem->size += realsize;
21        mem->data[mem->size] = 0;
22
23        return realsize;
24}
25
26gHttp *http_open(char* url) {
27        gHttp *ret = malloc(sizeof(gHttp));
28        ret->data = malloc(1024);
29        ret->size = 0;
30
31        curl_global_init(CURL_GLOBAL_ALL);
32        ret->curl_handle = curl_easy_init();
33
34        /* specify URL to get */
35        curl_easy_setopt(ret->curl_handle, CURLOPT_URL, url);
36
37        /* send all data to this function  */
38        curl_easy_setopt(ret->curl_handle, CURLOPT_WRITEFUNCTION, http_callback);
39
40        /* we pass our 'chunk' struct to the callback function */
41        curl_easy_setopt(ret->curl_handle, CURLOPT_WRITEDATA, (void *)&ret);
42
43        /* some servers don't like requests that are made without a user-agent
44           field, so we provide one */
45        curl_easy_setopt(ret->curl_handle, CURLOPT_USERAGENT, "Gaycko/0.1");
46
47        /* get it! */
48        curl_easy_perform(ret->curl_handle);
49
50        /* cleanup curl stuff */
51        curl_easy_cleanup(ret->curl_handle);
52
53        return ret;
54}
55
56void   http_close(gHttp *f) {
57        if(!f) return;
58
59        memset(f->data, 0, f->size);
60        free(f->data);
61        f->size = 0;
62
63        curl_global_cleanup();
64}
Note: See TracBrowser for help on using the repository browser.