-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathurl.c
107 lines (81 loc) · 2.64 KB
/
url.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "url.h"
//#define URL_DEBUG 1
void init_string(struct string *s) {
s->len = 0;
s->ptr = malloc(s->len+1);
if (s->ptr == NULL)
return;
s->ptr[0] = '\0';
}
void URL_init(struct URL_Request *u) {
u->formpost = NULL;
u->lastptr = NULL;
u->headerlist = NULL;
u->curl = curl_easy_init();
}
void URL_add_form(struct URL_Request *u, char *name, char *content) {
//char *econtent = NULL;
//econtent = curl_easy_escape(u->curl, content, 0);
curl_formadd(&u->formpost, &u->lastptr, CURLFORM_COPYNAME, name, CURLFORM_COPYCONTENTS, content, CURLFORM_END);
//curl_free(econtent);
}
void URL_add_header(struct URL_Request *u, char *str) {
u->headerlist = curl_slist_append(u->headerlist, str);
}
size_t URL_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)
return -1;
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
int URL_GET_request(struct URL_Request *u, char *url, struct string *out) {
int len = 0;
#ifdef URL_DEBUG
LOG_MSG(LOG_INFO, "GET URL: %s\n", url);
#endif
curl_easy_setopt(u->curl, CURLOPT_URL, url);
curl_easy_setopt(u->curl, CURLOPT_WRITEFUNCTION, URL_writefunc);
curl_easy_setopt(u->curl, CURLOPT_WRITEDATA, out);
curl_easy_setopt(u->curl, CURLOPT_FOLLOWLOCATION, 1);
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(u->curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(u->curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
u->res = curl_easy_perform(u->curl);
return len;
}
int URL_POST_request(struct URL_Request *u, char *url, struct string *out) {
int len = 0;
curl_easy_setopt(u->curl, CURLOPT_URL, url);
#ifdef URL_DEBUG
LOG_MSG(LOG_INFO, "POST URL: %s\n", url);
#endif
if (u->formpost != NULL)
curl_easy_setopt(u->curl, CURLOPT_HTTPPOST, u->formpost);
if (u->headerlist != NULL)
curl_easy_setopt(u->curl, CURLOPT_HTTPHEADER, u->headerlist);
curl_easy_setopt(u->curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(u->curl, CURLOPT_WRITEFUNCTION, URL_writefunc);
curl_easy_setopt(u->curl, CURLOPT_WRITEDATA, out);
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(u->curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(u->curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
u->res = curl_easy_perform(u->curl);
return len;
}
void URL_cleanup(struct URL_Request *u) {
if (u->headerlist != NULL)
curl_slist_free_all(u->headerlist);
if (u->formpost != NULL)
curl_formfree(u->formpost);
curl_easy_cleanup(u->curl);
}