main
xs_curl.h
1/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
2
3#ifndef _XS_CURL_H
4
5#define _XS_CURL_H
6
7#include "xs.h"
8
9xs_dict *xs_http_request(const char *method, const char *url,
10 const xs_dict *headers,
11 const xs_str *body, int b_size, int *status,
12 xs_str **payload, int *p_size, int timeout);
13
14int xs_smtp_request(const char *url, const char *user, const char *pass,
15 const char *from, const char *to, const xs_str *body,
16 int use_ssl);
17
18const char *xs_curl_strerr(int errnum);
19
20extern int xs_curl_force_http1;
21extern int xs_curl_force_ipv6;
22
23#ifdef XS_IMPLEMENTATION
24
25#include <curl/curl.h>
26
27int xs_curl_force_http1 = 0;
28int xs_curl_force_ipv6 = 0;
29
30static size_t _header_callback(char *buffer, size_t size,
31 size_t nitems, xs_dict **userdata)
32{
33 xs_dict *headers = *userdata;
34 xs *l;
35
36 /* get the line */
37 l = xs_str_new(NULL);
38 l = xs_append_m(l, buffer, size * nitems);
39 l = xs_strip_i(l);
40
41 /* only the HTTP/x 200 line and the last one doesn't have ': ' */
42 if (xs_str_in(l, ": ") != -1) {
43 xs *knv = xs_split_n(l, ": ", 1);
44
45 xs_tolower_i((xs_str *)xs_list_get(knv, 0));
46
47 headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
48 }
49 else
50 if (xs_startswith(l, "HTTP/"))
51 headers = xs_dict_set(headers, "_proto", l);
52
53 *userdata = headers;
54
55 return nitems * size;
56}
57
58
59struct _payload_data {
60 char *data;
61 int size;
62 int offset;
63};
64
65static size_t _data_callback(void *buffer, size_t size,
66 size_t nitems, struct _payload_data *pd)
67{
68 size_t sz = size * nitems;
69
70 /* open space */
71 pd->size += sz;
72 pd->data = xs_realloc(pd->data, _xs_blk_size(pd->size + 1));
73
74 /* copy data */
75 memcpy(pd->data + pd->offset, buffer, sz);
76 pd->offset += sz;
77
78 return sz;
79}
80
81
82static size_t _post_callback(char *buffer, size_t size,
83 size_t nitems, struct _payload_data *pd)
84{
85 /* size of data left */
86 size_t sz = pd->size - pd->offset;
87
88 /* if it's still bigger than the provided space, trim */
89 if (sz > (size_t) (size * nitems))
90 sz = size * nitems;
91
92 memcpy(buffer, pd->data + pd->offset, sz);
93
94 /* skip sent data */
95 pd->offset += sz;
96
97 return sz;
98}
99
100
101xs_dict *xs_http_request(const char *method, const char *url,
102 const xs_dict *headers,
103 const xs_str *body, int b_size, int *status,
104 xs_str **payload, int *p_size, int timeout)
105/* does an HTTP request */
106{
107 xs_dict *response;
108 CURL *curl;
109 struct curl_slist *list = NULL;
110 const xs_str *k;
111 const xs_val *v;
112 long lstatus = 0;
113 struct _payload_data pd;
114
115 response = xs_dict_new();
116
117 curl = curl_easy_init();
118
119 curl_easy_setopt(curl, CURLOPT_URL, url);
120
121 if (timeout <= 0)
122 timeout = 8;
123
124 curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout);
125
126 if (xs_curl_force_http1) {
127 /* force HTTP/1.1 */
128 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
129 }
130
131 if (xs_curl_force_ipv6) {
132 /* force IPv6 */
133 curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
134 }
135
136 /* obey redirections */
137 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
138
139 /* store response headers here */
140 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
141 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_read_callback) _header_callback);
142
143 struct _payload_data ipd = { NULL, 0, 0 };
144 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
145 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_read_callback) _data_callback);
146
147 if (strcmp(method, "POST") == 0 || strcmp(method, "PUT") == 0) {
148 CURLoption curl_method = method[1] == 'O' ? CURLOPT_POST : CURLOPT_UPLOAD;
149 curl_easy_setopt(curl, curl_method, 1L);
150
151 if (body != NULL) {
152 if (b_size <= 0)
153 b_size = xs_size(body);
154
155 /* add the content-length header */
156 curl_easy_setopt(curl, curl_method == CURLOPT_POST ? CURLOPT_POSTFIELDSIZE : CURLOPT_INFILESIZE, b_size);
157
158 pd.data = (char *)body;
159 pd.size = b_size;
160 pd.offset = 0;
161
162 curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
163 curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback) _post_callback);
164 }
165 }
166
167 /* fill the request headers */
168 xs_dict_foreach(headers, k, v) {
169 xs *h = xs_fmt("%s: %s", k, v);
170
171 list = curl_slist_append(list, h);
172 }
173
174 /* disable server support for 100-continue */
175 list = curl_slist_append(list, "Expect:");
176
177 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
178
179 /* do it */
180 CURLcode cc = curl_easy_perform(curl);
181
182 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
183
184 curl_easy_cleanup(curl);
185
186 curl_slist_free_all(list);
187
188 if (status != NULL) {
189 if (lstatus == 0) {
190 /* set the timeout error to a fake HTTP status, or propagate as is */
191 if (cc == CURLE_OPERATION_TIMEDOUT)
192 lstatus = 599;
193 else
194 lstatus = -cc;
195 }
196
197 *status = (int) lstatus;
198 }
199
200 if (p_size != NULL)
201 *p_size = ipd.size;
202
203 if (payload != NULL) {
204 *payload = ipd.data;
205
206 /* add an asciiz just in case (but not touching p_size) */
207 if (ipd.data != NULL)
208 ipd.data[ipd.size] = '\0';
209 }
210 else
211 xs_free(ipd.data);
212
213 return response;
214}
215
216
217int xs_smtp_request(const char *url, const char *user, const char *pass,
218 const char *from, const char *to, const xs_str *body,
219 int use_ssl)
220{
221 CURL *curl;
222 CURLcode res = CURLE_OK;
223 struct curl_slist *rcpt = NULL;
224 struct _payload_data pd = {
225 .data = (char *)body,
226 .size = strlen(body),
227 .offset = 0
228 };
229
230 curl = curl_easy_init();
231
232 curl_easy_setopt(curl, CURLOPT_URL, url);
233 if (user && pass) {
234 /* allow authless connections, to, e.g. localhost */
235 curl_easy_setopt(curl, CURLOPT_USERNAME, user);
236 curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
237 }
238
239 if (use_ssl)
240 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
241
242 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
243
244 rcpt = curl_slist_append(rcpt, to);
245 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt);
246
247 curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
248 curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback) _post_callback);
249 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
250
251 res = curl_easy_perform(curl);
252
253 curl_easy_cleanup(curl);
254 curl_slist_free_all(rcpt);
255
256 return (int)res;
257}
258
259
260const char *xs_curl_strerr(int errnum)
261{
262 CURLcode cc = errnum < 0 ? -errnum : errnum;
263
264 return curl_easy_strerror(cc);
265}
266
267
268#endif /* XS_IMPLEMENTATION */
269
270#endif /* _XS_CURL_H */