-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmlcurl.c
151 lines (120 loc) · 3.12 KB
/
smlcurl.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <curl/curl.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct handle_t {
char *buffer;
size_t buffer_size;
enum { ERR_OK, ERR_OOM, ERR_OTHER } error;
CURL *curl;
} handle_t;
static size_t
smlcurl_collect_data (void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
handle_t *hdl = (handle_t *)userp;
char *data;
/* There has been an error, so skip the data */
if (hdl->error != ERR_OK)
return realsize;
data = realloc(hdl->buffer, hdl->buffer_size + realsize + 1);
/* OOM */
if (!data) {
hdl->error = ERR_OOM;
/* Give up */
return realsize;
}
hdl->buffer = data;
/* Copy the data */
memcpy(&(hdl->buffer[hdl->buffer_size]), contents, realsize);
hdl->buffer_size += realsize;
hdl->buffer[hdl->buffer_size] = 0;
return realsize;
}
struct handle_t *smlcurl_easy_init(void)
{
handle_t *hdl = malloc(sizeof(handle_t));
hdl->buffer = NULL;
hdl->buffer_size = 0;
hdl->error = ERR_OK;
hdl->curl = curl_easy_init();
return hdl;
}
int smlcurl_easy_set_verbose(handle_t *hdl, bool verbose)
{
return curl_easy_setopt(hdl->curl, CURLOPT_VERBOSE, verbose ? 1L : 0L);
}
int smlcurl_easy_set_header(handle_t *hdl, bool header)
{
return curl_easy_setopt(hdl->curl, CURLOPT_HEADER, header ? 1L : 0L);
}
int smlcurl_easy_set_progress(handle_t *hdl, bool progress)
{
return curl_easy_setopt(hdl->curl,
CURLOPT_NOPROGRESS, progress ? 0L : 1L);
}
int smlcurl_easy_set_signal(handle_t *hdl, bool signal)
{
return curl_easy_setopt(hdl->curl, CURLOPT_NOSIGNAL, signal ? 0L : 1L);
}
int smlcurl_easy_set_url(handle_t *hdl, const char *url)
{
int r= curl_easy_setopt(hdl->curl, CURLOPT_URL, url);
return r;
}
int smlcurl_easy_set_proxy(handle_t *hdl, const char *proxy)
{
return curl_easy_setopt(hdl->curl, CURLOPT_PROXY, proxy);
}
int smlcurl_easy_set_useragent(handle_t *hdl, const char *useragent)
{
return curl_easy_setopt(hdl->curl, CURLOPT_USERAGENT, useragent);
}
int smlcurl_easy_perform_as_string(handle_t *hdl)
{
curl_easy_setopt(hdl->curl,
CURLOPT_WRITEFUNCTION, smlcurl_collect_data);
curl_easy_setopt(hdl->curl,
CURLOPT_WRITEDATA, (void *)hdl);
curl_easy_perform(hdl->curl);
return 0;
}
const char *smlcurl_easy_get_contents(handle_t *hdl)
{
return hdl->buffer ? hdl->buffer : "";
}
const char *smlcurl_easy_get_effective_url(handle_t *hdl)
{
char *ret;
int err=curl_easy_getinfo(hdl->curl, CURLINFO_EFFECTIVE_URL, &ret);
return ret;
}
int smlcurl_easy_get_response_code(handle_t *hdl)
{
long ret;
int err=curl_easy_getinfo(hdl->curl, CURLINFO_RESPONSE_CODE, &ret);
return ret;
}
int smlcurl_easy_get_connect_code(handle_t *hdl)
{
long ret;
int err=curl_easy_getinfo(hdl->curl, CURLINFO_HTTP_CONNECTCODE, &ret);
return ret;
}
double smlcurl_easy_get_total_time(handle_t *hdl)
{
double ret;
int err=curl_easy_getinfo(hdl->curl, CURLINFO_TOTAL_TIME, &ret);
return ret;
}
const char * smlcurl_easy_get_content_type(handle_t *hdl)
{
char *ret;
int err=curl_easy_getinfo(hdl->curl, CURLINFO_CONTENT_TYPE, &ret);
return ret ? ret : "";
}
void smlcurl_easy_cleanup(handle_t *hdl)
{
free(hdl->buffer);
curl_easy_cleanup(hdl->curl);
}