3 #include <sys/socket.h>
14 struct tabshead tabshead;
16 /* the first is also the fallback one */
17 static struct proto protos[] = {
18 { "gemini:", load_gemini_url },
19 { "about:", load_about_url },
23 static struct imsgbuf *netibuf;
25 static void die(void) __attribute__((__noreturn__));
26 static struct tab *tab_by_id(uint32_t);
27 static void handle_imsg_err(struct imsg*, size_t);
28 static void handle_imsg_check_cert(struct imsg*, size_t);
29 static void handle_imsg_got_code(struct imsg*, size_t);
30 static void handle_imsg_got_meta(struct imsg*, size_t);
31 static void handle_imsg_buf(struct imsg*, size_t);
32 static void handle_imsg_eof(struct imsg*, size_t);
33 static void dispatch_imsg(int, short, void*);
34 static void load_page_from_str(struct tab*, const char*);
35 static void do_load_url(struct tab*, const char*);
37 static imsg_handlerfn *handlers[] = {
38 [IMSG_ERR] = handle_imsg_err,
39 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
40 [IMSG_GOT_CODE] = handle_imsg_got_code,
41 [IMSG_GOT_META] = handle_imsg_got_meta,
42 [IMSG_BUF] = handle_imsg_buf,
43 [IMSG_EOF] = handle_imsg_eof,
46 static void __attribute__((__noreturn__))
53 tab_by_id(uint32_t id)
57 TAILQ_FOREACH(t, &tabshead, tabs) {
66 handle_imsg_err(struct imsg *imsg, size_t datalen)
71 tab = tab_by_id(imsg->hdr.peerid);
74 page[datalen-1] = '\0';
76 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
77 tab->hist_cur->h, page) == -1)
79 load_page_from_str(tab, page);
84 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
88 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
96 if (n == 10 || n == 11)
102 if (n == 30 || n == 31)
110 if (n <= 53 || n == 59)
118 return MALFORMED_RESPONSE;
122 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
126 tab = tab_by_id(imsg->hdr.peerid);
128 if (sizeof(tab->code) != datalen)
131 memcpy(&tab->code, imsg->data, sizeof(tab->code));
132 tab->code = normalize_code(tab->code);
133 if (tab->code != 30 && tab->code != 31)
134 tab->redirect_count = 0;
138 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
142 tab = tab_by_id(imsg->hdr.peerid);
144 if (sizeof(tab->meta) <= datalen)
147 memcpy(tab->meta, imsg->data, datalen);
149 if (tab->code < 10) { /* internal errors */
150 load_page_from_str(tab, err_pages[tab->code]);
151 } else if (tab->code < 20) { /* 1x */
152 load_page_from_str(tab, err_pages[tab->code]);
153 ui_require_input(tab, tab->code == 11);
154 } else if (tab->code == 20) {
155 if (setup_parser_for(tab)) {
156 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
159 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
161 } else if (tab->code < 40) { /* 3x */
162 tab->redirect_count++;
164 /* TODO: make customizable? */
165 if (tab->redirect_count > 5) {
166 load_page_from_str(tab,
167 err_pages[TOO_MUCH_REDIRECTS]);
169 do_load_url(tab, tab->meta);
170 } else { /* 4x, 5x & 6x */
171 load_page_from_str(tab, err_pages[tab->code]);
176 handle_imsg_buf(struct imsg *imsg, size_t datalen)
180 tab = tab_by_id(imsg->hdr.peerid);
182 if (!tab->page.parse(&tab->page, imsg->data, datalen))
185 ui_on_tab_refresh(tab);
189 handle_imsg_eof(struct imsg *imsg, size_t datalen)
193 t = tab_by_id(imsg->hdr.peerid);
194 if (!t->page.free(&t->page))
197 ui_on_tab_refresh(t);
202 dispatch_imsg(int fd, short ev, void *d)
208 if ((n = imsg_read(netibuf)) == -1) {
209 if (errno == EAGAIN || errno == EWOULDBLOCK)
215 fprintf(stderr, "other side is dead\n");
220 if ((n = imsg_get(netibuf, &imsg)) == -1)
224 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
225 handlers[imsg.hdr.type](&imsg, datalen);
231 load_page_from_str(struct tab *tab, const char *page)
233 gemtext_initparser(&tab->page);
234 if (!tab->page.parse(&tab->page, page, strlen(page)))
236 if (!tab->page.free(&tab->page))
238 ui_on_tab_refresh(tab);
239 ui_on_tab_loaded(tab);
243 load_about_url(struct tab *tab, const char *url)
249 memset(&tab->url, 0, sizeof(tab->url));
251 m = strchr(url, ':');
252 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
253 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
255 len = sizeof(tab->hist_cur->h)-1;
256 strlcpy(tab->hist_cur->h, url, len);
258 if (!strcmp(url, "about:new"))
259 load_page_from_str(tab, about_new);
261 load_page_from_str(tab, "# not found\n");
265 load_gemini_url(struct tab *tab, const char *url)
271 if (has_prefix(url, "gemini:")) {
272 if (!url_parse(url, &tab->url, &err))
275 if (!url_resolve_from(&tab->url, url, &err))
279 len = sizeof(tab->hist_cur->h)-1;
280 url_unparse(&tab->url, tab->hist_cur->h, len);
281 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
282 tab->hist_cur->h, len+1);
287 if (asprintf(&p, "#error loading %s\n>%s\n",
290 strlcpy(tab->hist_cur->h, url, len);
291 load_page_from_str(tab, p);
296 do_load_url(struct tab *tab, const char *url)
300 for (p = protos; p->schema != NULL; ++p) {
301 if (has_prefix(url, p->schema)) {
307 protos[0].loadfn(tab, url);
311 load_url(struct tab *tab, const char *url)
313 if (tab->hist_cur != NULL)
314 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
316 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
320 hist_push(&tab->hist, tab->hist_cur);
321 do_load_url(tab, url);
325 load_previous_page(struct tab *tab)
329 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
332 do_load_url(tab, h->h);
337 load_next_page(struct tab *tab)
341 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
344 do_load_url(tab, h->h);
349 stop_tab(struct tab *tab)
351 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
358 struct imsgbuf main_ibuf, network_ibuf;
361 signal(SIGCHLD, SIG_IGN);
363 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
364 err(1, "socketpair");
371 setproctitle("client");
373 imsg_init(&network_ibuf, imsg_fds[1]);
374 exit(client_main(&network_ibuf));
380 imsg_init(&main_ibuf, imsg_fds[0]);
381 netibuf = &main_ibuf;
383 TAILQ_INIT(&tabshead);
387 event_set(&imsgev, netibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, netibuf);
388 event_add(&imsgev, NULL);
392 sandbox_ui_process();
396 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);