-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcurl_utils.c
59 lines (50 loc) · 1.4 KB
/
curl_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include "curl_utils.h"
void init_string(struct string *s) {
s->len = 0;
s->ptr = malloc(s->len + 1);
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
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 = 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);
s->ptr[new_len] = '\0';
s->len = new_len;
return size * nmemb;
}
char *getContent(const char *url) {
char *result = NULL;
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
struct string s;
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "F your user agent tracking BS");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "cURL encountered an error: %s\n", curl_easy_strerror(res));
}
result = (char *)malloc(sizeof(char) * (strlen(s.ptr) + 1));
strcpy(result, s.ptr);
free(s.ptr);
/* always cleanup */
curl_easy_cleanup(curl);
}
return result;
}