-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.c
241 lines (200 loc) · 5.97 KB
/
api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
#include <ctype.h>
#include "api.h"
static const char *token = NULL;
static const char *api_url = NULL;
static const char *query_string = NULL;
void init_api(const char *url, const char *tok, const char *qs) {
curl_global_init(CURL_GLOBAL_ALL);
token = tok;
api_url = url;
query_string = qs;
}
static int curl_get(const char *url, char **buf) {
CURL *curl;
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
errbuf[0] = 0;
log_printf(LOG_DEBUG, "libcurl: Starting GET to %s\n", url);
mem_t response;
response.ptrs.write_ptr = NULL;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_USERNAME, token);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 128L);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 60L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
log_printf(LOG_ERROR, "libcurl: There was an error performing request:\n");
log_printf(LOG_ERROR, "libcurl: Error code %d\n", res);
size_t len = strlen(errbuf);
if (len) {
log_printf(LOG_ERROR, "libcurl: %s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : ""));
} else {
log_printf(LOG_ERROR, "%s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
*buf = NULL;
if (response.ptrs.write_ptr != NULL) {
free(response.ptrs.write_ptr);
}
return -1;
}
else {
log_printf(LOG_DEBUG, "libcurl: Request successful. Received %d bytes.\n", strlen(response.ptrs.write_ptr));
log_printf(LOG_DEBUG, "Received body: %s\n", response.ptrs.write_ptr);
*buf = response.ptrs.write_ptr;
}
curl_easy_cleanup(curl);
return 0;
}
static int curl_put(const char *url, const char *buf) {
CURL *curl;
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
errbuf[0] = 0;
log_printf(LOG_DEBUG, "libcurl: Starting PUT to %s\n", url);
log_printf(LOG_DEBUG, "libcurl: Body size %d\n", strlen(buf));
log_printf(LOG_DEBUG, "libcurl: Body contents: %s\n", buf);
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl = curl_easy_init();
mem_t mem;
mem.ptrs.read_ptr = buf;
mem.offset = 0;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_READDATA, &mem);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)strlen(buf));
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_USERNAME, token);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 128L);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 60L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60L);
res = curl_easy_perform(curl);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
if(res != CURLE_OK) {
log_printf(LOG_ERROR, "libcurl: There was an error performing request:\n");
log_printf(LOG_ERROR, "libcurl: Error code %d\n", res);
size_t len = strlen(errbuf);
if (len) {
log_printf(LOG_ERROR, "libcurl: %s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : ""));
} else {
log_printf(LOG_ERROR, "%s\n", curl_easy_strerror(res));
}
return -1;
}
log_printf(LOG_DEBUG, "libcurl: Request successful.\n");
return 0;
}
int api_job_statistics(const char *payload) {
char *path = alloc_sprintf("%s%s", api_url, API_JOBS_STATISTICS);
if(curl_put(path, payload) < 0) {
free(path);
return -1;
}
free(path);
return 0;
}
int api_jobs_shift(char **buf) {
if(api_url == NULL) {
return -1;
}
char *path;
if(query_string != NULL) {
path = alloc_sprintf("%s%s?%s", api_url, API_JOBS_SHIFT, query_string);
}
else {
path = alloc_sprintf("%s%s", api_url, API_JOBS_SHIFT);
}
if(curl_get(path, buf) < 0) {
free(path);
return -1;
}
free(path);
return 0;
}
int api_jobs_status(const char *build_id) {
if(api_url == NULL) {
return -1;
}
char *path = alloc_sprintf("%s%s" API_STATUS_QUERYSTRING, api_url, API_JOBS_STATUS, build_id);
char *ret;
if(curl_get(path, &ret) < 0) {
return 0;
}
int f;
if(strstr(ret, "USR1")) {
f = 1;
}
else {
f = 0;
}
free(ret);
free(path);
return f;
}
int api_jobs_feedback(const char *buf) {
if(api_url == NULL) {
return -1;
}
char *path = alloc_sprintf("%s%s", api_url, API_JOBS_FEEDBACK);
char *final_send = alloc_sprintf("{\"worker_queue\":\"%s\",\"worker_class\":\"%s\",\"worker_args\":[%s]}", OBSERVER_QUEUE, OBSERVER_CLASS, buf);
if(curl_put(path, final_send) < 0) {
free(final_send);
free(path);
return -1;
}
free(final_send);
free(path);
return 0;
}
int api_jobs_logs(const char *key, const char *buf) {
if(api_url == NULL) {
return -1;
}
char *path = alloc_sprintf("%s%s", api_url, API_JOBS_LOGS);
int len = strlen(buf), i;
char *escaped_buf = xmalloc(len * 2 + 1);
char *p = escaped_buf;
for(i = 0; i<len; i++) {
char c = buf[i];
switch(c) {
case '"': p += sprintf(p, "\\\""); break;
case '\\': p += sprintf(p, "\\\\"); break;
case '\b': p += sprintf(p, "\\b"); break;
case '\f': p += sprintf(p, "\\f"); break;
case '\n': p += sprintf(p, "\\n"); break;
case '\r': p += sprintf(p, "\\r"); break;
case '\t': p += sprintf(p, "\\t"); break;
default:
if (isprint(c)) {
*(p++) = c;
}
}
}
*p = '\0';
char *payload = alloc_sprintf(API_LOGS_PAYLOAD, key, escaped_buf);
free(escaped_buf);
curl_put(path, payload);
free(payload);
free(path);
return 0;
}