2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include "telescope.h"
28 static struct timeval tv = { 5 * 60, 0 };
29 static struct event timerev;
31 static struct ohash h;
45 mcache_free_entry(const char *url)
47 struct mcache_entry *e;
50 slot = ohash_qlookup(&h, url);
51 if ((e = ohash_remove(&h, slot)) == NULL)
62 clean_old_entries(int fd, short ev, void *data)
64 struct mcache_entry *e;
68 /* delete pages older than an hour */
69 treshold = time(NULL) - 60 * 60;
71 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
73 mcache_free_entry(e->url);
75 evtimer_add(&timerev, &tv);
81 struct ohash_info info = {
82 .key_offset = offsetof(struct mcache_entry, url),
83 .calloc = hash_calloc,
88 ohash_init(&h, 5, &info);
90 evtimer_set(&timerev, clean_old_entries, NULL);
94 mcache_tab(struct tab *tab)
96 struct mcache_entry *e;
102 url = tab->hist_cur->h;
104 len = sizeof(*e) + l + 1;
106 if ((e = calloc(1, len)) == NULL)
109 e->parser = tab->buffer.page.init;
110 e->trust = tab->trust;
111 memcpy(e->url, url, l);
113 if ((fp = open_memstream(&e->buf, &e->buflen)) == NULL)
116 if (!parser_serialize(tab, fp))
121 /* free any previously cached copies of this page */
122 mcache_free_entry(url);
124 slot = ohash_qlookup(&h, url);
125 ohash_insert(&h, slot, e);
130 if (!evtimer_pending(&timerev, NULL))
131 evtimer_add(&timerev, &tv);
145 mcache_lookup(const char *url, struct tab *tab)
147 struct mcache_entry *e;
150 slot = ohash_qlookup(&h, url);
151 if ((e = ohash_find(&h, slot)) == NULL)
154 parser_init(tab, e->parser);
155 if (!parser_parse(tab, e->buf, e->buflen))
157 if (!parser_free(tab))
160 tab->trust = e->trust;
165 erase_buffer(&tab->buffer);
170 mcache_info(size_t *r_npages, size_t *r_tot)