2 d5af38cc 2022-01-10 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 d5af38cc 2022-01-10 op * Permission to use, copy, modify, and distribute this software for any
5 d5af38cc 2022-01-10 op * purpose with or without fee is hereby granted, provided that the above
6 d5af38cc 2022-01-10 op * copyright notice and this permission notice appear in all copies.
8 d5af38cc 2022-01-10 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d5af38cc 2022-01-10 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d5af38cc 2022-01-10 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d5af38cc 2022-01-10 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d5af38cc 2022-01-10 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d5af38cc 2022-01-10 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d5af38cc 2022-01-10 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d5af38cc 2022-01-10 op #include "compat.h"
19 d5af38cc 2022-01-10 op #include <stdlib.h>
20 d5af38cc 2022-01-10 op #include <stdint.h>
21 d5af38cc 2022-01-10 op #include <string.h>
23 d5af38cc 2022-01-10 op #include "telescope.h"
24 d5af38cc 2022-01-10 op #include "mcache.h"
25 8f3c9af8 2022-01-11 op #include "parser.h"
26 9d65b1d9 2022-01-11 op #include "utils.h"
28 946d3439 2022-01-12 op static struct timeval tv = { 5 * 60, 0 };
29 4b4c3458 2022-01-11 op static struct event timerev;
31 a96922a1 2022-01-11 op static struct ohash h;
32 a96922a1 2022-01-11 op static size_t npages;
33 a96922a1 2022-01-11 op static size_t tot;
35 d5af38cc 2022-01-10 op struct mcache_entry {
37 fe84b750 2022-01-19 op parserfn parser;
39 d5af38cc 2022-01-10 op struct evbuffer *evb;
44 d54dd816 2022-04-13 op mcache_printf(void *d, const char *fmt, ...)
46 d54dd816 2022-04-13 op struct evbuffer *evb = d;
50 d54dd816 2022-04-13 op va_start(ap, fmt);
51 d54dd816 2022-04-13 op r = evbuffer_add_vprintf(evb, fmt, ap);
58 ffcd827c 2022-01-11 op mcache_free_entry(const char *url)
60 ffcd827c 2022-01-11 op struct mcache_entry *e;
61 ffcd827c 2022-01-11 op unsigned int slot;
63 a96922a1 2022-01-11 op slot = ohash_qlookup(&h, url);
64 a96922a1 2022-01-11 op if ((e = ohash_remove(&h, slot)) == NULL)
68 a96922a1 2022-01-11 op tot -= EVBUFFER_LENGTH(e->evb);
70 ffcd827c 2022-01-11 op evbuffer_free(e->evb);
75 4b4c3458 2022-01-11 op clean_old_entries(int fd, short ev, void *data)
77 4b4c3458 2022-01-11 op struct mcache_entry *e;
78 4b4c3458 2022-01-11 op unsigned int i;
79 4b4c3458 2022-01-11 op time_t treshold;
81 946d3439 2022-01-12 op /* delete pages older than an hour */
82 946d3439 2022-01-12 op treshold = time(NULL) - 60 * 60;
84 a96922a1 2022-01-11 op for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
85 4b4c3458 2022-01-11 op if (e->ts < treshold)
86 4b4c3458 2022-01-11 op mcache_free_entry(e->url);
88 4b4c3458 2022-01-11 op evtimer_add(&timerev, &tv);
92 d5af38cc 2022-01-10 op mcache_init(void)
94 d5af38cc 2022-01-10 op struct ohash_info info = {
95 d5af38cc 2022-01-10 op .key_offset = offsetof(struct mcache_entry, url),
96 d5af38cc 2022-01-10 op .calloc = hash_calloc,
97 d5af38cc 2022-01-10 op .free = hash_free,
98 d5af38cc 2022-01-10 op .alloc = hash_alloc,
101 a96922a1 2022-01-11 op ohash_init(&h, 5, &info);
103 4b4c3458 2022-01-11 op evtimer_set(&timerev, clean_old_entries, NULL);
107 8f3c9af8 2022-01-11 op mcache_tab(struct tab *tab)
109 d5af38cc 2022-01-10 op struct mcache_entry *e;
110 d5af38cc 2022-01-10 op unsigned int slot;
111 d5af38cc 2022-01-10 op size_t l, len;
112 8f3c9af8 2022-01-11 op const char *url;
114 8f3c9af8 2022-01-11 op url = tab->hist_cur->h;
115 d5af38cc 2022-01-10 op l = strlen(url);
116 d5af38cc 2022-01-10 op len = sizeof(*e) + l + 1;
118 d5af38cc 2022-01-10 op if ((e = calloc(1, len)) == NULL)
120 4b4c3458 2022-01-11 op e->ts = time(NULL);
121 fe84b750 2022-01-19 op e->parser = tab->buffer.page.init;
122 8f3c9af8 2022-01-11 op e->trust = tab->trust;
123 d5af38cc 2022-01-10 op memcpy(e->url, url, l);
125 d5af38cc 2022-01-10 op if ((e->evb = evbuffer_new()) == NULL)
128 d54dd816 2022-04-13 op if (!parser_serialize(tab, mcache_printf, e->evb))
131 ffcd827c 2022-01-11 op /* free any previously cached copies of this page */
132 ffcd827c 2022-01-11 op mcache_free_entry(url);
134 a96922a1 2022-01-11 op slot = ohash_qlookup(&h, url);
135 a96922a1 2022-01-11 op ohash_insert(&h, slot, e);
138 a96922a1 2022-01-11 op tot += EVBUFFER_LENGTH(e->evb);
140 4b4c3458 2022-01-11 op if (!evtimer_pending(&timerev, NULL))
141 4b4c3458 2022-01-11 op evtimer_add(&timerev, &tv);
146 d5af38cc 2022-01-10 op if (e->evb != NULL)
147 d5af38cc 2022-01-10 op evbuffer_free(e->evb);
153 8f3c9af8 2022-01-11 op mcache_lookup(const char *url, struct tab *tab)
155 d5af38cc 2022-01-10 op struct mcache_entry *e;
156 d5af38cc 2022-01-10 op unsigned int slot;
158 a96922a1 2022-01-11 op slot = ohash_qlookup(&h, url);
159 a96922a1 2022-01-11 op if ((e = ohash_find(&h, slot)) == NULL)
162 fe84b750 2022-01-19 op parser_init(tab, e->parser);
163 8f3c9af8 2022-01-11 op if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
165 8f3c9af8 2022-01-11 op if (!parser_free(tab))
168 8f3c9af8 2022-01-11 op tab->trust = e->trust;
172 8f3c9af8 2022-01-11 op parser_free(tab);
173 8f3c9af8 2022-01-11 op erase_buffer(&tab->buffer);
178 a96922a1 2022-01-11 op mcache_info(size_t *r_npages, size_t *r_tot)
180 a96922a1 2022-01-11 op *r_npages = npages;
181 a96922a1 2022-01-11 op *r_tot = tot;