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 <sys/time.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <time.h>
26 #include "ev.h"
27 #include "hist.h"
28 #include "mcache.h"
29 #include "parser.h"
30 #include "utils.h"
32 static struct timeval tv = { 5 * 60, 0 };
33 static unsigned int timeout;
35 static struct ohash h;
36 static size_t npages;
37 static size_t tot;
39 struct mcache_entry {
40 time_t ts;
41 parserfn parser;
42 int trust;
43 char *buf;
44 size_t buflen;
45 char url[];
46 };
48 static void
49 mcache_free_entry(const char *url)
50 {
51 struct mcache_entry *e;
52 unsigned int slot;
54 slot = ohash_qlookup(&h, url);
55 if ((e = ohash_remove(&h, slot)) == NULL)
56 return;
58 npages--;
59 tot -= e->buflen;
61 free(e->buf);
62 free(e);
63 }
65 static void
66 clean_old_entries(int fd, int ev, void *data)
67 {
68 struct mcache_entry *e;
69 unsigned int i;
70 time_t treshold;
72 /* delete pages older than an hour */
73 treshold = time(NULL) - 60 * 60;
75 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
76 if (e->ts < treshold)
77 mcache_free_entry(e->url);
79 timeout = ev_timer(&tv, clean_old_entries, NULL);
80 }
82 void
83 mcache_init(void)
84 {
85 struct ohash_info info = {
86 .key_offset = offsetof(struct mcache_entry, url),
87 .calloc = hash_calloc,
88 .free = hash_free,
89 .alloc = hash_alloc,
90 };
92 ohash_init(&h, 5, &info);
93 }
95 int
96 mcache_tab(struct tab *tab)
97 {
98 struct mcache_entry *e;
99 unsigned int slot;
100 size_t l, len;
101 const char *url;
102 FILE *fp;
104 url = hist_cur(tab->hist);
105 l = strlen(url);
106 len = sizeof(*e) + l + 1;
108 if ((e = calloc(1, len)) == NULL)
109 return -1;
110 e->ts = time(NULL);
111 e->parser = tab->buffer.page.init;
112 e->trust = tab->trust;
113 memcpy(e->url, url, l);
115 if ((fp = open_memstream(&e->buf, &e->buflen)) == NULL)
116 goto err;
118 if (!parser_serialize(tab, fp))
119 goto err;
121 fclose(fp);
123 /* free any previously cached copies of this page */
124 mcache_free_entry(url);
126 slot = ohash_qlookup(&h, url);
127 ohash_insert(&h, slot, e);
129 npages++;
130 tot += e->buflen;
132 if (!ev_timer_pending(timeout))
133 timeout = ev_timer(&tv, clean_old_entries, NULL);
135 return 0;
137 err:
138 if (fp != NULL)
139 fclose(fp);
140 if (e->buf != NULL)
141 free(e->buf);
142 free(e);
143 return -1;
146 int
147 mcache_lookup(const char *url, struct tab *tab)
149 struct mcache_entry *e;
150 unsigned int slot;
152 slot = ohash_qlookup(&h, url);
153 if ((e = ohash_find(&h, slot)) == NULL)
154 return 0;
156 parser_init(tab, e->parser);
157 if (!parser_parse(tab, e->buf, e->buflen))
158 goto err;
159 if (!parser_free(tab))
160 goto err;
162 tab->trust = e->trust;
163 return 1;
165 err:
166 parser_free(tab);
167 erase_buffer(&tab->buffer);
168 return 0;
171 void
172 mcache_info(size_t *r_npages, size_t *r_tot)
174 *r_npages = npages;
175 *r_tot = tot;