2.71
xs_curl.h
1/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
2
3#ifndef _XS_CURL_H
4
5#define _XS_CURL_H
6
7xs_dict *xs_http_request(const char *method, const char *url,
8 const xs_dict *headers,
9 const xs_str *body, int b_size, int *status,
10 xs_str **payload, int *p_size, int timeout);
11
12#ifdef XS_IMPLEMENTATION
13
14#include <curl/curl.h>
15
16static size_t _header_callback(char *buffer, size_t size,
17 size_t nitems, xs_dict **userdata)
18{
19 xs_dict *headers = *userdata;
20 xs *l;
21
22 /* get the line */
23 l = xs_str_new(NULL);
24 l = xs_append_m(l, buffer, size * nitems);
25 l = xs_strip_i(l);
26
27 /* only the HTTP/x 200 line and the last one doesn't have ': ' */
28 if (xs_str_in(l, ": ") != -1) {
29 xs *knv = xs_split_n(l, ": ", 1);
30
31 xs_tolower_i((xs_str *)xs_list_get(knv, 0));
32
33 headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
34 }
35 else
36 if (xs_startswith(l, "HTTP/"))
37 headers = xs_dict_set(headers, "_proto", l);
38
39 *userdata = headers;
40
41 return nitems * size;
42}
43
44
45struct _payload_data {
46 char *data;
47 int size;
48 int offset;
49};
50
51static int _data_callback(void *buffer, size_t size,
52 size_t nitems, struct _payload_data *pd)
53{
54 int sz = size * nitems;
55
56 /* open space */
57 pd->size += sz;
58 pd->data = xs_realloc(pd->data, _xs_blk_size(pd->size + 1));
59
60 /* copy data */
61 memcpy(pd->data + pd->offset, buffer, sz);
62 pd->offset += sz;
63
64 return sz;
65}
66
67
68static int _post_callback(char *buffer, size_t size,
69 size_t nitems, struct _payload_data *pd)
70{
71 /* size of data left */
72 int sz = pd->size - pd->offset;
73
74 /* if it's still bigger than the provided space, trim */
75 if (sz > (int) (size * nitems))
76 sz = size * nitems;
77
78 memcpy(buffer, pd->data + pd->offset, sz);
79
80 /* skip sent data */
81 pd->offset += sz;
82
83 return sz;
84}
85
86
87xs_dict *xs_http_request(const char *method, const char *url,
88 const xs_dict *headers,
89 const xs_str *body, int b_size, int *status,
90 xs_str **payload, int *p_size, int timeout)
91/* does an HTTP request */
92{
93 xs_dict *response;
94 CURL *curl;
95 struct curl_slist *list = NULL;
96 const xs_str *k;
97 const xs_val *v;
98 long lstatus = 0;
99 struct _payload_data pd;
100
101 response = xs_dict_new();
102
103 curl = curl_easy_init();
104
105 curl_easy_setopt(curl, CURLOPT_URL, url);
106
107 if (timeout <= 0)
108 timeout = 8;
109
110 curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout);
111
112#ifdef FORCE_HTTP_1_1
113 /* force HTTP/1.1 */
114 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
115#endif
116
117 /* obey redirections */
118 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
119
120 /* store response headers here */
121 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
122 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _header_callback);
123
124 struct _payload_data ipd = { NULL, 0, 0 };
125 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
126 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _data_callback);
127
128 if (strcmp(method, "POST") == 0 || strcmp(method, "PUT") == 0) {
129 CURLoption curl_method = method[1] == 'O' ? CURLOPT_POST : CURLOPT_UPLOAD;
130 curl_easy_setopt(curl, curl_method, 1L);
131
132 if (body != NULL) {
133 if (b_size <= 0)
134 b_size = xs_size(body);
135
136 /* add the content-length header */
137 curl_easy_setopt(curl, curl_method == CURLOPT_POST ? CURLOPT_POSTFIELDSIZE : CURLOPT_INFILESIZE, b_size);
138
139 pd.data = (char *)body;
140 pd.size = b_size;
141 pd.offset = 0;
142
143 curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
144 curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
145 }
146 }
147
148 /* fill the request headers */
149 xs_dict_foreach(headers, k, v) {
150 xs *h = xs_fmt("%s: %s", k, v);
151
152 list = curl_slist_append(list, h);
153 }
154
155 /* disable server support for 100-continue */
156 list = curl_slist_append(list, "Expect:");
157
158 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
159
160 /* do it */
161 CURLcode cc = curl_easy_perform(curl);
162
163 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
164
165 curl_easy_cleanup(curl);
166
167 curl_slist_free_all(list);
168
169 if (status != NULL) {
170 if (lstatus == 0) {
171 /* set the timeout error to a fake HTTP status, or propagate as is */
172 if (cc == CURLE_OPERATION_TIMEDOUT)
173 lstatus = 599;
174 else
175 lstatus = -cc;
176 }
177
178 *status = (int) lstatus;
179 }
180
181 if (p_size != NULL)
182 *p_size = ipd.size;
183
184 if (payload != NULL) {
185 *payload = ipd.data;
186
187 /* add an asciiz just in case (but not touching p_size) */
188 if (ipd.data != NULL)
189 ipd.data[ipd.size] = '\0';
190 }
191 else
192 xs_free(ipd.data);
193
194 return response;
195}
196
197#endif /* XS_IMPLEMENTATION */
198
199#endif /* _XS_CURL_H */