snac2/xs_curl.h

201 lines
5.0 KiB
C
Raw Normal View History

2024-01-04 11:22:03 +03:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2022-09-19 21:41:11 +03:00
#ifndef _XS_CURL_H
#define _XS_CURL_H
2023-02-02 05:49:38 +03:00
xs_dict *xs_http_request(const char *method, const char *url,
const xs_dict *headers,
const xs_str *body, int b_size, int *status,
2023-01-28 19:49:02 +03:00
xs_str **payload, int *p_size, int timeout);
2022-09-19 21:41:11 +03:00
#ifdef XS_IMPLEMENTATION
#include <curl/curl.h>
static size_t _header_callback(char *buffer, size_t size,
2023-01-28 19:49:02 +03:00
size_t nitems, xs_dict **userdata)
2022-09-19 21:41:11 +03:00
{
2023-01-28 19:49:02 +03:00
xs_dict *headers = *userdata;
2022-09-19 21:41:11 +03:00
xs *l;
/* get the line */
l = xs_str_new(NULL);
l = xs_append_m(l, buffer, size * nitems);
2023-01-12 11:28:02 +03:00
l = xs_strip_i(l);
2022-09-19 21:41:11 +03:00
/* only the HTTP/x 200 line and the last one doesn't have ': ' */
if (xs_str_in(l, ": ") != -1) {
2022-09-20 00:08:59 +03:00
xs *knv = xs_split_n(l, ": ", 1);
2022-09-19 21:41:11 +03:00
2023-01-12 11:28:02 +03:00
xs_tolower_i(xs_list_get(knv, 0));
2022-09-20 14:36:20 +03:00
headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
2022-09-19 21:41:11 +03:00
}
else
if (xs_startswith(l, "HTTP/"))
headers = xs_dict_set(headers, "_proto", l);
2022-09-19 21:41:11 +03:00
*userdata = headers;
return nitems * size;
}
struct _payload_data {
char *data;
int size;
int offset;
};
static int _data_callback(void *buffer, size_t size,
size_t nitems, struct _payload_data *pd)
{
int sz = size * nitems;
/* open space */
pd->size += sz;
pd->data = xs_realloc(pd->data, _xs_blk_size(pd->size + 1));
2022-09-19 21:41:11 +03:00
/* copy data */
memcpy(pd->data + pd->offset, buffer, sz);
pd->offset += sz;
return sz;
}
static int _post_callback(char *buffer, size_t size,
size_t nitems, struct _payload_data *pd)
{
/* size of data left */
int sz = pd->size - pd->offset;
/* if it's still bigger than the provided space, trim */
2023-01-08 12:39:11 +03:00
if (sz > (int) (size * nitems))
2022-09-19 21:41:11 +03:00
sz = size * nitems;
memcpy(buffer, pd->data + pd->offset, sz);
/* skip sent data */
pd->offset += sz;
return sz;
}
2023-02-02 05:49:38 +03:00
xs_dict *xs_http_request(const char *method, const char *url,
const xs_dict *headers,
const xs_str *body, int b_size, int *status,
2023-01-28 19:49:02 +03:00
xs_str **payload, int *p_size, int timeout)
2022-09-19 21:41:11 +03:00
/* does an HTTP request */
{
2023-01-28 19:49:02 +03:00
xs_dict *response;
2022-09-19 21:41:11 +03:00
CURL *curl;
struct curl_slist *list = NULL;
2023-01-28 19:49:02 +03:00
xs_str *k;
xs_val *v;
2023-08-09 16:54:36 +03:00
long lstatus = 0;
struct _payload_data pd;
2022-09-19 21:41:11 +03:00
response = xs_dict_new();
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
if (timeout <= 0)
timeout = 8;
curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout);
2022-09-19 21:41:11 +03:00
#ifdef FORCE_HTTP_1_1
2022-09-19 21:41:11 +03:00
/* force HTTP/1.1 */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
#endif
2022-09-19 21:41:11 +03:00
/* obey redirections */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* store response headers here */
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _header_callback);
struct _payload_data ipd = { NULL, 0, 0 };
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _data_callback);
2023-02-02 05:37:17 +03:00
if (strcmp(method, "POST") == 0 || strcmp(method, "PUT") == 0) {
2023-05-07 08:42:47 +03:00
CURLoption curl_method = method[1] == 'O' ? CURLOPT_POST : CURLOPT_UPLOAD;
curl_easy_setopt(curl, curl_method, 1L);
2022-09-19 21:41:11 +03:00
if (body != NULL) {
if (b_size <= 0)
b_size = xs_size(body);
/* add the content-length header */
2023-05-07 08:42:47 +03:00
curl_easy_setopt(curl, curl_method == CURLOPT_POST ? CURLOPT_POSTFIELDSIZE : CURLOPT_INFILESIZE, b_size);
2022-09-19 21:41:11 +03:00
2023-02-02 05:49:38 +03:00
pd.data = (char *)body;
pd.size = b_size;
pd.offset = 0;
2022-09-19 21:41:11 +03:00
curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
}
}
/* fill the request headers */
2024-03-09 10:32:20 +03:00
int c = 0;
while (xs_dict_next(headers, &k, &v, &c)) {
2022-09-21 10:31:05 +03:00
xs *h = xs_fmt("%s: %s", k, v);
2022-09-19 21:41:11 +03:00
list = curl_slist_append(list, h);
}
2022-11-04 11:42:24 +03:00
/* disable server support for 100-continue */
list = curl_slist_append(list, "Expect:");
2022-09-19 21:41:11 +03:00
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
/* do it */
2023-08-09 16:54:36 +03:00
CURLcode cc = curl_easy_perform(curl);
2022-09-19 21:41:11 +03:00
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
curl_easy_cleanup(curl);
2022-10-26 09:29:11 +03:00
curl_slist_free_all(list);
2023-08-09 16:54:36 +03:00
if (status != NULL) {
if (lstatus == 0) {
/* set the timeout error to a fake HTTP status, or propagate as is */
if (cc == CURLE_OPERATION_TIMEDOUT)
lstatus = 599;
else
lstatus = -cc;
}
2023-08-09 16:54:36 +03:00
2022-09-19 21:41:11 +03:00
*status = (int) lstatus;
2023-08-09 16:54:36 +03:00
}
2022-09-19 21:41:11 +03:00
if (p_size != NULL)
*p_size = ipd.size;
if (payload != NULL) {
*payload = ipd.data;
/* add an asciiz just in case (but not touching p_size) */
if (ipd.data != NULL)
ipd.data[ipd.size] = '\0';
}
else
2022-10-25 10:32:41 +03:00
xs_free(ipd.data);
2022-09-19 21:41:11 +03:00
return response;
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_CURL_H */