Blame


1 d5af38cc 2022-01-10 op /*
2 d5af38cc 2022-01-10 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 d5af38cc 2022-01-10 op *
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.
7 d5af38cc 2022-01-10 op *
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.
15 d5af38cc 2022-01-10 op */
16 d5af38cc 2022-01-10 op
17 d5af38cc 2022-01-10 op #include "compat.h"
18 d5af38cc 2022-01-10 op
19 98d3e6c1 2024-02-18 op #include <sys/time.h>
20 98d3e6c1 2024-02-18 op
21 d5af38cc 2022-01-10 op #include <stdlib.h>
22 d5af38cc 2022-01-10 op #include <stdint.h>
23 d5af38cc 2022-01-10 op #include <string.h>
24 cfd1de02 2024-02-19 op #include <time.h>
25 d5af38cc 2022-01-10 op
26 98d3e6c1 2024-02-18 op #include "ev.h"
27 65c49665 2024-01-23 op #include "hist.h"
28 d5af38cc 2022-01-10 op #include "mcache.h"
29 8f3c9af8 2022-01-11 op #include "parser.h"
30 9d65b1d9 2022-01-11 op #include "utils.h"
31 d5af38cc 2022-01-10 op
32 946d3439 2022-01-12 op static struct timeval tv = { 5 * 60, 0 };
33 98d3e6c1 2024-02-18 op static unsigned int timeout;
34 4b4c3458 2022-01-11 op
35 a96922a1 2022-01-11 op static struct ohash h;
36 a96922a1 2022-01-11 op static size_t npages;
37 a96922a1 2022-01-11 op static size_t tot;
38 d5af38cc 2022-01-10 op
39 d5af38cc 2022-01-10 op struct mcache_entry {
40 4b4c3458 2022-01-11 op time_t ts;
41 fe84b750 2022-01-19 op parserfn parser;
42 d5af38cc 2022-01-10 op int trust;
43 0110411e 2022-04-13 op char *buf;
44 0110411e 2022-04-13 op size_t buflen;
45 d5af38cc 2022-01-10 op char url[];
46 d5af38cc 2022-01-10 op };
47 d5af38cc 2022-01-10 op
48 ffcd827c 2022-01-11 op static void
49 ffcd827c 2022-01-11 op mcache_free_entry(const char *url)
50 ffcd827c 2022-01-11 op {
51 ffcd827c 2022-01-11 op struct mcache_entry *e;
52 ffcd827c 2022-01-11 op unsigned int slot;
53 ffcd827c 2022-01-11 op
54 a96922a1 2022-01-11 op slot = ohash_qlookup(&h, url);
55 a96922a1 2022-01-11 op if ((e = ohash_remove(&h, slot)) == NULL)
56 ffcd827c 2022-01-11 op return;
57 ffcd827c 2022-01-11 op
58 a96922a1 2022-01-11 op npages--;
59 0110411e 2022-04-13 op tot -= e->buflen;
60 befa807e 2022-01-11 op
61 0110411e 2022-04-13 op free(e->buf);
62 ffcd827c 2022-01-11 op free(e);
63 ffcd827c 2022-01-11 op }
64 ffcd827c 2022-01-11 op
65 4b4c3458 2022-01-11 op static void
66 98d3e6c1 2024-02-18 op clean_old_entries(int fd, int ev, void *data)
67 4b4c3458 2022-01-11 op {
68 4b4c3458 2022-01-11 op struct mcache_entry *e;
69 4b4c3458 2022-01-11 op unsigned int i;
70 4b4c3458 2022-01-11 op time_t treshold;
71 4b4c3458 2022-01-11 op
72 946d3439 2022-01-12 op /* delete pages older than an hour */
73 946d3439 2022-01-12 op treshold = time(NULL) - 60 * 60;
74 4b4c3458 2022-01-11 op
75 a96922a1 2022-01-11 op for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
76 4b4c3458 2022-01-11 op if (e->ts < treshold)
77 4b4c3458 2022-01-11 op mcache_free_entry(e->url);
78 4b4c3458 2022-01-11 op
79 98d3e6c1 2024-02-18 op timeout = ev_timer(&tv, clean_old_entries, NULL);
80 4b4c3458 2022-01-11 op }
81 4b4c3458 2022-01-11 op
82 d5af38cc 2022-01-10 op void
83 d5af38cc 2022-01-10 op mcache_init(void)
84 d5af38cc 2022-01-10 op {
85 d5af38cc 2022-01-10 op struct ohash_info info = {
86 d5af38cc 2022-01-10 op .key_offset = offsetof(struct mcache_entry, url),
87 d5af38cc 2022-01-10 op .calloc = hash_calloc,
88 d5af38cc 2022-01-10 op .free = hash_free,
89 d5af38cc 2022-01-10 op .alloc = hash_alloc,
90 d5af38cc 2022-01-10 op };
91 d5af38cc 2022-01-10 op
92 a96922a1 2022-01-11 op ohash_init(&h, 5, &info);
93 d5af38cc 2022-01-10 op }
94 d5af38cc 2022-01-10 op
95 d5af38cc 2022-01-10 op int
96 8f3c9af8 2022-01-11 op mcache_tab(struct tab *tab)
97 d5af38cc 2022-01-10 op {
98 d5af38cc 2022-01-10 op struct mcache_entry *e;
99 d5af38cc 2022-01-10 op unsigned int slot;
100 d5af38cc 2022-01-10 op size_t l, len;
101 8f3c9af8 2022-01-11 op const char *url;
102 0110411e 2022-04-13 op FILE *fp;
103 d5af38cc 2022-01-10 op
104 65c49665 2024-01-23 op url = hist_cur(tab->hist);
105 d5af38cc 2022-01-10 op l = strlen(url);
106 d5af38cc 2022-01-10 op len = sizeof(*e) + l + 1;
107 d5af38cc 2022-01-10 op
108 d5af38cc 2022-01-10 op if ((e = calloc(1, len)) == NULL)
109 d5af38cc 2022-01-10 op return -1;
110 4b4c3458 2022-01-11 op e->ts = time(NULL);
111 fe84b750 2022-01-19 op e->parser = tab->buffer.page.init;
112 8f3c9af8 2022-01-11 op e->trust = tab->trust;
113 d5af38cc 2022-01-10 op memcpy(e->url, url, l);
114 d5af38cc 2022-01-10 op
115 0110411e 2022-04-13 op if ((fp = open_memstream(&e->buf, &e->buflen)) == NULL)
116 d5af38cc 2022-01-10 op goto err;
117 d5af38cc 2022-01-10 op
118 0110411e 2022-04-13 op if (!parser_serialize(tab, fp))
119 fe84b750 2022-01-19 op goto err;
120 d5af38cc 2022-01-10 op
121 0110411e 2022-04-13 op fclose(fp);
122 0110411e 2022-04-13 op
123 ffcd827c 2022-01-11 op /* free any previously cached copies of this page */
124 ffcd827c 2022-01-11 op mcache_free_entry(url);
125 ffcd827c 2022-01-11 op
126 a96922a1 2022-01-11 op slot = ohash_qlookup(&h, url);
127 a96922a1 2022-01-11 op ohash_insert(&h, slot, e);
128 befa807e 2022-01-11 op
129 a96922a1 2022-01-11 op npages++;
130 0110411e 2022-04-13 op tot += e->buflen;
131 befa807e 2022-01-11 op
132 98d3e6c1 2024-02-18 op if (!ev_timer_pending(timeout))
133 98d3e6c1 2024-02-18 op timeout = ev_timer(&tv, clean_old_entries, NULL);
134 4b4c3458 2022-01-11 op
135 d5af38cc 2022-01-10 op return 0;
136 d5af38cc 2022-01-10 op
137 d5af38cc 2022-01-10 op err:
138 0110411e 2022-04-13 op if (fp != NULL)
139 0110411e 2022-04-13 op fclose(fp);
140 0110411e 2022-04-13 op if (e->buf != NULL)
141 0110411e 2022-04-13 op free(e->buf);
142 d5af38cc 2022-01-10 op free(e);
143 d5af38cc 2022-01-10 op return -1;
144 d5af38cc 2022-01-10 op }
145 d5af38cc 2022-01-10 op
146 d5af38cc 2022-01-10 op int
147 8f3c9af8 2022-01-11 op mcache_lookup(const char *url, struct tab *tab)
148 d5af38cc 2022-01-10 op {
149 d5af38cc 2022-01-10 op struct mcache_entry *e;
150 d5af38cc 2022-01-10 op unsigned int slot;
151 d5af38cc 2022-01-10 op
152 a96922a1 2022-01-11 op slot = ohash_qlookup(&h, url);
153 a96922a1 2022-01-11 op if ((e = ohash_find(&h, slot)) == NULL)
154 d5af38cc 2022-01-10 op return 0;
155 d5af38cc 2022-01-10 op
156 fe84b750 2022-01-19 op parser_init(tab, e->parser);
157 0110411e 2022-04-13 op if (!parser_parse(tab, e->buf, e->buflen))
158 8f3c9af8 2022-01-11 op goto err;
159 8f3c9af8 2022-01-11 op if (!parser_free(tab))
160 8f3c9af8 2022-01-11 op goto err;
161 8f3c9af8 2022-01-11 op
162 8f3c9af8 2022-01-11 op tab->trust = e->trust;
163 d5af38cc 2022-01-10 op return 1;
164 8f3c9af8 2022-01-11 op
165 8f3c9af8 2022-01-11 op err:
166 8f3c9af8 2022-01-11 op parser_free(tab);
167 8f3c9af8 2022-01-11 op erase_buffer(&tab->buffer);
168 8f3c9af8 2022-01-11 op return 0;
169 d5af38cc 2022-01-10 op }
170 befa807e 2022-01-11 op
171 befa807e 2022-01-11 op void
172 a96922a1 2022-01-11 op mcache_info(size_t *r_npages, size_t *r_tot)
173 befa807e 2022-01-11 op {
174 a96922a1 2022-01-11 op *r_npages = npages;
175 a96922a1 2022-01-11 op *r_tot = tot;
176 befa807e 2022-01-11 op }