commit 8f3c9af8369ac653695ca22777db99bfa1162cfe from: Omar Polo date: Tue Jan 11 16:49:32 2022 UTC simplify the caching API - don't expose the evbuffer where we store the cached page - cache to/from tabs instead of buffers commit - 5df02e0d8d9027c56450c94857949b3826454369 commit + 8f3c9af8369ac653695ca22777db99bfa1162cfe blob - aa3e2186e09bffc8a9dab0eaaa5367afee2de12e blob + d2b4e4ddb16064ef991bcf62b90dfd300ce90032 --- mcache.c +++ mcache.c @@ -22,6 +22,7 @@ #include "telescope.h" #include "mcache.h" +#include "parser.h" const char *gemtext_prefixes[] = { [LINE_TEXT] = "", @@ -86,25 +87,27 @@ mcache_init(void) } int -mcache_buffer(const char *url, struct buffer *buf, int trust) +mcache_tab(struct tab *tab) { struct mcache_entry *e; struct line *line; unsigned int slot; size_t l, len; + const char *url; + url = tab->hist_cur->h; l = strlen(url); len = sizeof(*e) + l + 1; if ((e = calloc(1, len)) == NULL) return -1; - e->trust = trust; + e->trust = tab->trust; memcpy(e->url, url, l); if ((e->evb = evbuffer_new()) == NULL) goto err; - TAILQ_FOREACH(line, &buf->page.head, lines) { + TAILQ_FOREACH(line, &tab->buffer.page.head, lines) { const char *text, *alt; int r; @@ -169,7 +172,7 @@ err: } int -mcache_lookup(const char *url, struct evbuffer **ret, int *trust) +mcache_lookup(const char *url, struct tab *tab) { struct mcache_entry *e; unsigned int slot; @@ -178,7 +181,17 @@ mcache_lookup(const char *url, struct evbuffer **ret, if ((e = ohash_find(&mcache.h, slot)) == NULL) return 0; - *ret = e->evb; - *trust = e->trust; + parser_init(tab, gemtext_initparser); + if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb))) + goto err; + if (!parser_free(tab)) + goto err; + + tab->trust = e->trust; return 1; + +err: + parser_free(tab); + erase_buffer(&tab->buffer); + return 0; } blob - 0542e67d232dce7f2d73b304bcefa4b8d15614bc blob + 4a4c1d502422c2383b9466d4884daf994e58fd1b --- mcache.h +++ mcache.h @@ -17,10 +17,10 @@ #ifndef MCACHE_H #define MCACHE_H -struct buffer; +struct tab; void mcache_init(void); -int mcache_buffer(const char *, struct buffer *, int); -int mcache_lookup(const char *, struct evbuffer **, int *); +int mcache_tab(struct tab *); +int mcache_lookup(const char *, struct tab *); #endif blob - c58ab78a40612c01cb3d9532f56b7efc65bce35e blob + 05f53eaf6ae6a4b4dbc962b829cee8f1bc721c67 --- telescope.c +++ telescope.c @@ -608,7 +608,7 @@ handle_imsg_eof(struct imsg *imsg, size_t datalen) die(); if (has_prefix(tab->hist_cur->h, "gemini://") || has_prefix(tab->hist_cur->h, "gopher://")) - mcache_buffer(tab->hist_cur->h, &tab->buffer, tab->trust); + mcache_tab(tab); ui_on_tab_refresh(tab); ui_on_tab_loaded(tab); } else { @@ -891,30 +891,6 @@ gopher_send_search_req(struct tab *tab, const char *te parser_init(tab, gophermap_initparser); make_request(tab, &req, PROTO_GOPHER, NULL); -} - -static int -try_load_cache(struct tab *tab) -{ - struct evbuffer *evb; - int trust; - - if (!mcache_lookup(tab->hist_cur->h, &evb, &trust)) - return 0; - - parser_init(tab, gemtext_initparser); - if (!parser_parse(tab, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb))) - goto err; - parser_free(tab); - tab->trust = trust; - ui_on_tab_refresh(tab); - ui_on_tab_loaded(tab); - return 1; - -err: - parser_free(tab); - erase_buffer(&tab->buffer); - return 0; } /* @@ -929,6 +905,7 @@ do_load_url(struct tab *tab, const char *url, const ch struct phos_uri uri; struct proto *p; struct proxy *proxy; + int nocache = mode & LU_MODE_NOCACHE; char *t; tab->proxy = NULL; @@ -952,8 +929,11 @@ do_load_url(struct tab *tab, const char *url, const ch phos_serialize_uri(&tab->uri, tab->hist_cur->h, sizeof(tab->hist_cur->h)); - if (!(mode & LU_MODE_NOCACHE) && try_load_cache(tab)) + if (!nocache && mcache_lookup(tab->hist_cur->h, tab)) { + ui_on_tab_refresh(tab); + ui_on_tab_loaded(tab); return 0; + } for (p = protos; p->schema != NULL; ++p) { if (!strcmp(tab->uri.scheme, p->schema)) {