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