summaryrefslogtreecommitdiff
path: root/xs_curl.h
diff options
context:
space:
mode:
authorGravatar default2022-09-19 20:41:11 +0200
committerGravatar default2022-09-19 20:41:11 +0200
commit67288a763b8bceadbb128d2cf5bdc431ba8a686f (patch)
tree0a0bc1922016bbd3d3cd2ec3c80a033970280a9f /xs_curl.h
parentProject started. (diff)
downloadpenes-snac2-67288a763b8bceadbb128d2cf5bdc431ba8a686f.tar.gz
penes-snac2-67288a763b8bceadbb128d2cf5bdc431ba8a686f.tar.xz
penes-snac2-67288a763b8bceadbb128d2cf5bdc431ba8a686f.zip
Imported xs.
Diffstat (limited to 'xs_curl.h')
-rw-r--r--xs_curl.h177
1 files changed, 177 insertions, 0 deletions
diff --git a/xs_curl.h b/xs_curl.h
new file mode 100644
index 0000000..01d9311
--- /dev/null
+++ b/xs_curl.h
@@ -0,0 +1,177 @@
1/* copyright (c) 2022 grunfink - MIT license */
2
3#ifndef _XS_CURL_H
4
5#define _XS_CURL_H
6
7d_char *xs_http_request(char *method, char *url, d_char *headers,
8 d_char *body, int b_size,
9 int *status, d_char **payload, int *p_size);
10
11#ifdef XS_IMPLEMENTATION
12
13#include <curl/curl.h>
14
15static size_t _header_callback(char *buffer, size_t size,
16 size_t nitems, d_char **userdata)
17{
18 d_char *headers = *userdata;
19 xs *l;
20
21 /* get the line */
22 l = xs_str_new(NULL);
23 l = xs_append_m(l, buffer, size * nitems);
24 l = xs_strip(l);
25
26 /* only the HTTP/x 200 line and the last one doesn't have ': ' */
27 if (xs_str_in(l, ": ") != -1) {
28 xs *knv = xs_splitn(l, ": ", 1);
29
30 headers = xs_dict_append(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
31 }
32 else
33 if (xs_startswith(l, "HTTP/"))
34 headers = xs_dict_append(headers, "_proto", l);
35
36 *userdata = headers;
37
38 return nitems * size;
39}
40
41
42struct _payload_data {
43 char *data;
44 int size;
45 int offset;
46};
47
48static int _data_callback(void *buffer, size_t size,
49 size_t nitems, struct _payload_data *pd)
50{
51 int sz = size * nitems;
52
53 /* open space */
54 pd->size += sz;
55 pd->data = realloc(pd->data, pd->size + 1);
56
57 /* copy data */
58 memcpy(pd->data + pd->offset, buffer, sz);
59 pd->offset += sz;
60
61 return sz;
62}
63
64
65static int _post_callback(char *buffer, size_t size,
66 size_t nitems, struct _payload_data *pd)
67{
68 /* size of data left */
69 int sz = pd->size - pd->offset;
70
71 /* if it's still bigger than the provided space, trim */
72 if (sz > size * nitems)
73 sz = size * nitems;
74
75 memcpy(buffer, pd->data + pd->offset, sz);
76
77 /* skip sent data */
78 pd->offset += sz;
79
80 return sz;
81}
82
83
84d_char *xs_http_request(char *method, char *url, d_char *headers,
85 d_char *body, int b_size,
86 int *status, d_char **payload, int *p_size)
87/* does an HTTP request */
88{
89 d_char *response;
90 CURL *curl;
91 struct curl_slist *list = NULL;
92 char *k, *v, *p;
93 long lstatus;
94
95 response = xs_dict_new();
96
97 curl = curl_easy_init();
98
99 curl_easy_setopt(curl, CURLOPT_URL, url);
100
101 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
102
103 /* force HTTP/1.1 */
104 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
105
106 /* obey redirections */
107 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
108
109 /* store response headers here */
110 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
111 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _header_callback);
112
113 struct _payload_data ipd = { NULL, 0, 0 };
114 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
115 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _data_callback);
116
117 if (strcmp(method, "POST") == 0) {
118 curl_easy_setopt(curl, CURLOPT_POST, 1L);
119
120 if (body != NULL) {
121 if (b_size <= 0)
122 b_size = xs_size(body);
123
124 /* add the content-length header */
125 char tmp[32];
126 sprintf(tmp, "content-length: %d", b_size);
127 list = curl_slist_append(list, tmp);
128
129 struct _payload_data pd = { body, b_size, 0 };
130
131 curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
132 curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
133 }
134 }
135
136 /* fill the request headers */
137 p = headers;
138 while (xs_dict_iter(&p, &k, &v)) {
139 xs *h;
140
141 const char *args[] = { k, v, NULL };
142 h = xs_fmt("%s: %s", args);
143
144 list = curl_slist_append(list, h);
145 }
146
147 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
148
149 /* do it */
150 curl_easy_perform(curl);
151
152 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
153
154 curl_easy_cleanup(curl);
155
156 if (status != NULL)
157 *status = (int) lstatus;
158
159 if (p_size != NULL)
160 *p_size = ipd.size;
161
162 if (payload != NULL) {
163 *payload = ipd.data;
164
165 /* add an asciiz just in case (but not touching p_size) */
166 if (ipd.data != NULL)
167 ipd.data[ipd.size] = '\0';
168 }
169 else
170 free(ipd.data);
171
172 return response;
173}
174
175#endif /* XS_IMPLEMENTATION */
176
177#endif /* _XS_CURL_H */