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