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 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
19 5e11c00c 2021-03-02 op static void handle_imsg_err(struct imsg*, size_t);
20 5e11c00c 2021-03-02 op static void handle_imsg_check_cert(struct imsg*, size_t);
21 5e11c00c 2021-03-02 op static void handle_imsg_got_code(struct imsg*, size_t);
22 5e11c00c 2021-03-02 op static void handle_imsg_got_meta(struct imsg*, size_t);
23 5e11c00c 2021-03-02 op static void handle_imsg_buf(struct imsg*, size_t);
24 5e11c00c 2021-03-02 op static void handle_imsg_eof(struct imsg*, size_t);
26 0972d8b2 2021-03-02 op static void load_page_from_str(struct tab*, const char*);
28 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
29 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
30 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
31 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
32 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
33 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
34 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
37 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
40 5e11c00c 2021-03-02 op abort(); /* TODO */
43 5e11c00c 2021-03-02 op static struct tab *
44 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
46 5e11c00c 2021-03-02 op struct tab *t;
48 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
49 5e11c00c 2021-03-02 op if (t->id == id)
57 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
59 3a9b9365 2021-03-09 op struct tab *tab;
62 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
64 3a9b9365 2021-03-09 op page = imsg->data;
65 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
67 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
68 3a9b9365 2021-03-09 op tab->url, page) == -1)
70 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
75 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
77 5e11c00c 2021-03-02 op int tofu_res = 1;
79 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
80 5e11c00c 2021-03-02 op imsg_flush(ibuf);
84 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
86 0972d8b2 2021-03-02 op const char *errpage;
87 0972d8b2 2021-03-02 op struct tab *tab;
89 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
91 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
93 0972d8b2 2021-03-02 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
95 0972d8b2 2021-03-02 op if (tab->code < 20) {
96 0972d8b2 2021-03-02 op if (tab->code != 10 && tab->code != 11)
97 0972d8b2 2021-03-02 op tab->code = 10;
98 0972d8b2 2021-03-02 op } else if (tab->code < 30)
99 0972d8b2 2021-03-02 op tab->code = 20;
100 0972d8b2 2021-03-02 op else if (tab->code < 40)
101 0972d8b2 2021-03-02 op tab->code = 30;
102 0972d8b2 2021-03-02 op else if (tab->code < 50)
103 0972d8b2 2021-03-02 op tab->code = 40;
104 0972d8b2 2021-03-02 op else if (tab->code < 60)
105 0972d8b2 2021-03-02 op tab->code = 50;
107 0972d8b2 2021-03-02 op tab->code = 60;
109 0972d8b2 2021-03-02 op if (tab->code != 30)
110 0972d8b2 2021-03-02 op tab->redirect_count = 0;
114 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
116 0972d8b2 2021-03-02 op struct tab *tab;
118 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
120 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
122 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
124 3a9b9365 2021-03-09 op if (tab->code != 30)
125 3a9b9365 2021-03-09 op tab->redirect_count = 0;
127 0972d8b2 2021-03-02 op if (tab->code == 20) {
128 3a9b9365 2021-03-09 op /* TODO: parse the MIME type */
129 3a9b9365 2021-03-09 op gemtext_initparser(&tab->page);
130 0972d8b2 2021-03-02 op imsg_compose(ibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
131 0972d8b2 2021-03-02 op imsg_flush(ibuf);
135 3a9b9365 2021-03-09 op if (tab->code == 30) {
136 3a9b9365 2021-03-09 op tab->redirect_count++;
138 3a9b9365 2021-03-09 op /* TODO: make customizable? */
139 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
140 3a9b9365 2021-03-09 op load_page_from_str(tab,
141 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
145 3a9b9365 2021-03-09 op load_url(tab, tab->meta);
149 3a9b9365 2021-03-09 op /* 4x, 5x or 6x */
150 3a9b9365 2021-03-09 op load_page_from_str(tab, err_pages[tab->code]);
154 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
156 0972d8b2 2021-03-02 op struct tab *tab;
158 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
160 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
163 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
167 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
169 a5c3e03d 2021-03-02 op struct tab *t;
171 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
172 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
175 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
176 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
180 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
182 5e11c00c 2021-03-02 op struct imsg imsg;
183 5e11c00c 2021-03-02 op size_t datalen;
186 5e11c00c 2021-03-02 op if ((n = imsg_read(ibuf)) == -1) {
187 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
192 5e11c00c 2021-03-02 op if (n == 0) {
193 5e11c00c 2021-03-02 op fprintf(stderr, "other side is dead\n");
198 5e11c00c 2021-03-02 op if ((n = imsg_get(ibuf, &imsg)) == -1)
202 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
203 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
204 5e11c00c 2021-03-02 op imsg_free(&imsg);
209 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
211 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
212 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
214 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
216 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
217 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
221 0972d8b2 2021-03-02 op load_url(struct tab *tab, const char *url)
223 3a9b9365 2021-03-09 op /* TODO: parsing the url here is a bit ugly. maybe store it
224 3a9b9365 2021-03-09 op * in the struct tab? */
225 3a9b9365 2021-03-09 op struct url u;
226 3a9b9365 2021-03-09 op const char *err;
229 0972d8b2 2021-03-02 op if (!strcmp(url, "about:new")) {
230 3a9b9365 2021-03-09 op strlcpy(tab->url, url, sizeof(tab->url));
231 0972d8b2 2021-03-02 op load_page_from_str(tab, about_new);
235 3a9b9365 2021-03-09 op if (has_prefix(tab->url, "about:")) {
236 3a9b9365 2021-03-09 op strlcpy(tab->url, url, sizeof(tab->url));
238 3a9b9365 2021-03-09 op if (!url_parse(tab->url, &u, &err))
241 3a9b9365 2021-03-09 op if (!url_resolve_from(&u, url, &err))
244 3a9b9365 2021-03-09 op /* TODO: this is nowhere good enough */
245 3a9b9365 2021-03-09 op strlcpy(tab->url, u.scheme, sizeof(tab->url));
246 3a9b9365 2021-03-09 op strlcat(tab->url, "://", sizeof(tab->url));
247 3a9b9365 2021-03-09 op strlcat(tab->url, u.host, sizeof(tab->url));
248 3a9b9365 2021-03-09 op strlcat(tab->url, "/", sizeof(tab->url));
249 3a9b9365 2021-03-09 op strlcat(tab->url, u.path, sizeof(tab->url));
252 0972d8b2 2021-03-02 op imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1, url, strlen(url)+1);
253 0972d8b2 2021-03-02 op imsg_flush(ibuf);
257 3a9b9365 2021-03-09 op if (asprintf(&page, "# error resolving %s from %s\n\n> %s\n",
258 3a9b9365 2021-03-09 op url, tab->url, err) == -1)
260 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
267 5e11c00c 2021-03-02 op struct imsgbuf main_ibuf, network_ibuf;
268 5e11c00c 2021-03-02 op int imsg_fds[2];
270 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
272 5e11c00c 2021-03-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
273 5e11c00c 2021-03-02 op err(1, "socketpair");
275 5e11c00c 2021-03-02 op switch (fork()) {
277 5e11c00c 2021-03-02 op err(1, "fork");
280 5e11c00c 2021-03-02 op setproctitle("client");
281 5e11c00c 2021-03-02 op close(imsg_fds[0]);
282 5e11c00c 2021-03-02 op imsg_init(&network_ibuf, imsg_fds[1]);
283 5e11c00c 2021-03-02 op exit(client_main(&network_ibuf));
286 5e11c00c 2021-03-02 op close(imsg_fds[1]);
287 5e11c00c 2021-03-02 op imsg_init(&main_ibuf, imsg_fds[0]);
288 5e11c00c 2021-03-02 op ibuf = &main_ibuf;
290 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
292 5e11c00c 2021-03-02 op event_init();
294 5e11c00c 2021-03-02 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
295 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
299 5e11c00c 2021-03-02 op event_dispatch();
301 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
302 5e11c00c 2021-03-02 op imsg_flush(ibuf);