2 94a3f4e9 2024-03-30 thomas * Copyright (c) 2024 Omar Polo <op@openbsd.org>
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.
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.
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>
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>
32 94a3f4e9 2024-03-30 thomas #include "got_opentemp.h"
33 94a3f4e9 2024-03-30 thomas #include "got_version.h"
35 94a3f4e9 2024-03-30 thomas #include "bufio.h"
36 fcfdd0a1 2024-03-30 thomas #include "utf8d.h"
38 94a3f4e9 2024-03-30 thomas #define USERAGENT "got-notify-http/" GOT_VERSION_STR
40 94a3f4e9 2024-03-30 thomas static int http_timeout = 300; /* 5 minutes in seconds */
42 94a3f4e9 2024-03-30 thomas __dead static void
43 94a3f4e9 2024-03-30 thomas usage(void)
45 94a3f4e9 2024-03-30 thomas fprintf(stderr, "usage: %s [-c] -h host -p port path\n",
46 94a3f4e9 2024-03-30 thomas getprogname());
50 94a3f4e9 2024-03-30 thomas static int
51 94a3f4e9 2024-03-30 thomas dial(const char *host, const char *port)
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;
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));
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";
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;
78 94a3f4e9 2024-03-30 thomas errno = save_errno;
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);
92 94a3f4e9 2024-03-30 thomas static void
93 94a3f4e9 2024-03-30 thomas escape(FILE *fp, const uint8_t *s)
95 fcfdd0a1 2024-03-30 thomas uint32_t codepoint, state;
96 fcfdd0a1 2024-03-30 thomas const uint8_t *start = s;
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);
107 fcfdd0a1 2024-03-30 thomas case '\b':
108 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\b");
110 fcfdd0a1 2024-03-30 thomas case '\f':
111 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\f");
113 fcfdd0a1 2024-03-30 thomas case '\n':
114 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\n");
116 fcfdd0a1 2024-03-30 thomas case '\r':
117 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\r");
119 fcfdd0a1 2024-03-30 thomas case '\t':
120 fcfdd0a1 2024-03-30 thomas fprintf(fp, "\\t");
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);
128 fcfdd0a1 2024-03-30 thomas fwrite(start, 1, s - start + 1, fp);
131 fcfdd0a1 2024-03-30 thomas start = s + 1;
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;
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)
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 ? "," : "");
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)
155 9c485a11 2024-03-30 thomas char *gt, *lt, *at, *email, *endname;
157 9c485a11 2024-03-30 thomas fprintf(fp, "\"%s\":{", type);
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>" */
163 9c485a11 2024-03-30 thomas json_field(fp, "full", address, 1);
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--;
169 9c485a11 2024-03-30 thomas *endname = '\0';
170 9c485a11 2024-03-30 thomas json_field(fp, "name", address, 1);
172 9c485a11 2024-03-30 thomas email = gt + 1;
173 9c485a11 2024-03-30 thomas lt = strchr(email, '>');
175 9c485a11 2024-03-30 thomas *lt = '\0';
177 9c485a11 2024-03-30 thomas json_field(fp, "mail", email, 1);
179 9c485a11 2024-03-30 thomas at = strchr(email, '@');
181 9c485a11 2024-03-30 thomas *at = '\0';
183 9c485a11 2024-03-30 thomas json_field(fp, "user", email, 0);
185 9c485a11 2024-03-30 thomas /* short format only shows the username */
186 9c485a11 2024-03-30 thomas json_field(fp, "user", address, 0);
189 9c485a11 2024-03-30 thomas fprintf(fp, "}%s", comma ? "," : "");
192 94a3f4e9 2024-03-30 thomas static int
193 20d96a10 2024-03-30 thomas jsonify_commit_short(FILE *fp, char *line)
195 94a3f4e9 2024-03-30 thomas char *t, *date, *id, *author, *message;
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';
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';
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';
213 20d96a10 2024-03-30 thomas message = t;
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, "}");
222 94a3f4e9 2024-03-30 thomas return 0;
225 94a3f4e9 2024-03-30 thomas static int
226 20d96a10 2024-03-30 thomas jsonify_commit(FILE *fp, char **line, ssize_t *linesize)
228 94a3f4e9 2024-03-30 thomas const char *errstr;
229 9c485a11 2024-03-30 thomas char *author = NULL;
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;
239 94a3f4e9 2024-03-30 thomas P_PARENT,
240 94a3f4e9 2024-03-30 thomas P_MSGLEN,
244 20d96a10 2024-03-30 thomas } phase = P_FROM;
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);
250 20d96a10 2024-03-30 thomas fprintf(fp, "{\"short\":false,");
251 20d96a10 2024-03-30 thomas json_field(fp, "id", l, 1);
253 20d96a10 2024-03-30 thomas while (!done) {
254 20d96a10 2024-03-30 thomas if ((linelen = getline(line, linesize, stdin)) == -1)
257 20d96a10 2024-03-30 thomas if ((*line)[linelen - 1] == '\n')
258 20d96a10 2024-03-30 thomas (*line)[--linelen] = '\0';
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");
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");
271 9c485a11 2024-03-30 thomas json_author(fp, "author", l, 1);
273 94a3f4e9 2024-03-30 thomas phase = P_VIA;
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)) {
280 9c485a11 2024-03-30 thomas json_author(fp, "committer", l, 1);
281 94a3f4e9 2024-03-30 thomas phase = P_DATE;
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;
291 94a3f4e9 2024-03-30 thomas phase = P_DATE;
292 94a3f4e9 2024-03-30 thomas /* fallthrough */
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)) {
298 94a3f4e9 2024-03-30 thomas json_field(fp, "date", l, 1);
299 94a3f4e9 2024-03-30 thomas phase = P_PARENT;
302 94a3f4e9 2024-03-30 thomas phase = P_PARENT;
303 94a3f4e9 2024-03-30 thomas /* fallthough */
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)) {
309 94a3f4e9 2024-03-30 thomas l += strcspn(l, ":");
310 94a3f4e9 2024-03-30 thomas l += strspn(l, " ");
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\":[");
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);
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;
327 94a3f4e9 2024-03-30 thomas phase = P_MSGLEN;
328 94a3f4e9 2024-03-30 thomas /* fallthrough */
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");
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);
338 94a3f4e9 2024-03-30 thomas phase = P_MSG;
341 94a3f4e9 2024-03-30 thomas case P_MSG:
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.
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.
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--;
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");
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;
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;
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;
391 20d96a10 2024-03-30 thomas /* unreachable */
392 20d96a10 2024-03-30 thomas errx(1, "unexpected line: %s", *line);
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");
400 20d96a10 2024-03-30 thomas return 0;
403 20d96a10 2024-03-30 thomas static int
404 20d96a10 2024-03-30 thomas jsonify(FILE *fp)
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;
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';
416 20d96a10 2024-03-30 thomas if (*line == '\0')
417 20d96a10 2024-03-30 thomas continue;
419 20d96a10 2024-03-30 thomas if (needcomma)
420 20d96a10 2024-03-30 thomas fputc(',', fp);
421 20d96a10 2024-03-30 thomas needcomma = 1;
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;
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;
435 20d96a10 2024-03-30 thomas errx(1, "unexpected line: %s", line);
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, "]}");
441 94a3f4e9 2024-03-30 thomas return 0;
444 94a3f4e9 2024-03-30 thomas static char *
445 94a3f4e9 2024-03-30 thomas basic_auth(const char *username, const char *password)
447 94a3f4e9 2024-03-30 thomas char *tmp;
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");
454 94a3f4e9 2024-03-30 thomas /* XXX base64-ify */
455 94a3f4e9 2024-03-30 thomas return tmp;
458 94a3f4e9 2024-03-30 thomas static inline int
459 94a3f4e9 2024-03-30 thomas bufio2poll(struct bufio *bio)
461 94a3f4e9 2024-03-30 thomas int f, ret = 0;
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;
472 94a3f4e9 2024-03-30 thomas main(int argc, char **argv)
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;
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");
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':
501 94a3f4e9 2024-03-30 thomas case 'h':
502 94a3f4e9 2024-03-30 thomas host = optarg;
504 94a3f4e9 2024-03-30 thomas case 'p':
505 94a3f4e9 2024-03-30 thomas port = optarg;
511 94a3f4e9 2024-03-30 thomas argc -= optind;
512 94a3f4e9 2024-03-30 thomas argv += optind;
514 94a3f4e9 2024-03-30 thomas if (host == NULL || argc != 1)
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];
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");
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);
537 94a3f4e9 2024-03-30 thomas memset(&timeout, 0, sizeof(timeout));
538 94a3f4e9 2024-03-30 thomas timeout.tv_sec = http_timeout;
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");
544 20d96a10 2024-03-30 thomas jsonify(tmpfp);
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");
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");
558 94a3f4e9 2024-03-30 thomas memset(&pfd, 0, sizeof(pfd));
559 94a3f4e9 2024-03-30 thomas pfd.fd = dial(host, port);
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)");
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");
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");
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;
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");
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",
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);
604 94a3f4e9 2024-03-30 thomas if (bufio_compose(&bio, "\r\n", 2) == -1)
605 94a3f4e9 2024-03-30 thomas err(1, "bufio_compose");
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];
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");
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));
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");
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)
637 94a3f4e9 2024-03-30 thomas if (response_code && *line == '\0') {
639 94a3f4e9 2024-03-30 thomas * end of headers, don't bother
640 94a3f4e9 2024-03-30 thomas * reading the body, if there is.
642 94a3f4e9 2024-03-30 thomas done = 1;
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;
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",
656 94a3f4e9 2024-03-30 thomas line = spc;
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';
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);
669 94a3f4e9 2024-03-30 thomas buf_drain(&bio.rbuf, len);
671 94a3f4e9 2024-03-30 thomas if (done)
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;
683 94a3f4e9 2024-03-30 thomas if (bufio_compose(&bio, buf, len) == -1)
684 94a3f4e9 2024-03-30 thomas err(1, "buf_compose");
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);