Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
23 #include "telescope.h"
24 #include "mcache.h"
25 #include "parser.h"
26 #include "utils.h"
28 static struct timeval tv = { 5 * 60, 0 };
29 static struct event timerev;
31 static struct ohash h;
32 static size_t npages;
33 static size_t tot;
35 struct mcache_entry {
36 time_t ts;
37 parserfn parser;
38 int trust;
39 struct evbuffer *evb;
40 char url[];
41 };
43 static int
44 mcache_printf(void *d, const char *fmt, ...)
45 {
46 struct evbuffer *evb = d;
47 int r;
48 va_list ap;
50 va_start(ap, fmt);
51 r = evbuffer_add_vprintf(evb, fmt, ap);
52 va_end(ap);
54 return r;
55 }
57 static void
58 mcache_free_entry(const char *url)
59 {
60 struct mcache_entry *e;
61 unsigned int slot;
63 slot = ohash_qlookup(&h, url);
64 if ((e = ohash_remove(&h, slot)) == NULL)
65 return;
67 npages--;
68 tot -= EVBUFFER_LENGTH(e->evb);
70 evbuffer_free(e->evb);
71 free(e);
72 }
74 static void
75 clean_old_entries(int fd, short ev, void *data)
76 {
77 struct mcache_entry *e;
78 unsigned int i;
79 time_t treshold;
81 /* delete pages older than an hour */
82 treshold = time(NULL) - 60 * 60;
84 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
85 if (e->ts < treshold)
86 mcache_free_entry(e->url);
88 evtimer_add(&timerev, &tv);
89 }
91 void
92 mcache_init(void)
93 {
94 struct ohash_info info = {
95 .key_offset = offsetof(struct mcache_entry, url),
96 .calloc = hash_calloc,
97 .free = hash_free,
98 .alloc = hash_alloc,
99 };
101 ohash_init(&h, 5, &info);
103 evtimer_set(&timerev, clean_old_entries, NULL);
106 int
107 mcache_tab(struct tab *tab)
109 struct mcache_entry *e;
110 unsigned int slot;
111 size_t l, len;
112 const char *url;
114 url = tab->hist_cur->h;
115 l = strlen(url);
116 len = sizeof(*e) + l + 1;
118 if ((e = calloc(1, len)) == NULL)
119 return -1;
120 e->ts = time(NULL);
121 e->parser = tab->buffer.page.init;
122 e->trust = tab->trust;
123 memcpy(e->url, url, l);
125 if ((e->evb = evbuffer_new()) == NULL)
126 goto err;
128 if (!parser_serialize(tab, mcache_printf, e->evb))
129 goto err;
131 /* free any previously cached copies of this page */
132 mcache_free_entry(url);
134 slot = ohash_qlookup(&h, url);
135 ohash_insert(&h, slot, e);
137 npages++;
138 tot += EVBUFFER_LENGTH(e->evb);
140 if (!evtimer_pending(&timerev, NULL))
141 evtimer_add(&timerev, &tv);
143 return 0;
145 err:
146 if (e->evb != NULL)
147 evbuffer_free(e->evb);
148 free(e);
149 return -1;
152 int
153 mcache_lookup(const char *url, struct tab *tab)
155 struct mcache_entry *e;
156 unsigned int slot;
158 slot = ohash_qlookup(&h, url);
159 if ((e = ohash_find(&h, slot)) == NULL)
160 return 0;
162 parser_init(tab, e->parser);
163 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
164 goto err;
165 if (!parser_free(tab))
166 goto err;
168 tab->trust = e->trust;
169 return 1;
171 err:
172 parser_free(tab);
173 erase_buffer(&tab->buffer);
174 return 0;
177 void
178 mcache_info(size_t *r_npages, size_t *r_tot)
180 *r_npages = npages;
181 *r_tot = tot;