1 5e11c00c 2021-03-02 op #include "telescope.h"
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <event.h>
8 5e11c00c 2021-03-02 op #include <signal.h>
9 5e11c00c 2021-03-02 op #include <stdio.h>
10 5e11c00c 2021-03-02 op #include <stdlib.h>
11 5e11c00c 2021-03-02 op #include <string.h>
12 5e11c00c 2021-03-02 op #include <unistd.h>
14 5e11c00c 2021-03-02 op struct event imsgev;
15 5e11c00c 2021-03-02 op struct tabshead tabshead;
17 c07ac996 2021-03-12 op /* the first is also the fallback one */
18 c07ac996 2021-03-12 op static struct proto protos[] = {
19 4d3785b1 2021-03-09 op { "gemini:", load_gemini_url },
20 4d3785b1 2021-03-09 op { "about:", load_about_url },
21 4d3785b1 2021-03-09 op { NULL, NULL },
24 1b8a4bbf 2021-03-12 op static struct imsgbuf *netibuf;
26 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
27 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
28 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
29 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
30 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
33 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
34 2051e653 2021-03-13 op static void dispatch_imsg(int, short, void*);
35 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
36 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
38 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
39 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
40 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
41 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
42 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
43 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
44 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
47 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
50 5e11c00c 2021-03-02 op abort(); /* TODO */
53 5e11c00c 2021-03-02 op static struct tab *
54 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
56 5e11c00c 2021-03-02 op struct tab *t;
58 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
59 5e11c00c 2021-03-02 op if (t->id == id)
67 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
69 3a9b9365 2021-03-09 op struct tab *tab;
72 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
74 3a9b9365 2021-03-09 op page = imsg->data;
75 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
77 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
78 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
80 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
85 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
87 5e11c00c 2021-03-02 op int tofu_res = 1;
89 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
90 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
93 5cd2ebb1 2021-03-11 op static inline int
94 5cd2ebb1 2021-03-11 op normalize_code(int n)
97 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
100 5cd2ebb1 2021-03-11 op } else if (n < 30) {
102 5cd2ebb1 2021-03-11 op } else if (n < 40) {
103 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
106 5cd2ebb1 2021-03-11 op } else if (n < 50) {
110 5cd2ebb1 2021-03-11 op } else if (n < 60) {
111 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
114 5cd2ebb1 2021-03-11 op } else if (n < 70) {
119 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
123 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
125 0972d8b2 2021-03-02 op struct tab *tab;
127 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
129 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
132 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
133 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
134 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
135 0972d8b2 2021-03-02 op tab->redirect_count = 0;
139 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
141 0972d8b2 2021-03-02 op struct tab *tab;
143 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
145 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
148 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
150 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
151 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
152 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
153 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
154 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
155 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
156 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
157 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
158 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
160 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
162 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
163 3a9b9365 2021-03-09 op tab->redirect_count++;
165 3a9b9365 2021-03-09 op /* TODO: make customizable? */
166 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
167 3a9b9365 2021-03-09 op load_page_from_str(tab,
168 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
170 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
171 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
172 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
177 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
179 0972d8b2 2021-03-02 op struct tab *tab;
181 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
183 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
186 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
190 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
192 a5c3e03d 2021-03-02 op struct tab *t;
194 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
195 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
198 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
199 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
203 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
205 5e11c00c 2021-03-02 op struct imsg imsg;
206 5e11c00c 2021-03-02 op size_t datalen;
209 1b8a4bbf 2021-03-12 op if ((n = imsg_read(netibuf)) == -1) {
210 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
215 5e11c00c 2021-03-02 op if (n == 0) {
216 5e11c00c 2021-03-02 op fprintf(stderr, "other side is dead\n");
221 1b8a4bbf 2021-03-12 op if ((n = imsg_get(netibuf, &imsg)) == -1)
225 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
226 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
227 5e11c00c 2021-03-02 op imsg_free(&imsg);
232 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
234 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
235 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
237 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
239 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
240 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
244 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
250 4d3785b1 2021-03-09 op memset(&tab->url, 0, sizeof(tab->url));
252 4d3785b1 2021-03-09 op m = strchr(url, ':');
253 4d3785b1 2021-03-09 op strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
254 4d3785b1 2021-03-09 op strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
256 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
257 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
259 4d3785b1 2021-03-09 op if (!strcmp(url, "about:new"))
260 0972d8b2 2021-03-02 op load_page_from_str(tab, about_new);
262 4d3785b1 2021-03-09 op load_page_from_str(tab, "# not found\n");
266 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
268 4d3785b1 2021-03-09 op const char *err;
272 4d3785b1 2021-03-09 op if (has_prefix(url, "gemini:")) {
273 4d3785b1 2021-03-09 op if (!url_parse(url, &tab->url, &err))
276 4d3785b1 2021-03-09 op if (!url_resolve_from(&tab->url, url, &err))
280 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
281 2051e653 2021-03-13 op url_unparse(&tab->url, tab->hist_cur->h, len);
282 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
283 2051e653 2021-03-13 op tab->hist_cur->h, len+1);
284 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
288 4d3785b1 2021-03-09 op if (asprintf(&p, "#error loading %s\n>%s\n",
289 4d3785b1 2021-03-09 op url, err) == -1)
291 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
292 4d3785b1 2021-03-09 op load_page_from_str(tab, p);
297 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
299 4d3785b1 2021-03-09 op struct proto *p;
301 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
302 4d3785b1 2021-03-09 op if (has_prefix(url, p->schema)) {
303 4d3785b1 2021-03-09 op p->loadfn(tab, url);
308 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
312 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
314 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
315 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
317 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
318 2051e653 2021-03-13 op event_loopbreak();
321 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
322 2051e653 2021-03-13 op do_load_url(tab, url);
326 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
328 2051e653 2021-03-13 op struct hist *h;
330 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
332 2051e653 2021-03-13 op tab->hist_cur = h;
333 2051e653 2021-03-13 op do_load_url(tab, h->h);
338 2051e653 2021-03-13 op load_next_page(struct tab *tab)
340 2051e653 2021-03-13 op struct hist *h;
342 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
344 2051e653 2021-03-13 op tab->hist_cur = h;
345 2051e653 2021-03-13 op do_load_url(tab, h->h);
350 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
352 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
353 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
359 5e11c00c 2021-03-02 op struct imsgbuf main_ibuf, network_ibuf;
360 5e11c00c 2021-03-02 op int imsg_fds[2];
362 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
364 5e11c00c 2021-03-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
365 5e11c00c 2021-03-02 op err(1, "socketpair");
367 5e11c00c 2021-03-02 op switch (fork()) {
369 5e11c00c 2021-03-02 op err(1, "fork");
372 5e11c00c 2021-03-02 op setproctitle("client");
373 5e11c00c 2021-03-02 op close(imsg_fds[0]);
374 5e11c00c 2021-03-02 op imsg_init(&network_ibuf, imsg_fds[1]);
375 5e11c00c 2021-03-02 op exit(client_main(&network_ibuf));
378 5e11c00c 2021-03-02 op close(imsg_fds[1]);
379 5e11c00c 2021-03-02 op imsg_init(&main_ibuf, imsg_fds[0]);
380 1b8a4bbf 2021-03-12 op netibuf = &main_ibuf;
382 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
384 5e11c00c 2021-03-02 op event_init();
386 1b8a4bbf 2021-03-12 op event_set(&imsgev, netibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, netibuf);
387 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
391 5e11c00c 2021-03-02 op event_dispatch();
393 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
394 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);