2 04e4e993 2023-08-14 op * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 04e4e993 2023-08-14 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 04e4e993 2023-08-14 op * Permission to use, copy, modify, and distribute this software for any
6 04e4e993 2023-08-14 op * purpose with or without fee is hereby granted, provided that the above
7 04e4e993 2023-08-14 op * copyright notice and this permission notice appear in all copies.
9 04e4e993 2023-08-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 04e4e993 2023-08-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 04e4e993 2023-08-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 04e4e993 2023-08-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 04e4e993 2023-08-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 04e4e993 2023-08-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 04e4e993 2023-08-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 04e4e993 2023-08-14 op #include "config.h"
20 04e4e993 2023-08-14 op #include <sys/socket.h>
21 04e4e993 2023-08-14 op #include <sys/types.h>
22 04e4e993 2023-08-14 op #include <sys/un.h>
24 04e4e993 2023-08-14 op #include <ctype.h>
25 04e4e993 2023-08-14 op #include <errno.h>
26 04e4e993 2023-08-14 op #include <fnmatch.h>
27 04e4e993 2023-08-14 op #include <limits.h>
28 04e4e993 2023-08-14 op #include <locale.h>
29 04e4e993 2023-08-14 op #include <netdb.h>
30 04e4e993 2023-08-14 op #include <poll.h>
31 04e4e993 2023-08-14 op #include <signal.h>
32 04e4e993 2023-08-14 op #include <stdio.h>
33 04e4e993 2023-08-14 op #include <stdlib.h>
34 04e4e993 2023-08-14 op #include <string.h>
35 04e4e993 2023-08-14 op #include <syslog.h>
36 04e4e993 2023-08-14 op #include <unistd.h>
38 04e4e993 2023-08-14 op #include "amused.h"
39 3634fa70 2023-08-31 op #include "bufio.h"
40 80d5f5ad 2023-08-25 op #include "ev.h"
41 04e4e993 2023-08-14 op #include "http.h"
42 04e4e993 2023-08-14 op #include "log.h"
43 04e4e993 2023-08-14 op #include "playlist.h"
44 04e4e993 2023-08-14 op #include "xmalloc.h"
46 04e4e993 2023-08-14 op #ifndef nitems
47 04e4e993 2023-08-14 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
50 04e4e993 2023-08-14 op #define FORM_URLENCODED "application/x-www-form-urlencoded"
52 04e4e993 2023-08-14 op #define ICON_REPEAT_ALL "🔁"
53 04e4e993 2023-08-14 op #define ICON_REPEAT_ONE "🔂"
54 04e4e993 2023-08-14 op #define ICON_PREV "⏮"
55 04e4e993 2023-08-14 op #define ICON_NEXT "⏭"
56 04e4e993 2023-08-14 op #define ICON_STOP "⏹"
57 04e4e993 2023-08-14 op #define ICON_PAUSE "⏸"
58 04e4e993 2023-08-14 op #define ICON_TOGGLE "⏯"
59 04e4e993 2023-08-14 op #define ICON_PLAY "⏵"
61 04e4e993 2023-08-14 op static struct imsgbuf ibuf;
62 04e4e993 2023-08-14 op static const char *prefix = "";
63 04e4e993 2023-08-14 op static size_t prefixlen;
65 04e4e993 2023-08-14 op const char *head = "<!doctype html>"
68 04e4e993 2023-08-14 op "<meta name='viewport' content='width=device-width, initial-scale=1'/>"
69 04e4e993 2023-08-14 op "<title>Amused Web</title>"
71 04e4e993 2023-08-14 op "*{box-sizing:border-box}"
73 04e4e993 2023-08-14 op " padding: 0;"
78 04e4e993 2023-08-14 op " display: flex;"
79 04e4e993 2023-08-14 op " flex-direction: column;"
81 04e4e993 2023-08-14 op "button{cursor:pointer}"
83 04e4e993 2023-08-14 op " position: sticky;"
86 04e4e993 2023-08-14 op ".searchbox input{"
87 04e4e993 2023-08-14 op " width: 100%;"
88 04e4e993 2023-08-14 op " padding: 9px;"
90 04e4e993 2023-08-14 op ".playlist-wrapper{min-height:80vh}"
92 04e4e993 2023-08-14 op " list-style: none;"
93 04e4e993 2023-08-14 op " padding: 0;"
96 04e4e993 2023-08-14 op ".playlist button{"
97 04e4e993 2023-08-14 op " font-family: monospace;"
98 04e4e993 2023-08-14 op " text-align: left;"
99 04e4e993 2023-08-14 op " width: 100%;"
100 04e4e993 2023-08-14 op " padding: 5px;"
101 04e4e993 2023-08-14 op " border: 0;"
102 04e4e993 2023-08-14 op " background: transparent;"
103 04e4e993 2023-08-14 op " transition: background-color .25s ease-in-out;"
105 04e4e993 2023-08-14 op ".playlist button::before{"
106 04e4e993 2023-08-14 op " content: \"\";"
107 04e4e993 2023-08-14 op " width: 2ch;"
108 04e4e993 2023-08-14 op " display: inline-block;"
110 04e4e993 2023-08-14 op ".playlist button:hover{"
111 04e4e993 2023-08-14 op " background-color: #dfdddd;"
113 04e4e993 2023-08-14 op ".playlist #current button{"
114 04e4e993 2023-08-14 op " font-weight: bold;"
116 04e4e993 2023-08-14 op ".playlist #current button::before{"
117 04e4e993 2023-08-14 op " content: \"→ \";"
118 04e4e993 2023-08-14 op " font-weight: bold;"
121 04e4e993 2023-08-14 op " position: sticky;"
122 04e4e993 2023-08-14 op " width: 100%;"
123 04e4e993 2023-08-14 op " max-width: 800px;"
124 04e4e993 2023-08-14 op " margin: 0 auto;"
125 04e4e993 2023-08-14 op " bottom: 0;"
126 04e4e993 2023-08-14 op " background-color: white;"
127 04e4e993 2023-08-14 op " background: #3d3d3d;"
128 04e4e993 2023-08-14 op " color: white;"
129 04e4e993 2023-08-14 op " border-radius: 10px 10px 0 0;"
130 04e4e993 2023-08-14 op " padding: 10px;"
131 04e4e993 2023-08-14 op " text-align: center;"
134 04e4e993 2023-08-14 op ".controls p{"
135 04e4e993 2023-08-14 op " margin: .4rem;"
137 04e4e993 2023-08-14 op ".controls a{"
138 04e4e993 2023-08-14 op " color: white;"
140 04e4e993 2023-08-14 op ".controls .status{"
141 04e4e993 2023-08-14 op " font-size: 0.9rem;"
143 04e4e993 2023-08-14 op ".controls button{"
144 04e4e993 2023-08-14 op " margin: 5px;"
145 04e4e993 2023-08-14 op " padding: 5px 20px;"
147 04e4e993 2023-08-14 op ".mode-active{"
148 04e4e993 2023-08-14 op " color: #0064ff;"
154 04e4e993 2023-08-14 op const char *foot = "<script>"
155 04e4e993 2023-08-14 op "function cur(e) {"
156 04e4e993 2023-08-14 op " if (e) {e.preventDefault()}"
157 04e4e993 2023-08-14 op " let cur = document.querySelector('#current');"
158 04e4e993 2023-08-14 op " if (cur) {cur.scrollIntoView(); window.scrollBy(0, -100);}"
161 04e4e993 2023-08-14 op "document.querySelector('.controls a').addEventListener('click',cur)"
162 04e4e993 2023-08-14 op "</script></body></html>";
165 04e4e993 2023-08-14 op dial(const char *sock)
167 04e4e993 2023-08-14 op struct sockaddr_un sa;
171 04e4e993 2023-08-14 op memset(&sa, 0, sizeof(sa));
172 04e4e993 2023-08-14 op sa.sun_family = AF_UNIX;
173 04e4e993 2023-08-14 op len = strlcpy(sa.sun_path, sock, sizeof(sa.sun_path));
174 04e4e993 2023-08-14 op if (len >= sizeof(sa.sun_path))
175 04e4e993 2023-08-14 op err(1, "path too long: %s", sock);
177 04e4e993 2023-08-14 op if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
178 04e4e993 2023-08-14 op err(1, "socket");
179 04e4e993 2023-08-14 op if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
180 04e4e993 2023-08-14 op err(1, "failed to connect to %s", sock);
187 04e4e993 2023-08-14 op * Adapted from usr.sbin/httpd/httpd.c' url_decode.
190 04e4e993 2023-08-14 op url_decode(char *url)
193 04e4e993 2023-08-14 op char hex[3] = {0};
194 04e4e993 2023-08-14 op unsigned long x;
197 04e4e993 2023-08-14 op while (*p != '\0') {
198 04e4e993 2023-08-14 op switch (*p) {
200 04e4e993 2023-08-14 op /* Encoding character is followed by two hex chars */
201 04e4e993 2023-08-14 op if (!isxdigit((unsigned char)p[1]) ||
202 04e4e993 2023-08-14 op !isxdigit((unsigned char)p[2]) ||
203 04e4e993 2023-08-14 op (p[1] == '0' && p[2] == '0'))
206 04e4e993 2023-08-14 op hex[0] = p[1];
207 04e4e993 2023-08-14 op hex[1] = p[2];
210 04e4e993 2023-08-14 op * We don't have to validate "hex" because it is
211 04e4e993 2023-08-14 op * guaranteed to include two hex chars followed
214 04e4e993 2023-08-14 op x = strtoul(hex, NULL, 16);
215 04e4e993 2023-08-14 op *q = (char)x;
234 b478a4bd 2023-08-14 op unexpected_imsg(struct imsg *imsg, const char *expected)
236 b478a4bd 2023-08-14 op const char *msg;
237 b478a4bd 2023-08-14 op size_t datalen;
239 b478a4bd 2023-08-14 op if (imsg->hdr.type != IMSG_CTL_ERR) {
240 b478a4bd 2023-08-14 op log_warnx("got event %d while expecting %s",
241 b478a4bd 2023-08-14 op imsg->hdr.type, expected);
245 b478a4bd 2023-08-14 op datalen = IMSG_DATA_SIZE(*imsg);
246 b478a4bd 2023-08-14 op msg = imsg->data;
247 b478a4bd 2023-08-14 op if (datalen == 0 || msg[datalen - 1] != '\0')
248 b478a4bd 2023-08-14 op fatalx("malformed error message");
249 b478a4bd 2023-08-14 op log_warnx("failure: %s", msg);
253 3634fa70 2023-08-31 op route_notfound(struct client *clt)
255 3634fa70 2023-08-31 op if (http_reply(clt, 404, "Not Found", "text/plain") == -1 ||
256 3634fa70 2023-08-31 op http_writes(clt, "Page not found\n") == -1)
261 3634fa70 2023-08-31 op render_playlist(struct client *clt)
263 04e4e993 2023-08-14 op struct imsg imsg;
264 04e4e993 2023-08-14 op struct player_status ps;
266 04e4e993 2023-08-14 op const char *p;
267 04e4e993 2023-08-14 op int current, done;
269 04e4e993 2023-08-14 op imsg_compose(&ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
270 04e4e993 2023-08-14 op imsg_flush(&ibuf);
272 3634fa70 2023-08-31 op http_writes(clt, "<section class='playlist-wrapper'>");
273 3634fa70 2023-08-31 op http_writes(clt, "<form action=jump method=post"
274 04e4e993 2023-08-14 op " enctype='"FORM_URLENCODED"'>");
275 3634fa70 2023-08-31 op http_writes(clt, "<ul class=playlist>");
278 04e4e993 2023-08-14 op while (!done) {
279 04e4e993 2023-08-14 op if ((n = imsg_read(&ibuf)) == -1)
280 04e4e993 2023-08-14 op fatal("imsg_read");
282 04e4e993 2023-08-14 op fatalx("pipe closed");
285 04e4e993 2023-08-14 op if ((n = imsg_get(&ibuf, &imsg)) == -1)
286 04e4e993 2023-08-14 op fatal("imsg_get");
290 04e4e993 2023-08-14 op if (imsg.hdr.type != IMSG_CTL_SHOW) {
291 b478a4bd 2023-08-14 op unexpected_imsg(&imsg, "IMSG_CTL_SHOW");
292 04e4e993 2023-08-14 op imsg_free(&imsg);
296 04e4e993 2023-08-14 op if (IMSG_DATA_SIZE(imsg) == 0) {
301 04e4e993 2023-08-14 op if (IMSG_DATA_SIZE(imsg) != sizeof(ps))
302 04e4e993 2023-08-14 op fatalx("wrong size for seek ctl");
303 04e4e993 2023-08-14 op memcpy(&ps, imsg.data, sizeof(ps));
304 04e4e993 2023-08-14 op if (ps.path[sizeof(ps.path) - 1] != '\0')
305 04e4e993 2023-08-14 op fatalx("received corrupted data");
307 04e4e993 2023-08-14 op current = ps.status == STATE_PLAYING;
310 04e4e993 2023-08-14 op if (!strncmp(p, prefix, prefixlen))
311 04e4e993 2023-08-14 op p += prefixlen;
313 3634fa70 2023-08-31 op http_fmt(clt, "<li%s>",
314 04e4e993 2023-08-14 op current ? " id=current" : "");
315 3634fa70 2023-08-31 op http_writes(clt,
316 04e4e993 2023-08-14 op "<button type=submit name=jump value=\"");
317 3634fa70 2023-08-31 op http_htmlescape(clt, ps.path);
318 3634fa70 2023-08-31 op http_writes(clt, "\">");
319 3634fa70 2023-08-31 op http_htmlescape(clt, p);
320 3634fa70 2023-08-31 op http_writes(clt, "</button></li>");
322 04e4e993 2023-08-14 op imsg_free(&imsg);
326 3634fa70 2023-08-31 op http_writes(clt, "</ul>");
327 3634fa70 2023-08-31 op http_writes(clt, "</form>");
328 3634fa70 2023-08-31 op http_writes(clt, "</section>");
332 3634fa70 2023-08-31 op render_controls(struct client *clt)
334 04e4e993 2023-08-14 op struct imsg imsg;
335 04e4e993 2023-08-14 op struct player_status ps;
337 04e4e993 2023-08-14 op const char *oc, *ac, *p;
340 04e4e993 2023-08-14 op imsg_compose(&ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
341 04e4e993 2023-08-14 op imsg_flush(&ibuf);
343 04e4e993 2023-08-14 op if ((n = imsg_read(&ibuf)) == -1)
344 04e4e993 2023-08-14 op fatal("imsg_read");
346 04e4e993 2023-08-14 op fatalx("pipe closed");
348 04e4e993 2023-08-14 op if ((n = imsg_get(&ibuf, &imsg)) == -1)
349 04e4e993 2023-08-14 op fatal("imsg_get");
353 04e4e993 2023-08-14 op if (imsg.hdr.type != IMSG_CTL_STATUS) {
354 b478a4bd 2023-08-14 op unexpected_imsg(&imsg, "IMSG_CTL_STATUS");
357 04e4e993 2023-08-14 op if (IMSG_DATA_SIZE(imsg) != sizeof(ps))
358 04e4e993 2023-08-14 op fatalx("wrong size for IMSG_CTL_STATUS");
359 04e4e993 2023-08-14 op memcpy(&ps, imsg.data, sizeof(ps));
360 04e4e993 2023-08-14 op if (ps.path[sizeof(ps.path) - 1] != '\0')
361 04e4e993 2023-08-14 op fatalx("received corrupted data");
363 04e4e993 2023-08-14 op ac = ps.mode.repeat_all ? " class='mode-active'" : "";
364 04e4e993 2023-08-14 op oc = ps.mode.repeat_one ? " class='mode-active'" : "";
365 04e4e993 2023-08-14 op playing = ps.status == STATE_PLAYING;
367 04e4e993 2023-08-14 op if ((p = strrchr(ps.path, '/')) != NULL)
372 3634fa70 2023-08-31 op if (http_writes(clt, "<section class=controls>") == -1 ||
373 3634fa70 2023-08-31 op http_writes(clt, "<p><a href='#current'>") == -1 ||
374 3634fa70 2023-08-31 op http_htmlescape(clt, p) == -1 ||
375 3634fa70 2023-08-31 op http_writes(clt, "</a></p>") == -1 ||
376 3634fa70 2023-08-31 op http_writes(clt, "<form action=ctrls method=post"
377 04e4e993 2023-08-14 op " enctype='"FORM_URLENCODED"'>") == -1 ||
378 3634fa70 2023-08-31 op http_writes(clt, "<button type=submit name=ctl value=prev>"
379 04e4e993 2023-08-14 op ICON_PREV"</button>") == -1 ||
380 3634fa70 2023-08-31 op http_fmt(clt, "<button type=submit name=ctl value=%s>"
381 04e4e993 2023-08-14 op "%s</button>", playing ? "pause" : "play",
382 04e4e993 2023-08-14 op playing ? ICON_PAUSE : ICON_PLAY) == -1 ||
383 3634fa70 2023-08-31 op http_writes(clt, "<button type=submit name=ctl value=next>"
384 04e4e993 2023-08-14 op ICON_NEXT"</button>") == -1 ||
385 3634fa70 2023-08-31 op http_writes(clt, "</form>") == -1 ||
386 3634fa70 2023-08-31 op http_writes(clt, "<form action=mode method=post"
387 3634fa70 2023-08-31 op " enctype='"FORM_URLENCODED"'>") == -1 ||
388 3634fa70 2023-08-31 op http_fmt(clt, "<button%s type=submit name=mode value=all>"
389 04e4e993 2023-08-14 op ICON_REPEAT_ALL"</button>", ac) == -1 ||
390 3634fa70 2023-08-31 op http_fmt(clt, "<button%s type=submit name=mode value=one>"
391 04e4e993 2023-08-14 op ICON_REPEAT_ONE"</button>", oc) == -1 ||
392 3634fa70 2023-08-31 op http_writes(clt, "</form>") == -1 ||
393 3634fa70 2023-08-31 op http_writes(clt, "</section>") == -1)
397 04e4e993 2023-08-14 op imsg_free(&imsg);
401 3634fa70 2023-08-31 op route_home(struct client *clt)
403 3634fa70 2023-08-31 op if (http_reply(clt, 200, "OK", "text/html;charset=UTF-8") == -1)
406 3634fa70 2023-08-31 op if (http_write(clt, head, strlen(head)) == -1)
409 3634fa70 2023-08-31 op if (http_writes(clt, "<main>") == -1)
412 3634fa70 2023-08-31 op if (http_writes(clt, "<section class=searchbox>"
413 04e4e993 2023-08-14 op "<input type=search name=filter aria-label='Filter playlist'"
414 04e4e993 2023-08-14 op " placeholder='Filter playlist' id=search />"
415 04e4e993 2023-08-14 op "</section>") == -1)
418 3634fa70 2023-08-31 op render_controls(clt);
419 3634fa70 2023-08-31 op render_playlist(clt);
421 3634fa70 2023-08-31 op if (http_writes(clt, "</main>") == -1)
424 3634fa70 2023-08-31 op http_write(clt, foot, strlen(foot));
428 3634fa70 2023-08-31 op route_jump(struct client *clt)
430 04e4e993 2023-08-14 op struct imsg imsg;
431 04e4e993 2023-08-14 op struct player_status ps;
433 04e4e993 2023-08-14 op char path[PATH_MAX];
434 04e4e993 2023-08-14 op char *form, *field;
435 04e4e993 2023-08-14 op int found = 0;
437 3634fa70 2023-08-31 op form = clt->buf;
438 04e4e993 2023-08-14 op while ((field = strsep(&form, "&")) != NULL) {
439 04e4e993 2023-08-14 op if (url_decode(field) == -1)
442 04e4e993 2023-08-14 op if (strncmp(field, "jump=", 5) != 0)
447 99b28969 2023-08-14 op memset(&path, 0, sizeof(path));
448 04e4e993 2023-08-14 op if (strlcpy(path, field, sizeof(path)) >= sizeof(path))
451 04e4e993 2023-08-14 op log_warnx("path is %s", path);
452 04e4e993 2023-08-14 op imsg_compose(&ibuf, IMSG_CTL_JUMP, 0, 0, -1,
453 04e4e993 2023-08-14 op path, sizeof(path));
454 04e4e993 2023-08-14 op imsg_flush(&ibuf);
456 04e4e993 2023-08-14 op if ((n = imsg_read(&ibuf)) == -1)
457 04e4e993 2023-08-14 op fatal("imsg_read");
459 04e4e993 2023-08-14 op fatalx("pipe closed");
462 04e4e993 2023-08-14 op if ((n = imsg_get(&ibuf, &imsg)) == -1)
463 04e4e993 2023-08-14 op fatal("imsg_get");
467 04e4e993 2023-08-14 op if (imsg.hdr.type != IMSG_CTL_STATUS) {
468 b478a4bd 2023-08-14 op unexpected_imsg(&imsg, "IMSG_CTL_STATUS");
469 04e4e993 2023-08-14 op imsg_free(&imsg);
473 04e4e993 2023-08-14 op if (IMSG_DATA_SIZE(imsg) != sizeof(ps))
474 04e4e993 2023-08-14 op fatalx("data size mismatch");
475 04e4e993 2023-08-14 op memcpy(&ps, imsg.data, sizeof(ps));
476 04e4e993 2023-08-14 op if (ps.path[sizeof(ps.path) - 1] != '\0')
477 04e4e993 2023-08-14 op fatalx("received corrupted data");
478 04e4e993 2023-08-14 op log_debug("jumped to %s", ps.path);
487 3634fa70 2023-08-31 op http_reply(clt, 302, "See Other", "/");
491 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", "text/plain");
492 3634fa70 2023-08-31 op http_writes(clt, "Bad Request.\n");
496 3634fa70 2023-08-31 op route_controls(struct client *clt)
498 04e4e993 2023-08-14 op char *form, *field;
499 04e4e993 2023-08-14 op int cmd, found = 0;
501 3634fa70 2023-08-31 op form = clt->buf;
502 04e4e993 2023-08-14 op while ((field = strsep(&form, "&")) != NULL) {
503 04e4e993 2023-08-14 op if (url_decode(field) == -1)
506 04e4e993 2023-08-14 op if (strncmp(field, "ctl=", 4) != 0)
511 04e4e993 2023-08-14 op if (!strcmp(field, "play"))
512 04e4e993 2023-08-14 op cmd = IMSG_CTL_PLAY;
513 04e4e993 2023-08-14 op else if (!strcmp(field, "pause"))
514 04e4e993 2023-08-14 op cmd = IMSG_CTL_PAUSE;
515 04e4e993 2023-08-14 op else if (!strcmp(field, "next"))
516 04e4e993 2023-08-14 op cmd = IMSG_CTL_NEXT;
517 04e4e993 2023-08-14 op else if (!strcmp(field, "prev"))
518 04e4e993 2023-08-14 op cmd = IMSG_CTL_PREV;
522 04e4e993 2023-08-14 op imsg_compose(&ibuf, cmd, 0, 0, -1, NULL, 0);
523 04e4e993 2023-08-14 op imsg_flush(&ibuf);
530 3634fa70 2023-08-31 op http_reply(clt, 302, "See Other", "/");
534 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", "text/plain");
535 3634fa70 2023-08-31 op http_writes(clt, "Bad Request.\n");
539 3634fa70 2023-08-31 op route_mode(struct client *clt)
541 04e4e993 2023-08-14 op char *form, *field;
542 04e4e993 2023-08-14 op int found = 0;
544 04e4e993 2023-08-14 op struct player_status ps;
545 04e4e993 2023-08-14 op struct player_mode pm;
546 04e4e993 2023-08-14 op struct imsg imsg;
548 04e4e993 2023-08-14 op pm.repeat_one = pm.repeat_all = pm.consume = MODE_UNDEF;
550 3634fa70 2023-08-31 op form = clt->buf;
551 04e4e993 2023-08-14 op while ((field = strsep(&form, "&")) != NULL) {
552 04e4e993 2023-08-14 op if (url_decode(field) == -1)
555 04e4e993 2023-08-14 op if (strncmp(field, "mode=", 5) != 0)
560 04e4e993 2023-08-14 op if (!strcmp(field, "all"))
561 04e4e993 2023-08-14 op pm.repeat_all = MODE_TOGGLE;
562 04e4e993 2023-08-14 op else if (!strcmp(field, "one"))
563 04e4e993 2023-08-14 op pm.repeat_one = MODE_TOGGLE;
567 04e4e993 2023-08-14 op imsg_compose(&ibuf, IMSG_CTL_MODE, 0, 0, -1, &pm, sizeof(pm));
568 04e4e993 2023-08-14 op imsg_flush(&ibuf);
570 04e4e993 2023-08-14 op if ((n = imsg_read(&ibuf)) == -1)
571 04e4e993 2023-08-14 op fatal("imsg_read");
573 04e4e993 2023-08-14 op fatalx("pipe closed");
576 04e4e993 2023-08-14 op if ((n = imsg_get(&ibuf, &imsg)) == -1)
577 04e4e993 2023-08-14 op fatal("imsg_get");
581 04e4e993 2023-08-14 op if (imsg.hdr.type != IMSG_CTL_STATUS) {
582 b478a4bd 2023-08-14 op unexpected_imsg(&imsg, "IMSG_CTL_STATUS");
583 04e4e993 2023-08-14 op imsg_free(&imsg);
587 04e4e993 2023-08-14 op if (IMSG_DATA_SIZE(imsg) != sizeof(ps))
588 04e4e993 2023-08-14 op fatalx("data size mismatch");
589 04e4e993 2023-08-14 op memcpy(&ps, imsg.data, sizeof(ps));
590 04e4e993 2023-08-14 op if (ps.path[sizeof(ps.path) - 1] != '\0')
591 04e4e993 2023-08-14 op fatalx("received corrupted data");
600 3634fa70 2023-08-31 op http_reply(clt, 302, "See Other", "/");
604 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", "text/plain");
605 3634fa70 2023-08-31 op http_writes(clt, "Bad Request.\n");
609 3634fa70 2023-08-31 op route_dispatch(struct client *clt)
611 04e4e993 2023-08-14 op static const struct route {
613 3634fa70 2023-08-31 op const char *path;
614 3634fa70 2023-08-31 op route_fn route;
615 04e4e993 2023-08-14 op } routes[] = {
616 04e4e993 2023-08-14 op { METHOD_GET, "/", &route_home },
617 04e4e993 2023-08-14 op { METHOD_POST, "/jump", &route_jump },
618 04e4e993 2023-08-14 op { METHOD_POST, "/ctrls", &route_controls },
619 04e4e993 2023-08-14 op { METHOD_POST, "/mode", &route_mode },
621 04e4e993 2023-08-14 op { METHOD_GET, "*", &route_notfound },
622 04e4e993 2023-08-14 op { METHOD_POST, "*", &route_notfound },
624 3634fa70 2023-08-31 op struct request *req = &clt->req;
627 04e4e993 2023-08-14 op if ((req->method != METHOD_GET && req->method != METHOD_POST) ||
628 04e4e993 2023-08-14 op (req->ctype != NULL && strcmp(req->ctype, FORM_URLENCODED) != 0) ||
629 04e4e993 2023-08-14 op req->path == NULL) {
630 3634fa70 2023-08-31 op http_reply(clt, 400, "Bad Request", NULL);
634 04e4e993 2023-08-14 op for (i = 0; i < nitems(routes); ++i) {
635 04e4e993 2023-08-14 op if (req->method != routes[i].method ||
636 04e4e993 2023-08-14 op fnmatch(routes[i].path, req->path, 0) != 0)
638 3634fa70 2023-08-31 op clt->done = 1; /* assume with one round is done */
639 3634fa70 2023-08-31 op clt->route = routes[i].route;
640 3634fa70 2023-08-31 op clt->route(clt);
641 3634fa70 2023-08-31 op if (clt->done)
642 3634fa70 2023-08-31 op http_close(clt);
648 3634fa70 2023-08-31 op client_ev(int fd, int ev, void *d)
650 3634fa70 2023-08-31 op struct client *clt = d;
652 3634fa70 2023-08-31 op if (ev & (POLLIN|POLLHUP)) {
653 3634fa70 2023-08-31 op if (bufio_read(&clt->bio) == -1 && errno != EAGAIN) {
654 3634fa70 2023-08-31 op log_warn("bufio_read");
659 3634fa70 2023-08-31 op if (ev & POLLOUT) {
660 3634fa70 2023-08-31 op if (bufio_write(&clt->bio) == -1 && errno != EAGAIN) {
661 3634fa70 2023-08-31 op log_warn("bufio_read");
666 3634fa70 2023-08-31 op if (clt->route == NULL) {
667 3634fa70 2023-08-31 op if (http_parse(clt) == -1) {
668 3634fa70 2023-08-31 op if (errno == EAGAIN)
670 3634fa70 2023-08-31 op log_warnx("HTTP parse request failed");
673 3634fa70 2023-08-31 op if (clt->req.method == METHOD_POST &&
674 3634fa70 2023-08-31 op http_read(clt) == -1) {
675 3634fa70 2023-08-31 op if (errno == EAGAIN)
677 3634fa70 2023-08-31 op log_warnx("failed to read POST data");
680 3634fa70 2023-08-31 op route_dispatch(clt);
684 3634fa70 2023-08-31 op if (!clt->done)
685 3634fa70 2023-08-31 op clt->route(clt);
688 3634fa70 2023-08-31 op ev = bufio_pollev(&clt->bio);
689 3634fa70 2023-08-31 op if (ev == POLLIN && clt->done) {
690 3634fa70 2023-08-31 op goto err; /* done with this client */
693 3634fa70 2023-08-31 op ev_add(fd, ev, client_ev, clt);
698 3634fa70 2023-08-31 op http_free(clt);
702 703c260b 2023-08-25 op web_accept(int psock, int ev, void *d)
704 3634fa70 2023-08-31 op struct client *clt;
707 04e4e993 2023-08-14 op if ((sock = accept(psock, NULL, NULL)) == -1) {
708 04e4e993 2023-08-14 op warn("accept");
711 3634fa70 2023-08-31 op clt = xcalloc(1, sizeof(*clt));
712 3634fa70 2023-08-31 op if ((clt = calloc(1, sizeof(*clt))) == NULL ||
713 3634fa70 2023-08-31 op http_init(clt, sock) == -1) {
714 3634fa70 2023-08-31 op log_warn("failed to initialize client");
720 3634fa70 2023-08-31 op client_ev(sock, POLLIN, clt);
727 04e4e993 2023-08-14 op fprintf(stderr, "usage: %s [-v] [-s sock] [-t prefix] [[host] port]\n",
728 04e4e993 2023-08-14 op getprogname());
733 04e4e993 2023-08-14 op main(int argc, char **argv)
735 04e4e993 2023-08-14 op struct addrinfo hints, *res, *res0;
736 04e4e993 2023-08-14 op const char *cause = NULL;
737 04e4e993 2023-08-14 op const char *host = NULL;
738 04e4e993 2023-08-14 op const char *port = "9090";
739 04e4e993 2023-08-14 op char *sock = NULL;
740 80d5f5ad 2023-08-25 op size_t nsock, error, save_errno;
741 80d5f5ad 2023-08-25 op int ch, v, amused_sock, fd;
742 04e4e993 2023-08-14 op int verbose = 0;
744 04e4e993 2023-08-14 op setlocale(LC_ALL, NULL);
746 04e4e993 2023-08-14 op log_init(1, LOG_DAEMON);
748 04e4e993 2023-08-14 op if (pledge("stdio rpath unix inet dns", NULL) == -1)
749 04e4e993 2023-08-14 op err(1, "pledge");
751 04e4e993 2023-08-14 op while ((ch = getopt(argc, argv, "s:t:v")) != -1) {
752 04e4e993 2023-08-14 op switch (ch) {
754 04e4e993 2023-08-14 op sock = optarg;
757 04e4e993 2023-08-14 op prefix = optarg;
758 04e4e993 2023-08-14 op prefixlen = strlen(prefix);
767 04e4e993 2023-08-14 op argc -= optind;
768 04e4e993 2023-08-14 op argv += optind;
770 04e4e993 2023-08-14 op if (argc == 1)
771 04e4e993 2023-08-14 op port = argv[0];
772 04e4e993 2023-08-14 op if (argc == 2) {
773 04e4e993 2023-08-14 op host = argv[0];
774 04e4e993 2023-08-14 op port = argv[1];
776 04e4e993 2023-08-14 op if (argc > 2)
779 04e4e993 2023-08-14 op log_setverbose(verbose);
781 04e4e993 2023-08-14 op if (sock == NULL)
782 04e4e993 2023-08-14 op xasprintf(&sock, "/tmp/amused-%d", getuid());
784 04e4e993 2023-08-14 op signal(SIGPIPE, SIG_IGN);
786 80d5f5ad 2023-08-25 op if (ev_init() == -1)
787 80d5f5ad 2023-08-25 op fatal("ev_init");
789 04e4e993 2023-08-14 op amused_sock = dial(sock);
790 04e4e993 2023-08-14 op imsg_init(&ibuf, amused_sock);
792 04e4e993 2023-08-14 op memset(&hints, 0, sizeof(hints));
793 04e4e993 2023-08-14 op hints.ai_family = AF_UNSPEC;
794 04e4e993 2023-08-14 op hints.ai_socktype = SOCK_STREAM;
795 04e4e993 2023-08-14 op hints.ai_flags = AI_PASSIVE;
796 04e4e993 2023-08-14 op error = getaddrinfo(host, port, &hints, &res0);
798 04e4e993 2023-08-14 op errx(1, "%s", gai_strerror(error));
801 80d5f5ad 2023-08-25 op for (res = res0; res; res = res->ai_next) {
802 80d5f5ad 2023-08-25 op fd = socket(res->ai_family, res->ai_socktype,
803 04e4e993 2023-08-14 op res->ai_protocol);
804 80d5f5ad 2023-08-25 op if (fd == -1) {
805 04e4e993 2023-08-14 op cause = "socket";
810 80d5f5ad 2023-08-25 op if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
811 04e4e993 2023-08-14 op &v, sizeof(v)) == -1)
812 04e4e993 2023-08-14 op fatal("setsockopt(SO_REUSEADDR)");
814 80d5f5ad 2023-08-25 op if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
815 04e4e993 2023-08-14 op cause = "bind";
816 04e4e993 2023-08-14 op save_errno = errno;
818 04e4e993 2023-08-14 op errno = save_errno;
822 80d5f5ad 2023-08-25 op if (listen(fd, 5) == -1)
823 04e4e993 2023-08-14 op err(1, "listen");
825 703c260b 2023-08-25 op if (ev_add(fd, POLLIN, web_accept, NULL) == -1)
826 80d5f5ad 2023-08-25 op fatal("ev_add");
829 04e4e993 2023-08-14 op if (nsock == 0)
830 04e4e993 2023-08-14 op err(1, "%s", cause);
831 04e4e993 2023-08-14 op freeaddrinfo(res0);
833 04e4e993 2023-08-14 op if (pledge("stdio inet", NULL) == -1)
834 04e4e993 2023-08-14 op err(1, "pledge");
836 04e4e993 2023-08-14 op log_info("starting");