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