Blame


1 5565365c 2024-03-27 op /*
2 5565365c 2024-03-27 op * Copyright (c) 2024 Omar Polo <op@openbsd.org>
3 5565365c 2024-03-27 op *
4 5565365c 2024-03-27 op * Permission to use, copy, modify, and distribute this software for any
5 5565365c 2024-03-27 op * purpose with or without fee is hereby granted, provided that the above
6 5565365c 2024-03-27 op * copyright notice and this permission notice appear in all copies.
7 5565365c 2024-03-27 op *
8 5565365c 2024-03-27 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5565365c 2024-03-27 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5565365c 2024-03-27 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5565365c 2024-03-27 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5565365c 2024-03-27 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5565365c 2024-03-27 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5565365c 2024-03-27 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5565365c 2024-03-27 op */
16 5565365c 2024-03-27 op
17 5565365c 2024-03-27 op #include <sys/time.h>
18 5565365c 2024-03-27 op #include <sys/types.h>
19 5565365c 2024-03-27 op #include <sys/socket.h>
20 5565365c 2024-03-27 op
21 5565365c 2024-03-27 op #include <err.h>
22 5565365c 2024-03-27 op #include <errno.h>
23 5565365c 2024-03-27 op #include <fcntl.h>
24 5565365c 2024-03-27 op #include <limits.h>
25 5565365c 2024-03-27 op #include <netdb.h>
26 5565365c 2024-03-27 op #include <poll.h>
27 cb29e255 2024-04-18 stsp #include <stdarg.h>
28 02dab75a 2024-04-18 stsp #include <stdio.h>
29 5565365c 2024-03-27 op #include <stdlib.h>
30 5565365c 2024-03-27 op #include <string.h>
31 cb29e255 2024-04-18 stsp #include <syslog.h>
32 939d3016 2024-04-23 op #include <time.h>
33 5565365c 2024-03-27 op #include <unistd.h>
34 5565365c 2024-03-27 op
35 5565365c 2024-03-27 op #include "got_opentemp.h"
36 5565365c 2024-03-27 op #include "got_version.h"
37 5565365c 2024-03-27 op
38 5565365c 2024-03-27 op #include "bufio.h"
39 cb29e255 2024-04-18 stsp #include "log.h"
40 02dab75a 2024-04-18 stsp #include "utf8d.h"
41 5565365c 2024-03-27 op
42 5565365c 2024-03-27 op #define USERAGENT "got-notify-http/" GOT_VERSION_STR
43 5565365c 2024-03-27 op
44 5565365c 2024-03-27 op static int http_timeout = 300; /* 5 minutes in seconds */
45 5565365c 2024-03-27 op
46 5565365c 2024-03-27 op __dead static void
47 5565365c 2024-03-27 op usage(void)
48 5565365c 2024-03-27 op {
49 d36998ae 2024-04-29 stsp fprintf(stderr, "usage: %s [-c] -r repo -h host -p port -u user path\n",
50 5565365c 2024-03-27 op getprogname());
51 5565365c 2024-03-27 op exit(1);
52 5565365c 2024-03-27 op }
53 5565365c 2024-03-27 op
54 5565365c 2024-03-27 op static int
55 5565365c 2024-03-27 op dial(const char *host, const char *port)
56 5565365c 2024-03-27 op {
57 5565365c 2024-03-27 op struct addrinfo hints, *res, *res0;
58 5565365c 2024-03-27 op const char *cause = NULL;
59 5565365c 2024-03-27 op int s, error, save_errno;
60 5565365c 2024-03-27 op
61 5565365c 2024-03-27 op memset(&hints, 0, sizeof(hints));
62 5565365c 2024-03-27 op hints.ai_family = AF_UNSPEC;
63 5565365c 2024-03-27 op hints.ai_socktype = SOCK_STREAM;
64 5565365c 2024-03-27 op error = getaddrinfo(host, port, &hints, &res0);
65 5565365c 2024-03-27 op if (error)
66 5565365c 2024-03-27 op errx(1, "failed to resolve %s:%s: %s", host, port,
67 5565365c 2024-03-27 op gai_strerror(error));
68 5565365c 2024-03-27 op
69 5565365c 2024-03-27 op s = -1;
70 5565365c 2024-03-27 op for (res = res0; res; res = res->ai_next) {
71 5565365c 2024-03-27 op s = socket(res->ai_family, res->ai_socktype,
72 5565365c 2024-03-27 op res->ai_protocol);
73 5565365c 2024-03-27 op if (s == -1) {
74 5565365c 2024-03-27 op cause = "socket";
75 5565365c 2024-03-27 op continue;
76 5565365c 2024-03-27 op }
77 5565365c 2024-03-27 op
78 5565365c 2024-03-27 op if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
79 5565365c 2024-03-27 op cause = "connect";
80 5565365c 2024-03-27 op save_errno = errno;
81 5565365c 2024-03-27 op close(s);
82 5565365c 2024-03-27 op errno = save_errno;
83 5565365c 2024-03-27 op s = -1;
84 5565365c 2024-03-27 op continue;
85 5565365c 2024-03-27 op }
86 5565365c 2024-03-27 op
87 5565365c 2024-03-27 op break;
88 5565365c 2024-03-27 op }
89 5565365c 2024-03-27 op
90 5565365c 2024-03-27 op freeaddrinfo(res0);
91 5565365c 2024-03-27 op if (s == -1)
92 50ccbcd9 2024-05-11 stsp fatal("%s %s:%s", cause, host, port);
93 5565365c 2024-03-27 op return s;
94 5565365c 2024-03-27 op }
95 5565365c 2024-03-27 op
96 5565365c 2024-03-27 op static void
97 5565365c 2024-03-27 op escape(FILE *fp, const uint8_t *s)
98 5565365c 2024-03-27 op {
99 ea5e974d 2024-03-28 op uint32_t codepoint, state;
100 ea5e974d 2024-03-28 op const uint8_t *start = s;
101 5565365c 2024-03-27 op
102 ea5e974d 2024-03-28 op state = 0;
103 ea5e974d 2024-03-28 op for (; *s; ++s) {
104 ea5e974d 2024-03-28 op switch (decode(&state, &codepoint, *s)) {
105 ea5e974d 2024-03-28 op case UTF8_ACCEPT:
106 ea5e974d 2024-03-28 op switch (codepoint) {
107 ea5e974d 2024-03-28 op case '"':
108 ea5e974d 2024-03-28 op case '\\':
109 ea5e974d 2024-03-28 op fprintf(fp, "\\%c", *s);
110 ea5e974d 2024-03-28 op break;
111 ea5e974d 2024-03-28 op case '\b':
112 ea5e974d 2024-03-28 op fprintf(fp, "\\b");
113 ea5e974d 2024-03-28 op break;
114 ea5e974d 2024-03-28 op case '\f':
115 ea5e974d 2024-03-28 op fprintf(fp, "\\f");
116 ea5e974d 2024-03-28 op break;
117 ea5e974d 2024-03-28 op case '\n':
118 ea5e974d 2024-03-28 op fprintf(fp, "\\n");
119 ea5e974d 2024-03-28 op break;
120 ea5e974d 2024-03-28 op case '\r':
121 ea5e974d 2024-03-28 op fprintf(fp, "\\r");
122 ea5e974d 2024-03-28 op break;
123 ea5e974d 2024-03-28 op case '\t':
124 ea5e974d 2024-03-28 op fprintf(fp, "\\t");
125 ea5e974d 2024-03-28 op break;
126 ea5e974d 2024-03-28 op default:
127 ea5e974d 2024-03-28 op /* other control characters */
128 ea5e974d 2024-03-28 op if (codepoint < ' ' || codepoint == 0x7F) {
129 ea5e974d 2024-03-28 op fprintf(fp, "\\u%04x", codepoint);
130 ea5e974d 2024-03-28 op break;
131 ea5e974d 2024-03-28 op }
132 ea5e974d 2024-03-28 op fwrite(start, 1, s - start + 1, fp);
133 ea5e974d 2024-03-28 op break;
134 ea5e974d 2024-03-28 op }
135 ea5e974d 2024-03-28 op start = s + 1;
136 5565365c 2024-03-27 op break;
137 ea5e974d 2024-03-28 op
138 ea5e974d 2024-03-28 op case UTF8_REJECT:
139 ea5e974d 2024-03-28 op /* bad UTF-8 sequence; try to recover */
140 ea5e974d 2024-03-28 op fputs("\\uFFFD", fp);
141 ea5e974d 2024-03-28 op state = UTF8_ACCEPT;
142 ea5e974d 2024-03-28 op start = s + 1;
143 5565365c 2024-03-27 op break;
144 5565365c 2024-03-27 op }
145 5565365c 2024-03-27 op }
146 5565365c 2024-03-27 op }
147 5565365c 2024-03-27 op
148 5565365c 2024-03-27 op static void
149 5565365c 2024-03-27 op json_field(FILE *fp, const char *key, const char *val, int comma)
150 5565365c 2024-03-27 op {
151 5565365c 2024-03-27 op fprintf(fp, "\"%s\":\"", key);
152 5565365c 2024-03-27 op escape(fp, val);
153 5565365c 2024-03-27 op fprintf(fp, "\"%s", comma ? "," : "");
154 5565365c 2024-03-27 op }
155 5565365c 2024-03-27 op
156 ac0a4dfc 2024-03-28 op static void
157 939d3016 2024-04-23 op json_date(FILE *fp, const char *key, const char *date, int comma)
158 939d3016 2024-04-23 op {
159 939d3016 2024-04-23 op fprintf(fp, "\"%s\":%s%s", key, date, comma ? "," : "");
160 939d3016 2024-04-23 op }
161 939d3016 2024-04-23 op
162 939d3016 2024-04-23 op static void
163 ac0a4dfc 2024-03-28 op json_author(FILE *fp, const char *type, char *address, int comma)
164 ac0a4dfc 2024-03-28 op {
165 ac0a4dfc 2024-03-28 op char *gt, *lt, *at, *email, *endname;
166 ac0a4dfc 2024-03-28 op
167 ac0a4dfc 2024-03-28 op fprintf(fp, "\"%s\":{", type);
168 ac0a4dfc 2024-03-28 op
169 ac0a4dfc 2024-03-28 op gt = strchr(address, '<');
170 ac0a4dfc 2024-03-28 op if (gt != NULL) {
171 ac0a4dfc 2024-03-28 op /* long format, e.g. "Omar Polo <op@openbsd.org>" */
172 ac0a4dfc 2024-03-28 op
173 ac0a4dfc 2024-03-28 op json_field(fp, "full", address, 1);
174 ac0a4dfc 2024-03-28 op
175 ac0a4dfc 2024-03-28 op endname = gt;
176 ac0a4dfc 2024-03-28 op while (endname > address && endname[-1] == ' ')
177 ac0a4dfc 2024-03-28 op endname--;
178 ac0a4dfc 2024-03-28 op
179 ac0a4dfc 2024-03-28 op *endname = '\0';
180 ac0a4dfc 2024-03-28 op json_field(fp, "name", address, 1);
181 ac0a4dfc 2024-03-28 op
182 ac0a4dfc 2024-03-28 op email = gt + 1;
183 ac0a4dfc 2024-03-28 op lt = strchr(email, '>');
184 ac0a4dfc 2024-03-28 op if (lt)
185 ac0a4dfc 2024-03-28 op *lt = '\0';
186 ac0a4dfc 2024-03-28 op
187 ac0a4dfc 2024-03-28 op json_field(fp, "mail", email, 1);
188 ac0a4dfc 2024-03-28 op
189 ac0a4dfc 2024-03-28 op at = strchr(email, '@');
190 ac0a4dfc 2024-03-28 op if (at)
191 ac0a4dfc 2024-03-28 op *at = '\0';
192 ac0a4dfc 2024-03-28 op
193 ac0a4dfc 2024-03-28 op json_field(fp, "user", email, 0);
194 ac0a4dfc 2024-03-28 op } else {
195 ac0a4dfc 2024-03-28 op /* short format only shows the username */
196 ac0a4dfc 2024-03-28 op json_field(fp, "user", address, 0);
197 ac0a4dfc 2024-03-28 op }
198 ac0a4dfc 2024-03-28 op
199 ac0a4dfc 2024-03-28 op fprintf(fp, "}%s", comma ? "," : "");
200 ac0a4dfc 2024-03-28 op }
201 ac0a4dfc 2024-03-28 op
202 5565365c 2024-03-27 op static int
203 d36998ae 2024-04-29 stsp jsonify_branch_rm(FILE *fp, char *line, const char *repo, const char *user)
204 d6057084 2024-03-28 op {
205 d6057084 2024-03-28 op char *ref, *id;
206 d6057084 2024-03-28 op
207 d6057084 2024-03-28 op line = strchr(line, ' ');
208 d6057084 2024-03-28 op if (line == NULL)
209 d6057084 2024-03-28 op errx(1, "invalid branch rm line");
210 d6057084 2024-03-28 op line += strspn(line, " ");
211 d6057084 2024-03-28 op
212 d6057084 2024-03-28 op ref = line;
213 d6057084 2024-03-28 op
214 d6057084 2024-03-28 op line = strchr(line, ':');
215 d6057084 2024-03-28 op if (line == NULL)
216 d6057084 2024-03-28 op errx(1, "invalid branch rm line");
217 d6057084 2024-03-28 op *line++ = '\0';
218 d6057084 2024-03-28 op id = line + strspn(line, " ");
219 d6057084 2024-03-28 op
220 d6057084 2024-03-28 op fputc('{', fp);
221 d6057084 2024-03-28 op json_field(fp, "type", "branch-deleted", 1);
222 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
223 841969e1 2024-04-30 op json_field(fp, "authenticated_user", user, 1);
224 d6057084 2024-03-28 op json_field(fp, "ref", ref, 1);
225 d6057084 2024-03-28 op json_field(fp, "id", id, 0);
226 d6057084 2024-03-28 op fputc('}', fp);
227 d6057084 2024-03-28 op
228 d6057084 2024-03-28 op return 0;
229 d6057084 2024-03-28 op }
230 d6057084 2024-03-28 op
231 d6057084 2024-03-28 op static int
232 d36998ae 2024-04-29 stsp jsonify_commit_short(FILE *fp, char *line, const char *repo, const char *user)
233 5565365c 2024-03-27 op {
234 5565365c 2024-03-27 op char *t, *date, *id, *author, *message;
235 5565365c 2024-03-27 op
236 ec405b99 2024-03-28 op t = line;
237 ec405b99 2024-03-28 op date = t;
238 ec405b99 2024-03-28 op if ((t = strchr(t, ' ')) == NULL)
239 ec405b99 2024-03-28 op errx(1, "malformed line");
240 ec405b99 2024-03-28 op *t++ = '\0';
241 5565365c 2024-03-27 op
242 ec405b99 2024-03-28 op id = t;
243 ec405b99 2024-03-28 op if ((t = strchr(t, ' ')) == NULL)
244 ec405b99 2024-03-28 op errx(1, "malformed line");
245 ec405b99 2024-03-28 op *t++ = '\0';
246 5565365c 2024-03-27 op
247 ec405b99 2024-03-28 op author = t;
248 ec405b99 2024-03-28 op if ((t = strchr(t, ' ')) == NULL)
249 ec405b99 2024-03-28 op errx(1, "malformed line");
250 ec405b99 2024-03-28 op *t++ = '\0';
251 5565365c 2024-03-27 op
252 ec405b99 2024-03-28 op message = t;
253 5565365c 2024-03-27 op
254 93623901 2024-03-28 op fprintf(fp, "{\"type\":\"commit\",\"short\":true,");
255 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
256 841969e1 2024-04-30 op json_field(fp, "authenticated_user", user, 1);
257 ec405b99 2024-03-28 op json_field(fp, "id", id, 1);
258 ec405b99 2024-03-28 op json_author(fp, "committer", author, 1);
259 939d3016 2024-04-23 op json_date(fp, "date", date, 1);
260 ec405b99 2024-03-28 op json_field(fp, "short_message", message, 0);
261 ec405b99 2024-03-28 op fprintf(fp, "}");
262 5565365c 2024-03-27 op
263 5565365c 2024-03-27 op return 0;
264 5565365c 2024-03-27 op }
265 5565365c 2024-03-27 op
266 5565365c 2024-03-27 op static int
267 d36998ae 2024-04-29 stsp jsonify_commit(FILE *fp, const char *repo, const char *user,
268 d36998ae 2024-04-29 stsp char **line, ssize_t *linesize)
269 5565365c 2024-03-27 op {
270 5565365c 2024-03-27 op const char *errstr;
271 ac0a4dfc 2024-03-28 op char *author = NULL;
272 763b7f49 2024-03-28 op char *filename, *t;
273 5565365c 2024-03-27 op char *l;
274 5565365c 2024-03-27 op ssize_t linelen;
275 5565365c 2024-03-27 op int parent = 0;
276 5565365c 2024-03-27 op int msglen = 0, msgwrote = 0;
277 763b7f49 2024-03-28 op int n, files = 0;
278 ec405b99 2024-03-28 op int done = 0;
279 5565365c 2024-03-27 op enum {
280 5565365c 2024-03-27 op P_FROM,
281 5565365c 2024-03-27 op P_VIA,
282 5565365c 2024-03-27 op P_DATE,
283 5565365c 2024-03-27 op P_PARENT,
284 5565365c 2024-03-27 op P_MSGLEN,
285 5565365c 2024-03-27 op P_MSG,
286 5565365c 2024-03-27 op P_DST,
287 5565365c 2024-03-27 op P_SUM,
288 ec405b99 2024-03-28 op } phase = P_FROM;
289 5565365c 2024-03-27 op
290 ec405b99 2024-03-28 op l = *line;
291 ec405b99 2024-03-28 op if (strncmp(l, "commit ", 7) != 0)
292 ec405b99 2024-03-28 op errx(1, "%s: unexpected line: %s", __func__, l);
293 ec405b99 2024-03-28 op l += 7;
294 763b7f49 2024-03-28 op
295 93623901 2024-03-28 op fprintf(fp, "{\"type\":\"commit\",\"short\":false,");
296 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
297 841969e1 2024-04-30 op json_field(fp, "authenticated_user", user, 1);
298 ec405b99 2024-03-28 op json_field(fp, "id", l, 1);
299 5565365c 2024-03-27 op
300 ec405b99 2024-03-28 op while (!done) {
301 ec405b99 2024-03-28 op if ((linelen = getline(line, linesize, stdin)) == -1)
302 ec405b99 2024-03-28 op break;
303 5565365c 2024-03-27 op
304 ec405b99 2024-03-28 op if ((*line)[linelen - 1] == '\n')
305 ec405b99 2024-03-28 op (*line)[--linelen] = '\0';
306 5565365c 2024-03-27 op
307 ec405b99 2024-03-28 op l = *line;
308 ec405b99 2024-03-28 op switch (phase) {
309 5565365c 2024-03-27 op case P_FROM:
310 5565365c 2024-03-27 op if (strncmp(l, "from: ", 6) != 0)
311 5565365c 2024-03-27 op errx(1, "unexpected from line");
312 5565365c 2024-03-27 op l += 6;
313 ac0a4dfc 2024-03-28 op
314 ac0a4dfc 2024-03-28 op author = strdup(l);
315 ac0a4dfc 2024-03-28 op if (author == NULL)
316 b3fabc96 2024-04-26 op fatal("strdup");
317 ac0a4dfc 2024-03-28 op
318 ac0a4dfc 2024-03-28 op json_author(fp, "author", l, 1);
319 ac0a4dfc 2024-03-28 op
320 5565365c 2024-03-27 op phase = P_VIA;
321 5565365c 2024-03-27 op break;
322 5565365c 2024-03-27 op
323 5565365c 2024-03-27 op case P_VIA:
324 5565365c 2024-03-27 op /* optional */
325 5565365c 2024-03-27 op if (!strncmp(l, "via: ", 5)) {
326 5565365c 2024-03-27 op l += 5;
327 ac0a4dfc 2024-03-28 op json_author(fp, "committer", l, 1);
328 5565365c 2024-03-27 op phase = P_DATE;
329 5565365c 2024-03-27 op break;
330 5565365c 2024-03-27 op }
331 ac0a4dfc 2024-03-28 op
332 ac0a4dfc 2024-03-28 op if (author == NULL) /* impossible */
333 b3fabc96 2024-04-26 op fatalx("from not specified");
334 ac0a4dfc 2024-03-28 op json_author(fp, "committer", author, 1);
335 ac0a4dfc 2024-03-28 op free(author);
336 ac0a4dfc 2024-03-28 op author = NULL;
337 ac0a4dfc 2024-03-28 op
338 5565365c 2024-03-27 op phase = P_DATE;
339 5565365c 2024-03-27 op /* fallthrough */
340 5565365c 2024-03-27 op
341 5565365c 2024-03-27 op case P_DATE:
342 5565365c 2024-03-27 op /* optional */
343 5565365c 2024-03-27 op if (!strncmp(l, "date: ", 6)) {
344 5565365c 2024-03-27 op l += 6;
345 939d3016 2024-04-23 op json_date(fp, "date", l, 1);
346 5565365c 2024-03-27 op phase = P_PARENT;
347 5565365c 2024-03-27 op break;
348 5565365c 2024-03-27 op }
349 5565365c 2024-03-27 op phase = P_PARENT;
350 5565365c 2024-03-27 op /* fallthough */
351 5565365c 2024-03-27 op
352 5565365c 2024-03-27 op case P_PARENT:
353 5565365c 2024-03-27 op /* optional - more than one */
354 5565365c 2024-03-27 op if (!strncmp(l, "parent ", 7)) {
355 5565365c 2024-03-27 op l += 7;
356 5565365c 2024-03-27 op l += strcspn(l, ":");
357 5565365c 2024-03-27 op l += strspn(l, " ");
358 5565365c 2024-03-27 op
359 5565365c 2024-03-27 op if (parent == 0) {
360 5565365c 2024-03-27 op parent = 1;
361 5565365c 2024-03-27 op fprintf(fp, "\"parents\":[");
362 5565365c 2024-03-27 op }
363 5565365c 2024-03-27 op
364 5565365c 2024-03-27 op fputc('"', fp);
365 5565365c 2024-03-27 op escape(fp, l);
366 5565365c 2024-03-27 op fputc('"', fp);
367 5565365c 2024-03-27 op
368 5565365c 2024-03-27 op break;
369 5565365c 2024-03-27 op }
370 5565365c 2024-03-27 op if (parent != 0) {
371 5565365c 2024-03-27 op fprintf(fp, "],");
372 5565365c 2024-03-27 op parent = 0;
373 5565365c 2024-03-27 op }
374 5565365c 2024-03-27 op phase = P_MSGLEN;
375 5565365c 2024-03-27 op /* fallthrough */
376 5565365c 2024-03-27 op
377 5565365c 2024-03-27 op case P_MSGLEN:
378 5565365c 2024-03-27 op if (strncmp(l, "messagelen: ", 12) != 0)
379 5565365c 2024-03-27 op errx(1, "unexpected messagelen line");
380 5565365c 2024-03-27 op l += 12;
381 5565365c 2024-03-27 op msglen = strtonum(l, 1, INT_MAX, &errstr);
382 5565365c 2024-03-27 op if (errstr)
383 5565365c 2024-03-27 op errx(1, "message len is %s: %s", errstr, l);
384 5565365c 2024-03-27 op
385 763b7f49 2024-03-28 op msglen++;
386 763b7f49 2024-03-28 op
387 5565365c 2024-03-27 op phase = P_MSG;
388 5565365c 2024-03-27 op break;
389 5565365c 2024-03-27 op
390 5565365c 2024-03-27 op case P_MSG:
391 5565365c 2024-03-27 op /*
392 5565365c 2024-03-27 op * The commit message is indented with one extra
393 5565365c 2024-03-27 op * space which is not accounted for in messagelen,
394 5565365c 2024-03-27 op * but we also strip the trailing \n so that
395 5565365c 2024-03-27 op * accounts for it.
396 5565365c 2024-03-27 op *
397 5565365c 2024-03-27 op * Since we read line-by-line and there is always
398 5565365c 2024-03-27 op * a \n added at the end of the message,
399 5565365c 2024-03-27 op * tolerate one byte less than advertised.
400 5565365c 2024-03-27 op */
401 763b7f49 2024-03-28 op if (*l != ' ')
402 763b7f49 2024-03-28 op errx(1, "unexpected line in commit message");
403 5565365c 2024-03-27 op
404 763b7f49 2024-03-28 op l++; /* skip leading space */
405 763b7f49 2024-03-28 op linelen--;
406 763b7f49 2024-03-28 op
407 763b7f49 2024-03-28 op if (msgwrote == 0 && linelen != 0) {
408 763b7f49 2024-03-28 op json_field(fp, "short_message", l, 1);
409 763b7f49 2024-03-28 op fprintf(fp, "\"message\":\"");
410 763b7f49 2024-03-28 op escape(fp, l);
411 763b7f49 2024-03-28 op escape(fp, "\n");
412 763b7f49 2024-03-28 op msgwrote += linelen;
413 763b7f49 2024-03-28 op } else if (msgwrote != 0) {
414 763b7f49 2024-03-28 op escape(fp, l);
415 763b7f49 2024-03-28 op escape(fp, "\n");
416 5565365c 2024-03-27 op }
417 763b7f49 2024-03-28 op
418 ac0a4dfc 2024-03-28 op msglen -= linelen + 1;
419 5565365c 2024-03-27 op if (msglen <= 1) {
420 5565365c 2024-03-27 op fprintf(fp, "\",");
421 5565365c 2024-03-27 op phase = P_DST;
422 763b7f49 2024-03-28 op break;
423 5565365c 2024-03-27 op }
424 5565365c 2024-03-27 op break;
425 5565365c 2024-03-27 op
426 5565365c 2024-03-27 op case P_DST:
427 763b7f49 2024-03-28 op if (files == 0 && !strcmp(l, " "))
428 763b7f49 2024-03-28 op break;
429 763b7f49 2024-03-28 op
430 763b7f49 2024-03-28 op if (files == 0)
431 763b7f49 2024-03-28 op fputs("\"diffstat\":{\"files\":[", fp);
432 763b7f49 2024-03-28 op
433 ec405b99 2024-03-28 op if (*l == '\0') {
434 763b7f49 2024-03-28 op fputs("],", fp);
435 5565365c 2024-03-27 op phase = P_SUM;
436 5565365c 2024-03-27 op break;
437 5565365c 2024-03-27 op }
438 763b7f49 2024-03-28 op
439 763b7f49 2024-03-28 op if (*l != ' ')
440 763b7f49 2024-03-28 op errx(1, "bad diffstat line");
441 763b7f49 2024-03-28 op l++;
442 763b7f49 2024-03-28 op
443 763b7f49 2024-03-28 op if (files != 0)
444 763b7f49 2024-03-28 op fputc(',', fp);
445 763b7f49 2024-03-28 op fputc('{', fp);
446 763b7f49 2024-03-28 op
447 763b7f49 2024-03-28 op switch (*l) {
448 763b7f49 2024-03-28 op case 'A':
449 763b7f49 2024-03-28 op json_field(fp, "action", "added", 1);
450 763b7f49 2024-03-28 op break;
451 763b7f49 2024-03-28 op case 'D':
452 763b7f49 2024-03-28 op json_field(fp, "action", "deleted", 1);
453 763b7f49 2024-03-28 op break;
454 763b7f49 2024-03-28 op case 'M':
455 763b7f49 2024-03-28 op json_field(fp, "action", "modified", 1);
456 763b7f49 2024-03-28 op break;
457 763b7f49 2024-03-28 op case 'm':
458 763b7f49 2024-03-28 op json_field(fp, "action", "mode changed", 1);
459 763b7f49 2024-03-28 op break;
460 763b7f49 2024-03-28 op default:
461 763b7f49 2024-03-28 op json_field(fp, "action", "unknown", 1);
462 763b7f49 2024-03-28 op break;
463 763b7f49 2024-03-28 op }
464 763b7f49 2024-03-28 op
465 763b7f49 2024-03-28 op l++;
466 763b7f49 2024-03-28 op while (*l == ' ')
467 763b7f49 2024-03-28 op *l++ = '\0';
468 763b7f49 2024-03-28 op if (*l == '\0')
469 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no filename");
470 763b7f49 2024-03-28 op
471 763b7f49 2024-03-28 op filename = l;
472 763b7f49 2024-03-28 op l = strrchr(l, '|');
473 763b7f49 2024-03-28 op if (l == NULL)
474 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no separator");
475 763b7f49 2024-03-28 op t = l - 1;
476 763b7f49 2024-03-28 op while (t > filename && *t == ' ')
477 763b7f49 2024-03-28 op *t-- = '\0';
478 763b7f49 2024-03-28 op json_field(fp, "file", filename, 1);
479 763b7f49 2024-03-28 op
480 763b7f49 2024-03-28 op l++;
481 763b7f49 2024-03-28 op while (*l == ' ')
482 763b7f49 2024-03-28 op l++;
483 763b7f49 2024-03-28 op
484 763b7f49 2024-03-28 op t = strchr(l, '+');
485 763b7f49 2024-03-28 op if (t == NULL)
486 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no added counter");
487 763b7f49 2024-03-28 op *t++ = '\0';
488 763b7f49 2024-03-28 op
489 763b7f49 2024-03-28 op n = strtonum(l, 0, INT_MAX, &errstr);
490 763b7f49 2024-03-28 op if (errstr)
491 763b7f49 2024-03-28 op errx(1, "added counter is %s: %s", errstr, l);
492 763b7f49 2024-03-28 op fprintf(fp, "\"added\":%d,", n);
493 763b7f49 2024-03-28 op
494 763b7f49 2024-03-28 op l = ++t;
495 763b7f49 2024-03-28 op while (*l == ' ')
496 763b7f49 2024-03-28 op l++;
497 763b7f49 2024-03-28 op
498 763b7f49 2024-03-28 op t = strchr(l, '-');
499 763b7f49 2024-03-28 op if (t == NULL)
500 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no del counter");
501 763b7f49 2024-03-28 op *t = '\0';
502 763b7f49 2024-03-28 op
503 763b7f49 2024-03-28 op n = strtonum(l, 0, INT_MAX, &errstr);
504 763b7f49 2024-03-28 op if (errstr)
505 763b7f49 2024-03-28 op errx(1, "del counter is %s: %s", errstr, l);
506 763b7f49 2024-03-28 op fprintf(fp, "\"removed\":%d", n);
507 763b7f49 2024-03-28 op
508 763b7f49 2024-03-28 op fputc('}', fp);
509 763b7f49 2024-03-28 op
510 763b7f49 2024-03-28 op files++;
511 763b7f49 2024-03-28 op
512 5565365c 2024-03-27 op break;
513 5565365c 2024-03-27 op
514 5565365c 2024-03-27 op case P_SUM:
515 763b7f49 2024-03-28 op fputs("\"total\":{", fp);
516 763b7f49 2024-03-28 op
517 763b7f49 2024-03-28 op t = l;
518 763b7f49 2024-03-28 op l = strchr(l, ' ');
519 763b7f49 2024-03-28 op if (l == NULL)
520 763b7f49 2024-03-28 op errx(1, "missing number of additions");
521 763b7f49 2024-03-28 op *l++ = '\0';
522 763b7f49 2024-03-28 op
523 763b7f49 2024-03-28 op n = strtonum(t, 0, INT_MAX, &errstr);
524 763b7f49 2024-03-28 op if (errstr)
525 763b7f49 2024-03-28 op errx(1, "add counter is %s: %s", errstr, t);
526 763b7f49 2024-03-28 op fprintf(fp, "\"added\":%d,", n);
527 763b7f49 2024-03-28 op
528 763b7f49 2024-03-28 op l = strchr(l, ',');
529 763b7f49 2024-03-28 op if (l == NULL)
530 763b7f49 2024-03-28 op errx(1, "missing number of deletions");
531 763b7f49 2024-03-28 op l++;
532 763b7f49 2024-03-28 op while (*l == ' ')
533 763b7f49 2024-03-28 op l++;
534 763b7f49 2024-03-28 op
535 763b7f49 2024-03-28 op t = strchr(l, ' ');
536 763b7f49 2024-03-28 op if (t == NULL)
537 763b7f49 2024-03-28 op errx(1, "malformed diffstat sum line");
538 763b7f49 2024-03-28 op *t = '\0';
539 763b7f49 2024-03-28 op
540 763b7f49 2024-03-28 op n = strtonum(l, 0, INT_MAX, &errstr);
541 763b7f49 2024-03-28 op if (errstr)
542 763b7f49 2024-03-28 op errx(1, "del counter is %s: %s", errstr, l);
543 763b7f49 2024-03-28 op fprintf(fp, "\"removed\":%d", n);
544 763b7f49 2024-03-28 op
545 763b7f49 2024-03-28 op fputs("}}", fp);
546 ec405b99 2024-03-28 op done = 1;
547 5565365c 2024-03-27 op break;
548 5565365c 2024-03-27 op
549 5565365c 2024-03-27 op default:
550 ec405b99 2024-03-28 op /* unreachable */
551 ec405b99 2024-03-28 op errx(1, "unexpected line: %s", *line);
552 5565365c 2024-03-27 op }
553 5565365c 2024-03-27 op }
554 5565365c 2024-03-27 op if (ferror(stdin))
555 b3fabc96 2024-04-26 op fatalx("getline");
556 ec405b99 2024-03-28 op if (!done)
557 b3fabc96 2024-04-26 op fatalx("%s: unexpected EOF", __func__);
558 763b7f49 2024-03-28 op fputc('}', fp);
559 ec405b99 2024-03-28 op
560 ec405b99 2024-03-28 op return 0;
561 ec405b99 2024-03-28 op }
562 ec405b99 2024-03-28 op
563 ec405b99 2024-03-28 op static int
564 d36998ae 2024-04-29 stsp jsonify_tag(FILE *fp, const char *repo, const char *user,
565 d36998ae 2024-04-29 stsp char **line, ssize_t *linesize)
566 553d8347 2024-03-28 op {
567 553d8347 2024-03-28 op const char *errstr;
568 553d8347 2024-03-28 op char *l;
569 553d8347 2024-03-28 op ssize_t linelen;
570 553d8347 2024-03-28 op int msglen = 0, msgwrote = 0;
571 553d8347 2024-03-28 op int done = 0;
572 553d8347 2024-03-28 op enum {
573 553d8347 2024-03-28 op P_FROM,
574 553d8347 2024-03-28 op P_DATE,
575 553d8347 2024-03-28 op P_OBJECT,
576 553d8347 2024-03-28 op P_MSGLEN,
577 553d8347 2024-03-28 op P_MSG,
578 553d8347 2024-03-28 op } phase = P_FROM;
579 553d8347 2024-03-28 op
580 553d8347 2024-03-28 op l = *line;
581 553d8347 2024-03-28 op if (strncmp(l, "tag ", 4) != 0)
582 553d8347 2024-03-28 op errx(1, "%s: unexpected line: %s", __func__, l);
583 553d8347 2024-03-28 op l += 4;
584 553d8347 2024-03-28 op
585 553d8347 2024-03-28 op fputc('{', fp);
586 553d8347 2024-03-28 op json_field(fp, "type", "tag", 1);
587 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
588 841969e1 2024-04-30 op json_field(fp, "authenticated_user", user, 1);
589 553d8347 2024-03-28 op json_field(fp, "tag", l, 1);
590 553d8347 2024-03-28 op
591 553d8347 2024-03-28 op while (!done) {
592 553d8347 2024-03-28 op if ((linelen = getline(line, linesize, stdin)) == -1)
593 553d8347 2024-03-28 op break;
594 553d8347 2024-03-28 op
595 553d8347 2024-03-28 op if ((*line)[linelen - 1] == '\n')
596 553d8347 2024-03-28 op (*line)[--linelen] = '\0';
597 553d8347 2024-03-28 op
598 553d8347 2024-03-28 op l = *line;
599 553d8347 2024-03-28 op switch (phase) {
600 553d8347 2024-03-28 op case P_FROM:
601 553d8347 2024-03-28 op if (strncmp(l, "from: ", 6) != 0)
602 553d8347 2024-03-28 op errx(1, "unexpected from line");
603 553d8347 2024-03-28 op l += 6;
604 553d8347 2024-03-28 op
605 553d8347 2024-03-28 op json_author(fp, "tagger", l, 1);
606 553d8347 2024-03-28 op
607 553d8347 2024-03-28 op phase = P_DATE;
608 553d8347 2024-03-28 op break;
609 553d8347 2024-03-28 op
610 553d8347 2024-03-28 op case P_DATE:
611 553d8347 2024-03-28 op /* optional */
612 553d8347 2024-03-28 op if (!strncmp(l, "date: ", 6)) {
613 553d8347 2024-03-28 op l += 6;
614 939d3016 2024-04-23 op json_date(fp, "date", l, 1);
615 553d8347 2024-03-28 op phase = P_OBJECT;
616 553d8347 2024-03-28 op break;
617 553d8347 2024-03-28 op }
618 553d8347 2024-03-28 op phase = P_OBJECT;
619 553d8347 2024-03-28 op /* fallthough */
620 553d8347 2024-03-28 op
621 553d8347 2024-03-28 op case P_OBJECT:
622 553d8347 2024-03-28 op /* optional */
623 553d8347 2024-03-28 op if (!strncmp(l, "object: ", 8)) {
624 553d8347 2024-03-28 op char *type, *id;
625 553d8347 2024-03-28 op
626 553d8347 2024-03-28 op l += 8;
627 553d8347 2024-03-28 op type = l;
628 553d8347 2024-03-28 op id = strchr(l, ' ');
629 553d8347 2024-03-28 op if (id == NULL)
630 553d8347 2024-03-28 op errx(1, "malformed tag object line");
631 553d8347 2024-03-28 op *id++ = '\0';
632 553d8347 2024-03-28 op
633 553d8347 2024-03-28 op fputs("\"object\":{", fp);
634 553d8347 2024-03-28 op json_field(fp, "type", type, 1);
635 553d8347 2024-03-28 op json_field(fp, "id", id, 0);
636 553d8347 2024-03-28 op fputs("},", fp);
637 553d8347 2024-03-28 op
638 553d8347 2024-03-28 op phase = P_MSGLEN;
639 553d8347 2024-03-28 op break;
640 553d8347 2024-03-28 op }
641 553d8347 2024-03-28 op phase = P_MSGLEN;
642 553d8347 2024-03-28 op /* fallthrough */
643 553d8347 2024-03-28 op
644 553d8347 2024-03-28 op case P_MSGLEN:
645 553d8347 2024-03-28 op if (strncmp(l, "messagelen: ", 12) != 0)
646 553d8347 2024-03-28 op errx(1, "unexpected messagelen line");
647 553d8347 2024-03-28 op l += 12;
648 553d8347 2024-03-28 op msglen = strtonum(l, 1, INT_MAX, &errstr);
649 553d8347 2024-03-28 op if (errstr)
650 553d8347 2024-03-28 op errx(1, "message len is %s: %s", errstr, l);
651 553d8347 2024-03-28 op
652 553d8347 2024-03-28 op msglen++;
653 553d8347 2024-03-28 op
654 553d8347 2024-03-28 op phase = P_MSG;
655 553d8347 2024-03-28 op break;
656 553d8347 2024-03-28 op
657 553d8347 2024-03-28 op case P_MSG:
658 763b7f49 2024-03-28 op if (*l != ' ')
659 763b7f49 2024-03-28 op errx(1, "unexpected line in tag message");
660 553d8347 2024-03-28 op
661 763b7f49 2024-03-28 op l++; /* skip leading space */
662 763b7f49 2024-03-28 op linelen--;
663 763b7f49 2024-03-28 op
664 763b7f49 2024-03-28 op if (msgwrote == 0 && linelen != 0) {
665 763b7f49 2024-03-28 op fprintf(fp, "\"message\":\"");
666 763b7f49 2024-03-28 op escape(fp, l);
667 763b7f49 2024-03-28 op escape(fp, "\n");
668 763b7f49 2024-03-28 op msgwrote += linelen;
669 763b7f49 2024-03-28 op } else if (msgwrote != 0) {
670 763b7f49 2024-03-28 op escape(fp, l);
671 763b7f49 2024-03-28 op escape(fp, "\n");
672 553d8347 2024-03-28 op }
673 763b7f49 2024-03-28 op
674 553d8347 2024-03-28 op msglen -= linelen + 1;
675 e789f02b 2024-03-28 op if (msglen <= 0) {
676 553d8347 2024-03-28 op fprintf(fp, "\"");
677 553d8347 2024-03-28 op done = 1;
678 553d8347 2024-03-28 op break;
679 553d8347 2024-03-28 op }
680 553d8347 2024-03-28 op break;
681 553d8347 2024-03-28 op
682 553d8347 2024-03-28 op default:
683 553d8347 2024-03-28 op /* unreachable */
684 553d8347 2024-03-28 op errx(1, "unexpected line: %s", *line);
685 553d8347 2024-03-28 op }
686 553d8347 2024-03-28 op }
687 553d8347 2024-03-28 op if (ferror(stdin))
688 b3fabc96 2024-04-26 op fatal("getline");
689 553d8347 2024-03-28 op if (!done)
690 b3fabc96 2024-04-26 op fatalx("%s: unexpected EOF", __func__);
691 553d8347 2024-03-28 op fputc('}', fp);
692 553d8347 2024-03-28 op
693 553d8347 2024-03-28 op return 0;
694 553d8347 2024-03-28 op }
695 553d8347 2024-03-28 op
696 553d8347 2024-03-28 op static int
697 d36998ae 2024-04-29 stsp jsonify(FILE *fp, const char *repo, const char *user)
698 ec405b99 2024-03-28 op {
699 ec405b99 2024-03-28 op char *line = NULL;
700 ec405b99 2024-03-28 op size_t linesize = 0;
701 ec405b99 2024-03-28 op ssize_t linelen;
702 ec405b99 2024-03-28 op int needcomma = 0;
703 ec405b99 2024-03-28 op
704 ec405b99 2024-03-28 op fprintf(fp, "{\"notifications\":[");
705 ec405b99 2024-03-28 op while ((linelen = getline(&line, &linesize, stdin)) != -1) {
706 ec405b99 2024-03-28 op if (line[linelen - 1] == '\n')
707 ec405b99 2024-03-28 op line[--linelen] = '\0';
708 5565365c 2024-03-27 op
709 ec405b99 2024-03-28 op if (*line == '\0')
710 ec405b99 2024-03-28 op continue;
711 ec405b99 2024-03-28 op
712 ec405b99 2024-03-28 op if (needcomma)
713 ec405b99 2024-03-28 op fputc(',', fp);
714 ec405b99 2024-03-28 op needcomma = 1;
715 ec405b99 2024-03-28 op
716 d6057084 2024-03-28 op if (strncmp(line, "Removed refs/heads/", 19) == 0) {
717 d36998ae 2024-04-29 stsp if (jsonify_branch_rm(fp, line, repo, user) == -1)
718 b3fabc96 2024-04-26 op fatal("jsonify_branch_rm");
719 d6057084 2024-03-28 op continue;
720 d6057084 2024-03-28 op }
721 d6057084 2024-03-28 op
722 ec405b99 2024-03-28 op if (strncmp(line, "commit ", 7) == 0) {
723 d36998ae 2024-04-29 stsp if (jsonify_commit(fp, repo, user,
724 d36998ae 2024-04-29 stsp &line, &linesize) == -1)
725 b3fabc96 2024-04-26 op fatal("jsonify_commit");
726 ec405b99 2024-03-28 op continue;
727 ec405b99 2024-03-28 op }
728 ec405b99 2024-03-28 op
729 ec405b99 2024-03-28 op if (*line >= '0' && *line <= '9') {
730 d36998ae 2024-04-29 stsp if (jsonify_commit_short(fp, line, repo, user) == -1)
731 b3fabc96 2024-04-26 op fatal("jsonify_commit_short");
732 ec405b99 2024-03-28 op continue;
733 ec405b99 2024-03-28 op }
734 ec405b99 2024-03-28 op
735 553d8347 2024-03-28 op if (strncmp(line, "tag ", 4) == 0) {
736 d36998ae 2024-04-29 stsp if (jsonify_tag(fp, repo, user, &line, &linesize) == -1)
737 b3fabc96 2024-04-26 op fatal("jsonify_tag");
738 553d8347 2024-03-28 op continue;
739 553d8347 2024-03-28 op }
740 553d8347 2024-03-28 op
741 ec405b99 2024-03-28 op errx(1, "unexpected line: %s", line);
742 ec405b99 2024-03-28 op }
743 ec405b99 2024-03-28 op if (ferror(stdin))
744 b3fabc96 2024-04-26 op fatal("getline");
745 5565365c 2024-03-27 op fprintf(fp, "]}");
746 5565365c 2024-03-27 op
747 5565365c 2024-03-27 op return 0;
748 5565365c 2024-03-27 op }
749 050c0b8c 2024-04-16 op
750 050c0b8c 2024-04-16 op static char
751 050c0b8c 2024-04-16 op sixet2ch(int c)
752 050c0b8c 2024-04-16 op {
753 050c0b8c 2024-04-16 op c &= 0x3F;
754 5565365c 2024-03-27 op
755 050c0b8c 2024-04-16 op if (c < 26)
756 050c0b8c 2024-04-16 op return 'A' + c;
757 050c0b8c 2024-04-16 op c -= 26;
758 050c0b8c 2024-04-16 op if (c < 26)
759 050c0b8c 2024-04-16 op return 'a' + c;
760 050c0b8c 2024-04-16 op c -= 26;
761 050c0b8c 2024-04-16 op if (c < 10)
762 050c0b8c 2024-04-16 op return '0' + c;
763 050c0b8c 2024-04-16 op c -= 10;
764 050c0b8c 2024-04-16 op if (c == 0)
765 050c0b8c 2024-04-16 op return '+';
766 050c0b8c 2024-04-16 op if (c == 1)
767 050c0b8c 2024-04-16 op return '/';
768 050c0b8c 2024-04-16 op
769 050c0b8c 2024-04-16 op errx(1, "invalid sixet 0x%x", c);
770 050c0b8c 2024-04-16 op }
771 050c0b8c 2024-04-16 op
772 5565365c 2024-03-27 op static char *
773 5565365c 2024-03-27 op basic_auth(const char *username, const char *password)
774 5565365c 2024-03-27 op {
775 050c0b8c 2024-04-16 op char *str, *tmp, *end, *s, *p;
776 050c0b8c 2024-04-16 op char buf[3];
777 050c0b8c 2024-04-16 op int len, i, r;
778 5565365c 2024-03-27 op
779 050c0b8c 2024-04-16 op r = asprintf(&str, "%s:%s", username, password);
780 050c0b8c 2024-04-16 op if (r == -1)
781 b3fabc96 2024-04-26 op fatal("asprintf");
782 5565365c 2024-03-27 op
783 050c0b8c 2024-04-16 op /*
784 050c0b8c 2024-04-16 op * Will need 4 * r/3 bytes to encode the string, plus a
785 050c0b8c 2024-04-16 op * rounding to the next multiple of 4 for padding, plus NUL.
786 050c0b8c 2024-04-16 op */
787 050c0b8c 2024-04-16 op len = 4 * r / 3;
788 050c0b8c 2024-04-16 op len = (len + 3) & ~3;
789 050c0b8c 2024-04-16 op len++;
790 050c0b8c 2024-04-16 op
791 050c0b8c 2024-04-16 op tmp = calloc(1, len);
792 050c0b8c 2024-04-16 op if (tmp == NULL)
793 b3fabc96 2024-04-26 op fatal("calloc");
794 050c0b8c 2024-04-16 op
795 050c0b8c 2024-04-16 op s = str;
796 050c0b8c 2024-04-16 op p = tmp;
797 050c0b8c 2024-04-16 op while (*s != '\0') {
798 050c0b8c 2024-04-16 op memset(buf, 0, sizeof(buf));
799 050c0b8c 2024-04-16 op for (i = 0; i < 3 && *s != '\0'; ++i, ++s)
800 050c0b8c 2024-04-16 op buf[i] = *s;
801 050c0b8c 2024-04-16 op
802 050c0b8c 2024-04-16 op *p++ = sixet2ch(buf[0] >> 2);
803 050c0b8c 2024-04-16 op *p++ = sixet2ch((buf[1] >> 4) | (buf[0] << 4));
804 050c0b8c 2024-04-16 op if (i > 1)
805 050c0b8c 2024-04-16 op *p++ = sixet2ch((buf[1] << 2) | (buf[2] >> 6));
806 050c0b8c 2024-04-16 op if (i > 2)
807 050c0b8c 2024-04-16 op *p++ = sixet2ch(buf[2]);
808 050c0b8c 2024-04-16 op }
809 050c0b8c 2024-04-16 op
810 050c0b8c 2024-04-16 op for (end = tmp + len - 1; p < end; ++p)
811 050c0b8c 2024-04-16 op *p = '=';
812 050c0b8c 2024-04-16 op
813 050c0b8c 2024-04-16 op free(str);
814 5565365c 2024-03-27 op return tmp;
815 5565365c 2024-03-27 op }
816 5565365c 2024-03-27 op
817 5565365c 2024-03-27 op static inline int
818 5565365c 2024-03-27 op bufio2poll(struct bufio *bio)
819 5565365c 2024-03-27 op {
820 5565365c 2024-03-27 op int f, ret = 0;
821 770f8d29 2024-04-26 op
822 770f8d29 2024-04-26 op /*
823 770f8d29 2024-04-26 op * If we have data queued up, retry for both POLLIN and POLLOUT
824 770f8d29 2024-04-26 op * since we want to push this data to the server while still
825 770f8d29 2024-04-26 op * processing an eventual reply. Otherwise, we could wait
826 770f8d29 2024-04-26 op * indefinitely for the server to reply without us having
827 770f8d29 2024-04-26 op * sent the HTTP request completely.
828 770f8d29 2024-04-26 op */
829 770f8d29 2024-04-26 op if (bio->wbuf.len)
830 770f8d29 2024-04-26 op return POLLIN|POLLOUT;
831 5565365c 2024-03-27 op
832 5565365c 2024-03-27 op f = bufio_ev(bio);
833 5565365c 2024-03-27 op if (f & BUFIO_WANT_READ)
834 5565365c 2024-03-27 op ret |= POLLIN;
835 5565365c 2024-03-27 op if (f & BUFIO_WANT_WRITE)
836 5565365c 2024-03-27 op ret |= POLLOUT;
837 5565365c 2024-03-27 op return ret;
838 5565365c 2024-03-27 op }
839 5565365c 2024-03-27 op
840 5565365c 2024-03-27 op int
841 5565365c 2024-03-27 op main(int argc, char **argv)
842 5565365c 2024-03-27 op {
843 5565365c 2024-03-27 op FILE *tmpfp;
844 5565365c 2024-03-27 op struct bufio bio;
845 5565365c 2024-03-27 op struct pollfd pfd;
846 5565365c 2024-03-27 op struct timespec timeout;
847 5565365c 2024-03-27 op const char *username;
848 5565365c 2024-03-27 op const char *password;
849 5565365c 2024-03-27 op const char *timeoutstr;
850 5565365c 2024-03-27 op const char *errstr;
851 c1003102 2024-04-15 op const char *repo = NULL;
852 5565365c 2024-03-27 op const char *host = NULL, *port = NULL, *path = NULL;
853 d36998ae 2024-04-29 stsp const char *gotd_auth_user = NULL;
854 5565365c 2024-03-27 op char *auth, *line, *spc;
855 5565365c 2024-03-27 op size_t len;
856 5565365c 2024-03-27 op ssize_t r;
857 5565365c 2024-03-27 op off_t paylen;
858 5565365c 2024-03-27 op int tls = 0;
859 5565365c 2024-03-27 op int response_code = 0, done = 0;
860 5565365c 2024-03-27 op int ch, flags, ret, nonstd = 0;
861 5565365c 2024-03-27 op
862 5565365c 2024-03-27 op #ifndef PROFILE
863 5565365c 2024-03-27 op if (pledge("stdio rpath tmppath dns inet", NULL) == -1)
864 5565365c 2024-03-27 op err(1, "pledge");
865 5565365c 2024-03-27 op #endif
866 5565365c 2024-03-27 op
867 cb29e255 2024-04-18 stsp log_init(0, LOG_DAEMON);
868 cb29e255 2024-04-18 stsp
869 d36998ae 2024-04-29 stsp while ((ch = getopt(argc, argv, "ch:p:r:u:")) != -1) {
870 5565365c 2024-03-27 op switch (ch) {
871 5565365c 2024-03-27 op case 'c':
872 5565365c 2024-03-27 op tls = 1;
873 5565365c 2024-03-27 op break;
874 5565365c 2024-03-27 op case 'h':
875 5565365c 2024-03-27 op host = optarg;
876 5565365c 2024-03-27 op break;
877 5565365c 2024-03-27 op case 'p':
878 5565365c 2024-03-27 op port = optarg;
879 5565365c 2024-03-27 op break;
880 c1003102 2024-04-15 op case 'r':
881 c1003102 2024-04-15 op repo = optarg;
882 c1003102 2024-04-15 op break;
883 d36998ae 2024-04-29 stsp case 'u':
884 d36998ae 2024-04-29 stsp gotd_auth_user = optarg;
885 d36998ae 2024-04-29 stsp break;
886 5565365c 2024-03-27 op default:
887 5565365c 2024-03-27 op usage();
888 5565365c 2024-03-27 op }
889 5565365c 2024-03-27 op }
890 5565365c 2024-03-27 op argc -= optind;
891 5565365c 2024-03-27 op argv += optind;
892 5565365c 2024-03-27 op
893 9d42388a 2024-04-29 stsp if (host == NULL || repo == NULL || gotd_auth_user == NULL || argc != 1)
894 5565365c 2024-03-27 op usage();
895 5565365c 2024-03-27 op if (tls && port == NULL)
896 5565365c 2024-03-27 op port = "443";
897 5565365c 2024-03-27 op path = argv[0];
898 5565365c 2024-03-27 op
899 5565365c 2024-03-27 op username = getenv("GOT_NOTIFY_HTTP_USER");
900 5565365c 2024-03-27 op password = getenv("GOT_NOTIFY_HTTP_PASS");
901 5565365c 2024-03-27 op if ((username != NULL && password == NULL) ||
902 5565365c 2024-03-27 op (username == NULL && password != NULL))
903 cb29e255 2024-04-18 stsp fatalx("username or password are not specified");
904 5565365c 2024-03-27 op if (username && *password == '\0')
905 cb29e255 2024-04-18 stsp fatalx("password can't be empty");
906 5565365c 2024-03-27 op
907 5565365c 2024-03-27 op /* used by the regression test suite */
908 5565365c 2024-03-27 op timeoutstr = getenv("GOT_NOTIFY_TIMEOUT");
909 5565365c 2024-03-27 op if (timeoutstr) {
910 5565365c 2024-03-27 op http_timeout = strtonum(timeoutstr, 0, 600, &errstr);
911 5565365c 2024-03-27 op if (errstr != NULL)
912 cb29e255 2024-04-18 stsp fatalx("timeout in seconds is %s: %s",
913 5565365c 2024-03-27 op errstr, timeoutstr);
914 5565365c 2024-03-27 op }
915 5565365c 2024-03-27 op
916 5565365c 2024-03-27 op memset(&timeout, 0, sizeof(timeout));
917 5565365c 2024-03-27 op timeout.tv_sec = http_timeout;
918 5565365c 2024-03-27 op
919 5565365c 2024-03-27 op tmpfp = got_opentemp();
920 5565365c 2024-03-27 op if (tmpfp == NULL)
921 cb29e255 2024-04-18 stsp fatal("opentemp");
922 5565365c 2024-03-27 op
923 d36998ae 2024-04-29 stsp jsonify(tmpfp, repo, gotd_auth_user);
924 5565365c 2024-03-27 op
925 5565365c 2024-03-27 op paylen = ftello(tmpfp);
926 5565365c 2024-03-27 op if (paylen == -1)
927 cb29e255 2024-04-18 stsp fatal("ftello");
928 5565365c 2024-03-27 op if (fseeko(tmpfp, 0, SEEK_SET) == -1)
929 cb29e255 2024-04-18 stsp fatal("fseeko");
930 5565365c 2024-03-27 op
931 5565365c 2024-03-27 op #ifndef PROFILE
932 5565365c 2024-03-27 op /* drop tmppath */
933 5565365c 2024-03-27 op if (pledge("stdio rpath dns inet", NULL) == -1)
934 5565365c 2024-03-27 op err(1, "pledge");
935 5565365c 2024-03-27 op #endif
936 5565365c 2024-03-27 op
937 5565365c 2024-03-27 op memset(&pfd, 0, sizeof(pfd));
938 5565365c 2024-03-27 op pfd.fd = dial(host, port);
939 5565365c 2024-03-27 op
940 5565365c 2024-03-27 op if ((flags = fcntl(pfd.fd, F_GETFL)) == -1)
941 cb29e255 2024-04-18 stsp fatal("fcntl(F_GETFL)");
942 5565365c 2024-03-27 op if (fcntl(pfd.fd, F_SETFL, flags | O_NONBLOCK) == -1)
943 cb29e255 2024-04-18 stsp fatal("fcntl(F_SETFL)");
944 5565365c 2024-03-27 op
945 5565365c 2024-03-27 op if (bufio_init(&bio) == -1)
946 cb29e255 2024-04-18 stsp fatal("bufio_init");
947 5565365c 2024-03-27 op bufio_set_fd(&bio, pfd.fd);
948 5565365c 2024-03-27 op if (tls && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1)
949 cb29e255 2024-04-18 stsp fatal("bufio_starttls");
950 5565365c 2024-03-27 op
951 5565365c 2024-03-27 op #ifndef PROFILE
952 5565365c 2024-03-27 op /* drop rpath dns inet */
953 5565365c 2024-03-27 op if (pledge("stdio", NULL) == -1)
954 5565365c 2024-03-27 op err(1, "pledge");
955 5565365c 2024-03-27 op #endif
956 5565365c 2024-03-27 op
957 5565365c 2024-03-27 op if ((!tls && strcmp(port, "80") != 0) ||
958 5565365c 2024-03-27 op (tls && strcmp(port, "443")) != 0)
959 5565365c 2024-03-27 op nonstd = 1;
960 5565365c 2024-03-27 op
961 5565365c 2024-03-27 op ret = bufio_compose_fmt(&bio,
962 5565365c 2024-03-27 op "POST %s HTTP/1.1\r\n"
963 5565365c 2024-03-27 op "Host: %s%s%s\r\n"
964 5565365c 2024-03-27 op "Content-Type: application/json\r\n"
965 5565365c 2024-03-27 op "Content-Length: %lld\r\n"
966 5565365c 2024-03-27 op "User-Agent: %s\r\n"
967 5565365c 2024-03-27 op "Connection: close\r\n",
968 5565365c 2024-03-27 op path, host,
969 5565365c 2024-03-27 op nonstd ? ":" : "", nonstd ? port : "",
970 5565365c 2024-03-27 op (long long)paylen, USERAGENT);
971 5565365c 2024-03-27 op if (ret == -1)
972 cb29e255 2024-04-18 stsp fatal("bufio_compose_fmt");
973 5565365c 2024-03-27 op
974 5565365c 2024-03-27 op if (username) {
975 5565365c 2024-03-27 op auth = basic_auth(username, password);
976 5565365c 2024-03-27 op ret = bufio_compose_fmt(&bio, "Authorization: basic %s\r\n",
977 5565365c 2024-03-27 op auth);
978 5565365c 2024-03-27 op if (ret == -1)
979 cb29e255 2024-04-18 stsp fatal("bufio_compose_fmt");
980 5565365c 2024-03-27 op free(auth);
981 5565365c 2024-03-27 op }
982 5565365c 2024-03-27 op
983 5565365c 2024-03-27 op if (bufio_compose(&bio, "\r\n", 2) == -1)
984 cb29e255 2024-04-18 stsp fatal("bufio_compose");
985 5565365c 2024-03-27 op
986 5565365c 2024-03-27 op while (!done) {
987 5565365c 2024-03-27 op struct timespec elapsed, start, stop;
988 5565365c 2024-03-27 op char buf[BUFSIZ];
989 5565365c 2024-03-27 op
990 5565365c 2024-03-27 op pfd.events = bufio2poll(&bio);
991 5565365c 2024-03-27 op clock_gettime(CLOCK_MONOTONIC, &start);
992 5565365c 2024-03-27 op ret = ppoll(&pfd, 1, &timeout, NULL);
993 5565365c 2024-03-27 op if (ret == -1)
994 cb29e255 2024-04-18 stsp fatal("poll");
995 5565365c 2024-03-27 op clock_gettime(CLOCK_MONOTONIC, &stop);
996 5565365c 2024-03-27 op timespecsub(&stop, &start, &elapsed);
997 5565365c 2024-03-27 op timespecsub(&timeout, &elapsed, &timeout);
998 5565365c 2024-03-27 op if (ret == 0 || timeout.tv_sec <= 0)
999 cb29e255 2024-04-18 stsp fatalx("timeout");
1000 5565365c 2024-03-27 op
1001 a9d9f6e4 2024-04-18 op if (bio.wbuf.len > 0) {
1002 5565365c 2024-03-27 op if (bufio_write(&bio) == -1 && errno != EAGAIN)
1003 cb29e255 2024-04-18 stsp fatalx("bufio_write: %s", bufio_io_err(&bio));
1004 5565365c 2024-03-27 op }
1005 5565365c 2024-03-27 op
1006 a9d9f6e4 2024-04-18 op r = bufio_read(&bio);
1007 a9d9f6e4 2024-04-18 op if (r == -1 && errno != EAGAIN)
1008 a9d9f6e4 2024-04-18 op fatalx("bufio_read: %s", bufio_io_err(&bio));
1009 a9d9f6e4 2024-04-18 op if (r == 0)
1010 8aebbd0a 2024-04-26 op fatalx("unexpected EOF from upstream HTTP server");
1011 5565365c 2024-03-27 op
1012 a9d9f6e4 2024-04-18 op for (;;) {
1013 a9d9f6e4 2024-04-18 op line = buf_getdelim(&bio.rbuf, "\r\n", &len);
1014 a9d9f6e4 2024-04-18 op if (line == NULL)
1015 a9d9f6e4 2024-04-18 op break;
1016 a9d9f6e4 2024-04-18 op if (response_code && *line == '\0') {
1017 a9d9f6e4 2024-04-18 op /*
1018 a9d9f6e4 2024-04-18 op * end of headers, don't bother
1019 a9d9f6e4 2024-04-18 op * reading the body, if there is.
1020 a9d9f6e4 2024-04-18 op */
1021 a9d9f6e4 2024-04-18 op done = 1;
1022 a9d9f6e4 2024-04-18 op break;
1023 a9d9f6e4 2024-04-18 op }
1024 a9d9f6e4 2024-04-18 op if (response_code) {
1025 5565365c 2024-03-27 op buf_drain(&bio.rbuf, len);
1026 a9d9f6e4 2024-04-18 op continue;
1027 5565365c 2024-03-27 op }
1028 a9d9f6e4 2024-04-18 op spc = strchr(line, ' ');
1029 a9d9f6e4 2024-04-18 op if (spc == NULL)
1030 a9d9f6e4 2024-04-18 op fatalx("bad HTTP response from server");
1031 a9d9f6e4 2024-04-18 op *spc++ = '\0';
1032 a9d9f6e4 2024-04-18 op if (strcasecmp(line, "HTTP/1.1") != 0)
1033 a9d9f6e4 2024-04-18 op log_warnx("unexpected protocol: %s", line);
1034 a9d9f6e4 2024-04-18 op line = spc;
1035 a9d9f6e4 2024-04-18 op
1036 a9d9f6e4 2024-04-18 op spc = strchr(line, ' ');
1037 a9d9f6e4 2024-04-18 op if (spc == NULL)
1038 a9d9f6e4 2024-04-18 op fatalx("bad HTTP response from server");
1039 a9d9f6e4 2024-04-18 op *spc++ = '\0';
1040 a9d9f6e4 2024-04-18 op
1041 a9d9f6e4 2024-04-18 op response_code = strtonum(line, 100, 599,
1042 a9d9f6e4 2024-04-18 op &errstr);
1043 a9d9f6e4 2024-04-18 op if (errstr != NULL)
1044 a9d9f6e4 2024-04-18 op log_warnx("response code is %s: %s",
1045 a9d9f6e4 2024-04-18 op errstr, line);
1046 a9d9f6e4 2024-04-18 op
1047 a9d9f6e4 2024-04-18 op buf_drain(&bio.rbuf, len);
1048 5565365c 2024-03-27 op }
1049 a9d9f6e4 2024-04-18 op if (done)
1050 a9d9f6e4 2024-04-18 op break;
1051 5565365c 2024-03-27 op
1052 5565365c 2024-03-27 op if (!feof(tmpfp) && bio.wbuf.len < sizeof(buf)) {
1053 5565365c 2024-03-27 op len = fread(buf, 1, sizeof(buf), tmpfp);
1054 5565365c 2024-03-27 op if (len == 0) {
1055 5565365c 2024-03-27 op if (ferror(tmpfp))
1056 cb29e255 2024-04-18 stsp fatal("fread");
1057 5565365c 2024-03-27 op continue;
1058 5565365c 2024-03-27 op }
1059 5565365c 2024-03-27 op
1060 5565365c 2024-03-27 op if (bufio_compose(&bio, buf, len) == -1)
1061 cb29e255 2024-04-18 stsp fatal("buf_compose");
1062 5565365c 2024-03-27 op }
1063 5565365c 2024-03-27 op }
1064 5565365c 2024-03-27 op
1065 3b44bdbe 2024-03-27 op if (response_code >= 200 && response_code < 300)
1066 5565365c 2024-03-27 op return 0;
1067 9e2d0515 2024-04-25 op fatalx("request failed with code %d", response_code);
1068 5565365c 2024-03-27 op }