2.55
xs_curl.h
1/* copyright (c) 2022 - 2024 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 int c = 0;
150 while (xs_dict_next(headers, &k, &v, &c)) {
151 xs *h = xs_fmt("%s: %s", k, v);
152
153 list = curl_slist_append(list, h);
154 }
155
156 /* disable server support for 100-continue */
157 list = curl_slist_append(list, "Expect:");
158
159 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
160
161 /* do it */
162 CURLcode cc = curl_easy_perform(curl);
163
164 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
165
166 curl_easy_cleanup(curl);
167
168 curl_slist_free_all(list);
169
170 if (status != NULL) {
171 if (lstatus == 0) {
172 /* set the timeout error to a fake HTTP status, or propagate as is */
173 if (cc == CURLE_OPERATION_TIMEDOUT)
174 lstatus = 599;
175 else
176 lstatus = -cc;
177 }
178
179 *status = (int) lstatus;
180 }
181
182 if (p_size != NULL)
183 *p_size = ipd.size;
184
185 if (payload != NULL) {
186 *payload = ipd.data;
187
188 /* add an asciiz just in case (but not touching p_size) */
189 if (ipd.data != NULL)
190 ipd.data[ipd.size] = '\0';
191 }
192 else
193 xs_free(ipd.data);
194
195 return response;
196}
197
198#endif /* XS_IMPLEMENTATION */
199
200#endif /* _XS_CURL_H */