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 void
44 mcache_free_entry(const char *url)
45 {
46 struct mcache_entry *e;
47 unsigned int slot;
49 slot = ohash_qlookup(&h, url);
50 if ((e = ohash_remove(&h, slot)) == NULL)
51 return;
53 npages--;
54 tot -= EVBUFFER_LENGTH(e->evb);
56 evbuffer_free(e->evb);
57 free(e);
58 }
60 static void
61 clean_old_entries(int fd, short ev, void *data)
62 {
63 struct mcache_entry *e;
64 unsigned int i;
65 time_t treshold;
67 /* delete pages older than an hour */
68 treshold = time(NULL) - 60 * 60;
70 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
71 if (e->ts < treshold)
72 mcache_free_entry(e->url);
74 evtimer_add(&timerev, &tv);
75 }
77 void
78 mcache_init(void)
79 {
80 struct ohash_info info = {
81 .key_offset = offsetof(struct mcache_entry, url),
82 .calloc = hash_calloc,
83 .free = hash_free,
84 .alloc = hash_alloc,
85 };
87 ohash_init(&h, 5, &info);
89 evtimer_set(&timerev, clean_old_entries, NULL);
90 }
92 int
93 mcache_tab(struct tab *tab)
94 {
95 struct mcache_entry *e;
96 unsigned int slot;
97 size_t l, len;
98 const char *url;
100 url = tab->hist_cur->h;
101 l = strlen(url);
102 len = sizeof(*e) + l + 1;
104 if ((e = calloc(1, len)) == NULL)
105 return -1;
106 e->ts = time(NULL);
107 e->parser = tab->buffer.page.init;
108 e->trust = tab->trust;
109 memcpy(e->url, url, l);
111 if ((e->evb = evbuffer_new()) == NULL)
112 goto err;
114 if (!parser_serialize(tab, e->evb))
115 goto err;
117 /* free any previously cached copies of this page */
118 mcache_free_entry(url);
120 slot = ohash_qlookup(&h, url);
121 ohash_insert(&h, slot, e);
123 npages++;
124 tot += EVBUFFER_LENGTH(e->evb);
126 if (!evtimer_pending(&timerev, NULL))
127 evtimer_add(&timerev, &tv);
129 return 0;
131 err:
132 if (e->evb != NULL)
133 evbuffer_free(e->evb);
134 free(e);
135 return -1;
138 int
139 mcache_lookup(const char *url, struct tab *tab)
141 struct mcache_entry *e;
142 unsigned int slot;
144 slot = ohash_qlookup(&h, url);
145 if ((e = ohash_find(&h, slot)) == NULL)
146 return 0;
148 parser_init(tab, e->parser);
149 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
150 goto err;
151 if (!parser_free(tab))
152 goto err;
154 tab->trust = e->trust;
155 return 1;
157 err:
158 parser_free(tab);
159 erase_buffer(&tab->buffer);
160 return 0;
163 void
164 mcache_info(size_t *r_npages, size_t *r_tot)
166 *r_npages = npages;
167 *r_tot = tot;