Blame


1 94a3f4e9 2024-03-30 thomas /*
2 94a3f4e9 2024-03-30 thomas * Copyright (c) 2024 Omar Polo <op@openbsd.org>
3 94a3f4e9 2024-03-30 thomas *
4 94a3f4e9 2024-03-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 94a3f4e9 2024-03-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 94a3f4e9 2024-03-30 thomas * copyright notice and this permission notice appear in all copies.
7 94a3f4e9 2024-03-30 thomas *
8 94a3f4e9 2024-03-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 94a3f4e9 2024-03-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 94a3f4e9 2024-03-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 94a3f4e9 2024-03-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 94a3f4e9 2024-03-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 94a3f4e9 2024-03-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 94a3f4e9 2024-03-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 94a3f4e9 2024-03-30 thomas */
16 94a3f4e9 2024-03-30 thomas
17 94a3f4e9 2024-03-30 thomas #include <sys/time.h>
18 94a3f4e9 2024-03-30 thomas #include <sys/types.h>
19 94a3f4e9 2024-03-30 thomas #include <sys/socket.h>
20 94a3f4e9 2024-03-30 thomas
21 94a3f4e9 2024-03-30 thomas #include <err.h>
22 94a3f4e9 2024-03-30 thomas #include <errno.h>
23 94a3f4e9 2024-03-30 thomas #include <fcntl.h>
24 94a3f4e9 2024-03-30 thomas #include <limits.h>
25 94a3f4e9 2024-03-30 thomas #include <netdb.h>
26 94a3f4e9 2024-03-30 thomas #include <poll.h>
27 94a3f4e9 2024-03-30 thomas #include <stdio.h>
28 94a3f4e9 2024-03-30 thomas #include <stdlib.h>
29 94a3f4e9 2024-03-30 thomas #include <string.h>
30 94a3f4e9 2024-03-30 thomas #include <unistd.h>
31 94a3f4e9 2024-03-30 thomas
32 94a3f4e9 2024-03-30 thomas #include "got_opentemp.h"
33 94a3f4e9 2024-03-30 thomas #include "got_version.h"
34 94a3f4e9 2024-03-30 thomas
35 94a3f4e9 2024-03-30 thomas #include "bufio.h"
36 fcfdd0a1 2024-03-30 thomas #include "utf8d.h"
37 94a3f4e9 2024-03-30 thomas
38 94a3f4e9 2024-03-30 thomas #define USERAGENT "got-notify-http/" GOT_VERSION_STR
39 94a3f4e9 2024-03-30 thomas
40 94a3f4e9 2024-03-30 thomas static int http_timeout = 300; /* 5 minutes in seconds */
41 94a3f4e9 2024-03-30 thomas
42 94a3f4e9 2024-03-30 thomas __dead static void
43 94a3f4e9 2024-03-30 thomas usage(void)
44 94a3f4e9 2024-03-30 thomas {
45 94a3f4e9 2024-03-30 thomas fprintf(stderr, "usage: %s [-c] -h host -p port path\n",
46 94a3f4e9 2024-03-30 thomas getprogname());
47 94a3f4e9 2024-03-30 thomas exit(1);
48 94a3f4e9 2024-03-30 thomas }
49 94a3f4e9 2024-03-30 thomas
50 94a3f4e9 2024-03-30 thomas static int
51 94a3f4e9 2024-03-30 thomas dial(const char *host, const char *port)
52 94a3f4e9 2024-03-30 thomas {
53 94a3f4e9 2024-03-30 thomas struct addrinfo hints, *res, *res0;
54 94a3f4e9 2024-03-30 thomas const char *cause = NULL;
55 94a3f4e9 2024-03-30 thomas int s, error, save_errno;
56 94a3f4e9 2024-03-30 thomas
57 94a3f4e9 2024-03-30 thomas memset(&hints, 0, sizeof(hints));
58 94a3f4e9 2024-03-30 thomas hints.ai_family = AF_UNSPEC;
59 94a3f4e9 2024-03-30 thomas hints.ai_socktype = SOCK_STREAM;
60 94a3f4e9 2024-03-30 thomas error = getaddrinfo(host, port, &hints, &res0);
61 94a3f4e9 2024-03-30 thomas if (error)
62 94a3f4e9 2024-03-30 thomas errx(1, "failed to resolve %s:%s: %s", host, port,
63 94a3f4e9 2024-03-30 thomas gai_strerror(error));
64 94a3f4e9 2024-03-30 thomas
65 94a3f4e9 2024-03-30 thomas s = -1;
66 94a3f4e9 2024-03-30 thomas for (res = res0; res; res = res->ai_next) {
67 94a3f4e9 2024-03-30 thomas s = socket(res->ai_family, res->ai_socktype,
68 94a3f4e9 2024-03-30 thomas res->ai_protocol);
69 94a3f4e9 2024-03-30 thomas if (s == -1) {
70 94a3f4e9 2024-03-30 thomas cause = "socket";
71 94a3f4e9 2024-03-30 thomas continue;
72 94a3f4e9 2024-03-30 thomas }
73 94a3f4e9 2024-03-30 thomas
74 94a3f4e9 2024-03-30 thomas if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
75 94a3f4e9 2024-03-30 thomas cause = "connect";
76 94a3f4e9 2024-03-30 thomas save_errno = errno;
77 94a3f4e9 2024-03-30 thomas close(s);
78 94a3f4e9 2024-03-30 thomas errno = save_errno;
79 94a3f4e9 2024-03-30 thomas s = -1;
80 94a3f4e9 2024-03-30 thomas continue;
81 94a3f4e9 2024-03-30 thomas }
82 94a3f4e9 2024-03-30 thomas
83 94a3f4e9 2024-03-30 thomas break;
84 94a3f4e9 2024-03-30 thomas }
85 94a3f4e9 2024-03-30 thomas
86 94a3f4e9 2024-03-30 thomas freeaddrinfo(res0);
87 94a3f4e9 2024-03-30 thomas if (s == -1)
88 94a3f4e9 2024-03-30 thomas err(1, "%s", cause);
89 94a3f4e9 2024-03-30 thomas return s;
90 94a3f4e9 2024-03-30 thomas }
91 94a3f4e9 2024-03-30 thomas
92 94a3f4e9 2024-03-30 thomas static void
93 94a3f4e9 2024-03-30 thomas escape(FILE *fp, const uint8_t *s)
94 94a3f4e9 2024-03-30 thomas {
95 fcfdd0a1 2024-03-30 thomas uint32_t codepoint, state;
96 fcfdd0a1 2024-03-30 thomas const uint8_t *start = s;
97 94a3f4e9 2024-03-30 thomas
98 fcfdd0a1 2024-03-30 thomas state = 0;
99 fcfdd0a1 2024-03-30 thomas for (; *s; ++s) {
100 fcfdd0a1 2024-03-30 thomas switch (decode(&state, &codepoint, *s)) {
101 fcfdd0a1 2024-03-30 thomas case UTF8_ACCEPT:
102 fcfdd0a1 2024-03-30 thomas switch (codepoint) {
103 fcfdd0a1 2024-03-30 thomas case '"':
104 fcfdd0a1 2024-03-30 thomas case '\\':
105 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\%c", *s);
106 fcfdd0a1 2024-03-30 thomas break;
107 fcfdd0a1 2024-03-30 thomas case '\b':
108 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\b");
109 fcfdd0a1 2024-03-30 thomas break;
110 fcfdd0a1 2024-03-30 thomas case '\f':
111 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\f");
112 fcfdd0a1 2024-03-30 thomas break;
113 fcfdd0a1 2024-03-30 thomas case '\n':
114 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\n");
115 fcfdd0a1 2024-03-30 thomas break;
116 fcfdd0a1 2024-03-30 thomas case '\r':
117 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\r");
118 fcfdd0a1 2024-03-30 thomas break;
119 fcfdd0a1 2024-03-30 thomas case '\t':
120 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\t");
121 fcfdd0a1 2024-03-30 thomas break;
122 fcfdd0a1 2024-03-30 thomas default:
123 fcfdd0a1 2024-03-30 thomas /* other control characters */
124 fcfdd0a1 2024-03-30 thomas if (codepoint < ' ' || codepoint == 0x7F) {
125 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\u%04x", codepoint);
126 fcfdd0a1 2024-03-30 thomas break;
127 fcfdd0a1 2024-03-30 thomas }
128 fcfdd0a1 2024-03-30 thomas fwrite(start, 1, s - start + 1, fp);
129 fcfdd0a1 2024-03-30 thomas break;
130 fcfdd0a1 2024-03-30 thomas }
131 fcfdd0a1 2024-03-30 thomas start = s + 1;
132 94a3f4e9 2024-03-30 thomas break;
133 fcfdd0a1 2024-03-30 thomas
134 fcfdd0a1 2024-03-30 thomas case UTF8_REJECT:
135 fcfdd0a1 2024-03-30 thomas /* bad UTF-8 sequence; try to recover */
136 fcfdd0a1 2024-03-30 thomas fputs("\\uFFFD", fp);
137 fcfdd0a1 2024-03-30 thomas state = UTF8_ACCEPT;
138 fcfdd0a1 2024-03-30 thomas start = s + 1;
139 94a3f4e9 2024-03-30 thomas break;
140 94a3f4e9 2024-03-30 thomas }
141 94a3f4e9 2024-03-30 thomas }
142 94a3f4e9 2024-03-30 thomas }
143 94a3f4e9 2024-03-30 thomas
144 94a3f4e9 2024-03-30 thomas static void
145 94a3f4e9 2024-03-30 thomas json_field(FILE *fp, const char *key, const char *val, int comma)
146 94a3f4e9 2024-03-30 thomas {
147 94a3f4e9 2024-03-30 thomas fprintf(fp, "\"%s\":\"", key);
148 94a3f4e9 2024-03-30 thomas escape(fp, val);
149 94a3f4e9 2024-03-30 thomas fprintf(fp, "\"%s", comma ? "," : "");
150 94a3f4e9 2024-03-30 thomas }
151 94a3f4e9 2024-03-30 thomas
152 9c485a11 2024-03-30 thomas static void
153 9c485a11 2024-03-30 thomas json_author(FILE *fp, const char *type, char *address, int comma)
154 9c485a11 2024-03-30 thomas {
155 9c485a11 2024-03-30 thomas char *gt, *lt, *at, *email, *endname;
156 9c485a11 2024-03-30 thomas
157 9c485a11 2024-03-30 thomas fprintf(fp, "\"%s\":{", type);
158 9c485a11 2024-03-30 thomas
159 9c485a11 2024-03-30 thomas gt = strchr(address, '<');
160 9c485a11 2024-03-30 thomas if (gt != NULL) {
161 9c485a11 2024-03-30 thomas /* long format, e.g. "Omar Polo <op@openbsd.org>" */
162 9c485a11 2024-03-30 thomas
163 9c485a11 2024-03-30 thomas json_field(fp, "full", address, 1);
164 9c485a11 2024-03-30 thomas
165 9c485a11 2024-03-30 thomas endname = gt;
166 9c485a11 2024-03-30 thomas while (endname > address && endname[-1] == ' ')
167 9c485a11 2024-03-30 thomas endname--;
168 9c485a11 2024-03-30 thomas
169 9c485a11 2024-03-30 thomas *endname = '\0';
170 9c485a11 2024-03-30 thomas json_field(fp, "name", address, 1);
171 9c485a11 2024-03-30 thomas
172 9c485a11 2024-03-30 thomas email = gt + 1;
173 9c485a11 2024-03-30 thomas lt = strchr(email, '>');
174 9c485a11 2024-03-30 thomas if (lt)
175 9c485a11 2024-03-30 thomas *lt = '\0';
176 9c485a11 2024-03-30 thomas
177 9c485a11 2024-03-30 thomas json_field(fp, "mail", email, 1);
178 9c485a11 2024-03-30 thomas
179 9c485a11 2024-03-30 thomas at = strchr(email, '@');
180 9c485a11 2024-03-30 thomas if (at)
181 9c485a11 2024-03-30 thomas *at = '\0';
182 9c485a11 2024-03-30 thomas
183 9c485a11 2024-03-30 thomas json_field(fp, "user", email, 0);
184 9c485a11 2024-03-30 thomas } else {
185 9c485a11 2024-03-30 thomas /* short format only shows the username */
186 9c485a11 2024-03-30 thomas json_field(fp, "user", address, 0);
187 9c485a11 2024-03-30 thomas }
188 9c485a11 2024-03-30 thomas
189 9c485a11 2024-03-30 thomas fprintf(fp, "}%s", comma ? "," : "");
190 9c485a11 2024-03-30 thomas }
191 9c485a11 2024-03-30 thomas
192 94a3f4e9 2024-03-30 thomas static int
193 20d96a10 2024-03-30 thomas jsonify_commit_short(FILE *fp, char *line)
194 94a3f4e9 2024-03-30 thomas {
195 94a3f4e9 2024-03-30 thomas char *t, *date, *id, *author, *message;
196 94a3f4e9 2024-03-30 thomas
197 20d96a10 2024-03-30 thomas t = line;
198 20d96a10 2024-03-30 thomas date = t;
199 20d96a10 2024-03-30 thomas if ((t = strchr(t, ' ')) == NULL)
200 20d96a10 2024-03-30 thomas errx(1, "malformed line");
201 20d96a10 2024-03-30 thomas *t++ = '\0';
202 94a3f4e9 2024-03-30 thomas
203 20d96a10 2024-03-30 thomas id = t;
204 20d96a10 2024-03-30 thomas if ((t = strchr(t, ' ')) == NULL)
205 20d96a10 2024-03-30 thomas errx(1, "malformed line");
206 20d96a10 2024-03-30 thomas *t++ = '\0';
207 94a3f4e9 2024-03-30 thomas
208 20d96a10 2024-03-30 thomas author = t;
209 20d96a10 2024-03-30 thomas if ((t = strchr(t, ' ')) == NULL)
210 20d96a10 2024-03-30 thomas errx(1, "malformed line");
211 20d96a10 2024-03-30 thomas *t++ = '\0';
212 94a3f4e9 2024-03-30 thomas
213 20d96a10 2024-03-30 thomas message = t;
214 94a3f4e9 2024-03-30 thomas
215 20d96a10 2024-03-30 thomas fprintf(fp, "{\"short\":true,");
216 20d96a10 2024-03-30 thomas json_field(fp, "id", id, 1);
217 20d96a10 2024-03-30 thomas json_author(fp, "committer", author, 1);
218 20d96a10 2024-03-30 thomas json_field(fp, "date", date, 1);
219 20d96a10 2024-03-30 thomas json_field(fp, "short_message", message, 0);
220 20d96a10 2024-03-30 thomas fprintf(fp, "}");
221 94a3f4e9 2024-03-30 thomas
222 94a3f4e9 2024-03-30 thomas return 0;
223 94a3f4e9 2024-03-30 thomas }
224 94a3f4e9 2024-03-30 thomas
225 94a3f4e9 2024-03-30 thomas static int
226 20d96a10 2024-03-30 thomas jsonify_commit(FILE *fp, char **line, ssize_t *linesize)
227 94a3f4e9 2024-03-30 thomas {
228 94a3f4e9 2024-03-30 thomas const char *errstr;
229 9c485a11 2024-03-30 thomas char *author = NULL;
230 94a3f4e9 2024-03-30 thomas char *l;
231 94a3f4e9 2024-03-30 thomas ssize_t linelen;
232 94a3f4e9 2024-03-30 thomas int parent = 0;
233 94a3f4e9 2024-03-30 thomas int msglen = 0, msgwrote = 0;
234 20d96a10 2024-03-30 thomas int done = 0;
235 94a3f4e9 2024-03-30 thomas enum {
236 94a3f4e9 2024-03-30 thomas P_FROM,
237 94a3f4e9 2024-03-30 thomas P_VIA,
238 94a3f4e9 2024-03-30 thomas P_DATE,
239 94a3f4e9 2024-03-30 thomas P_PARENT,
240 94a3f4e9 2024-03-30 thomas P_MSGLEN,
241 94a3f4e9 2024-03-30 thomas P_MSG,
242 94a3f4e9 2024-03-30 thomas P_DST,
243 94a3f4e9 2024-03-30 thomas P_SUM,
244 20d96a10 2024-03-30 thomas } phase = P_FROM;
245 94a3f4e9 2024-03-30 thomas
246 20d96a10 2024-03-30 thomas l = *line;
247 20d96a10 2024-03-30 thomas if (strncmp(l, "commit ", 7) != 0)
248 20d96a10 2024-03-30 thomas errx(1, "%s: unexpected line: %s", __func__, l);
249 20d96a10 2024-03-30 thomas l += 7;
250 20d96a10 2024-03-30 thomas fprintf(fp, "{\"short\":false,");
251 20d96a10 2024-03-30 thomas json_field(fp, "id", l, 1);
252 94a3f4e9 2024-03-30 thomas
253 20d96a10 2024-03-30 thomas while (!done) {
254 20d96a10 2024-03-30 thomas if ((linelen = getline(line, linesize, stdin)) == -1)
255 20d96a10 2024-03-30 thomas break;
256 94a3f4e9 2024-03-30 thomas
257 20d96a10 2024-03-30 thomas if ((*line)[linelen - 1] == '\n')
258 20d96a10 2024-03-30 thomas (*line)[--linelen] = '\0';
259 94a3f4e9 2024-03-30 thomas
260 20d96a10 2024-03-30 thomas l = *line;
261 20d96a10 2024-03-30 thomas switch (phase) {
262 94a3f4e9 2024-03-30 thomas case P_FROM:
263 94a3f4e9 2024-03-30 thomas if (strncmp(l, "from: ", 6) != 0)
264 94a3f4e9 2024-03-30 thomas errx(1, "unexpected from line");
265 94a3f4e9 2024-03-30 thomas l += 6;
266 9c485a11 2024-03-30 thomas
267 9c485a11 2024-03-30 thomas author = strdup(l);
268 9c485a11 2024-03-30 thomas if (author == NULL)
269 9c485a11 2024-03-30 thomas err(1, "strdup");
270 9c485a11 2024-03-30 thomas
271 9c485a11 2024-03-30 thomas json_author(fp, "author", l, 1);
272 9c485a11 2024-03-30 thomas
273 94a3f4e9 2024-03-30 thomas phase = P_VIA;
274 94a3f4e9 2024-03-30 thomas break;
275 94a3f4e9 2024-03-30 thomas
276 94a3f4e9 2024-03-30 thomas case P_VIA:
277 94a3f4e9 2024-03-30 thomas /* optional */
278 94a3f4e9 2024-03-30 thomas if (!strncmp(l, "via: ", 5)) {
279 94a3f4e9 2024-03-30 thomas l += 5;
280 9c485a11 2024-03-30 thomas json_author(fp, "committer", l, 1);
281 94a3f4e9 2024-03-30 thomas phase = P_DATE;
282 94a3f4e9 2024-03-30 thomas break;
283 94a3f4e9 2024-03-30 thomas }
284 9c485a11 2024-03-30 thomas
285 9c485a11 2024-03-30 thomas if (author == NULL) /* impossible */
286 9c485a11 2024-03-30 thomas err(1, "from not specified");
287 9c485a11 2024-03-30 thomas json_author(fp, "committer", author, 1);
288 9c485a11 2024-03-30 thomas free(author);
289 9c485a11 2024-03-30 thomas author = NULL;
290 9c485a11 2024-03-30 thomas
291 94a3f4e9 2024-03-30 thomas phase = P_DATE;
292 94a3f4e9 2024-03-30 thomas /* fallthrough */
293 94a3f4e9 2024-03-30 thomas
294 94a3f4e9 2024-03-30 thomas case P_DATE:
295 94a3f4e9 2024-03-30 thomas /* optional */
296 94a3f4e9 2024-03-30 thomas if (!strncmp(l, "date: ", 6)) {
297 94a3f4e9 2024-03-30 thomas l += 6;
298 94a3f4e9 2024-03-30 thomas json_field(fp, "date", l, 1);
299 94a3f4e9 2024-03-30 thomas phase = P_PARENT;
300 94a3f4e9 2024-03-30 thomas break;
301 94a3f4e9 2024-03-30 thomas }
302 94a3f4e9 2024-03-30 thomas phase = P_PARENT;
303 94a3f4e9 2024-03-30 thomas /* fallthough */
304 94a3f4e9 2024-03-30 thomas
305 94a3f4e9 2024-03-30 thomas case P_PARENT:
306 94a3f4e9 2024-03-30 thomas /* optional - more than one */
307 94a3f4e9 2024-03-30 thomas if (!strncmp(l, "parent ", 7)) {
308 94a3f4e9 2024-03-30 thomas l += 7;
309 94a3f4e9 2024-03-30 thomas l += strcspn(l, ":");
310 94a3f4e9 2024-03-30 thomas l += strspn(l, " ");
311 94a3f4e9 2024-03-30 thomas
312 94a3f4e9 2024-03-30 thomas if (parent == 0) {
313 94a3f4e9 2024-03-30 thomas parent = 1;
314 94a3f4e9 2024-03-30 thomas fprintf(fp, "\"parents\":[");
315 94a3f4e9 2024-03-30 thomas }
316 94a3f4e9 2024-03-30 thomas
317 94a3f4e9 2024-03-30 thomas fputc('"', fp);
318 94a3f4e9 2024-03-30 thomas escape(fp, l);
319 94a3f4e9 2024-03-30 thomas fputc('"', fp);
320 94a3f4e9 2024-03-30 thomas
321 94a3f4e9 2024-03-30 thomas break;
322 94a3f4e9 2024-03-30 thomas }
323 94a3f4e9 2024-03-30 thomas if (parent != 0) {
324 94a3f4e9 2024-03-30 thomas fprintf(fp, "],");
325 94a3f4e9 2024-03-30 thomas parent = 0;
326 94a3f4e9 2024-03-30 thomas }
327 94a3f4e9 2024-03-30 thomas phase = P_MSGLEN;
328 94a3f4e9 2024-03-30 thomas /* fallthrough */
329 94a3f4e9 2024-03-30 thomas
330 94a3f4e9 2024-03-30 thomas case P_MSGLEN:
331 94a3f4e9 2024-03-30 thomas if (strncmp(l, "messagelen: ", 12) != 0)
332 94a3f4e9 2024-03-30 thomas errx(1, "unexpected messagelen line");
333 94a3f4e9 2024-03-30 thomas l += 12;
334 94a3f4e9 2024-03-30 thomas msglen = strtonum(l, 1, INT_MAX, &errstr);
335 94a3f4e9 2024-03-30 thomas if (errstr)
336 94a3f4e9 2024-03-30 thomas errx(1, "message len is %s: %s", errstr, l);
337 94a3f4e9 2024-03-30 thomas
338 94a3f4e9 2024-03-30 thomas phase = P_MSG;
339 94a3f4e9 2024-03-30 thomas break;
340 94a3f4e9 2024-03-30 thomas
341 94a3f4e9 2024-03-30 thomas case P_MSG:
342 94a3f4e9 2024-03-30 thomas /*
343 94a3f4e9 2024-03-30 thomas * The commit message is indented with one extra
344 94a3f4e9 2024-03-30 thomas * space which is not accounted for in messagelen,
345 94a3f4e9 2024-03-30 thomas * but we also strip the trailing \n so that
346 94a3f4e9 2024-03-30 thomas * accounts for it.
347 94a3f4e9 2024-03-30 thomas *
348 94a3f4e9 2024-03-30 thomas * Since we read line-by-line and there is always
349 94a3f4e9 2024-03-30 thomas * a \n added at the end of the message,
350 94a3f4e9 2024-03-30 thomas * tolerate one byte less than advertised.
351 94a3f4e9 2024-03-30 thomas */
352 94a3f4e9 2024-03-30 thomas if (*l == ' ') {
353 9c485a11 2024-03-30 thomas l++; /* skip leading space */
354 9c485a11 2024-03-30 thomas linelen--;
355 94a3f4e9 2024-03-30 thomas
356 9c485a11 2024-03-30 thomas if (msgwrote == 0 && linelen != 0) {
357 9c485a11 2024-03-30 thomas json_field(fp, "short_message", l, 1);
358 9c485a11 2024-03-30 thomas fprintf(fp, "\"message\":\"");
359 9c485a11 2024-03-30 thomas escape(fp, l);
360 9c485a11 2024-03-30 thomas escape(fp, "\n");
361 9c485a11 2024-03-30 thomas msgwrote += linelen;
362 9c485a11 2024-03-30 thomas } else if (msgwrote != 0) {
363 9c485a11 2024-03-30 thomas escape(fp, l);
364 94a3f4e9 2024-03-30 thomas escape(fp, "\n");
365 9c485a11 2024-03-30 thomas }
366 94a3f4e9 2024-03-30 thomas }
367 9c485a11 2024-03-30 thomas msglen -= linelen + 1;
368 94a3f4e9 2024-03-30 thomas if (msglen <= 1) {
369 94a3f4e9 2024-03-30 thomas fprintf(fp, "\",");
370 94a3f4e9 2024-03-30 thomas msgwrote = 0;
371 94a3f4e9 2024-03-30 thomas phase = P_DST;
372 94a3f4e9 2024-03-30 thomas }
373 94a3f4e9 2024-03-30 thomas break;
374 94a3f4e9 2024-03-30 thomas
375 94a3f4e9 2024-03-30 thomas case P_DST:
376 94a3f4e9 2024-03-30 thomas /* XXX: ignore the diffstat for now */
377 20d96a10 2024-03-30 thomas if (*l == '\0') {
378 94a3f4e9 2024-03-30 thomas fprintf(fp, "\"diffstat\":{},");
379 94a3f4e9 2024-03-30 thomas phase = P_SUM;
380 94a3f4e9 2024-03-30 thomas break;
381 94a3f4e9 2024-03-30 thomas }
382 94a3f4e9 2024-03-30 thomas break;
383 94a3f4e9 2024-03-30 thomas
384 94a3f4e9 2024-03-30 thomas case P_SUM:
385 94a3f4e9 2024-03-30 thomas /* XXX: ignore the sum of changes for now */
386 94a3f4e9 2024-03-30 thomas fprintf(fp, "\"changes\":{}}");
387 20d96a10 2024-03-30 thomas done = 1;
388 94a3f4e9 2024-03-30 thomas break;
389 94a3f4e9 2024-03-30 thomas
390 94a3f4e9 2024-03-30 thomas default:
391 20d96a10 2024-03-30 thomas /* unreachable */
392 20d96a10 2024-03-30 thomas errx(1, "unexpected line: %s", *line);
393 94a3f4e9 2024-03-30 thomas }
394 94a3f4e9 2024-03-30 thomas }
395 94a3f4e9 2024-03-30 thomas if (ferror(stdin))
396 94a3f4e9 2024-03-30 thomas err(1, "getline");
397 20d96a10 2024-03-30 thomas if (!done)
398 94a3f4e9 2024-03-30 thomas errx(1, "unexpected EOF");
399 20d96a10 2024-03-30 thomas
400 20d96a10 2024-03-30 thomas return 0;
401 20d96a10 2024-03-30 thomas }
402 20d96a10 2024-03-30 thomas
403 20d96a10 2024-03-30 thomas static int
404 20d96a10 2024-03-30 thomas jsonify(FILE *fp)
405 20d96a10 2024-03-30 thomas {
406 20d96a10 2024-03-30 thomas char *line = NULL;
407 20d96a10 2024-03-30 thomas size_t linesize = 0;
408 20d96a10 2024-03-30 thomas ssize_t linelen;
409 20d96a10 2024-03-30 thomas int needcomma = 0;
410 20d96a10 2024-03-30 thomas
411 20d96a10 2024-03-30 thomas fprintf(fp, "{\"notifications\":[");
412 20d96a10 2024-03-30 thomas while ((linelen = getline(&line, &linesize, stdin)) != -1) {
413 20d96a10 2024-03-30 thomas if (line[linelen - 1] == '\n')
414 20d96a10 2024-03-30 thomas line[--linelen] = '\0';
415 94a3f4e9 2024-03-30 thomas
416 20d96a10 2024-03-30 thomas if (*line == '\0')
417 20d96a10 2024-03-30 thomas continue;
418 20d96a10 2024-03-30 thomas
419 20d96a10 2024-03-30 thomas if (needcomma)
420 20d96a10 2024-03-30 thomas fputc(',', fp);
421 20d96a10 2024-03-30 thomas needcomma = 1;
422 20d96a10 2024-03-30 thomas
423 20d96a10 2024-03-30 thomas if (strncmp(line, "commit ", 7) == 0) {
424 20d96a10 2024-03-30 thomas if (jsonify_commit(fp, &line, &linesize) == -1)
425 20d96a10 2024-03-30 thomas err(1, "jsonify_commit");
426 20d96a10 2024-03-30 thomas continue;
427 20d96a10 2024-03-30 thomas }
428 20d96a10 2024-03-30 thomas
429 20d96a10 2024-03-30 thomas if (*line >= '0' && *line <= '9') {
430 20d96a10 2024-03-30 thomas if (jsonify_commit_short(fp, line) == -1)
431 20d96a10 2024-03-30 thomas err(1, "jsonify_commit_short");
432 20d96a10 2024-03-30 thomas continue;
433 20d96a10 2024-03-30 thomas }
434 20d96a10 2024-03-30 thomas
435 20d96a10 2024-03-30 thomas errx(1, "unexpected line: %s", line);
436 20d96a10 2024-03-30 thomas }
437 20d96a10 2024-03-30 thomas if (ferror(stdin))
438 20d96a10 2024-03-30 thomas err(1, "getline");
439 94a3f4e9 2024-03-30 thomas fprintf(fp, "]}");
440 94a3f4e9 2024-03-30 thomas
441 94a3f4e9 2024-03-30 thomas return 0;
442 94a3f4e9 2024-03-30 thomas }
443 94a3f4e9 2024-03-30 thomas
444 94a3f4e9 2024-03-30 thomas static char *
445 94a3f4e9 2024-03-30 thomas basic_auth(const char *username, const char *password)
446 94a3f4e9 2024-03-30 thomas {
447 94a3f4e9 2024-03-30 thomas char *tmp;
448 94a3f4e9 2024-03-30 thomas int len;
449 94a3f4e9 2024-03-30 thomas
450 94a3f4e9 2024-03-30 thomas len = asprintf(&tmp, "%s:%s", username, password);
451 94a3f4e9 2024-03-30 thomas if (len == -1)
452 94a3f4e9 2024-03-30 thomas err(1, "asprintf");
453 94a3f4e9 2024-03-30 thomas
454 94a3f4e9 2024-03-30 thomas /* XXX base64-ify */
455 94a3f4e9 2024-03-30 thomas return tmp;
456 94a3f4e9 2024-03-30 thomas }
457 94a3f4e9 2024-03-30 thomas
458 94a3f4e9 2024-03-30 thomas static inline int
459 94a3f4e9 2024-03-30 thomas bufio2poll(struct bufio *bio)
460 94a3f4e9 2024-03-30 thomas {
461 94a3f4e9 2024-03-30 thomas int f, ret = 0;
462 94a3f4e9 2024-03-30 thomas
463 94a3f4e9 2024-03-30 thomas f = bufio_ev(bio);
464 94a3f4e9 2024-03-30 thomas if (f & BUFIO_WANT_READ)
465 94a3f4e9 2024-03-30 thomas ret |= POLLIN;
466 94a3f4e9 2024-03-30 thomas if (f & BUFIO_WANT_WRITE)
467 94a3f4e9 2024-03-30 thomas ret |= POLLOUT;
468 94a3f4e9 2024-03-30 thomas return ret;
469 94a3f4e9 2024-03-30 thomas }
470 94a3f4e9 2024-03-30 thomas
471 94a3f4e9 2024-03-30 thomas int
472 94a3f4e9 2024-03-30 thomas main(int argc, char **argv)
473 94a3f4e9 2024-03-30 thomas {
474 94a3f4e9 2024-03-30 thomas FILE *tmpfp;
475 94a3f4e9 2024-03-30 thomas struct bufio bio;
476 94a3f4e9 2024-03-30 thomas struct pollfd pfd;
477 94a3f4e9 2024-03-30 thomas struct timespec timeout;
478 94a3f4e9 2024-03-30 thomas const char *username;
479 94a3f4e9 2024-03-30 thomas const char *password;
480 94a3f4e9 2024-03-30 thomas const char *timeoutstr;
481 94a3f4e9 2024-03-30 thomas const char *errstr;
482 94a3f4e9 2024-03-30 thomas const char *host = NULL, *port = NULL, *path = NULL;
483 94a3f4e9 2024-03-30 thomas char *auth, *line, *spc;
484 94a3f4e9 2024-03-30 thomas size_t len;
485 94a3f4e9 2024-03-30 thomas ssize_t r;
486 94a3f4e9 2024-03-30 thomas off_t paylen;
487 94a3f4e9 2024-03-30 thomas int tls = 0;
488 94a3f4e9 2024-03-30 thomas int response_code = 0, done = 0;
489 94a3f4e9 2024-03-30 thomas int ch, flags, ret, nonstd = 0;
490 94a3f4e9 2024-03-30 thomas
491 94a3f4e9 2024-03-30 thomas #ifndef PROFILE
492 94a3f4e9 2024-03-30 thomas if (pledge("stdio rpath tmppath dns inet", NULL) == -1)
493 94a3f4e9 2024-03-30 thomas err(1, "pledge");
494 94a3f4e9 2024-03-30 thomas #endif
495 94a3f4e9 2024-03-30 thomas
496 94a3f4e9 2024-03-30 thomas while ((ch = getopt(argc, argv, "ch:p:")) != -1) {
497 94a3f4e9 2024-03-30 thomas switch (ch) {
498 94a3f4e9 2024-03-30 thomas case 'c':
499 94a3f4e9 2024-03-30 thomas tls = 1;
500 94a3f4e9 2024-03-30 thomas break;
501 94a3f4e9 2024-03-30 thomas case 'h':
502 94a3f4e9 2024-03-30 thomas host = optarg;
503 94a3f4e9 2024-03-30 thomas break;
504 94a3f4e9 2024-03-30 thomas case 'p':
505 94a3f4e9 2024-03-30 thomas port = optarg;
506 94a3f4e9 2024-03-30 thomas break;
507 94a3f4e9 2024-03-30 thomas default:
508 94a3f4e9 2024-03-30 thomas usage();
509 94a3f4e9 2024-03-30 thomas }
510 94a3f4e9 2024-03-30 thomas }
511 94a3f4e9 2024-03-30 thomas argc -= optind;
512 94a3f4e9 2024-03-30 thomas argv += optind;
513 94a3f4e9 2024-03-30 thomas
514 94a3f4e9 2024-03-30 thomas if (host == NULL || argc != 1)
515 94a3f4e9 2024-03-30 thomas usage();
516 94a3f4e9 2024-03-30 thomas if (tls && port == NULL)
517 94a3f4e9 2024-03-30 thomas port = "443";
518 94a3f4e9 2024-03-30 thomas path = argv[0];
519 94a3f4e9 2024-03-30 thomas
520 94a3f4e9 2024-03-30 thomas username = getenv("GOT_NOTIFY_HTTP_USER");
521 94a3f4e9 2024-03-30 thomas password = getenv("GOT_NOTIFY_HTTP_PASS");
522 94a3f4e9 2024-03-30 thomas if ((username != NULL && password == NULL) ||
523 94a3f4e9 2024-03-30 thomas (username == NULL && password != NULL))
524 94a3f4e9 2024-03-30 thomas errx(1, "username or password are not specified");
525 94a3f4e9 2024-03-30 thomas if (username && *password == '\0')
526 94a3f4e9 2024-03-30 thomas errx(1, "password can't be empty");
527 94a3f4e9 2024-03-30 thomas
528 94a3f4e9 2024-03-30 thomas /* used by the regression test suite */
529 94a3f4e9 2024-03-30 thomas timeoutstr = getenv("GOT_NOTIFY_TIMEOUT");
530 94a3f4e9 2024-03-30 thomas if (timeoutstr) {
531 94a3f4e9 2024-03-30 thomas http_timeout = strtonum(timeoutstr, 0, 600, &errstr);
532 94a3f4e9 2024-03-30 thomas if (errstr != NULL)
533 94a3f4e9 2024-03-30 thomas errx(1, "timeout in seconds is %s: %s",
534 94a3f4e9 2024-03-30 thomas errstr, timeoutstr);
535 94a3f4e9 2024-03-30 thomas }
536 94a3f4e9 2024-03-30 thomas
537 94a3f4e9 2024-03-30 thomas memset(&timeout, 0, sizeof(timeout));
538 94a3f4e9 2024-03-30 thomas timeout.tv_sec = http_timeout;
539 94a3f4e9 2024-03-30 thomas
540 94a3f4e9 2024-03-30 thomas tmpfp = got_opentemp();
541 94a3f4e9 2024-03-30 thomas if (tmpfp == NULL)
542 94a3f4e9 2024-03-30 thomas err(1, "opentemp");
543 94a3f4e9 2024-03-30 thomas
544 20d96a10 2024-03-30 thomas jsonify(tmpfp);
545 94a3f4e9 2024-03-30 thomas
546 94a3f4e9 2024-03-30 thomas paylen = ftello(tmpfp);
547 94a3f4e9 2024-03-30 thomas if (paylen == -1)
548 94a3f4e9 2024-03-30 thomas err(1, "ftello");
549 94a3f4e9 2024-03-30 thomas if (fseeko(tmpfp, 0, SEEK_SET) == -1)
550 94a3f4e9 2024-03-30 thomas err(1, "fseeko");
551 94a3f4e9 2024-03-30 thomas
552 94a3f4e9 2024-03-30 thomas #ifndef PROFILE
553 94a3f4e9 2024-03-30 thomas /* drop tmppath */
554 94a3f4e9 2024-03-30 thomas if (pledge("stdio rpath dns inet", NULL) == -1)
555 94a3f4e9 2024-03-30 thomas err(1, "pledge");
556 94a3f4e9 2024-03-30 thomas #endif
557 94a3f4e9 2024-03-30 thomas
558 94a3f4e9 2024-03-30 thomas memset(&pfd, 0, sizeof(pfd));
559 94a3f4e9 2024-03-30 thomas pfd.fd = dial(host, port);
560 94a3f4e9 2024-03-30 thomas
561 94a3f4e9 2024-03-30 thomas if ((flags = fcntl(pfd.fd, F_GETFL)) == -1)
562 94a3f4e9 2024-03-30 thomas err(1, "fcntl(F_GETFL)");
563 94a3f4e9 2024-03-30 thomas if (fcntl(pfd.fd, F_SETFL, flags | O_NONBLOCK) == -1)
564 94a3f4e9 2024-03-30 thomas err(1, "fcntl(F_SETFL)");
565 94a3f4e9 2024-03-30 thomas
566 94a3f4e9 2024-03-30 thomas if (bufio_init(&bio) == -1)
567 94a3f4e9 2024-03-30 thomas err(1, "bufio_init");
568 94a3f4e9 2024-03-30 thomas bufio_set_fd(&bio, pfd.fd);
569 94a3f4e9 2024-03-30 thomas if (tls && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1)
570 94a3f4e9 2024-03-30 thomas err(1, "bufio_starttls");
571 94a3f4e9 2024-03-30 thomas
572 94a3f4e9 2024-03-30 thomas #ifndef PROFILE
573 94a3f4e9 2024-03-30 thomas /* drop rpath dns inet */
574 94a3f4e9 2024-03-30 thomas if (pledge("stdio", NULL) == -1)
575 94a3f4e9 2024-03-30 thomas err(1, "pledge");
576 94a3f4e9 2024-03-30 thomas #endif
577 94a3f4e9 2024-03-30 thomas
578 94a3f4e9 2024-03-30 thomas if ((!tls && strcmp(port, "80") != 0) ||
579 94a3f4e9 2024-03-30 thomas (tls && strcmp(port, "443")) != 0)
580 94a3f4e9 2024-03-30 thomas nonstd = 1;
581 94a3f4e9 2024-03-30 thomas
582 94a3f4e9 2024-03-30 thomas ret = bufio_compose_fmt(&bio,
583 94a3f4e9 2024-03-30 thomas "POST %s HTTP/1.1\r\n"
584 94a3f4e9 2024-03-30 thomas "Host: %s%s%s\r\n"
585 94a3f4e9 2024-03-30 thomas "Content-Type: application/json\r\n"
586 94a3f4e9 2024-03-30 thomas "Content-Length: %lld\r\n"
587 94a3f4e9 2024-03-30 thomas "User-Agent: %s\r\n"
588 94a3f4e9 2024-03-30 thomas "Connection: close\r\n",
589 94a3f4e9 2024-03-30 thomas path, host,
590 94a3f4e9 2024-03-30 thomas nonstd ? ":" : "", nonstd ? port : "",
591 94a3f4e9 2024-03-30 thomas (long long)paylen, USERAGENT);
592 94a3f4e9 2024-03-30 thomas if (ret == -1)
593 94a3f4e9 2024-03-30 thomas err(1, "bufio_compose_fmt");
594 94a3f4e9 2024-03-30 thomas
595 94a3f4e9 2024-03-30 thomas if (username) {
596 94a3f4e9 2024-03-30 thomas auth = basic_auth(username, password);
597 94a3f4e9 2024-03-30 thomas ret = bufio_compose_fmt(&bio, "Authorization: basic %s\r\n",
598 94a3f4e9 2024-03-30 thomas auth);
599 94a3f4e9 2024-03-30 thomas if (ret == -1)
600 94a3f4e9 2024-03-30 thomas err(1, "bufio_compose_fmt");
601 94a3f4e9 2024-03-30 thomas free(auth);
602 94a3f4e9 2024-03-30 thomas }
603 94a3f4e9 2024-03-30 thomas
604 94a3f4e9 2024-03-30 thomas if (bufio_compose(&bio, "\r\n", 2) == -1)
605 94a3f4e9 2024-03-30 thomas err(1, "bufio_compose");
606 94a3f4e9 2024-03-30 thomas
607 94a3f4e9 2024-03-30 thomas while (!done) {
608 94a3f4e9 2024-03-30 thomas struct timespec elapsed, start, stop;
609 94a3f4e9 2024-03-30 thomas char buf[BUFSIZ];
610 94a3f4e9 2024-03-30 thomas
611 94a3f4e9 2024-03-30 thomas pfd.events = bufio2poll(&bio);
612 94a3f4e9 2024-03-30 thomas clock_gettime(CLOCK_MONOTONIC, &start);
613 94a3f4e9 2024-03-30 thomas ret = ppoll(&pfd, 1, &timeout, NULL);
614 94a3f4e9 2024-03-30 thomas if (ret == -1)
615 94a3f4e9 2024-03-30 thomas err(1, "poll");
616 94a3f4e9 2024-03-30 thomas clock_gettime(CLOCK_MONOTONIC, &stop);
617 94a3f4e9 2024-03-30 thomas timespecsub(&stop, &start, &elapsed);
618 94a3f4e9 2024-03-30 thomas timespecsub(&timeout, &elapsed, &timeout);
619 94a3f4e9 2024-03-30 thomas if (ret == 0 || timeout.tv_sec <= 0)
620 94a3f4e9 2024-03-30 thomas errx(1, "timeout");
621 94a3f4e9 2024-03-30 thomas
622 94a3f4e9 2024-03-30 thomas if (bio.wbuf.len > 0 && (pfd.revents & POLLOUT)) {
623 94a3f4e9 2024-03-30 thomas if (bufio_write(&bio) == -1 && errno != EAGAIN)
624 94a3f4e9 2024-03-30 thomas errx(1, "bufio_write: %s", bufio_io_err(&bio));
625 94a3f4e9 2024-03-30 thomas }
626 94a3f4e9 2024-03-30 thomas if (pfd.revents & POLLIN) {
627 94a3f4e9 2024-03-30 thomas r = bufio_read(&bio);
628 94a3f4e9 2024-03-30 thomas if (r == -1 && errno != EAGAIN)
629 94a3f4e9 2024-03-30 thomas errx(1, "bufio_read: %s", bufio_io_err(&bio));
630 94a3f4e9 2024-03-30 thomas if (r == 0)
631 94a3f4e9 2024-03-30 thomas errx(1, "unexpected EOF");
632 94a3f4e9 2024-03-30 thomas
633 94a3f4e9 2024-03-30 thomas for (;;) {
634 94a3f4e9 2024-03-30 thomas line = buf_getdelim(&bio.rbuf, "\r\n", &len);
635 94a3f4e9 2024-03-30 thomas if (line == NULL)
636 94a3f4e9 2024-03-30 thomas break;
637 94a3f4e9 2024-03-30 thomas if (response_code && *line == '\0') {
638 94a3f4e9 2024-03-30 thomas /*
639 94a3f4e9 2024-03-30 thomas * end of headers, don't bother
640 94a3f4e9 2024-03-30 thomas * reading the body, if there is.
641 94a3f4e9 2024-03-30 thomas */
642 94a3f4e9 2024-03-30 thomas done = 1;
643 94a3f4e9 2024-03-30 thomas break;
644 94a3f4e9 2024-03-30 thomas }
645 94a3f4e9 2024-03-30 thomas if (response_code) {
646 94a3f4e9 2024-03-30 thomas buf_drain(&bio.rbuf, len);
647 94a3f4e9 2024-03-30 thomas continue;
648 94a3f4e9 2024-03-30 thomas }
649 94a3f4e9 2024-03-30 thomas spc = strchr(line, ' ');
650 94a3f4e9 2024-03-30 thomas if (spc == NULL)
651 94a3f4e9 2024-03-30 thomas errx(1, "bad reply");
652 94a3f4e9 2024-03-30 thomas *spc++ = '\0';
653 94a3f4e9 2024-03-30 thomas if (strcasecmp(line, "HTTP/1.1") != 0)
654 94a3f4e9 2024-03-30 thomas errx(1, "unexpected protocol: %s",
655 94a3f4e9 2024-03-30 thomas line);
656 94a3f4e9 2024-03-30 thomas line = spc;
657 94a3f4e9 2024-03-30 thomas
658 94a3f4e9 2024-03-30 thomas spc = strchr(line, ' ');
659 94a3f4e9 2024-03-30 thomas if (spc == NULL)
660 94a3f4e9 2024-03-30 thomas errx(1, "bad reply");
661 94a3f4e9 2024-03-30 thomas *spc++ = '\0';
662 94a3f4e9 2024-03-30 thomas
663 94a3f4e9 2024-03-30 thomas response_code = strtonum(line, 100, 599,
664 94a3f4e9 2024-03-30 thomas &errstr);
665 94a3f4e9 2024-03-30 thomas if (errstr != NULL)
666 94a3f4e9 2024-03-30 thomas errx(1, "response code is %s: %s",
667 94a3f4e9 2024-03-30 thomas errstr, line);
668 94a3f4e9 2024-03-30 thomas
669 94a3f4e9 2024-03-30 thomas buf_drain(&bio.rbuf, len);
670 94a3f4e9 2024-03-30 thomas }
671 94a3f4e9 2024-03-30 thomas if (done)
672 94a3f4e9 2024-03-30 thomas break;
673 94a3f4e9 2024-03-30 thomas }
674 94a3f4e9 2024-03-30 thomas
675 94a3f4e9 2024-03-30 thomas if (!feof(tmpfp) && bio.wbuf.len < sizeof(buf)) {
676 94a3f4e9 2024-03-30 thomas len = fread(buf, 1, sizeof(buf), tmpfp);
677 94a3f4e9 2024-03-30 thomas if (len == 0) {
678 94a3f4e9 2024-03-30 thomas if (ferror(tmpfp))
679 94a3f4e9 2024-03-30 thomas err(1, "fread");
680 94a3f4e9 2024-03-30 thomas continue;
681 94a3f4e9 2024-03-30 thomas }
682 94a3f4e9 2024-03-30 thomas
683 94a3f4e9 2024-03-30 thomas if (bufio_compose(&bio, buf, len) == -1)
684 94a3f4e9 2024-03-30 thomas err(1, "buf_compose");
685 94a3f4e9 2024-03-30 thomas }
686 94a3f4e9 2024-03-30 thomas }
687 94a3f4e9 2024-03-30 thomas
688 37038afa 2024-03-30 thomas if (response_code >= 200 && response_code < 300)
689 94a3f4e9 2024-03-30 thomas return 0;
690 94a3f4e9 2024-03-30 thomas errx(1, "request failed with code %d", response_code);
691 94a3f4e9 2024-03-30 thomas }