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_branch_rm(FILE *fp, char *line)
197 line = strchr(line, ' ');
199 errx(1, "invalid branch rm line");
200 line += strspn(line, " ");
204 line = strchr(line, ':');
206 errx(1, "invalid branch rm line");
208 id = line + strspn(line, " ");
211 json_field(fp, "type", "branch-deleted", 1);
212 json_field(fp, "ref", ref, 1);
213 json_field(fp, "id", id, 0);
220 jsonify_commit_short(FILE *fp, char *line)
222 char *t, *date, *id, *author, *message;
226 if ((t = strchr(t, ' ')) == NULL)
227 errx(1, "malformed line");
231 if ((t = strchr(t, ' ')) == NULL)
232 errx(1, "malformed line");
236 if ((t = strchr(t, ' ')) == NULL)
237 errx(1, "malformed line");
242 fprintf(fp, "{\"type\":\"commit\",\"short\":true,");
243 json_field(fp, "id", id, 1);
244 json_author(fp, "committer", author, 1);
245 json_field(fp, "date", date, 1);
246 json_field(fp, "short_message", message, 0);
253 jsonify_commit(FILE *fp, char **line, ssize_t *linesize)
260 int msglen = 0, msgwrote = 0;
274 if (strncmp(l, "commit ", 7) != 0)
275 errx(1, "%s: unexpected line: %s", __func__, l);
277 fprintf(fp, "{\"type\":\"commit\",\"short\":false,");
278 json_field(fp, "id", l, 1);
281 if ((linelen = getline(line, linesize, stdin)) == -1)
284 if ((*line)[linelen - 1] == '\n')
285 (*line)[--linelen] = '\0';
290 if (strncmp(l, "from: ", 6) != 0)
291 errx(1, "unexpected from line");
298 json_author(fp, "author", l, 1);
305 if (!strncmp(l, "via: ", 5)) {
307 json_author(fp, "committer", l, 1);
312 if (author == NULL) /* impossible */
313 err(1, "from not specified");
314 json_author(fp, "committer", author, 1);
323 if (!strncmp(l, "date: ", 6)) {
325 json_field(fp, "date", l, 1);
333 /* optional - more than one */
334 if (!strncmp(l, "parent ", 7)) {
336 l += strcspn(l, ":");
341 fprintf(fp, "\"parents\":[");
358 if (strncmp(l, "messagelen: ", 12) != 0)
359 errx(1, "unexpected messagelen line");
361 msglen = strtonum(l, 1, INT_MAX, &errstr);
363 errx(1, "message len is %s: %s", errstr, l);
370 * The commit message is indented with one extra
371 * space which is not accounted for in messagelen,
372 * but we also strip the trailing \n so that
375 * Since we read line-by-line and there is always
376 * a \n added at the end of the message,
377 * tolerate one byte less than advertised.
380 l++; /* skip leading space */
383 if (msgwrote == 0 && linelen != 0) {
384 json_field(fp, "short_message", l, 1);
385 fprintf(fp, "\"message\":\"");
389 } else if (msgwrote != 0) {
394 msglen -= linelen + 1;
403 /* XXX: ignore the diffstat for now */
405 fprintf(fp, "\"diffstat\":{},");
412 /* XXX: ignore the sum of changes for now */
413 fprintf(fp, "\"changes\":{}}");
419 errx(1, "unexpected line: %s", *line);
425 errx(1, "unexpected EOF");
431 jsonify_tag(FILE *fp, char **line, ssize_t *linesize)
436 int msglen = 0, msgwrote = 0;
447 if (strncmp(l, "tag ", 4) != 0)
448 errx(1, "%s: unexpected line: %s", __func__, l);
452 json_field(fp, "type", "tag", 1);
453 json_field(fp, "tag", l, 1);
456 if ((linelen = getline(line, linesize, stdin)) == -1)
459 if ((*line)[linelen - 1] == '\n')
460 (*line)[--linelen] = '\0';
465 if (strncmp(l, "from: ", 6) != 0)
466 errx(1, "unexpected from line");
469 json_author(fp, "tagger", l, 1);
476 if (!strncmp(l, "date: ", 6)) {
478 json_field(fp, "date", l, 1);
487 if (!strncmp(l, "object: ", 8)) {
494 errx(1, "malformed tag object line");
497 fputs("\"object\":{", fp);
498 json_field(fp, "type", type, 1);
499 json_field(fp, "id", id, 0);
509 if (strncmp(l, "messagelen: ", 12) != 0)
510 errx(1, "unexpected messagelen line");
512 msglen = strtonum(l, 1, INT_MAX, &errstr);
514 errx(1, "message len is %s: %s", errstr, l);
523 l++; /* skip leading space */
526 if (msgwrote == 0 && linelen != 0) {
527 fprintf(fp, "\"message\":\"");
531 } else if (msgwrote != 0) {
536 msglen -= linelen + 1;
547 errx(1, "unexpected line: %s", *line);
553 errx(1, "unexpected EOF");
567 fprintf(fp, "{\"notifications\":[");
568 while ((linelen = getline(&line, &linesize, stdin)) != -1) {
569 if (line[linelen - 1] == '\n')
570 line[--linelen] = '\0';
579 if (strncmp(line, "Removed refs/heads/", 19) == 0) {
580 if (jsonify_branch_rm(fp, line) == -1)
581 err(1, "jsonify_branch_rm");
585 if (strncmp(line, "commit ", 7) == 0) {
586 if (jsonify_commit(fp, &line, &linesize) == -1)
587 err(1, "jsonify_commit");
591 if (*line >= '0' && *line <= '9') {
592 if (jsonify_commit_short(fp, line) == -1)
593 err(1, "jsonify_commit_short");
597 if (strncmp(line, "tag ", 4) == 0) {
598 if (jsonify_tag(fp, &line, &linesize) == -1)
599 err(1, "jsonify_tag");
603 errx(1, "unexpected line: %s", line);
613 basic_auth(const char *username, const char *password)
618 len = asprintf(&tmp, "%s:%s", username, password);
627 bufio2poll(struct bufio *bio)
632 if (f & BUFIO_WANT_READ)
634 if (f & BUFIO_WANT_WRITE)
640 main(int argc, char **argv)
645 struct timespec timeout;
646 const char *username;
647 const char *password;
648 const char *timeoutstr;
650 const char *host = NULL, *port = NULL, *path = NULL;
651 char *auth, *line, *spc;
656 int response_code = 0, done = 0;
657 int ch, flags, ret, nonstd = 0;
660 if (pledge("stdio rpath tmppath dns inet", NULL) == -1)
664 while ((ch = getopt(argc, argv, "ch:p:")) != -1) {
682 if (host == NULL || argc != 1)
684 if (tls && port == NULL)
688 username = getenv("GOT_NOTIFY_HTTP_USER");
689 password = getenv("GOT_NOTIFY_HTTP_PASS");
690 if ((username != NULL && password == NULL) ||
691 (username == NULL && password != NULL))
692 errx(1, "username or password are not specified");
693 if (username && *password == '\0')
694 errx(1, "password can't be empty");
696 /* used by the regression test suite */
697 timeoutstr = getenv("GOT_NOTIFY_TIMEOUT");
699 http_timeout = strtonum(timeoutstr, 0, 600, &errstr);
701 errx(1, "timeout in seconds is %s: %s",
705 memset(&timeout, 0, sizeof(timeout));
706 timeout.tv_sec = http_timeout;
708 tmpfp = got_opentemp();
714 paylen = ftello(tmpfp);
717 if (fseeko(tmpfp, 0, SEEK_SET) == -1)
722 if (pledge("stdio rpath dns inet", NULL) == -1)
726 memset(&pfd, 0, sizeof(pfd));
727 pfd.fd = dial(host, port);
729 if ((flags = fcntl(pfd.fd, F_GETFL)) == -1)
730 err(1, "fcntl(F_GETFL)");
731 if (fcntl(pfd.fd, F_SETFL, flags | O_NONBLOCK) == -1)
732 err(1, "fcntl(F_SETFL)");
734 if (bufio_init(&bio) == -1)
735 err(1, "bufio_init");
736 bufio_set_fd(&bio, pfd.fd);
737 if (tls && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1)
738 err(1, "bufio_starttls");
741 /* drop rpath dns inet */
742 if (pledge("stdio", NULL) == -1)
746 if ((!tls && strcmp(port, "80") != 0) ||
747 (tls && strcmp(port, "443")) != 0)
750 ret = bufio_compose_fmt(&bio,
751 "POST %s HTTP/1.1\r\n"
753 "Content-Type: application/json\r\n"
754 "Content-Length: %lld\r\n"
756 "Connection: close\r\n",
758 nonstd ? ":" : "", nonstd ? port : "",
759 (long long)paylen, USERAGENT);
761 err(1, "bufio_compose_fmt");
764 auth = basic_auth(username, password);
765 ret = bufio_compose_fmt(&bio, "Authorization: basic %s\r\n",
768 err(1, "bufio_compose_fmt");
772 if (bufio_compose(&bio, "\r\n", 2) == -1)
773 err(1, "bufio_compose");
776 struct timespec elapsed, start, stop;
779 pfd.events = bufio2poll(&bio);
780 clock_gettime(CLOCK_MONOTONIC, &start);
781 ret = ppoll(&pfd, 1, &timeout, NULL);
784 clock_gettime(CLOCK_MONOTONIC, &stop);
785 timespecsub(&stop, &start, &elapsed);
786 timespecsub(&timeout, &elapsed, &timeout);
787 if (ret == 0 || timeout.tv_sec <= 0)
790 if (bio.wbuf.len > 0 && (pfd.revents & POLLOUT)) {
791 if (bufio_write(&bio) == -1 && errno != EAGAIN)
792 errx(1, "bufio_write: %s", bufio_io_err(&bio));
794 if (pfd.revents & POLLIN) {
795 r = bufio_read(&bio);
796 if (r == -1 && errno != EAGAIN)
797 errx(1, "bufio_read: %s", bufio_io_err(&bio));
799 errx(1, "unexpected EOF");
802 line = buf_getdelim(&bio.rbuf, "\r\n", &len);
805 if (response_code && *line == '\0') {
807 * end of headers, don't bother
808 * reading the body, if there is.
814 buf_drain(&bio.rbuf, len);
817 spc = strchr(line, ' ');
819 errx(1, "bad reply");
821 if (strcasecmp(line, "HTTP/1.1") != 0)
822 errx(1, "unexpected protocol: %s",
826 spc = strchr(line, ' ');
828 errx(1, "bad reply");
831 response_code = strtonum(line, 100, 599,
834 errx(1, "response code is %s: %s",
837 buf_drain(&bio.rbuf, len);
843 if (!feof(tmpfp) && bio.wbuf.len < sizeof(buf)) {
844 len = fread(buf, 1, sizeof(buf), tmpfp);
851 if (bufio_compose(&bio, buf, len) == -1)
852 err(1, "buf_compose");
856 if (response_code >= 200 && response_code < 300)
858 errx(1, "request failed with code %d", response_code);