2 * Copyright (c) 2024 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/socket.h>
32 #include "got_opentemp.h"
33 #include "got_version.h"
38 #define USERAGENT "got-notify-http/" GOT_VERSION_STR
40 static int http_timeout = 300; /* 5 minutes in seconds */
45 fprintf(stderr, "usage: %s [-c] -h host -p port path\n",
51 dial(const char *host, const char *port)
53 struct addrinfo hints, *res, *res0;
54 const char *cause = NULL;
55 int s, error, save_errno;
57 memset(&hints, 0, sizeof(hints));
58 hints.ai_family = AF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
60 error = getaddrinfo(host, port, &hints, &res0);
62 errx(1, "failed to resolve %s:%s: %s", host, port,
66 for (res = res0; res; res = res->ai_next) {
67 s = socket(res->ai_family, res->ai_socktype,
74 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
93 escape(FILE *fp, const uint8_t *s)
95 uint32_t codepoint, state;
96 const uint8_t *start = s;
100 switch (decode(&state, &codepoint, *s)) {
105 fprintf(fp, "\\%c", *s);
123 /* other control characters */
124 if (codepoint < ' ' || codepoint == 0x7F) {
125 fprintf(fp, "\\u%04x", codepoint);
128 fwrite(start, 1, s - start + 1, fp);
135 /* bad UTF-8 sequence; try to recover */
136 fputs("\\uFFFD", fp);
145 json_field(FILE *fp, const char *key, const char *val, int comma)
147 fprintf(fp, "\"%s\":\"", key);
149 fprintf(fp, "\"%s", comma ? "," : "");
153 json_author(FILE *fp, const char *type, char *address, int comma)
155 char *gt, *lt, *at, *email, *endname;
157 fprintf(fp, "\"%s\":{", type);
159 gt = strchr(address, '<');
161 /* long format, e.g. "Omar Polo <op@openbsd.org>" */
163 json_field(fp, "full", address, 1);
166 while (endname > address && endname[-1] == ' ')
170 json_field(fp, "name", address, 1);
173 lt = strchr(email, '>');
177 json_field(fp, "mail", email, 1);
179 at = strchr(email, '@');
183 json_field(fp, "user", email, 0);
185 /* short format only shows the username */
186 json_field(fp, "user", address, 0);
189 fprintf(fp, "}%s", comma ? "," : "");
193 jsonify_commit_short(FILE *fp, char *line)
195 char *t, *date, *id, *author, *message;
199 if ((t = strchr(t, ' ')) == NULL)
200 errx(1, "malformed line");
204 if ((t = strchr(t, ' ')) == NULL)
205 errx(1, "malformed line");
209 if ((t = strchr(t, ' ')) == NULL)
210 errx(1, "malformed line");
215 fprintf(fp, "{\"type\":\"commit\",\"short\":true,");
216 json_field(fp, "id", id, 1);
217 json_author(fp, "committer", author, 1);
218 json_field(fp, "date", date, 1);
219 json_field(fp, "short_message", message, 0);
226 jsonify_commit(FILE *fp, char **line, ssize_t *linesize)
233 int msglen = 0, msgwrote = 0;
247 if (strncmp(l, "commit ", 7) != 0)
248 errx(1, "%s: unexpected line: %s", __func__, l);
250 fprintf(fp, "{\"type\":\"commit\",\"short\":false,");
251 json_field(fp, "id", l, 1);
254 if ((linelen = getline(line, linesize, stdin)) == -1)
257 if ((*line)[linelen - 1] == '\n')
258 (*line)[--linelen] = '\0';
263 if (strncmp(l, "from: ", 6) != 0)
264 errx(1, "unexpected from line");
271 json_author(fp, "author", l, 1);
278 if (!strncmp(l, "via: ", 5)) {
280 json_author(fp, "committer", l, 1);
285 if (author == NULL) /* impossible */
286 err(1, "from not specified");
287 json_author(fp, "committer", author, 1);
296 if (!strncmp(l, "date: ", 6)) {
298 json_field(fp, "date", l, 1);
306 /* optional - more than one */
307 if (!strncmp(l, "parent ", 7)) {
309 l += strcspn(l, ":");
314 fprintf(fp, "\"parents\":[");
331 if (strncmp(l, "messagelen: ", 12) != 0)
332 errx(1, "unexpected messagelen line");
334 msglen = strtonum(l, 1, INT_MAX, &errstr);
336 errx(1, "message len is %s: %s", errstr, l);
343 * The commit message is indented with one extra
344 * space which is not accounted for in messagelen,
345 * but we also strip the trailing \n so that
348 * Since we read line-by-line and there is always
349 * a \n added at the end of the message,
350 * tolerate one byte less than advertised.
353 l++; /* skip leading space */
356 if (msgwrote == 0 && linelen != 0) {
357 json_field(fp, "short_message", l, 1);
358 fprintf(fp, "\"message\":\"");
362 } else if (msgwrote != 0) {
367 msglen -= linelen + 1;
376 /* XXX: ignore the diffstat for now */
378 fprintf(fp, "\"diffstat\":{},");
385 /* XXX: ignore the sum of changes for now */
386 fprintf(fp, "\"changes\":{}}");
392 errx(1, "unexpected line: %s", *line);
398 errx(1, "unexpected EOF");
411 fprintf(fp, "{\"notifications\":[");
412 while ((linelen = getline(&line, &linesize, stdin)) != -1) {
413 if (line[linelen - 1] == '\n')
414 line[--linelen] = '\0';
423 if (strncmp(line, "commit ", 7) == 0) {
424 if (jsonify_commit(fp, &line, &linesize) == -1)
425 err(1, "jsonify_commit");
429 if (*line >= '0' && *line <= '9') {
430 if (jsonify_commit_short(fp, line) == -1)
431 err(1, "jsonify_commit_short");
435 errx(1, "unexpected line: %s", line);
445 basic_auth(const char *username, const char *password)
450 len = asprintf(&tmp, "%s:%s", username, password);
459 bufio2poll(struct bufio *bio)
464 if (f & BUFIO_WANT_READ)
466 if (f & BUFIO_WANT_WRITE)
472 main(int argc, char **argv)
477 struct timespec timeout;
478 const char *username;
479 const char *password;
480 const char *timeoutstr;
482 const char *host = NULL, *port = NULL, *path = NULL;
483 char *auth, *line, *spc;
488 int response_code = 0, done = 0;
489 int ch, flags, ret, nonstd = 0;
492 if (pledge("stdio rpath tmppath dns inet", NULL) == -1)
496 while ((ch = getopt(argc, argv, "ch:p:")) != -1) {
514 if (host == NULL || argc != 1)
516 if (tls && port == NULL)
520 username = getenv("GOT_NOTIFY_HTTP_USER");
521 password = getenv("GOT_NOTIFY_HTTP_PASS");
522 if ((username != NULL && password == NULL) ||
523 (username == NULL && password != NULL))
524 errx(1, "username or password are not specified");
525 if (username && *password == '\0')
526 errx(1, "password can't be empty");
528 /* used by the regression test suite */
529 timeoutstr = getenv("GOT_NOTIFY_TIMEOUT");
531 http_timeout = strtonum(timeoutstr, 0, 600, &errstr);
533 errx(1, "timeout in seconds is %s: %s",
537 memset(&timeout, 0, sizeof(timeout));
538 timeout.tv_sec = http_timeout;
540 tmpfp = got_opentemp();
546 paylen = ftello(tmpfp);
549 if (fseeko(tmpfp, 0, SEEK_SET) == -1)
554 if (pledge("stdio rpath dns inet", NULL) == -1)
558 memset(&pfd, 0, sizeof(pfd));
559 pfd.fd = dial(host, port);
561 if ((flags = fcntl(pfd.fd, F_GETFL)) == -1)
562 err(1, "fcntl(F_GETFL)");
563 if (fcntl(pfd.fd, F_SETFL, flags | O_NONBLOCK) == -1)
564 err(1, "fcntl(F_SETFL)");
566 if (bufio_init(&bio) == -1)
567 err(1, "bufio_init");
568 bufio_set_fd(&bio, pfd.fd);
569 if (tls && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1)
570 err(1, "bufio_starttls");
573 /* drop rpath dns inet */
574 if (pledge("stdio", NULL) == -1)
578 if ((!tls && strcmp(port, "80") != 0) ||
579 (tls && strcmp(port, "443")) != 0)
582 ret = bufio_compose_fmt(&bio,
583 "POST %s HTTP/1.1\r\n"
585 "Content-Type: application/json\r\n"
586 "Content-Length: %lld\r\n"
588 "Connection: close\r\n",
590 nonstd ? ":" : "", nonstd ? port : "",
591 (long long)paylen, USERAGENT);
593 err(1, "bufio_compose_fmt");
596 auth = basic_auth(username, password);
597 ret = bufio_compose_fmt(&bio, "Authorization: basic %s\r\n",
600 err(1, "bufio_compose_fmt");
604 if (bufio_compose(&bio, "\r\n", 2) == -1)
605 err(1, "bufio_compose");
608 struct timespec elapsed, start, stop;
611 pfd.events = bufio2poll(&bio);
612 clock_gettime(CLOCK_MONOTONIC, &start);
613 ret = ppoll(&pfd, 1, &timeout, NULL);
616 clock_gettime(CLOCK_MONOTONIC, &stop);
617 timespecsub(&stop, &start, &elapsed);
618 timespecsub(&timeout, &elapsed, &timeout);
619 if (ret == 0 || timeout.tv_sec <= 0)
622 if (bio.wbuf.len > 0 && (pfd.revents & POLLOUT)) {
623 if (bufio_write(&bio) == -1 && errno != EAGAIN)
624 errx(1, "bufio_write: %s", bufio_io_err(&bio));
626 if (pfd.revents & POLLIN) {
627 r = bufio_read(&bio);
628 if (r == -1 && errno != EAGAIN)
629 errx(1, "bufio_read: %s", bufio_io_err(&bio));
631 errx(1, "unexpected EOF");
634 line = buf_getdelim(&bio.rbuf, "\r\n", &len);
637 if (response_code && *line == '\0') {
639 * end of headers, don't bother
640 * reading the body, if there is.
646 buf_drain(&bio.rbuf, len);
649 spc = strchr(line, ' ');
651 errx(1, "bad reply");
653 if (strcasecmp(line, "HTTP/1.1") != 0)
654 errx(1, "unexpected protocol: %s",
658 spc = strchr(line, ' ');
660 errx(1, "bad reply");
663 response_code = strtonum(line, 100, 599,
666 errx(1, "response code is %s: %s",
669 buf_drain(&bio.rbuf, len);
675 if (!feof(tmpfp) && bio.wbuf.len < sizeof(buf)) {
676 len = fread(buf, 1, sizeof(buf), tmpfp);
683 if (bufio_compose(&bio, buf, len) == -1)
684 err(1, "buf_compose");
688 if (response_code >= 200 && response_code < 300)
690 errx(1, "request failed with code %d", response_code);